{"version":3,"file":"format.cjs","names":["unlock","privateApis","FOOTNOTES_BLOCK_NAME","__","useRegistry","blockEditorStore","useSelect","blocksStore","coreDataStore","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\n/**\n * Finds the first footnotes block in the post content, searching breadth\n * first. Inner blocks of synced patterns are not searched.\n *\n * @param {Function} getBlocks             The block editor's `getBlocks` selector.\n * @param {string}   [postContentClientId] Client ID of the post content block, if\n *                                         there is one.\n * @return {?Object} The footnotes block, or `null` if there is none.\n */\nfunction findFootnotesBlock( getBlocks, postContentClientId ) {\n\t// When called with a post content block, getBlocks will return\n\t// the block with controlled inner blocks included.\n\tconst queue = [ ...getBlocks( postContentClientId ) ];\n\twhile ( queue.length ) {\n\t\tconst block = queue.shift();\n\t\tif ( block.name === FOOTNOTES_BLOCK_NAME ) {\n\t\t\treturn block;\n\t\t}\n\t\tqueue.push( ...block.innerBlocks );\n\t}\n\treturn null;\n}\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\tgetBlocks: _getBlocks,\n\t\t\t\t\tcanInsertBlockType: _canInsertBlockType,\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\t// When the post has no footnotes block, adding a footnote inserts\n\t\t\t\t// one. Don't offer it when that isn't possible, such as in a post\n\t\t\t\t// type with a locked template.\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\t\t\t\tconst rootClientId = parentPostContent.length\n\t\t\t\t\t? parentPostContent[ parentPostContent.length - 1 ]\n\t\t\t\t\t: null;\n\t\t\t\tif (\n\t\t\t\t\t! _canInsertBlockType(\n\t\t\t\t\t\tFOOTNOTES_BLOCK_NAME,\n\t\t\t\t\t\trootClientId\n\t\t\t\t\t) &&\n\t\t\t\t\t! findFootnotesBlock( _getBlocks, parentPostContent[ 0 ] )\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\tlet fnBlock = findFootnotesBlock(\n\t\t\t\t\tgetBlocks,\n\t\t\t\t\tparentPostContent[ 0 ]\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;;;;;;;;;;AAWlC,SAAS,mBAAoB,WAAW,qBAAsB;CAG7D,MAAM,QAAQ,CAAE,GAAG,UAAW,mBAAoB,CAAE;CACpD,OAAQ,MAAM,QAAS;EACtB,MAAM,QAAQ,MAAM,MAAM;EAC1B,IAAK,MAAM,SAASC,WAAAA,MACnB,OAAO;EAER,MAAM,KAAM,GAAG,MAAM,WAAY;CAClC;CACA,OAAO;AACR;AAEA,MAAa,SAAS;CACrB,QAAA,GAAOC,gBAAAA,GAAAA,CAAI,UAAW;CACtB,SAAS;CACT,WAAW;CACX,YAAY,EACX,WAAW,UACZ;CACA,aAAa;CACb,iBAAiB;EACf,iBAAkB,CAAE,YAAY,QAAS;CAC3C,MAAM,SAAS,KAAM,EACpB,OACA,UACA,gBACA,SAAS,EAAE,UAAU,YAClB;EACH,MAAM,YAAA,GAAWC,gBAAAA,YAAAA,CAAY;EAC7B,MAAM,EACL,0BACA,WACA,sBACA,cACA,+BACG,SAAS,OAAQC,wBAAAA,KAAiB;EACtC,MAAM,wBAAA,GAAuBC,gBAAAA,UAAAA,EAC1B,WAAY;GACb,IACC,CAAE,OAAQC,kBAAAA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