@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
8 lines (7 loc) • 5.6 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/hooks/use-post-editor-awareness-state.ts"],
"sourcesContent": ["/**\n * External dependencies\n */\nimport { useEffect, useState } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { getSyncManager } from '../sync';\nimport type {\n\tPostEditorAwarenessState as ActiveCollaborator,\n\tYDocDebugData,\n} from '../awareness/types';\nimport type { SelectionCursor } from '../types';\nimport type { PostEditorAwareness } from '../awareness/post-editor-awareness';\n\ninterface AwarenessState {\n\tactiveCollaborators: ActiveCollaborator[];\n\tgetAbsolutePositionIndex: ( selection: SelectionCursor ) => number | null;\n\tgetDebugData: () => YDocDebugData;\n\tisCurrentCollaboratorDisconnected: boolean;\n}\n\nconst defaultState: AwarenessState = {\n\tactiveCollaborators: [],\n\tgetAbsolutePositionIndex: () => null,\n\tgetDebugData: () => ( {\n\t\tdoc: {},\n\t\tclients: {},\n\t\tcollaboratorMap: {},\n\t} ),\n\tisCurrentCollaboratorDisconnected: false,\n};\n\nfunction getAwarenessState(\n\tawareness: PostEditorAwareness,\n\tnewState?: ActiveCollaborator[]\n): AwarenessState {\n\tconst activeCollaborators = newState ?? awareness.getCurrentState();\n\n\treturn {\n\t\tactiveCollaborators,\n\t\tgetAbsolutePositionIndex: ( selection: SelectionCursor ) =>\n\t\t\tawareness.getAbsolutePositionIndex( selection ),\n\t\tgetDebugData: () => awareness.getDebugData(),\n\t\tisCurrentCollaboratorDisconnected:\n\t\t\tactiveCollaborators.find( ( collaborator ) => collaborator.isMe )\n\t\t\t\t?.isConnected === false,\n\t};\n}\n\nfunction usePostEditorAwarenessState(\n\tpostId: number | null,\n\tpostType: string | null\n): AwarenessState {\n\tconst [ state, setState ] = useState< AwarenessState >( defaultState );\n\n\tuseEffect( () => {\n\t\tif ( null === postId || null === postType ) {\n\t\t\tsetState( defaultState );\n\t\t\treturn;\n\t\t}\n\n\t\tconst objectType = `postType/${ postType }`;\n\t\tconst objectId = postId.toString();\n\t\tconst awareness = getSyncManager()?.getAwareness< PostEditorAwareness >(\n\t\t\tobjectType,\n\t\t\tobjectId\n\t\t);\n\n\t\tif ( ! awareness ) {\n\t\t\tsetState( defaultState );\n\t\t\treturn;\n\t\t}\n\n\t\tawareness.setUp();\n\n\t\t// Initialize with current awareness state.\n\t\tsetState( getAwarenessState( awareness ) );\n\n\t\tconst unsubscribe = awareness?.onStateChange(\n\t\t\t( newState: ActiveCollaborator[] ) => {\n\t\t\t\tsetState( getAwarenessState( awareness, newState ) );\n\t\t\t}\n\t\t);\n\n\t\treturn unsubscribe;\n\t}, [ postId, postType ] );\n\n\treturn state;\n}\n\n/**\n * Hook to get the active collaborators for a post editor.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {ActiveCollaborator[]} The active collaborators.\n */\nexport function useActiveCollaborators(\n\tpostId: number | null,\n\tpostType: string | null\n): ActiveCollaborator[] {\n\treturn usePostEditorAwarenessState( postId, postType ).activeCollaborators;\n}\n\n/**\n * Hook to get the absolute position index for a post editor.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {SelectionCursor} The absolute position index.\n */\nexport function useGetAbsolutePositionIndex(\n\tpostId: number | null,\n\tpostType: string | null\n): ( selection: SelectionCursor ) => number | null {\n\treturn usePostEditorAwarenessState( postId, postType )\n\t\t.getAbsolutePositionIndex;\n}\n\n/**\n * Hook to get data for debugging, using the awareness state.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {YDocDebugData} The debug data.\n */\nexport function useGetDebugData(\n\tpostId: number | null,\n\tpostType: string | null\n): YDocDebugData {\n\treturn usePostEditorAwarenessState( postId, postType ).getDebugData();\n}\n\n/**\n * Hook to check if the current collaborator is disconnected.\n *\n * @param postId - The ID of the post.\n * @param postType - The type of the post.\n * @return {boolean} Whether the current collaborator is disconnected.\n */\nexport function useIsDisconnected(\n\tpostId: number | null,\n\tpostType: string | null\n): boolean {\n\treturn usePostEditorAwarenessState( postId, postType )\n\t\t.isCurrentCollaboratorDisconnected;\n}\n"],
"mappings": ";AAGA,SAAS,WAAW,gBAAgB;AAKpC,SAAS,sBAAsB;AAe/B,IAAM,eAA+B;AAAA,EACpC,qBAAqB,CAAC;AAAA,EACtB,0BAA0B,MAAM;AAAA,EAChC,cAAc,OAAQ;AAAA,IACrB,KAAK,CAAC;AAAA,IACN,SAAS,CAAC;AAAA,IACV,iBAAiB,CAAC;AAAA,EACnB;AAAA,EACA,mCAAmC;AACpC;AAEA,SAAS,kBACR,WACA,UACiB;AACjB,QAAM,sBAAsB,YAAY,UAAU,gBAAgB;AAElE,SAAO;AAAA,IACN;AAAA,IACA,0BAA0B,CAAE,cAC3B,UAAU,yBAA0B,SAAU;AAAA,IAC/C,cAAc,MAAM,UAAU,aAAa;AAAA,IAC3C,mCACC,oBAAoB,KAAM,CAAE,iBAAkB,aAAa,IAAK,GAC7D,gBAAgB;AAAA,EACrB;AACD;AAEA,SAAS,4BACR,QACA,UACiB;AACjB,QAAM,CAAE,OAAO,QAAS,IAAI,SAA4B,YAAa;AAErE,YAAW,MAAM;AAChB,QAAK,SAAS,UAAU,SAAS,UAAW;AAC3C,eAAU,YAAa;AACvB;AAAA,IACD;AAEA,UAAM,aAAa,YAAa,QAAS;AACzC,UAAM,WAAW,OAAO,SAAS;AACjC,UAAM,YAAY,eAAe,GAAG;AAAA,MACnC;AAAA,MACA;AAAA,IACD;AAEA,QAAK,CAAE,WAAY;AAClB,eAAU,YAAa;AACvB;AAAA,IACD;AAEA,cAAU,MAAM;AAGhB,aAAU,kBAAmB,SAAU,CAAE;AAEzC,UAAM,cAAc,WAAW;AAAA,MAC9B,CAAE,aAAoC;AACrC,iBAAU,kBAAmB,WAAW,QAAS,CAAE;AAAA,MACpD;AAAA,IACD;AAEA,WAAO;AAAA,EACR,GAAG,CAAE,QAAQ,QAAS,CAAE;AAExB,SAAO;AACR;AASO,SAAS,uBACf,QACA,UACuB;AACvB,SAAO,4BAA6B,QAAQ,QAAS,EAAE;AACxD;AASO,SAAS,4BACf,QACA,UACkD;AAClD,SAAO,4BAA6B,QAAQ,QAAS,EACnD;AACH;AASO,SAAS,gBACf,QACA,UACgB;AAChB,SAAO,4BAA6B,QAAQ,QAAS,EAAE,aAAa;AACrE;AASO,SAAS,kBACf,QACA,UACU;AACV,SAAO,4BAA6B,QAAQ,QAAS,EACnD;AACH;",
"names": []
}