UNPKG

@wordpress/block-library

Version:
100 lines (99 loc) • 4.74 kB
import { v4 } from "uuid"; import { __ } from "@wordpress/i18n"; import { formatListNumbered } from "@wordpress/icons"; import { insertObject } from "@wordpress/rich-text"; import { RichTextToolbarButton, privateApis, store } from "@wordpress/block-editor"; import { useDispatch, useRegistry, useSelect } from "@wordpress/data"; import { store as store$1 } from "@wordpress/core-data"; import { createBlock, store as store$2 } from "@wordpress/blocks"; import { unlock } from "../lock-unlock.mjs"; import { name } from "./index.mjs"; import { jsx } from "react/jsx-runtime"; // packages/block-library/src/footnotes/format.jsx const formatName = "core/footnote"; const { usesContextKey } = unlock(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 === name) return block; queue.push(...block.innerBlocks); } return null; } const format = { title: __("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 = useRegistry(); const { getSelectedBlockClientId, getBlocks, getBlockRootClientId, getBlockName, getBlockParentsByBlockName } = registry.select(store); const isFootnotesSupported = useSelect((select) => { if (!select(store$2).getBlockType("core/footnotes")) return false; const allowedBlocks = select(store).getSettings().allowedBlockTypes; if (allowedBlocks === false || Array.isArray(allowedBlocks) && !allowedBlocks.includes("core/footnotes")) return false; if ("string" !== typeof select(store$1).getEntityRecord("postType", postType, postId)?.meta?.footnotes) return false; const { getBlockParentsByBlockName: _getBlockParentsByBlockName, getSelectedBlockClientId: _getSelectedBlockClientId, getBlockName: _getBlockName, getBlocks: _getBlocks, canInsertBlockType: _canInsertBlockType } = select(store); const selectedClientId = _getSelectedBlockClientId(); if (!selectedClientId) return false; if (_getBlockName(selectedClientId) === name) return false; const parentPostContent = _getBlockParentsByBlockName(selectedClientId, POST_CONTENT_BLOCK_NAME); const rootClientId = parentPostContent.length ? parentPostContent[parentPostContent.length - 1] : null; if (!_canInsertBlockType(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 } = useDispatch(store); if (!isFootnotesSupported) return null; function onClick() { registry.batch(() => { let id; if (isObjectActive) id = value.replacements[value.start]?.attributes?.["data-fn"]; else { id = `fn-${v4()}`; const newValue = 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 = createBlock("core/footnotes"); insertBlock(fnBlock, void 0, rootClientId); } selectionChange(fnBlock.clientId, id, 0, 0); }); } return /* @__PURE__ */ jsx(RichTextToolbarButton, { name: "footnote", icon: formatListNumbered, title: __("Footnote"), onClick, isActive: isObjectActive }); } }; export { format, formatName }; //# sourceMappingURL=format.mjs.map