@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
140 lines (139 loc) • 4.49 kB
JavaScript
// packages/editor/src/components/collaborators-overlay/use-block-highlighting.ts
import {
privateApis as coreDataPrivateApis,
SelectionType
} from "@wordpress/core-data";
import { useEffect, useRef, useState } from "@wordpress/element";
import { unlock } from "../../lock-unlock.mjs";
import { getAvatarBorderColor } from "../collab-sidebar/utils.mjs";
import { getAvatarUrl } from "./get-avatar-url.mjs";
import { useDebouncedRecompute } from "./use-debounced-recompute.mjs";
var { useActiveCollaborators, useResolvedSelection } = unlock(coreDataPrivateApis);
function useBlockHighlighting(overlayElement, blockEditorDocument, postId, postType, delayMs) {
const highlightedBlockIds = useRef(/* @__PURE__ */ new Set());
const userStates = useActiveCollaborators(
postId ?? null,
postType ?? null
);
const resolveSelection = useResolvedSelection(
postId ?? null,
postType ?? null
);
const [highlights, setHighlights] = useState(
[]
);
const [recomputeToken, rerenderHighlightsAfterDelay] = useDebouncedRecompute(delayMs);
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 === 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)" : getAvatarBorderColor(userState.collaboratorInfo.id),
userName: userState.collaboratorInfo.name,
avatarUrl: 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}"]`);
};
export {
useBlockHighlighting
};
//# sourceMappingURL=use-block-highlighting.mjs.map