UNPKG

@wordpress/editor

Version:
162 lines (160 loc) 5.73 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/editor/src/components/collaborators-overlay/use-block-highlighting.ts var use_block_highlighting_exports = {}; __export(use_block_highlighting_exports, { useBlockHighlighting: () => useBlockHighlighting }); module.exports = __toCommonJS(use_block_highlighting_exports); var import_core_data = require("@wordpress/core-data"); var import_element = require("@wordpress/element"); var import_lock_unlock = require("../../lock-unlock.cjs"); var import_utils = require("../collab-sidebar/utils.cjs"); var import_get_avatar_url = require("./get-avatar-url.cjs"); var import_use_debounced_recompute = require("./use-debounced-recompute.cjs"); var { useActiveCollaborators, useResolvedSelection } = (0, import_lock_unlock.unlock)(import_core_data.privateApis); function useBlockHighlighting(overlayElement, blockEditorDocument, postId, postType, delayMs) { const highlightedBlockIds = (0, import_element.useRef)(/* @__PURE__ */ new Set()); const userStates = useActiveCollaborators( postId ?? null, postType ?? null ); const resolveSelection = useResolvedSelection( postId ?? null, postType ?? null ); const [highlights, setHighlights] = (0, import_element.useState)( [] ); const [recomputeToken, rerenderHighlightsAfterDelay] = (0, import_use_debounced_recompute.useDebouncedRecompute)(delayMs); (0, import_element.useEffect)(() => { if (!blockEditorDocument) { setHighlights([]); return; } const currentHighlightedIds = highlightedBlockIds.current; const seen = /* @__PURE__ */ new Set(); const blocksToHighlight = userStates.filter((userState) => { const isWholeBlockSelected = userState.editorState?.selection?.type === import_core_data.SelectionType.WholeBlock; return !userState.isMe && isWholeBlockSelected; }).map((userState) => { let localClientId; try { ({ localClientId } = resolveSelection( userState.editorState?.selection )); } catch { return null; } if (!localClientId) { return null; } return { blockId: localClientId, color: userState.isMe ? "var(--wp-admin-theme-color)" : (0, import_utils.getAvatarBorderColor)(userState.collaboratorInfo.id), userName: userState.collaboratorInfo.name, avatarUrl: (0, import_get_avatar_url.getAvatarUrl)( userState.collaboratorInfo.avatar_urls ) }; }).filter((block) => { if (!block) { return false; } if (seen.has(block.blockId)) { return false; } seen.add(block.blockId); return true; }); const selectedBlockIds = new Set( blocksToHighlight.map((block) => block.blockId) ); for (const blockId of currentHighlightedIds) { if (!selectedBlockIds.has(blockId)) { const blockElement = getBlockElementById( blockEditorDocument, blockId ); if (blockElement) { blockElement.classList.remove("is-collaborator-selected"); blockElement.style.removeProperty( "--collaborator-outline-color" ); } currentHighlightedIds.delete(blockId); } } const results = []; const overlayRect = overlayElement?.getBoundingClientRect() ?? null; blocksToHighlight.forEach((block) => { const { color, blockId, userName, avatarUrl } = block; const blockElement = getBlockElementById( blockEditorDocument, blockId ); if (!blockElement) { return; } blockElement.classList.add("is-collaborator-selected"); blockElement.style.setProperty( "--collaborator-outline-color", color ); currentHighlightedIds.add(blockId); if (overlayRect) { const blockRect = blockElement.getBoundingClientRect(); results.push({ blockId, userName, avatarUrl, color, x: blockRect.left - overlayRect.left, y: blockRect.top - overlayRect.top }); } }); setHighlights(results); return () => { for (const blockId of currentHighlightedIds) { const el = getBlockElementById(blockEditorDocument, blockId); if (el) { el.classList.remove("is-collaborator-selected"); el.style.removeProperty("--collaborator-outline-color"); } } currentHighlightedIds.clear(); }; }, [ userStates, blockEditorDocument, overlayElement, recomputeToken, resolveSelection ]); return { highlights, rerenderHighlightsAfterDelay }; } var getBlockElementById = (blockEditorDocument, blockId) => { return blockEditorDocument.querySelector(`[data-block="${blockId}"]`); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useBlockHighlighting }); //# sourceMappingURL=use-block-highlighting.cjs.map