From 5b905d8b6aeff1a44ed6743165f30077aa3acc0c Mon Sep 17 00:00:00 2001 From: Carlos Garcia Date: Fri, 12 Feb 2021 17:46:54 +0100 Subject: [PATCH] Add editing to Reusable blocks (WIP) --- .../src/block/edit-title.native.js | 50 ++---- .../block-library/src/block/edit.native.js | 154 ++++++++---------- .../src/block/editor.native.scss | 24 ++- 3 files changed, 98 insertions(+), 130 deletions(-) diff --git a/packages/block-library/src/block/edit-title.native.js b/packages/block-library/src/block/edit-title.native.js index 0a574f2f0cfa83..329dc0ca3853ca 100644 --- a/packages/block-library/src/block/edit-title.native.js +++ b/packages/block-library/src/block/edit-title.native.js @@ -6,56 +6,30 @@ import { Text, View } from 'react-native'; /** * WordPress dependencies */ -import { Icon } from '@wordpress/components'; +import { Button } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { withPreferredColorScheme } from '@wordpress/compose'; -import { help, lock } from '@wordpress/icons'; /** * Internal dependencies */ import styles from './editor.scss'; -function EditTitle( { getStylesFromColorScheme, title } ) { - const lockIconStyle = getStylesFromColorScheme( - styles.lockIcon, - styles.lockIconDark - ); - const titleStyle = getStylesFromColorScheme( - styles.title, - styles.titleDark - ); - const infoIconStyle = getStylesFromColorScheme( - styles.infoIcon, - styles.infoIconDark - ); - const separatorStyle = getStylesFromColorScheme( - styles.separator, - styles.separatorDark - ); +function EditTitle( { onClickEdit, onClickSave, isEditing } ) { + const buttonText = isEditing ? __( 'Save' ) : __( 'Edit' ); + const buttonHandler = isEditing ? onClickSave : onClickEdit; return ( - - - - - { title } - - - + + - ); } diff --git a/packages/block-library/src/block/edit.native.js b/packages/block-library/src/block/edit.native.js index 06b3dfbc364e4b..0e84feedb17cb5 100644 --- a/packages/block-library/src/block/edit.native.js +++ b/packages/block-library/src/block/edit.native.js @@ -1,26 +1,30 @@ /** * External dependencies */ -import { - ActivityIndicator, - Platform, - Text, - TouchableWithoutFeedback, - View, -} from 'react-native'; +import { ActivityIndicator, Text, View } from 'react-native'; /** * WordPress dependencies */ import { useState } from '@wordpress/element'; -import { useEntityBlockEditor } from '@wordpress/core-data'; -import { BottomSheet, Icon, Disabled } from '@wordpress/components'; -import { useSelect } from '@wordpress/data'; +import { useEntityBlockEditor, useEntityProp } from '@wordpress/core-data'; +import { + Disabled, + ToolbarGroup, + ToolbarButton, + TextControl, + PanelBody, +} from '@wordpress/components'; +import { useDispatch, useSelect } from '@wordpress/data'; import { __ } from '@wordpress/i18n'; -import { BlockEditorProvider, BlockList } from '@wordpress/block-editor'; +import { + InnerBlocks, + BlockControls, + InspectorControls, +} from '@wordpress/block-editor'; import { usePreferredColorSchemeStyle } from '@wordpress/compose'; -import { help } from '@wordpress/icons'; import { store as reusableBlocksStore } from '@wordpress/reusable-blocks'; +import { ungroup } from '@wordpress/icons'; /** * Internal dependencies @@ -35,25 +39,14 @@ export default function ReusableBlockEdit( { } ) { const recordArgs = [ 'postType', 'wp_block', 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 [ isEditing, setIsEditing ] = useState( false ); + const spinnerStyle = usePreferredColorSchemeStyle( styles.spinner, styles.spinnerDark ); - const { reusableBlock, hasResolved, isEditing, settings } = useSelect( + const { reusableBlock, hasResolved, isSaving } = useSelect( ( select ) => { const { getSettings } = select( 'core/block-editor' ); @@ -68,63 +61,37 @@ export default function ReusableBlockEdit( { isSaving: select( 'core' ).isSavingEntityRecord( ...recordArgs ), - canUserUpdate: select( 'core' ).canUser( - 'update', - 'blocks', - ref - ), - isEditing: select( - reusableBlocksStore - ).__experimentalIsEditingReusableBlock( clientId ), settings: getSettings(), }; }, [ ref, clientId ] ); + const { saveEditedEntityRecord } = useDispatch( 'core' ); + const [ blocks, onInput, onChange ] = useEntityBlockEditor( 'postType', 'wp_block', { id: ref } ); - function openSheet() { - setShowHelp( true ); - } - - function closeSheet() { - setShowHelp( false ); - } + const [ title, editTitle ] = useEntityProp( + 'postType', + 'wp_block', + 'title', + ref + ); - function renderSheet() { - const infoTitle = - Platform.OS === 'android' - ? __( - "Reusable blocks aren't editable on WordPress for Android" - ) - : __( "Reusable blocks aren't editable on WordPress for iOS" ); + const setTitle = ( value ) => { + editTitle( value ); + setIsEditing( true ); + }; - return ( - - - - - { infoTitle } - - - - ); - } + const { + __experimentalConvertBlockToStatic: convertBlockToStatic, + } = useDispatch( reusableBlocksStore ); - if ( ! hasResolved ) { + if ( ! hasResolved || isSaving ) { return ( @@ -138,16 +105,12 @@ export default function ReusableBlockEdit( { ); } - const { title } = reusableBlock; let element = ( - - - + /> ); if ( ! isEditing ) { @@ -155,18 +118,41 @@ export default function ReusableBlockEdit( { } return ( - + + + + convertBlockToStatic( clientId ) } + label={ __( 'Convert to regular blocks' ) } + icon={ ungroup } + showTooltip + /> + + + + + + + - { isSelected && } + { isSelected && ( + { + setIsEditing( true ); + } } + onClickSave={ () => { + saveEditedEntityRecord( ...recordArgs ); + setIsEditing( false ); + } } + /> + ) } { element } - { renderSheet() } - + ); } diff --git a/packages/block-library/src/block/editor.native.scss b/packages/block-library/src/block/editor.native.scss index e4c8dac29cd5c7..9b2041601acb3c 100644 --- a/packages/block-library/src/block/editor.native.scss +++ b/packages/block-library/src/block/editor.native.scss @@ -1,14 +1,22 @@ .titleContainer { - flex-direction: row; - align-items: center; - padding-bottom: 12; + height: $panel-header-height; + align-items: flex-end; } -.title { - font-weight: bold; +.buttonContainer { + position: absolute; + right: -$dashed-border-space; + padding: 4px; + padding-left: 12px; + padding-right: 12px; + border-radius: 8px; + border-color: $light-secondary; + border-width: 1px; +} + +.saveButton { color: $light-secondary; - flex-grow: 1; - flex-shrink: 1; + text-align: center; } .titleDark { @@ -21,7 +29,7 @@ position: absolute; left: -$block-selected-to-content + $block-selected-border-width; right: -$block-selected-to-content + $block-selected-border-width; - bottom: 0; + bottom: 16; } .separatorDark {