Skip to content

Commit

Permalink
Initial commit:
Browse files Browse the repository at this point in the history
- Prevent footnote creation withing core/block
- Only insert a footnote if one isn't found in the entity block list
  • Loading branch information
ramonjd committed Jul 26, 2023
1 parent 168bce1 commit 165e6c7
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions packages/block-library/src/footnotes/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '@wordpress/block-editor';
import { useSelect, useDispatch, useRegistry } from '@wordpress/data';
import { createBlock, store as blocksStore } from '@wordpress/blocks';
import { useEntityBlockEditor } from '@wordpress/core-data';

/**
* Internal dependencies
Expand All @@ -34,23 +35,26 @@ export const format = {
'data-fn': 'data-fn',
},
contentEditable: false,
[ usesContextKey ]: [ 'postType' ],
[ usesContextKey ]: [ 'postType', 'postId' ],
edit: function Edit( {
value,
onChange,
isObjectActive,
context: { postType },
context: { postType, postId },
} ) {
const registry = useRegistry();
const {
getSelectedBlockClientId,
getBlockRootClientId,
getBlockName,
getBlocks,
getBlockParentsByBlockName,
} = useSelect( blockEditorStore );
const footnotesBlockType = useSelect( ( select ) =>
select( blocksStore ).getBlockType( name )
);
const [ blocks ] = useEntityBlockEditor( 'postType', postType, {
id: postId,
} );
const { selectionChange, insertBlock } =
useDispatch( blockEditorStore );

Expand All @@ -62,6 +66,16 @@ export const format = {
return null;
}

// Checks if the selected block lives within a pattern.
if (
getBlockParentsByBlockName(
getSelectedBlockClientId(),
'core/block'
).length > 0
) {
return null;
}

function onClick() {
registry.batch( () => {
let id;
Expand All @@ -86,19 +100,8 @@ export const format = {
onChange( newValue );
}

// BFS search to find the first footnote block.
let fnBlock = null;
{
const queue = [ ...getBlocks() ];
while ( queue.length ) {
const block = queue.shift();
if ( block.name === name ) {
fnBlock = block;
break;
}
queue.push( ...block.innerBlocks );
}
}
// Finds the first footnote block.
let fnBlock = blocks?.find( ( block ) => block.name === name );

// Maybe this should all also be moved to the entity provider.
// When there is no footnotes block in the post, create one and
Expand Down

0 comments on commit 165e6c7

Please sign in to comment.