Skip to content

Commit

Permalink
Flash editable outlines on template patterns with templateLock = 'con…
Browse files Browse the repository at this point in the history
…tentOnly'
  • Loading branch information
noisysocks committed Jan 31, 2024
1 parent fc1f606 commit 9eca467
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 45 deletions.
6 changes: 4 additions & 2 deletions packages/block-editor/src/components/block-list/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,6 @@ function BlockListBlockProvider( props ) {
getActiveBlockVariation,
} = select( blocksStore );
const _isSelected = isBlockSelected( clientId );
const templateLock = getTemplateLock( rootClientId );
const canRemove = canRemoveBlock( clientId, rootClientId );
const canMove = canMoveBlock( clientId, rootClientId );
const { name: blockName, attributes, isValid } = block;
Expand All @@ -559,7 +558,8 @@ function BlockListBlockProvider( props ) {
return {
mode: getBlockMode( clientId ),
isSelectionEnabled: isSelectionEnabled(),
isLocked: !! templateLock,
isLocked: !! getTemplateLock( rootClientId ),
templateLock: getTemplateLock( clientId ),
canRemove,
canMove,
// Users of the editor.BlockListBlock filter used to be able to
Expand Down Expand Up @@ -663,6 +663,7 @@ function BlockListBlockProvider( props ) {
removeOutline,
isBlockMovingMode,
canInsertMovingBlock,
templateLock,
isEditingDisabled,
hasEditableOutline,
className,
Expand Down Expand Up @@ -699,6 +700,7 @@ function BlockListBlockProvider( props ) {
removeOutline,
isBlockMovingMode,
canInsertMovingBlock,
templateLock,
isEditingDisabled,
hasEditableOutline,
isTemporarilyEditingAsBlocks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useEventHandlers } from './use-selected-block-event-handlers';
import { useNavModeExit } from './use-nav-mode-exit';
import { useBlockRefProvider } from './use-block-refs';
import { useIntersectionObserver } from './use-intersection-observer';
import { useFlashEditableBlocks } from '../../use-flash-editable-blocks';

/**
* This hook is used to lightly mark an element as a block element. The element
Expand Down Expand Up @@ -98,6 +99,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
hasEditableOutline,
isTemporarilyEditingAsBlocks,
defaultClassName,
templateLock,
} = useContext( PrivateBlockContext );

// translators: %s: Type of block (i.e. Text, Image etc)
Expand All @@ -114,6 +116,10 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
useIntersectionObserver(),
useMovingAnimation( { triggerAnimationOnChange: index, clientId } ),
useDisabled( { isDisabled: ! hasOverlay } ),
useFlashEditableBlocks( {
clientId,
isEnabled: name === 'core/block' || templateLock === 'contentOnly',
} ),
] );

const blockEditContext = useBlockEditContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,53 @@ import { useSelect } from '@wordpress/data';
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';

export default function useFlashEditableBlocks( rootClientId = '' ) {
export function useFlashEditableBlocks( {
clientId = '',
isEnabled = true,
} = {} ) {
const { getEnabledClientIdsTree } = unlock( useSelect( blockEditorStore ) );

return useRefEffect( ( element ) => {
const flashEditableBlocks = () => {
getEnabledClientIdsTree( rootClientId ).forEach(
( { clientId } ) => {
const blockElement = element.querySelector(
`[data-block="${ clientId }"]`
);
if ( ! blockElement ) {
return;
}
blockElement.classList.remove( 'has-editable-outline' );
// Force reflow to trigger the animation.
// eslint-disable-next-line no-unused-expressions
blockElement.offsetWidth;
blockElement.classList.add( 'has-editable-outline' );
}
);
};

const handleClick = ( event ) => {
const shouldFlash =
event.target === element ||
event.target.classList.contains( 'is-root-container' );
if ( ! shouldFlash ) {
return;
}
if ( event.defaultPrevented ) {
return useRefEffect(
( element ) => {
if ( ! isEnabled ) {
return;
}
event.preventDefault();
flashEditableBlocks();
};

element.addEventListener( 'click', handleClick );
return () => {
element.removeEventListener( 'click', handleClick );
};
} );
const flashEditableBlocks = () => {
getEnabledClientIdsTree( clientId ).forEach(
( { clientId: id } ) => {
const block = element.querySelector(
`[data-block="${ id }"]`
);
if ( ! block ) {
return;
}
block.classList.remove( 'has-editable-outline' );
// Force reflow to trigger the animation.
// eslint-disable-next-line no-unused-expressions
block.offsetWidth;
block.classList.add( 'has-editable-outline' );
}
);
};

const handleClick = ( event ) => {
const shouldFlash =
event.target === element ||
event.target.classList.contains( 'is-root-container' );
if ( ! shouldFlash ) {
return;
}
if ( event.defaultPrevented ) {
return;
}
event.preventDefault();
flashEditableBlocks();
};

element.addEventListener( 'click', handleClick );
return () => element.removeEventListener( 'click', handleClick );
},
[ isEnabled ]
);
}
2 changes: 1 addition & 1 deletion packages/block-editor/src/private-apis.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { usesContextKey } from './components/rich-text/format-edit';
import { ExperimentalBlockCanvas } from './components/block-canvas';
import { getDuotoneFilter } from './components/duotone/utils';
import useFlashEditableBlocks from './components/use-flash-editable-blocks';
import { useFlashEditableBlocks } from './components/use-flash-editable-blocks';

/**
* Private @wordpress/block-editor APIs.
Expand Down
5 changes: 1 addition & 4 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ import { parse, cloneBlock } from '@wordpress/blocks';
*/
import { unlock } from '../lock-unlock';

const { useLayoutClasses, useFlashEditableBlocks } = unlock(
blockEditorPrivateApis
);
const { useLayoutClasses } = unlock( blockEditorPrivateApis );
const { PARTIAL_SYNCING_SUPPORTED_BLOCKS } = unlock( patternsPrivateApis );

function isPartiallySynced( block ) {
Expand Down Expand Up @@ -314,7 +312,6 @@ export default function ReusableBlockEdit( {
const layoutClasses = useLayoutClasses( { layout }, name );

const blockProps = useBlockProps( {
ref: useFlashEditableBlocks( patternClientId ),
className: classnames(
'block-library-block__reusable-block-container',
layout && layoutClasses,
Expand Down
5 changes: 3 additions & 2 deletions packages/editor/src/components/editor-canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,12 @@ function EditorCanvas( {

const localRef = useRef();
const typewriterRef = useTypewriter();
const flashEditableBlocksRef = useFlashEditableBlocks();
const contentRef = useMergeRefs( [
localRef,
renderingMode === 'post-only' ? typewriterRef : noop,
renderingMode === 'template-locked' ? flashEditableBlocksRef : noop,
useFlashEditableBlocks( {
isEnabled: renderingMode === 'template-locked',
} ),
] );

return (
Expand Down

0 comments on commit 9eca467

Please sign in to comment.