UNPKG

@wordpress/block-editor

Version:
422 lines (420 loc) 15.9 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/index.js var content_only_controls_exports = {}; __export(content_only_controls_exports, { default: () => ContentOnlyControls }); module.exports = __toCommonJS(content_only_controls_exports); var import_blocks = require("@wordpress/blocks"); var import_components = require("@wordpress/components"); var import_data = require("@wordpress/data"); var import_i18n = require("@wordpress/i18n"); var import_icons = require("@wordpress/icons"); var import_dataviews = require("@wordpress/dataviews"); var import_element = require("@wordpress/element"); var import_lock_unlock = require("../../lock-unlock"); var import_store = require("../../store"); var import_block_icon = __toESM(require("../block-icon")); var import_use_block_display_title = __toESM(require("../block-title/use-block-display-title")); var import_use_block_display_information = __toESM(require("../use-block-display-information")); var import_fields_dropdown_menu = __toESM(require("./fields-dropdown-menu")); var import_rich_text = __toESM(require("./rich-text")); var import_media = __toESM(require("./media")); var import_link = __toESM(require("./link")); var import_jsx_runtime = require("react/jsx-runtime"); var { fieldsKey, formKey } = (0, import_lock_unlock.unlock)(import_blocks.privateApis); var CONTROLS = { richtext: import_rich_text.default, media: import_media.default, link: import_link.default }; function createConfiguredControl(config) { const { control, ...controlConfig } = config; const ControlComponent = CONTROLS[control]; if (!ControlComponent) { throw new Error(`Control type "${control}" not found`); } return function ConfiguredControl(props) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ControlComponent, { ...props, config: controlConfig }); }; } function normalizeMediaValue(value, fieldDef) { const defaults = { id: null, url: "", caption: "", alt: "", type: "image", poster: "", featuredImage: false, link: "" }; const result = {}; if (fieldDef?.mapping) { Object.keys(fieldDef.mapping).forEach((key) => { result[key] = value?.[key] ?? defaults[key] ?? ""; }); return result; } Object.keys(defaults).forEach((key) => { result[key] = value?.[key] ?? defaults[key]; }); return result; } function denormalizeMediaValue(value, fieldDef) { if (!fieldDef.mapping) { return value; } const result = {}; Object.entries(fieldDef.mapping).forEach(([key]) => { if (key in value) { result[key] = value[key]; } }); return result; } function normalizeLinkValue(value, fieldDef) { const defaults = { url: "", rel: "", linkTarget: "", destination: "" }; const result = {}; if (fieldDef?.mapping) { Object.keys(fieldDef.mapping).forEach((key) => { result[key] = value?.[key] ?? defaults[key] ?? ""; }); return result; } Object.keys(defaults).forEach((key) => { result[key] = value?.[key] ?? defaults[key]; }); return result; } function denormalizeLinkValue(value, fieldDef) { if (!fieldDef.mapping) { return value; } const result = {}; Object.entries(fieldDef.mapping).forEach(([key]) => { if (key in value) { result[key] = value[key]; } }); return result; } function BlockFields({ clientId }) { const { attributes, blockType } = (0, import_data.useSelect)( (select) => { const { getBlockAttributes, getBlockName } = select(import_store.store); const { getBlockType } = select(import_blocks.store); const blockName = getBlockName(clientId); return { attributes: getBlockAttributes(clientId), blockType: getBlockType(blockName) }; }, [clientId] ); const { updateBlockAttributes } = (0, import_data.useDispatch)(import_store.store); const blockTitle = (0, import_use_block_display_title.default)({ clientId, context: "list-view" }); const blockInformation = (0, import_use_block_display_information.default)(clientId); const blockTypeFields = blockType?.[fieldsKey]; const [form, setForm] = (0, import_element.useState)(() => { return blockType?.[formKey]; }); const dataFormFields = (0, import_element.useMemo)(() => { if (!blockTypeFields?.length) { return []; } return blockTypeFields.map((fieldDef) => { const ControlComponent = CONTROLS[fieldDef.type]; const defaultValues = {}; if (fieldDef.mapping && blockType?.attributes) { Object.entries(fieldDef.mapping).forEach( ([key, attrKey]) => { defaultValues[key] = blockType.attributes[attrKey]?.defaultValue ?? void 0; } ); } const field = { id: fieldDef.id, label: fieldDef.label, type: fieldDef.type, // Use the field's type; DataForm will use built-in or custom Edit config: { ...fieldDef.args, defaultValues }, hideLabelFromVision: fieldDef.id === "content", // getValue and setValue handle the mapping to block attributes getValue: ({ item }) => { if (fieldDef.mapping) { const mappedValue = {}; Object.entries(fieldDef.mapping).forEach( ([key, attrKey]) => { mappedValue[key] = item[attrKey]; } ); if (fieldDef.type === "media") { return normalizeMediaValue(mappedValue, fieldDef); } if (fieldDef.type === "link") { return normalizeLinkValue(mappedValue, fieldDef); } return mappedValue; } return item[fieldDef.id]; }, setValue: ({ item, value }) => { if (fieldDef.mapping) { let denormalizedValue = value; if (fieldDef.type === "media") { denormalizedValue = denormalizeMediaValue( value, fieldDef ); } else if (fieldDef.type === "link") { denormalizedValue = denormalizeLinkValue( value, fieldDef ); } const updates = {}; Object.entries(fieldDef.mapping).forEach( ([key, attrKey]) => { if (key in denormalizedValue) { updates[attrKey] = denormalizedValue[key]; } else { updates[attrKey] = item[attrKey]; } } ); return updates; } return { [fieldDef.id]: value }; } }; if (ControlComponent) { field.Edit = createConfiguredControl({ control: fieldDef.type, clientId, fieldDef }); } return field; }); }, [blockTypeFields, blockType?.attributes, clientId]); const handleToggleField = (fieldId) => { setForm((prev) => { if (prev.fields?.includes(fieldId)) { return { ...prev, fields: prev.fields.filter((id) => id !== fieldId) }; } return { ...prev, fields: [...prev.fields || [], fieldId] }; }); }; if (!blockTypeFields?.length) { return null; } return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "block-editor-content-only-controls__fields-container", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor-content-only-controls__fields-header", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { spacing: 1, justify: "space-between", expanded: true, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { spacing: 1, justify: "flex-start", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_icon.default, { icon: blockInformation?.icon }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: blockTitle }) ] }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_fields_dropdown_menu.default, { fields: dataFormFields, visibleFields: form.fields, onToggleField: handleToggleField } ) ] }) }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_dataviews.DataForm, { data: attributes, fields: dataFormFields, form, onChange: (changes) => { updateBlockAttributes(clientId, changes); } } ) ] }); } function DrillDownButton({ clientId }) { const blockTitle = (0, import_use_block_display_title.default)({ clientId, context: "list-view" }); const blockInformation = (0, import_use_block_display_information.default)(clientId); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor-content-only-controls__button-panel", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Navigator.Button, { path: `/${clientId}`, className: "block-editor-content-only-controls__drill-down-button", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { expanded: true, justify: "space-between", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { justify: "flex-start", spacing: 1, children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_block_icon.default, { icon: blockInformation?.icon }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: blockTitle }) ] }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Icon, { icon: import_icons.arrowRight }) ] }) } ) }); } function ContentOnlyControlsScreen({ rootClientId, contentClientIds, parentClientIds, isNested }) { const isRootContentBlock = (0, import_data.useSelect)( (select) => { const { getBlockName } = select(import_store.store); const blockName = getBlockName(rootClientId); const { hasContentRoleAttribute } = (0, import_lock_unlock.unlock)(select(import_blocks.store)); return hasContentRoleAttribute(blockName); }, [rootClientId] ); if (!isRootContentBlock && !contentClientIds.length) { return null; } return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ isNested && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "block-editor-content-only-controls__button-panel", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Navigator.BackButton, { className: "block-editor-content-only-controls__back-button", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.__experimentalHStack, { expanded: true, spacing: 1, justify: "flex-start", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Icon, { icon: import_icons.arrowLeft }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { children: (0, import_i18n.__)("Back") }) ] }) }) }), isRootContentBlock && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockFields, { clientId: rootClientId }), contentClientIds.map((clientId) => { if (parentClientIds?.[clientId]) { return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( DrillDownButton, { clientId }, clientId ); } return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BlockFields, { clientId }, clientId); }) ] }); } function ContentOnlyControls({ rootClientId }) { const { updatedRootClientId, nestedContentClientIds, contentClientIds } = (0, import_data.useSelect)( (select) => { const { getClientIdsOfDescendants, getBlockEditingMode } = select(import_store.store); const _nestedContentClientIds = {}; const _contentClientIds = []; let allNestedClientIds = []; const allContentClientIds = getClientIdsOfDescendants( rootClientId ).filter( (clientId) => getBlockEditingMode(clientId) === "contentOnly" ); for (const clientId of allContentClientIds) { const childClientIds = getClientIdsOfDescendants( clientId ).filter( (childClientId) => getBlockEditingMode(childClientId) === "contentOnly" ); if (childClientIds.length > 1 && !allNestedClientIds.includes(clientId)) { _nestedContentClientIds[clientId] = childClientIds; allNestedClientIds = [ allNestedClientIds, ...childClientIds ]; } if (!allNestedClientIds.includes(clientId)) { _contentClientIds.push(clientId); } } if (_contentClientIds.length === 1 && Object.keys(_nestedContentClientIds).length === 1) { const onlyParentClientId = Object.keys( _nestedContentClientIds )[0]; return { updatedRootClientId: onlyParentClientId, contentClientIds: _nestedContentClientIds[onlyParentClientId], nestedContentClientIds: {} }; } return { nestedContentClientIds: _nestedContentClientIds, contentClientIds: _contentClientIds }; }, [rootClientId] ); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_components.Navigator, { initialPath: "/", children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Navigator.Screen, { path: "/", className: "block-editor-content-only-controls__screen", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( ContentOnlyControlsScreen, { rootClientId: updatedRootClientId ?? rootClientId, contentClientIds, parentClientIds: nestedContentClientIds } ) } ), Object.keys(nestedContentClientIds).map((clientId) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_components.Navigator.Screen, { path: `/${clientId}`, className: "block-editor-content-only-controls__screen", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( ContentOnlyControlsScreen, { isNested: true, rootClientId: clientId, contentClientIds: nestedContentClientIds[clientId] } ) }, clientId )) ] }); } //# sourceMappingURL=index.js.map