UNPKG

@wordpress/format-library

Version:
82 lines (79 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.code = void 0; var _i18n = require("@wordpress/i18n"); var _richText = require("@wordpress/rich-text"); var _blockEditor = require("@wordpress/block-editor"); var _icons = require("@wordpress/icons"); var _jsxRuntime = require("react/jsx-runtime"); /** * WordPress dependencies */ const name = 'core/code'; const title = (0, _i18n.__)('Inline code'); const code = exports.code = { name, title, tagName: 'code', className: null, __unstableInputRule(value) { const BACKTICK = '`'; const { start, text } = value; const characterBefore = text[start - 1]; // Quick check the text for the necessary character. 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 = (0, _richText.remove)(value, startIndex, startIndex + 1); value = (0, _richText.remove)(value, endIndex, endIndex + 1); value = (0, _richText.applyFormat)(value, { type: name }, startIndex, endIndex); return value; }, edit({ value, onChange, onFocus, isActive }) { function onClick() { onChange((0, _richText.toggleFormat)(value, { type: name, title })); onFocus(); } return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, { children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.RichTextShortcut, { type: "access", character: "x", onUse: onClick }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.RichTextToolbarButton, { icon: _icons.code, title: title, onClick: onClick, isActive: isActive, role: "menuitemcheckbox" })] }); } }; //# sourceMappingURL=index.js.map