UNPKG

@wordpress/core-data

Version:
98 lines (96 loc) 3.59 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/hooks/use-post-editor-awareness-state.ts var use_post_editor_awareness_state_exports = {}; __export(use_post_editor_awareness_state_exports, { useActiveCollaborators: () => useActiveCollaborators, useGetAbsolutePositionIndex: () => useGetAbsolutePositionIndex, useGetDebugData: () => useGetDebugData, useIsDisconnected: () => useIsDisconnected }); module.exports = __toCommonJS(use_post_editor_awareness_state_exports); var import_element = require("@wordpress/element"); var import_sync = require("../sync.cjs"); var defaultState = { activeCollaborators: [], getAbsolutePositionIndex: () => null, getDebugData: () => ({ doc: {}, clients: {}, collaboratorMap: {} }), isCurrentCollaboratorDisconnected: false }; function getAwarenessState(awareness, newState) { const activeCollaborators = newState ?? awareness.getCurrentState(); return { activeCollaborators, getAbsolutePositionIndex: (selection) => awareness.getAbsolutePositionIndex(selection), getDebugData: () => awareness.getDebugData(), isCurrentCollaboratorDisconnected: activeCollaborators.find((collaborator) => collaborator.isMe)?.isConnected === false }; } function usePostEditorAwarenessState(postId, postType) { const [state, setState] = (0, import_element.useState)(defaultState); (0, import_element.useEffect)(() => { if (null === postId || null === postType) { setState(defaultState); return; } const objectType = `postType/${postType}`; const objectId = postId.toString(); const awareness = (0, import_sync.getSyncManager)()?.getAwareness( objectType, objectId ); if (!awareness) { setState(defaultState); return; } awareness.setUp(); setState(getAwarenessState(awareness)); const unsubscribe = awareness?.onStateChange( (newState) => { setState(getAwarenessState(awareness, newState)); } ); return unsubscribe; }, [postId, postType]); return state; } function useActiveCollaborators(postId, postType) { return usePostEditorAwarenessState(postId, postType).activeCollaborators; } function useGetAbsolutePositionIndex(postId, postType) { return usePostEditorAwarenessState(postId, postType).getAbsolutePositionIndex; } function useGetDebugData(postId, postType) { return usePostEditorAwarenessState(postId, postType).getDebugData(); } function useIsDisconnected(postId, postType) { return usePostEditorAwarenessState(postId, postType).isCurrentCollaboratorDisconnected; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useActiveCollaborators, useGetAbsolutePositionIndex, useGetDebugData, useIsDisconnected }); //# sourceMappingURL=use-post-editor-awareness-state.cjs.map