UNPKG

@wordpress/format-library

Version:
72 lines (71 loc) 1.82 kB
// packages/format-library/src/code/index.js import { __ } from "@wordpress/i18n"; import { toggleFormat, remove, applyFormat } from "@wordpress/rich-text"; import { RichTextToolbarButton, RichTextShortcut } from "@wordpress/block-editor"; import { code as codeIcon } from "@wordpress/icons"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; var name = "core/code"; var title = __("Inline code"); var code = { name, title, tagName: "code", className: null, __unstableInputRule(value) { const BACKTICK = "`"; const { start, text } = value; const characterBefore = text[start - 1]; if (characterBefore !== BACKTICK) { return value; } if (start - 2 < 0) { return value; } const indexBefore = text.lastIndexOf(BACKTICK, start - 2); if (indexBefore === -1) { return value; } const startIndex = indexBefore; const endIndex = start - 2; if (startIndex === endIndex) { return value; } value = remove(value, startIndex, startIndex + 1); value = remove(value, endIndex, endIndex + 1); value = applyFormat(value, { type: name }, startIndex, endIndex); return value; }, edit({ value, onChange, onFocus, isActive }) { function onClick() { onChange(toggleFormat(value, { type: name, title })); onFocus(); } return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx( RichTextShortcut, { type: "access", character: "x", onUse: onClick } ), /* @__PURE__ */ jsx( RichTextToolbarButton, { icon: codeIcon, title, onClick, isActive, role: "menuitemcheckbox" } ) ] }); } }; export { code }; //# sourceMappingURL=index.js.map