UNPKG

@wordpress/block-library

Version:
106 lines (105 loc) • 5.32 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let uuid = require("uuid"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_icons = require("@wordpress/icons"); let _wordpress_rich_text = require("@wordpress/rich-text"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_data = require("@wordpress/data"); let _wordpress_core_data = require("@wordpress/core-data"); let _wordpress_blocks = require("@wordpress/blocks"); let ___lock_unlock_ts = require("../lock-unlock.cjs"); let __index_js = require("./index.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/footnotes/format.jsx const formatName = "core/footnote"; const { usesContextKey } = (0, ___lock_unlock_ts.unlock)(_wordpress_block_editor.privateApis); const POST_CONTENT_BLOCK_NAME = "core/post-content"; const SYNCED_PATTERN_BLOCK_NAME = "core/block"; /** * Finds the first footnotes block in the post content, searching breadth * first. Inner blocks of synced patterns are not searched. * * @param {Function} getBlocks The block editor's `getBlocks` selector. * @param {string} [postContentClientId] Client ID of the post content block, if * there is one. * @return {?Object} The footnotes block, or `null` if there is none. */ function findFootnotesBlock(getBlocks, postContentClientId) { const queue = [...getBlocks(postContentClientId)]; while (queue.length) { const block = queue.shift(); if (block.name === __index_js.name) return block; queue.push(...block.innerBlocks); } return null; } const format = { title: (0, _wordpress_i18n.__)("Footnote"), tagName: "sup", className: "fn", attributes: { "data-fn": "data-fn" }, interactive: true, contentEditable: false, [usesContextKey]: ["postType", "postId"], edit: function Edit({ value, onChange, isObjectActive, context: { postType, postId } }) { const registry = (0, _wordpress_data.useRegistry)(); const { getSelectedBlockClientId, getBlocks, getBlockRootClientId, getBlockName, getBlockParentsByBlockName } = registry.select(_wordpress_block_editor.store); const isFootnotesSupported = (0, _wordpress_data.useSelect)((select) => { if (!select(_wordpress_blocks.store).getBlockType("core/footnotes")) return false; const allowedBlocks = select(_wordpress_block_editor.store).getSettings().allowedBlockTypes; if (allowedBlocks === false || Array.isArray(allowedBlocks) && !allowedBlocks.includes("core/footnotes")) return false; if ("string" !== typeof select(_wordpress_core_data.store).getEntityRecord("postType", postType, postId)?.meta?.footnotes) return false; const { getBlockParentsByBlockName: _getBlockParentsByBlockName, getSelectedBlockClientId: _getSelectedBlockClientId, getBlockName: _getBlockName, getBlocks: _getBlocks, canInsertBlockType: _canInsertBlockType } = select(_wordpress_block_editor.store); const selectedClientId = _getSelectedBlockClientId(); if (!selectedClientId) return false; if (_getBlockName(selectedClientId) === __index_js.name) return false; const parentPostContent = _getBlockParentsByBlockName(selectedClientId, POST_CONTENT_BLOCK_NAME); const rootClientId = parentPostContent.length ? parentPostContent[parentPostContent.length - 1] : null; if (!_canInsertBlockType(__index_js.name, rootClientId) && !findFootnotesBlock(_getBlocks, parentPostContent[0])) return false; const parentCoreBlocks = _getBlockParentsByBlockName(selectedClientId, SYNCED_PATTERN_BLOCK_NAME); return !parentCoreBlocks || parentCoreBlocks.length === 0; }, [postType, postId]); const { selectionChange, insertBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); if (!isFootnotesSupported) return null; function onClick() { registry.batch(() => { let id; if (isObjectActive) id = value.replacements[value.start]?.attributes?.["data-fn"]; else { id = `fn-${(0, uuid.v4)()}`; const newValue = (0, _wordpress_rich_text.insertObject)(value, { type: formatName, attributes: { "data-fn": id }, innerHTML: `<a href="#${id}" id="${id}-link">*</a>` }, value.end, value.end); newValue.start = newValue.end - 1; onChange(newValue); } const selectedClientId = getSelectedBlockClientId(); const parentPostContent = getBlockParentsByBlockName(selectedClientId, POST_CONTENT_BLOCK_NAME); let fnBlock = findFootnotesBlock(getBlocks, parentPostContent[0]); if (!fnBlock) { let rootClientId = getBlockRootClientId(selectedClientId); while (rootClientId && getBlockName(rootClientId) !== POST_CONTENT_BLOCK_NAME) rootClientId = getBlockRootClientId(rootClientId); fnBlock = (0, _wordpress_blocks.createBlock)("core/footnotes"); insertBlock(fnBlock, void 0, rootClientId); } selectionChange(fnBlock.clientId, id, 0, 0); }); } return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.RichTextToolbarButton, { name: "footnote", icon: _wordpress_icons.formatListNumbered, title: (0, _wordpress_i18n.__)("Footnote"), onClick, isActive: isObjectActive }); } }; exports.format = format; exports.formatName = formatName; //# sourceMappingURL=format.cjs.map