UNPKG

@wordpress/block-editor

Version:
96 lines (94 loc) 4.1 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/inner-blocks/use-inner-block-template-sync.js var use_inner_block_template_sync_exports = {}; __export(use_inner_block_template_sync_exports, { default: () => useInnerBlockTemplateSync }); module.exports = __toCommonJS(use_inner_block_template_sync_exports); var import_es6 = __toESM(require("fast-deep-equal/es6")); var import_element = require("@wordpress/element"); var import_data = require("@wordpress/data"); var import_blocks = require("@wordpress/blocks"); var import_store = require("../../store"); function useInnerBlockTemplateSync(clientId, template, templateLock, templateInsertUpdatesSelection) { const registry = (0, import_data.useRegistry)(); const existingTemplateRef = (0, import_element.useRef)(null); (0, import_element.useLayoutEffect)(() => { let isCancelled = false; const { getBlocks, getSelectedBlocksInitialCaretPosition, isBlockSelected } = registry.select(import_store.store); const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } = registry.dispatch(import_store.store); window.queueMicrotask(() => { if (isCancelled) { return; } const currentInnerBlocks = getBlocks(clientId); const shouldApplyTemplate = currentInnerBlocks.length === 0 || templateLock === "all" || templateLock === "contentOnly"; const hasTemplateChanged = !(0, import_es6.default)( template, existingTemplateRef.current ); if (!shouldApplyTemplate || !hasTemplateChanged) { return; } existingTemplateRef.current = template; const nextBlocks = (0, import_blocks.synchronizeBlocksWithTemplate)( currentInnerBlocks, template ); if (!(0, import_es6.default)(nextBlocks, currentInnerBlocks)) { __unstableMarkNextChangeAsNotPersistent(); replaceInnerBlocks( clientId, nextBlocks, currentInnerBlocks.length === 0 && templateInsertUpdatesSelection && nextBlocks.length !== 0 && isBlockSelected(clientId), // This ensures the "initialPosition" doesn't change when applying the template // If we're supposed to focus the block, we'll focus the first inner block // otherwise, we won't apply any auto-focus. // This ensures for instance that the focus stays in the inserter when inserting the "buttons" block. getSelectedBlocksInitialCaretPosition() ); } }); return () => { isCancelled = true; }; }, [ template, templateLock, clientId, registry, templateInsertUpdatesSelection ]); } //# sourceMappingURL=use-inner-block-template-sync.js.map