@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
62 lines (61 loc) • 1.81 kB
JavaScript
// packages/editor/src/components/collaborators-overlay/cursor-registry.ts
function highlightCursor(element, duration) {
element.classList.add("collaborators-overlay-cursor-highlighted");
setTimeout(() => {
element.classList.remove("collaborators-overlay-cursor-highlighted");
}, duration);
}
function createCursorRegistry() {
const cursorMap = /* @__PURE__ */ new Map();
return {
/**
* Register a cursor element when it's created.
*
* @param clientId - The clientId of the cursor to register.
* @param element - The cursor element to register.
*/
registerCursor(clientId, element) {
cursorMap.set(clientId, element);
},
/**
* Unregister a cursor element when it's removed.
*
* @param clientId - The clientId of the cursor to unregister.
*/
unregisterCursor(clientId) {
cursorMap.delete(clientId);
},
/**
* Scroll to a cursor by clientId.
*
* @param clientId - The clientId of the cursor to scroll to.
* @param options - The options for the scroll.
* @return true if cursor was found and scrolled to, false otherwise.
*/
scrollToCursor(clientId, options) {
const cursorElement = cursorMap.get(clientId);
if (!cursorElement) {
return false;
}
cursorElement.scrollIntoView({
behavior: options?.behavior ?? "smooth",
block: options?.block ?? "center",
inline: options?.inline ?? "nearest"
});
if (options?.highlightDuration) {
highlightCursor(cursorElement, options.highlightDuration);
}
return true;
},
/**
* Clear the registry.
*/
removeAll() {
cursorMap.clear();
}
};
}
export {
createCursorRegistry
};
//# sourceMappingURL=cursor-registry.mjs.map