UNPKG

@wordpress/core-data

Version:
105 lines (103 loc) 3.46 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/core-data/src/awareness/block-lookup.ts var block_lookup_exports = {}; __export(block_lookup_exports, { getBlockPathInYdoc: () => getBlockPathInYdoc, getContainingBlockYMap: () => getContainingBlockYMap, resolveBlockClientIdByPath: () => resolveBlockClientIdByPath, usePostContentBlocks: () => usePostContentBlocks }); module.exports = __toCommonJS(block_lookup_exports); var import_data = require("@wordpress/data"); var import_sync = require("@wordpress/sync"); var import_block_editor = require("@wordpress/block-editor"); var import_lock_unlock = require("../lock-unlock.cjs"); function getContainingBlockYMap(yType) { let current = yType; while (current) { const parent = current.parent; if (parent instanceof import_sync.Y.Map && parent.parent instanceof import_sync.Y.Array && parent.get("clientId") !== void 0 && parent.get("innerBlocks") instanceof import_sync.Y.Array) { return parent; } current = parent instanceof import_sync.Y.AbstractType ? parent : null; } return null; } function getBlockPathInYdoc(yType) { const path = []; let current = yType; while (current) { const parentArray = current.parent; if (!parentArray || !(parentArray instanceof import_sync.Y.Array)) { return null; } let index = -1; for (let i = 0; i < parentArray.length; i++) { if (parentArray.get(i) === current) { index = i; break; } } if (index === -1) { return null; } path.unshift(index); const grandparent = parentArray.parent; if (grandparent instanceof import_sync.Y.Map && grandparent.get("clientId") !== void 0) { current = grandparent; } else { break; } } return path; } function resolveBlockClientIdByPath(path, blocks) { if (path.length === 0) { return null; } for (let i = 0; i < path.length; i++) { const block = blocks[path[i]]; if (!block) { return null; } if (i === path.length - 1) { return block.clientId; } blocks = block.innerBlocks; } return null; } function usePostContentBlocks() { return (0, import_data.useSelect)((select) => { const { getBlocksByName, getClientIdsTree } = (0, import_lock_unlock.unlock)( select(import_block_editor.store) ); const [postContentClientId] = getBlocksByName("core/post-content"); return getClientIdsTree(postContentClientId ?? ""); }, []); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { getBlockPathInYdoc, getContainingBlockYMap, resolveBlockClientIdByPath, usePostContentBlocks }); //# sourceMappingURL=block-lookup.cjs.map