UNPKG

@wordpress/core-data

Version:
135 lines (133 loc) 4.82 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/core-data/src/footnotes/index.js var footnotes_exports = {}; __export(footnotes_exports, { updateFootnotesFromMeta: () => updateFootnotesFromMeta }); module.exports = __toCommonJS(footnotes_exports); var import_rich_text = require("@wordpress/rich-text"); var import_get_footnotes_order = __toESM(require("./get-footnotes-order")); var oldFootnotes = {}; function updateFootnotesFromMeta(blocks, meta) { const output = { blocks }; if (!meta) { return output; } if (meta.footnotes === void 0) { return output; } const newOrder = (0, import_get_footnotes_order.default)(blocks); const footnotes = meta.footnotes ? JSON.parse(meta.footnotes) : []; const currentOrder = footnotes.map((fn) => fn.id); if (currentOrder.join("") === newOrder.join("")) { return output; } const newFootnotes = newOrder.map( (fnId) => footnotes.find((fn) => fn.id === fnId) || oldFootnotes[fnId] || { id: fnId, content: "" } ); function updateAttributes(attributes) { if (!attributes || Array.isArray(attributes) || typeof attributes !== "object") { return attributes; } attributes = { ...attributes }; for (const key in attributes) { const value = attributes[key]; if (Array.isArray(value)) { attributes[key] = value.map(updateAttributes); continue; } if (typeof value !== "string" && !(value instanceof import_rich_text.RichTextData)) { continue; } const richTextValue = typeof value === "string" ? import_rich_text.RichTextData.fromHTMLString(value) : new import_rich_text.RichTextData(value); let hasFootnotes = false; richTextValue.replacements.forEach((replacement) => { if (replacement.type === "core/footnote") { const id = replacement.attributes["data-fn"]; const index = newOrder.indexOf(id); const countValue = (0, import_rich_text.create)({ html: replacement.innerHTML }); countValue.text = String(index + 1); countValue.formats = Array.from( { length: countValue.text.length }, () => countValue.formats[0] ); countValue.replacements = Array.from( { length: countValue.text.length }, () => countValue.replacements[0] ); replacement.innerHTML = (0, import_rich_text.toHTMLString)({ value: countValue }); hasFootnotes = true; } }); if (hasFootnotes) { attributes[key] = typeof value === "string" ? richTextValue.toHTMLString() : richTextValue; } } return attributes; } function updateBlocksAttributes(__blocks) { return __blocks.map((block) => { return { ...block, attributes: updateAttributes(block.attributes), innerBlocks: updateBlocksAttributes(block.innerBlocks) }; }); } const newBlocks = updateBlocksAttributes(blocks); oldFootnotes = { ...oldFootnotes, ...footnotes.reduce((acc, fn) => { if (!newOrder.includes(fn.id)) { acc[fn.id] = fn; } return acc; }, {}) }; return { meta: { ...meta, footnotes: JSON.stringify(newFootnotes) }, blocks: newBlocks }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { updateFootnotesFromMeta }); //# sourceMappingURL=index.js.map