@wordpress/block-library
Version:
Block library for the WordPress editor.
79 lines (78 loc) • 3.06 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_core_data = require("@wordpress/core-data");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_components = require("@wordpress/components");
let _wordpress_icons = require("@wordpress/icons");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/footnotes/edit.jsx
function FootnotesEdit({ context: { postType, postId } }) {
const [meta, updateMeta] = (0, _wordpress_core_data.useEntityProp)("postType", postType, "meta", postId);
const footnotesSupported = "string" === typeof meta?.footnotes;
let parsed;
try {
parsed = JSON.parse(meta?.footnotes || "[]");
} catch {}
const footnotes = Array.isArray(parsed) ? parsed : [];
const blockProps = (0, _wordpress_block_editor.useBlockProps)();
if (!footnotesSupported) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Placeholder, {
icon: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockIcon, { icon: _wordpress_icons.formatListNumbered }),
label: (0, _wordpress_i18n.__)("Footnotes"),
instructions: (0, _wordpress_i18n.__)("Footnotes are not supported here. Add this block to post or page content.")
})
});
if (!footnotes.length) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
...blockProps,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Placeholder, {
icon: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockIcon, { icon: _wordpress_icons.formatListNumbered }),
label: (0, _wordpress_i18n.__)("Footnotes"),
instructions: (0, _wordpress_i18n.__)("Footnotes found in blocks within this document will be displayed here.")
})
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("ol", {
...blockProps,
children: footnotes.map(({ id, content }) => /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("li", {
onMouseDown: (event) => {
if (event.target === event.currentTarget) {
event.target.firstElementChild.focus();
event.preventDefault();
}
},
children: [
/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.RichText, {
id,
tagName: "span",
value: content,
identifier: id,
onFocus: (event) => {
if (!event.target.textContent.trim()) event.target.scrollIntoView();
},
onChange: (nextFootnote) => {
updateMeta({
...meta,
footnotes: JSON.stringify(footnotes.map((footnote) => {
return footnote.id === id ? {
content: nextFootnote,
id
} : footnote;
}))
});
}
}),
" ",
/* @__PURE__ */ (0, react_jsx_runtime.jsx)("a", {
href: `#${id}-link`,
children: "↩︎"
})
]
}, id))
});
}
exports.default = FootnotesEdit;
//# sourceMappingURL=edit.cjs.map