From e21da1d2bc88510782f090adf2f7e2b1c241d167 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 23 Jan 2024 15:52:18 +0800 Subject: [PATCH] Restore react native file --- .../block-library/src/block/edit.native.js | 247 ++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 packages/block-library/src/block/edit.native.js diff --git a/packages/block-library/src/block/edit.native.js b/packages/block-library/src/block/edit.native.js new file mode 100644 index 00000000000000..3a649921b3dda1 --- /dev/null +++ b/packages/block-library/src/block/edit.native.js @@ -0,0 +1,247 @@ +/** + * External dependencies + */ +import { + ActivityIndicator, + Platform, + Text, + TouchableWithoutFeedback, + View, +} from 'react-native'; + +/** + * WordPress dependencies + */ +import { useState, useCallback } from '@wordpress/element'; +import { + useEntityBlockEditor, + useEntityProp, + store as coreStore, +} from '@wordpress/core-data'; +import { + BottomSheet, + Icon, + Disabled, + TextControl, +} from '@wordpress/components'; +import { useSelect, useDispatch } from '@wordpress/data'; +import { __, sprintf } from '@wordpress/i18n'; +import { + __experimentalRecursionProvider as RecursionProvider, + __experimentalUseHasRecursion as useHasRecursion, + InnerBlocks, + Warning, + store as blockEditorStore, +} from '@wordpress/block-editor'; +import { usePreferredColorSchemeStyle } from '@wordpress/compose'; +import { help } from '@wordpress/icons'; +import { store as reusableBlocksStore } from '@wordpress/reusable-blocks'; +import { store as editorStore } from '@wordpress/editor'; +import { store as noticesStore } from '@wordpress/notices'; + +/** + * Internal dependencies + */ +import styles from '../editor.scss'; +import EditTitle from '../edit-title'; + +export default function ReusableBlockEdit( { + attributes: { ref }, + clientId, + isSelected, +} ) { + const hasAlreadyRendered = useHasRecursion( ref ); + + const [ showHelp, setShowHelp ] = useState( false ); + const infoTextStyle = usePreferredColorSchemeStyle( + styles.infoText, + styles.infoTextDark + ); + const infoTitleStyle = usePreferredColorSchemeStyle( + styles.infoTitle, + styles.infoTitleDark + ); + const infoSheetIconStyle = usePreferredColorSchemeStyle( + styles.infoSheetIcon, + styles.infoSheetIconDark + ); + const infoDescriptionStyle = usePreferredColorSchemeStyle( + styles.infoDescription, + styles.infoDescriptionDark + ); + const actionButtonStyle = usePreferredColorSchemeStyle( + styles.actionButton, + styles.actionButtonDark + ); + const spinnerStyle = usePreferredColorSchemeStyle( + styles.spinner, + styles.spinnerDark + ); + + const { hasResolved, isEditing, isMissing } = useSelect( + ( select ) => { + const persistedBlock = select( coreStore ).getEntityRecord( + 'postType', + 'wp_block', + ref + ); + const hasResolvedBlock = select( coreStore ).hasFinishedResolution( + 'getEntityRecord', + [ 'postType', 'wp_block', ref ] + ); + + const { getBlockCount } = select( blockEditorStore ); + + return { + hasResolved: hasResolvedBlock, + isEditing: + select( + reusableBlocksStore + ).__experimentalIsEditingReusableBlock( clientId ), + isMissing: hasResolvedBlock && ! persistedBlock, + innerBlockCount: getBlockCount( clientId ), + }; + }, + [ ref, clientId ] + ); + const hostAppNamespace = useSelect( + ( select ) => + select( editorStore ).getEditorSettings().hostAppNamespace, + [] + ); + + const { createSuccessNotice } = useDispatch( noticesStore ); + const { __experimentalConvertBlockToStatic: convertBlockToStatic } = + useDispatch( reusableBlocksStore ); + const { clearSelectedBlock } = useDispatch( blockEditorStore ); + + const [ blocks, onInput, onChange ] = useEntityBlockEditor( + 'postType', + 'wp_block', + { id: ref } + ); + + const [ title ] = useEntityProp( 'postType', 'wp_block', 'title', ref ); + + function openSheet() { + setShowHelp( true ); + } + + function closeSheet() { + setShowHelp( false ); + } + + const onConvertToRegularBlocks = useCallback( () => { + /* translators: %s: name of the synced block */ + const successNotice = __( '%s detached' ); + createSuccessNotice( sprintf( successNotice, title ) ); + + clearSelectedBlock(); + // Convert action is executed at the end of the current JavaScript execution block + // to prevent issues related to undo/redo actions. + setImmediate( () => convertBlockToStatic( clientId ) ); + }, [ title, clientId ] ); + + function renderSheet() { + const infoTitle = + Platform.OS === 'android' + ? sprintf( + /* translators: %s: name of the host app (e.g. WordPress) */ + __( + 'Editing synced patterns is not yet supported on %s for Android' + ), + hostAppNamespace + ) + : sprintf( + /* translators: %s: name of the host app (e.g. WordPress) */ + __( + 'Editing synced patterns is not yet supported on %s for iOS' + ), + hostAppNamespace + ); + + return ( + + + + + { infoTitle } + + + { __( + 'Alternatively, you can detach and edit this block separately by tapping “Detach”.' + ) } + + + + + ); + } + + if ( hasAlreadyRendered ) { + return ( + + ); + } + + if ( isMissing ) { + return ( + + ); + } + + if ( ! hasResolved ) { + return ( + + + + ); + } + + let element = ( + + ); + + if ( ! isEditing ) { + element = { element }; + } + + return ( + + + + { isSelected && } + { element } + { renderSheet() } + + + + ); +}