From 2bc28f107c1f422555f328eef3f4418665ac2d86 Mon Sep 17 00:00:00 2001 From: Miguel Fonseca <150562+mcsf@users.noreply.github.com> Date: Tue, 11 Jul 2023 11:18:19 +0100 Subject: [PATCH] Allow editing existing footnote from formats toolbar --- .../block-library/src/footnotes/format.js | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/packages/block-library/src/footnotes/format.js b/packages/block-library/src/footnotes/format.js index 40de6a132ea99d..eb700787d02eeb 100644 --- a/packages/block-library/src/footnotes/format.js +++ b/packages/block-library/src/footnotes/format.js @@ -40,24 +40,30 @@ export const format = { } = useSelect( blockEditorStore ); const { selectionChange, insertBlock } = useDispatch( blockEditorStore ); + function onClick() { registry.batch( () => { - const id = createId(); - const newValue = insertObject( - value, - { - type: formatName, - attributes: { - 'data-fn': id, + let id; + if ( isObjectActive ) { + const object = value.replacements[ value.start ]; + id = object?.attributes?.[ 'data-fn' ]; + } else { + id = createId(); + const newValue = insertObject( + value, + { + type: formatName, + attributes: { + 'data-fn': id, + }, + innerHTML: `*`, }, - innerHTML: `*`, - }, - value.end, - value.end - ); - newValue.start = newValue.end - 1; - - onChange( newValue ); + value.end, + value.end + ); + newValue.start = newValue.end - 1; + onChange( newValue ); + } // BFS search to find the first footnote block. let fnBlock = null;