UNPKG

@wordpress/block-editor

Version:
190 lines (188 loc) 7.35 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/block-editor/src/components/content-only-controls/rich-text/index.js var rich_text_exports = {}; __export(rich_text_exports, { default: () => RichTextControl }); module.exports = __toCommonJS(rich_text_exports); var import_components = require("@wordpress/components"); var import_compose = require("@wordpress/compose"); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_rich_text = require("@wordpress/rich-text"); var import_use_format_types = require("../../rich-text/use-format-types"); var import_utils = require("../../rich-text/utils"); var import_event_listeners = require("../../rich-text/event-listeners"); var import_format_edit = __toESM(require("../../rich-text/format-edit")); var import_rich_text2 = require("../../rich-text"); var import_jsx_runtime = require("react/jsx-runtime"); function RichTextControl({ data, field, hideLabelFromVision, onChange, config = {} }) { const registry = (0, import_data.useRegistry)(); const attrValue = field.getValue({ item: data }); const fieldConfig = field.config || {}; const { clientId } = config; const updateAttributes = (html) => { const mappedChanges = field.setValue({ item: data, value: html }); onChange(mappedChanges); }; const [selection, setSelection] = (0, import_element.useState)({ start: void 0, end: void 0 }); const [isSelected, setIsSelected] = (0, import_element.useState)(false); const anchorRef = (0, import_element.useRef)(); const inputEvents = (0, import_element.useRef)(/* @__PURE__ */ new Set()); const keyboardShortcuts = (0, import_element.useRef)(/* @__PURE__ */ new Set()); const adjustedAllowedFormats = (0, import_utils.getAllowedFormats)({ allowedFormats: fieldConfig?.allowedFormats, disableFormats: fieldConfig?.disableFormats }); const { formatTypes, prepareHandlers, valueHandlers, changeHandlers, dependencies } = (0, import_use_format_types.useFormatTypes)({ clientId, identifier: field.id, allowedFormats: adjustedAllowedFormats, withoutInteractiveFormatting: fieldConfig?.withoutInteractiveFormatting, disableNoneEssentialFormatting: true }); function addEditorOnlyFormats(value2) { return valueHandlers.reduce( (accumulator, fn) => fn(accumulator, value2.text), value2.formats ); } function removeEditorOnlyFormats(value2) { formatTypes.forEach((formatType) => { if (formatType.__experimentalCreatePrepareEditableTree) { value2 = (0, import_rich_text.removeFormat)( value2, formatType.name, 0, value2.text.length ); } }); return value2.formats; } function addInvisibleFormats(value2) { return prepareHandlers.reduce( (accumulator, fn) => fn(accumulator, value2.text), value2.formats ); } function onFocus() { anchorRef.current?.focus(); } const { value, getValue, onChange: onRichTextChange, ref: richTextRef } = (0, import_rich_text.__unstableUseRichText)({ value: attrValue, onChange(html, { __unstableFormats, __unstableText }) { updateAttributes(html); Object.values(changeHandlers).forEach((changeHandler) => { changeHandler(__unstableFormats, __unstableText); }); }, selectionStart: selection.start, selectionEnd: selection.end, onSelectionChange: (start, end) => setSelection({ start, end }), __unstableIsSelected: isSelected, preserveWhiteSpace: !!fieldConfig?.preserveWhiteSpace, placeholder: fieldConfig?.placeholder, __unstableDisableFormats: fieldConfig?.disableFormats, __unstableDependencies: dependencies, __unstableAfterParse: addEditorOnlyFormats, __unstableBeforeSerialize: removeEditorOnlyFormats, __unstableAddInvisibleFormats: addInvisibleFormats }); const { baseControlProps, controlProps } = (0, import_components.useBaseControlProps)({ hideLabelFromVision: hideLabelFromVision ?? field.hideLabelFromVision, label: field.label }); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ isSelected && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_rich_text2.keyboardShortcutContext.Provider, { value: keyboardShortcuts, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_rich_text2.inputEventContext.Provider, { value: inputEvents, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_format_edit.default, { value, onChange: onRichTextChange, onFocus, formatTypes, forwardedRef: anchorRef, isVisible: false } ) }) }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.BaseControl, { __nextHasNoMarginBottom: true, ...baseControlProps, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "div", { className: "block-editor-content-only-controls__rich-text", role: "textbox", "aria-multiline": !fieldConfig?.disableLineBreaks, ref: (0, import_compose.useMergeRefs)([ richTextRef, (0, import_event_listeners.useEventListeners)({ registry, getValue, onChange: onRichTextChange, formatTypes, selectionChange: setSelection, isSelected, disableFormats: fieldConfig?.disableFormats, value, tagName: "div", removeEditorOnlyFormats, disableLineBreaks: fieldConfig?.disableLineBreaks, keyboardShortcuts, inputEvents }), anchorRef ]), onFocus: () => setIsSelected(true), onBlur: () => setIsSelected(false), contentEditable: true, ...controlProps } ) }) ] }); } //# sourceMappingURL=index.js.map