{"version":3,"file":"format.cjs","names":["unlock","privateApis","__","useRegistry","blockEditorStore","useSelect","blocksStore","coreDataStore","FOOTNOTES_BLOCK_NAME","useDispatch","createId","insertObject","createBlock","RichTextToolbarButton","icon"],"sources":["../../src/footnotes/format.jsx"],"sourcesContent":["import { v4 as createId } from 'uuid';\nimport { __ } from '@wordpress/i18n';\nimport { formatListNumbered as icon } from '@wordpress/icons';\nimport { insertObject } from '@wordpress/rich-text';\nimport {\n\tRichTextToolbarButton,\n\tstore as blockEditorStore,\n\tprivateApis,\n} from '@wordpress/block-editor';\nimport { useSelect, useDispatch, useRegistry } from '@wordpress/data';\nimport { store as coreDataStore } from '@wordpress/core-data';\nimport { createBlock, store as blocksStore } from '@wordpress/blocks';\nimport { unlock } from '../lock-unlock';\nimport { name as FOOTNOTES_BLOCK_NAME } from './';\n\nexport const formatName = 'core/footnote';\n\nconst { usesContextKey } = unlock( privateApis );\nconst POST_CONTENT_BLOCK_NAME = 'core/post-content';\nconst SYNCED_PATTERN_BLOCK_NAME = 'core/block';\n\nexport const format = {\n\ttitle: __( 'Footnote' ),\n\ttagName: 'sup',\n\tclassName: 'fn',\n\tattributes: {\n\t\t'data-fn': 'data-fn',\n\t},\n\tinteractive: true,\n\tcontentEditable: false,\n\t[ usesContextKey ]: [ 'postType', 'postId' ],\n\tedit: function Edit( {\n\t\tvalue,\n\t\tonChange,\n\t\tisObjectActive,\n\t\tcontext: { postType, postId },\n\t} ) {\n\t\tconst registry = useRegistry();\n\t\tconst {\n\t\t\tgetSelectedBlockClientId,\n\t\t\tgetBlocks,\n\t\t\tgetBlockRootClientId,\n\t\t\tgetBlockName,\n\t\t\tgetBlockParentsByBlockName,\n\t\t} = registry.select( blockEditorStore );\n\t\tconst isFootnotesSupported = useSelect(\n\t\t\t( select ) => {\n\t\t\t\tif (\n\t\t\t\t\t! select( blocksStore ).getBlockType( 'core/footnotes' )\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\tconst allowedBlocks =\n\t\t\t\t\tselect( blockEditorStore ).getSettings().allowedBlockTypes;\n\t\t\t\tif (\n\t\t\t\t\tallowedBlocks === false ||\n\t\t\t\t\t( Array.isArray( allowedBlocks ) &&\n\t\t\t\t\t\t! allowedBlocks.includes( 'core/footnotes' ) )\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\tconst entityRecord = select( coreDataStore ).getEntityRecord(\n\t\t\t\t\t'postType',\n\t\t\t\t\tpostType,\n\t\t\t\t\tpostId\n\t\t\t\t);\n\n\t\t\t\tif ( 'string' !== typeof entityRecord?.meta?.footnotes ) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\t// Checks if the selected block lives within a pattern.\n\t\t\t\tconst {\n\t\t\t\t\tgetBlockParentsByBlockName: _getBlockParentsByBlockName,\n\t\t\t\t\tgetSelectedBlockClientId: _getSelectedBlockClientId,\n\t\t\t\t\tgetBlockName: _getBlockName,\n\t\t\t\t} = select( blockEditorStore );\n\n\t\t\t\tconst selectedClientId = _getSelectedBlockClientId();\n\n\t\t\t\tif ( ! selectedClientId ) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\t// Check if the selected block itself is a footnotes block.\n\t\t\t\tif (\n\t\t\t\t\t_getBlockName( selectedClientId ) === FOOTNOTES_BLOCK_NAME\n\t\t\t\t) {\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\n\t\t\t\tconst parentCoreBlocks = _getBlockParentsByBlockName(\n\t\t\t\t\tselectedClientId,\n\t\t\t\t\tSYNCED_PATTERN_BLOCK_NAME\n\t\t\t\t);\n\t\t\t\treturn ! parentCoreBlocks || parentCoreBlocks.length === 0;\n\t\t\t},\n\t\t\t[ postType, postId ]\n\t\t);\n\n\t\tconst { selectionChange, insertBlock } =\n\t\t\tuseDispatch( blockEditorStore );\n\n\t\tif ( ! isFootnotesSupported ) {\n\t\t\treturn null;\n\t\t}\n\n\t\tfunction onClick() {\n\t\t\tregistry.batch( () => {\n\t\t\t\tlet id;\n\t\t\t\tif ( isObjectActive ) {\n\t\t\t\t\tconst object = value.replacements[ value.start ];\n\t\t\t\t\tid = object?.attributes?.[ 'data-fn' ];\n\t\t\t\t} else {\n\t\t\t\t\t// The ID doubles as the anchor target of the footnote link.\n\t\t\t\t\t// A CSS identifier cannot start with a digit, so a bare UUID\n\t\t\t\t\t// (which often does) breaks `querySelector( '#' + id )` and\n\t\t\t\t\t// `#id` style rules on the front end. Prefix it with a letter.\n\t\t\t\t\tid = `fn-${ createId() }`;\n\t\t\t\t\tconst newValue = insertObject(\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttype: formatName,\n\t\t\t\t\t\t\tattributes: {\n\t\t\t\t\t\t\t\t'data-fn': id,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tinnerHTML: `<a href=\"#${ id }\" id=\"${ id }-link\">*</a>`,\n\t\t\t\t\t\t},\n\t\t\t\t\t\tvalue.end,\n\t\t\t\t\t\tvalue.end\n\t\t\t\t\t);\n\t\t\t\t\tnewValue.start = newValue.end - 1;\n\t\t\t\t\tonChange( newValue );\n\t\t\t\t}\n\n\t\t\t\tconst selectedClientId = getSelectedBlockClientId();\n\n\t\t\t\t/*\n\t\t\t\t * Attempts to find a common parent post content block.\n\t\t\t\t * This allows for locating blocks within a page edited in the site editor.\n\t\t\t\t */\n\t\t\t\tconst parentPostContent = getBlockParentsByBlockName(\n\t\t\t\t\tselectedClientId,\n\t\t\t\t\tPOST_CONTENT_BLOCK_NAME\n\t\t\t\t);\n\n\t\t\t\t// When called with a post content block, getBlocks will return\n\t\t\t\t// the block with controlled inner blocks included.\n\t\t\t\tconst blocks = parentPostContent.length\n\t\t\t\t\t? getBlocks( parentPostContent[ 0 ] )\n\t\t\t\t\t: getBlocks();\n\n\t\t\t\t// BFS search to find the first footnote block.\n\t\t\t\tlet fnBlock = null;\n\t\t\t\t{\n\t\t\t\t\tconst queue = [ ...blocks ];\n\t\t\t\t\twhile ( queue.length ) {\n\t\t\t\t\t\tconst block = queue.shift();\n\t\t\t\t\t\tif ( block.name === 'core/footnotes' ) {\n\t\t\t\t\t\t\tfnBlock = block;\n\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tqueue.push( ...block.innerBlocks );\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Maybe this should all also be moved to the entity provider.\n\t\t\t\t// When there is no footnotes block in the post, create one and\n\t\t\t\t// insert it at the bottom.\n\t\t\t\tif ( ! fnBlock ) {\n\t\t\t\t\tlet rootClientId = getBlockRootClientId( selectedClientId );\n\n\t\t\t\t\twhile (\n\t\t\t\t\t\trootClientId &&\n\t\t\t\t\t\tgetBlockName( rootClientId ) !== POST_CONTENT_BLOCK_NAME\n\t\t\t\t\t) {\n\t\t\t\t\t\trootClientId = getBlockRootClientId( rootClientId );\n\t\t\t\t\t}\n\n\t\t\t\t\tfnBlock = createBlock( 'core/footnotes' );\n\n\t\t\t\t\tinsertBlock( fnBlock, undefined, rootClientId );\n\t\t\t\t}\n\n\t\t\t\tselectionChange( fnBlock.clientId, id, 0, 0 );\n\t\t\t} );\n\t\t}\n\n\t\treturn (\n\t\t\t<RichTextToolbarButton\n\t\t\t\tname=\"footnote\"\n\t\t\t\ticon={ icon }\n\t\t\t\ttitle={ __( 'Footnote' ) }\n\t\t\t\tonClick={ onClick }\n\t\t\t\tisActive={ isObjectActive }\n\t\t\t/>\n\t\t);\n\t},\n};\n"],"mappings":";;;;;;;;;;;;;;;;;AAeA,MAAa,aAAa;AAE1B,MAAM,EAAE,oBAAA,GAAmBA,kBAAAA,OAAAA,CAAQC,wBAAAA,WAAY;AAC/C,MAAM,0BAA0B;AAChC,MAAM,4BAA4B;AAElC,MAAa,SAAS;CACrB,QAAA,GAAOC,gBAAAA,GAAAA,CAAI,UAAW;CACtB,SAAS;CACT,WAAW;CACX,YAAY,EACX,WAAW,UACZ;CACA,aAAa;CACb,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