@wordpress/block-editor
Version:
506 lines (504 loc) • 18.4 kB
JavaScript
;
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/rich-text/index.js
var rich_text_exports = {};
__export(rich_text_exports, {
PrivateRichText: () => PrivateRichText,
RichTextShortcut: () => import_shortcut.RichTextShortcut,
RichTextToolbarButton: () => import_toolbar_button.RichTextToolbarButton,
RichTextWrapper: () => RichTextWrapper,
__unstableRichTextInputEvent: () => import_input_event.__unstableRichTextInputEvent,
default: () => rich_text_default,
inputEventContext: () => inputEventContext,
keyboardShortcutContext: () => keyboardShortcutContext
});
module.exports = __toCommonJS(rich_text_exports);
var import_clsx = __toESM(require("clsx"));
var import_es6 = __toESM(require("fast-deep-equal/es6"));
var import_element = require("@wordpress/element");
var import_data = require("@wordpress/data");
var import_compose = require("@wordpress/compose");
var import_rich_text = require("@wordpress/rich-text");
var import_components = require("@wordpress/components");
var import_blocks = require("@wordpress/blocks");
var import_deprecated = __toESM(require("@wordpress/deprecated"));
var import_i18n = require("@wordpress/i18n");
var import_autocomplete = require("../autocomplete");
var import_block_edit = require("../block-edit");
var import_context = require("../block-edit/context");
var import_format_toolbar_container = __toESM(require("./format-toolbar-container"));
var import_store = require("../../store");
var import_use_mark_persistent = require("./use-mark-persistent");
var import_use_format_types = require("./use-format-types");
var import_event_listeners = require("./event-listeners");
var import_format_edit = __toESM(require("./format-edit"));
var import_utils = require("./utils");
var import_content = require("./content");
var import_with_deprecations = require("./with-deprecations");
var import_block_context = __toESM(require("../block-context"));
var import_private_block_context = require("../block-list/private-block-context");
var import_shortcut = require("./shortcut");
var import_toolbar_button = require("./toolbar-button");
var import_input_event = require("./input-event");
var import_jsx_runtime = require("react/jsx-runtime");
var keyboardShortcutContext = (0, import_element.createContext)();
keyboardShortcutContext.displayName = "keyboardShortcutContext";
var inputEventContext = (0, import_element.createContext)();
inputEventContext.displayName = "inputEventContext";
var instanceIdKey = Symbol("instanceId");
function removeNativeProps(props) {
const {
__unstableMobileNoFocusOnMount,
deleteEnter,
placeholderTextColor,
textAlign,
selectionColor,
tagsToEliminate,
disableEditingMenu,
fontSize,
fontFamily,
fontWeight,
fontStyle,
minWidth,
maxWidth,
disableSuggestions,
disableAutocorrection,
...restProps
} = props;
return restProps;
}
function RichTextWrapper({
children,
tagName = "div",
value: adjustedValue = "",
onChange: adjustedOnChange,
isSelected: originalIsSelected,
multiline,
inlineToolbar,
wrapperClassName,
autocompleters,
onReplace,
placeholder,
allowedFormats,
withoutInteractiveFormatting,
onRemove,
onMerge,
onSplit,
__unstableOnSplitAtEnd: onSplitAtEnd,
__unstableOnSplitAtDoubleLineEnd: onSplitAtDoubleLineEnd,
identifier,
preserveWhiteSpace,
__unstablePastePlainText: pastePlainText,
__unstableEmbedURLOnPaste,
__unstableDisableFormats: disableFormats,
disableLineBreaks,
__unstableAllowPrefixTransformations,
readOnly,
...props
}, forwardedRef) {
props = removeNativeProps(props);
if (onSplit) {
(0, import_deprecated.default)("wp.blockEditor.RichText onSplit prop", {
since: "6.4",
alternative: 'block.json support key: "splitting"'
});
}
const instanceId = (0, import_compose.useInstanceId)(RichTextWrapper);
const anchorRef = (0, import_element.useRef)();
const context = (0, import_block_edit.useBlockEditContext)();
const { clientId, isSelected: isBlockSelected } = context;
const blockBindings = context[import_context.blockBindingsKey];
const blockContext = (0, import_element.useContext)(import_block_context.default);
const { bindableAttributes } = (0, import_element.useContext)(import_private_block_context.PrivateBlockContext);
const registry = (0, import_data.useRegistry)();
const selector = (select) => {
if (!isBlockSelected) {
return { isSelected: false };
}
const { getSelectionStart: getSelectionStart2, getSelectionEnd: getSelectionEnd2, getBlockEditingMode } = select(import_store.store);
const selectionStart2 = getSelectionStart2();
const selectionEnd2 = getSelectionEnd2();
let isSelected2;
if (originalIsSelected === void 0) {
isSelected2 = selectionStart2.clientId === clientId && selectionEnd2.clientId === clientId && (identifier ? selectionStart2.attributeKey === identifier : selectionStart2[instanceIdKey] === instanceId);
} else if (originalIsSelected) {
isSelected2 = selectionStart2.clientId === clientId;
}
return {
selectionStart: isSelected2 ? selectionStart2.offset : void 0,
selectionEnd: isSelected2 ? selectionEnd2.offset : void 0,
isSelected: isSelected2,
isContentOnly: getBlockEditingMode(clientId) === "contentOnly"
};
};
const { selectionStart, selectionEnd, isSelected, isContentOnly } = (0, import_data.useSelect)(selector, [
clientId,
identifier,
instanceId,
originalIsSelected,
isBlockSelected
]);
const { disableBoundBlock, bindingsPlaceholder, bindingsLabel } = (0, import_data.useSelect)(
(select) => {
if (!blockBindings?.[identifier] || !bindableAttributes) {
return {};
}
const relatedBinding = blockBindings[identifier];
const blockBindingsSource = (0, import_blocks.getBlockBindingsSource)(
relatedBinding.source
);
const blockBindingsContext = {};
if (blockBindingsSource?.usesContext?.length) {
for (const key of blockBindingsSource.usesContext) {
blockBindingsContext[key] = blockContext[key];
}
}
const _disableBoundBlock = !blockBindingsSource?.canUserEditValue?.({
select,
context: blockBindingsContext,
args: relatedBinding.args
});
if (adjustedValue.length > 0) {
return {
disableBoundBlock: _disableBoundBlock,
// Null values will make them fall back to the default behavior.
bindingsPlaceholder: null,
bindingsLabel: null
};
}
const { getBlockAttributes } = select(import_store.store);
const blockAttributes = getBlockAttributes(clientId);
let clientSideFieldLabel = null;
if (blockBindingsSource?.getFieldsList) {
const fieldsItems = blockBindingsSource.getFieldsList({
select,
context: blockBindingsContext
});
clientSideFieldLabel = fieldsItems?.find(
(item) => (0, import_es6.default)(item.args, relatedBinding?.args)
)?.label;
}
const bindingKey = clientSideFieldLabel ?? blockBindingsSource?.label;
const _bindingsPlaceholder = _disableBoundBlock ? bindingKey : (0, import_i18n.sprintf)(
/* translators: %s: connected field label or source label */
(0, import_i18n.__)("Add %s"),
bindingKey
);
const _bindingsLabel = _disableBoundBlock ? relatedBinding?.args?.key || blockBindingsSource?.label : (0, import_i18n.sprintf)(
/* translators: %s: source label or key */
(0, import_i18n.__)("Empty %s; start writing to edit its value"),
relatedBinding?.args?.key || blockBindingsSource?.label
);
return {
disableBoundBlock: _disableBoundBlock,
bindingsPlaceholder: blockAttributes?.placeholder || _bindingsPlaceholder,
bindingsLabel: _bindingsLabel
};
},
[
blockBindings,
identifier,
bindableAttributes,
adjustedValue,
clientId,
blockContext
]
);
const isInsidePatternOverrides = !!blockContext?.["pattern/overrides"];
const hasOverrideEnabled = blockBindings?.__default?.source === "core/pattern-overrides";
const shouldDisableForPattern = isInsidePatternOverrides && !hasOverrideEnabled;
const shouldDisableEditing = readOnly || disableBoundBlock || shouldDisableForPattern;
const { getSelectionStart, getSelectionEnd, getBlockRootClientId } = (0, import_data.useSelect)(import_store.store);
const { selectionChange } = (0, import_data.useDispatch)(import_store.store);
const adjustedAllowedFormats = (0, import_utils.getAllowedFormats)({
allowedFormats,
disableFormats
});
const hasFormats = !adjustedAllowedFormats || adjustedAllowedFormats.length > 0;
const onSelectionChange = (0, import_element.useCallback)(
(start, end) => {
const selection = {};
const unset = start === void 0 && end === void 0;
const baseSelection = {
clientId,
[identifier ? "attributeKey" : instanceIdKey]: identifier ? identifier : instanceId
};
if (typeof start === "number" || unset) {
if (end === void 0 && getBlockRootClientId(clientId) !== getBlockRootClientId(getSelectionEnd().clientId)) {
return;
}
selection.start = {
...baseSelection,
offset: start
};
}
if (typeof end === "number" || unset) {
if (start === void 0 && getBlockRootClientId(clientId) !== getBlockRootClientId(getSelectionStart().clientId)) {
return;
}
selection.end = {
...baseSelection,
offset: end
};
}
selectionChange(selection);
},
[
clientId,
getBlockRootClientId,
getSelectionEnd,
getSelectionStart,
identifier,
instanceId,
selectionChange
]
);
const {
formatTypes,
prepareHandlers,
valueHandlers,
changeHandlers,
dependencies
} = (0, import_use_format_types.useFormatTypes)({
clientId,
identifier,
allowedFormats: adjustedAllowedFormats,
withoutInteractiveFormatting,
disableNoneEssentialFormatting: isContentOnly
});
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
);
}
const {
value,
getValue,
onChange,
ref: richTextRef
} = (0, import_rich_text.__unstableUseRichText)({
value: adjustedValue,
onChange(html, { __unstableFormats, __unstableText }) {
adjustedOnChange(html);
Object.values(changeHandlers).forEach((changeHandler) => {
changeHandler(__unstableFormats, __unstableText);
});
},
selectionStart,
selectionEnd,
onSelectionChange,
placeholder: bindingsPlaceholder || placeholder,
__unstableIsSelected: isSelected,
__unstableDisableFormats: disableFormats,
preserveWhiteSpace,
__unstableDependencies: [...dependencies, tagName],
__unstableAfterParse: addEditorOnlyFormats,
__unstableBeforeSerialize: removeEditorOnlyFormats,
__unstableAddInvisibleFormats: addInvisibleFormats
});
const autocompleteProps = (0, import_autocomplete.useBlockEditorAutocompleteProps)({
onReplace,
completers: autocompleters,
record: value,
onChange
});
(0, import_use_mark_persistent.useMarkPersistent)({ html: adjustedValue, value });
const keyboardShortcuts = (0, import_element.useRef)(/* @__PURE__ */ new Set());
const inputEvents = (0, import_element.useRef)(/* @__PURE__ */ new Set());
function onFocus() {
anchorRef.current?.focus();
}
const TagName = tagName;
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
isSelected && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(keyboardShortcutContext.Provider, { value: keyboardShortcuts, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(inputEventContext.Provider, { value: inputEvents, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.Popover.__unstableSlotNameProvider, { value: "__unstable-block-tools-after", children: [
children && children({ value, onChange, onFocus }),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_format_edit.default,
{
value,
onChange,
onFocus,
formatTypes,
forwardedRef: anchorRef
}
)
] }) }) }),
isSelected && hasFormats && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
import_format_toolbar_container.default,
{
inline: inlineToolbar,
editableContentElement: anchorRef.current
}
),
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
TagName,
{
role: "textbox",
"aria-multiline": !disableLineBreaks,
"aria-readonly": shouldDisableEditing,
...props,
draggable: void 0,
"aria-label": bindingsLabel || props["aria-label"] || placeholder,
...autocompleteProps,
ref: (0, import_compose.useMergeRefs)([
// Rich text ref must be first because its focus listener
// must be set up before any other ref calls .focus() on
// mount.
richTextRef,
forwardedRef,
autocompleteProps.ref,
props.ref,
(0, import_event_listeners.useEventListeners)({
registry,
getValue,
onChange,
__unstableAllowPrefixTransformations,
formatTypes,
onReplace,
selectionChange,
isSelected,
disableFormats,
value,
tagName,
onSplit,
__unstableEmbedURLOnPaste,
pastePlainText,
onMerge,
onRemove,
removeEditorOnlyFormats,
disableLineBreaks,
onSplitAtEnd,
onSplitAtDoubleLineEnd,
keyboardShortcuts,
inputEvents
}),
anchorRef
]),
contentEditable: !shouldDisableEditing,
suppressContentEditableWarning: true,
className: (0, import_clsx.default)(
"block-editor-rich-text__editable",
props.className,
"rich-text"
),
tabIndex: props.tabIndex === 0 && !shouldDisableEditing ? null : props.tabIndex,
"data-wp-block-attribute-key": identifier
}
)
] });
}
var PrivateRichText = (0, import_with_deprecations.withDeprecations)(
(0, import_element.forwardRef)(RichTextWrapper)
);
PrivateRichText.Content = import_content.Content;
PrivateRichText.isEmpty = (value) => {
return !value || value.length === 0;
};
var PublicForwardedRichTextContainer = (0, import_element.forwardRef)((props, ref) => {
const context = (0, import_block_edit.useBlockEditContext)();
const isPreviewMode = context[import_context.isPreviewModeKey];
if (isPreviewMode) {
const {
children,
tagName: Tag = "div",
value,
onChange,
isSelected,
multiline,
inlineToolbar,
wrapperClassName,
autocompleters,
onReplace,
placeholder,
allowedFormats,
withoutInteractiveFormatting,
onRemove,
onMerge,
onSplit,
__unstableOnSplitAtEnd,
__unstableOnSplitAtDoubleLineEnd,
identifier,
preserveWhiteSpace,
__unstablePastePlainText,
__unstableEmbedURLOnPaste,
__unstableDisableFormats,
disableLineBreaks,
__unstableAllowPrefixTransformations,
readOnly,
...contentProps
} = removeNativeProps(props);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
Tag,
{
ref,
...contentProps,
dangerouslySetInnerHTML: {
__html: (0, import_content.valueToHTMLString)(value, multiline)
}
}
);
}
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PrivateRichText, { ref, ...props, readOnly: false });
});
PublicForwardedRichTextContainer.Content = import_content.Content;
PublicForwardedRichTextContainer.isEmpty = (value) => {
return !value || value.length === 0;
};
var rich_text_default = PublicForwardedRichTextContainer;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PrivateRichText,
RichTextShortcut,
RichTextToolbarButton,
RichTextWrapper,
__unstableRichTextInputEvent,
inputEventContext,
keyboardShortcutContext
});
//# sourceMappingURL=index.js.map