@lexical/react 0.39.1-nightly.20260105.0 → 0.39.1-nightly.20260107.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -248,6 +248,32 @@ function useProvider(editor, provider, name, color, isReloadingDoc, awarenessDat
248
248
  return true;
249
249
  }, lexical.COMMAND_PRIORITY_EDITOR);
250
250
  }, [connect, disconnect, editor]);
251
+
252
+ // Clear awareness state immediately when tab is refreshed or closed
253
+ // This prevents ghost cursors from appearing for several seconds after disconnect
254
+ // See: https://github.com/facebook/lexical/issues/8061
255
+ React.useEffect(() => {
256
+ const clearAwarenessState = () => {
257
+ // Immediately clear local awareness state to signal disconnection
258
+ // This broadcasts to other clients that this client has disconnected,
259
+ // causing them to remove the cursor immediately instead of waiting for timeout
260
+ try {
261
+ provider.awareness.setLocalState(null);
262
+ } catch (_e) {
263
+ // Ignore errors during cleanup (e.g., if provider is already disconnected)
264
+ }
265
+ };
266
+
267
+ // Use both beforeunload and pagehide for maximum browser compatibility
268
+ // beforeunload: fires before page unloads (may be cancelable)
269
+ // pagehide: fires when page is being unloaded (more reliable, especially on mobile)
270
+ window.addEventListener('beforeunload', clearAwarenessState);
271
+ window.addEventListener('pagehide', clearAwarenessState);
272
+ return () => {
273
+ window.removeEventListener('beforeunload', clearAwarenessState);
274
+ window.removeEventListener('pagehide', clearAwarenessState);
275
+ };
276
+ }, [provider]);
251
277
  }
252
278
  function useAwareness(binding, provider) {
253
279
  React.useEffect(() => {
@@ -234,6 +234,32 @@ function useProvider(editor, provider, name, color, isReloadingDoc, awarenessDat
234
234
  return true;
235
235
  }, COMMAND_PRIORITY_EDITOR);
236
236
  }, [connect, disconnect, editor]);
237
+
238
+ // Clear awareness state immediately when tab is refreshed or closed
239
+ // This prevents ghost cursors from appearing for several seconds after disconnect
240
+ // See: https://github.com/facebook/lexical/issues/8061
241
+ useEffect(() => {
242
+ const clearAwarenessState = () => {
243
+ // Immediately clear local awareness state to signal disconnection
244
+ // This broadcasts to other clients that this client has disconnected,
245
+ // causing them to remove the cursor immediately instead of waiting for timeout
246
+ try {
247
+ provider.awareness.setLocalState(null);
248
+ } catch (_e) {
249
+ // Ignore errors during cleanup (e.g., if provider is already disconnected)
250
+ }
251
+ };
252
+
253
+ // Use both beforeunload and pagehide for maximum browser compatibility
254
+ // beforeunload: fires before page unloads (may be cancelable)
255
+ // pagehide: fires when page is being unloaded (more reliable, especially on mobile)
256
+ window.addEventListener('beforeunload', clearAwarenessState);
257
+ window.addEventListener('pagehide', clearAwarenessState);
258
+ return () => {
259
+ window.removeEventListener('beforeunload', clearAwarenessState);
260
+ window.removeEventListener('pagehide', clearAwarenessState);
261
+ };
262
+ }, [provider]);
237
263
  }
238
264
  function useAwareness(binding, provider) {
239
265
  useEffect(() => {
@@ -6,4 +6,4 @@
6
6
  *
7
7
  */
8
8
 
9
- "use strict";var e=require("@lexical/react/LexicalCollaborationContext"),t=require("@lexical/react/LexicalComposerContext"),o=require("@lexical/yjs"),r=require("react"),n=require("@lexical/utils"),s=require("lexical"),a=require("react-dom"),c=require("yjs"),i=require("react/jsx-runtime");function u(e){var t=Object.create(null);if(e)for(var o in e)t[o]=e[o];return t.default=e,t}var l=u(r);function d(e,t,n,a,i,u,l,d,p,_,M,g,R=o.syncCursorPositions){const O=r.useRef(!1),D=r.useCallback(()=>{const{root:t}=d;l&&t.isEmpty()&&0===t._xmlText._length&&m(e,M)},[d,e,M,l]);return r.useEffect(()=>{const{root:t}=d,r=(e,t)=>{const r=t.origin;if(r!==d){const t=r instanceof c.UndoManager;o.syncYjsChangesToLexical(d,n,e,t,R)}};t.getSharedType().observeDeep(r);const a=e.registerUpdateListener(({prevEditorState:e,editorState:t,dirtyLeaves:r,dirtyElements:a,normalizedNodes:c,tags:i})=>{i.has(s.SKIP_COLLAB_TAG)||o.syncLexicalUpdateToYjs(d,n,e,t,a,r,c,i)});return()=>{t.getSharedType().unobserveDeep(r),a()}},[d,n,e,p,a,t,R]),r.useEffect(()=>{const o=o=>{!function(e,t){if(e.update(()=>{const e=s.$getRoot();e.clear(),e.select()},{tag:s.SKIP_COLLAB_TAG}),null==t.cursors)return;const o=t.cursors;if(null==o)return;const r=t.cursorsContainer;if(null==r)return;const n=Array.from(o.values());for(let e=0;e<n.length;e++){const t=n[e].selection;if(t&&null!=t.selections){const o=t.selections;for(let t=0;t<o.length;t++)r.removeChild(o[e])}}}(e,d),p(o),a.set(t,o),O.current=!0},r=()=>{O.current=!1};return n.on("reload",o),n.on("sync",r),()=>{n.off("reload",o),n.off("sync",r)}},[d,n,e,p,a,t]),f(e,n,i,u,O,g,D),E(d,n),C(d,_)}function f(e,t,n,a,c,i,u){const l=r.useCallback(()=>t.connect(),[t]),d=r.useCallback(()=>{try{t.disconnect()}catch(e){}},[t]);r.useEffect(()=>{const r=({status:t})=>{e.dispatchCommand(o.CONNECTED_COMMAND,"connected"===t)},s=e=>{e&&!1===c.current&&u&&u()};o.initLocalState(t,n,a,document.activeElement===e.getRootElement(),i||{}),t.on("status",r),t.on("sync",s);const f=l();return()=>{!1===c.current&&(f?f.then(d):d()),t.off("sync",s),t.off("status",r)}},[e,t,n,a,c,i,u,l,d]),r.useEffect(()=>e.registerCommand(o.TOGGLE_CONNECT_COMMAND,e=>(e?(console.log("Collaboration connected!"),l()):(console.log("Collaboration disconnected!"),d()),!0),s.COMMAND_PRIORITY_EDITOR),[l,d,e])}function E(e,t){r.useEffect(()=>{const{awareness:r}=t,n=()=>{o.syncCursorPositions(e,t)};return r.on("update",n),()=>{r.off("update",n)}},[e,t])}function C(e,t){return r.useMemo(()=>a.createPortal(i.jsx("div",{ref:t=>{e.cursorsContainer=t}}),t&&t.current||document.body),[e,t])}function p(e,t,a,c,i){r.useEffect(()=>n.mergeRegister(e.registerCommand(s.FOCUS_COMMAND,()=>(o.setLocalStateFocus(t,a,c,!0,i||{}),!1),s.COMMAND_PRIORITY_EDITOR),e.registerCommand(s.BLUR_COMMAND,()=>(o.setLocalStateFocus(t,a,c,!1,i||{}),!1),s.COMMAND_PRIORITY_EDITOR)),[c,e,a,t,i])}function _(e,t){r.useEffect(()=>n.mergeRegister(e.registerCommand(s.UNDO_COMMAND,()=>(t.undo(),!0),s.COMMAND_PRIORITY_EDITOR),e.registerCommand(s.REDO_COMMAND,()=>(t.redo(),!0),s.COMMAND_PRIORITY_EDITOR)));const o=r.useCallback(()=>{t.clear()},[t]);return l.useEffect(()=>{const o=()=>{e.dispatchCommand(s.CAN_UNDO_COMMAND,t.undoStack.length>0),e.dispatchCommand(s.CAN_REDO_COMMAND,t.redoStack.length>0)};return t.on("stack-item-added",o),t.on("stack-item-popped",o),t.on("stack-cleared",o),()=>{t.off("stack-item-added",o),t.off("stack-item-popped",o),t.off("stack-cleared",o)}},[e,t]),o}function m(e,t){e.update(()=>{const o=s.$getRoot();if(o.isEmpty())if(t)switch(typeof t){case"string":{const o=e.parseEditorState(t);e.setEditorState(o,{tag:s.HISTORY_MERGE_TAG});break}case"object":e.setEditorState(t,{tag:s.HISTORY_MERGE_TAG});break;case"function":e.update(()=>{s.$getRoot().isEmpty()&&t(e)},{tag:s.HISTORY_MERGE_TAG})}else{const t=s.$createParagraphNode();o.append(t);const{activeElement:r}=document;(null!==s.$getSelection()||null!==r&&r===e.getRootElement())&&t.select()}},{tag:s.HISTORY_MERGE_TAG})}function M({editor:e,id:t,provider:n,yjsDocMap:s,name:a,color:c,shouldBootstrap:i,cursorsContainerRef:u,initialEditorState:l,awarenessData:f,collabContext:E,binding:C,setDoc:m,syncCursorPositionsFn:M}){const g=d(e,t,n,s,a,c,i,C,m,u,l,f,M);return function(e,t){_(e,r.useMemo(()=>o.createUndoManager(t,t.root.getSharedType()),[t]))}(e,C),p(e,n,a,c,f),g}const g=(e,t)=>{r.useEffect(()=>(e.isCollabActive=!0,()=>{null==t._parentEditor&&(e.isCollabActive=!1)}),[e,t])};exports.CollaborationPlugin=function({id:n,providerFactory:s,shouldBootstrap:a,username:c,cursorColor:u,cursorsContainerRef:l,initialEditorState:d,excludedProperties:f,awarenessData:E,syncCursorPositionsFn:C}){const p=r.useRef(!1),_=r.useRef(!1),m=e.useCollaborationContext(c,u),{yjsDocMap:R,name:O,color:D}=m,[T]=t.useLexicalComposerContext();g(m,T);const[A,I]=r.useState(),[N,S]=r.useState();r.useEffect(()=>{if(_.current)return;_.current=!0;const e=s(n,R);return I(e),S(R.get(n)),()=>{e.disconnect()}},[n,s,R]);const[x,y]=r.useState();return r.useEffect(()=>{if(!A)return;if(p.current)return;p.current=!0;const e=o.createBinding(T,A,n,N||R.get(n),R,f);return y(e),()=>{e.root.destroy(e)}},[T,A,n,R,N,f]),A&&x?i.jsx(M,{awarenessData:E,binding:x,collabContext:m,color:D,cursorsContainerRef:l,editor:T,id:n,initialEditorState:d,name:O,provider:A,setDoc:S,shouldBootstrap:a,yjsDocMap:R,syncCursorPositionsFn:C}):i.jsx(i.Fragment,{})},exports.CollaborationPluginV2__EXPERIMENTAL=function({id:a,doc:i,provider:u,__shouldBootstrapUnsafe:l,username:d,cursorColor:M,cursorsContainerRef:R,excludedProperties:O,awarenessData:D}){const T=e.useCollaborationContext(d,M),{yjsDocMap:A,name:I,color:N}=T,[S]=t.useLexicalComposerContext();g(T,S);const x=function(e,t,a,i,u,l,d,C={}){const{awarenessData:p,excludedProperties:_,rootName:M,__shouldBootstrapUnsafe:g}=C,R=r.useMemo(()=>({current:!1}),[]),O=r.useMemo(()=>o.createBindingV2__EXPERIMENTAL(e,t,a,u,{excludedProperties:_,rootName:M}),[e,t,a,u,_,M]);r.useEffect(()=>(u.set(t,a),()=>{u.delete(t)}),[a,u,t]);const D=r.useCallback(()=>{const{root:t}=O;g&&0===t._length&&m(e)},[O,e,g]),[T,A]=r.useState();return r.useEffect(()=>{n.mergeRegister(e.registerCommand(o.CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL,()=>(A(null),o.syncYjsStateToLexicalV2__EXPERIMENTAL(O,i),!0),s.COMMAND_PRIORITY_EDITOR),e.registerCommand(o.DIFF_VERSIONS_COMMAND__EXPERIMENTAL,({prevSnapshot:e,snapshot:t})=>(A({prevSnapshot:e,snapshot:t}),!0),s.COMMAND_PRIORITY_EDITOR))},[e,O,i]),r.useEffect(()=>{const{root:t}=O;if(T)return void o.renderSnapshot__EXPERIMENTAL(O,T.snapshot,T.prevSnapshot);const r=(e,t)=>{const r=t.origin;if(r!==O){const n=r instanceof c.UndoManager;o.syncYjsChangesToLexicalV2__EXPERIMENTAL(O,i,e,t,n)}};t.observeDeep(r);const n=e.registerUpdateListener(({prevEditorState:e,editorState:t,dirtyElements:r,normalizedNodes:n,tags:a})=>{a.has(s.SKIP_COLLAB_TAG)||o.syncLexicalUpdateToYjsV2__EXPERIMENTAL(O,i,e,t,r,n,a)});return()=>{t.unobserveDeep(r),n()}},[O,i,e,T]),f(e,i,l,d,R,p,D),E(O,i),O}(S,a,i,u,A,I,N,{__shouldBootstrapUnsafe:l,awarenessData:D,excludedProperties:O});return function(e,t){_(e,r.useMemo(()=>o.createUndoManager(t,t.root),[t]))}(S,x),p(S,u,I,N,D),C(x,R)};
9
+ "use strict";var e=require("@lexical/react/LexicalCollaborationContext"),t=require("@lexical/react/LexicalComposerContext"),o=require("@lexical/yjs"),r=require("react"),n=require("@lexical/utils"),s=require("lexical"),a=require("react-dom"),c=require("yjs"),i=require("react/jsx-runtime");function u(e){var t=Object.create(null);if(e)for(var o in e)t[o]=e[o];return t.default=e,t}var l=u(r);function d(e,t,n,a,i,u,l,d,p,_,g,M,R=o.syncCursorPositions){const O=r.useRef(!1),D=r.useCallback(()=>{const{root:t}=d;l&&t.isEmpty()&&0===t._xmlText._length&&m(e,g)},[d,e,g,l]);return r.useEffect(()=>{const{root:t}=d,r=(e,t)=>{const r=t.origin;if(r!==d){const t=r instanceof c.UndoManager;o.syncYjsChangesToLexical(d,n,e,t,R)}};t.getSharedType().observeDeep(r);const a=e.registerUpdateListener(({prevEditorState:e,editorState:t,dirtyLeaves:r,dirtyElements:a,normalizedNodes:c,tags:i})=>{i.has(s.SKIP_COLLAB_TAG)||o.syncLexicalUpdateToYjs(d,n,e,t,a,r,c,i)});return()=>{t.getSharedType().unobserveDeep(r),a()}},[d,n,e,p,a,t,R]),r.useEffect(()=>{const o=o=>{!function(e,t){if(e.update(()=>{const e=s.$getRoot();e.clear(),e.select()},{tag:s.SKIP_COLLAB_TAG}),null==t.cursors)return;const o=t.cursors;if(null==o)return;const r=t.cursorsContainer;if(null==r)return;const n=Array.from(o.values());for(let e=0;e<n.length;e++){const t=n[e].selection;if(t&&null!=t.selections){const o=t.selections;for(let t=0;t<o.length;t++)r.removeChild(o[e])}}}(e,d),p(o),a.set(t,o),O.current=!0},r=()=>{O.current=!1};return n.on("reload",o),n.on("sync",r),()=>{n.off("reload",o),n.off("sync",r)}},[d,n,e,p,a,t]),f(e,n,i,u,O,M,D),E(d,n),C(d,_)}function f(e,t,n,a,c,i,u){const l=r.useCallback(()=>t.connect(),[t]),d=r.useCallback(()=>{try{t.disconnect()}catch(e){}},[t]);r.useEffect(()=>{const r=({status:t})=>{e.dispatchCommand(o.CONNECTED_COMMAND,"connected"===t)},s=e=>{e&&!1===c.current&&u&&u()};o.initLocalState(t,n,a,document.activeElement===e.getRootElement(),i||{}),t.on("status",r),t.on("sync",s);const f=l();return()=>{!1===c.current&&(f?f.then(d):d()),t.off("sync",s),t.off("status",r)}},[e,t,n,a,c,i,u,l,d]),r.useEffect(()=>e.registerCommand(o.TOGGLE_CONNECT_COMMAND,e=>(e?(console.log("Collaboration connected!"),l()):(console.log("Collaboration disconnected!"),d()),!0),s.COMMAND_PRIORITY_EDITOR),[l,d,e]),r.useEffect(()=>{const e=()=>{try{t.awareness.setLocalState(null)}catch(e){}};return window.addEventListener("beforeunload",e),window.addEventListener("pagehide",e),()=>{window.removeEventListener("beforeunload",e),window.removeEventListener("pagehide",e)}},[t])}function E(e,t){r.useEffect(()=>{const{awareness:r}=t,n=()=>{o.syncCursorPositions(e,t)};return r.on("update",n),()=>{r.off("update",n)}},[e,t])}function C(e,t){return r.useMemo(()=>a.createPortal(i.jsx("div",{ref:t=>{e.cursorsContainer=t}}),t&&t.current||document.body),[e,t])}function p(e,t,a,c,i){r.useEffect(()=>n.mergeRegister(e.registerCommand(s.FOCUS_COMMAND,()=>(o.setLocalStateFocus(t,a,c,!0,i||{}),!1),s.COMMAND_PRIORITY_EDITOR),e.registerCommand(s.BLUR_COMMAND,()=>(o.setLocalStateFocus(t,a,c,!1,i||{}),!1),s.COMMAND_PRIORITY_EDITOR)),[c,e,a,t,i])}function _(e,t){r.useEffect(()=>n.mergeRegister(e.registerCommand(s.UNDO_COMMAND,()=>(t.undo(),!0),s.COMMAND_PRIORITY_EDITOR),e.registerCommand(s.REDO_COMMAND,()=>(t.redo(),!0),s.COMMAND_PRIORITY_EDITOR)));const o=r.useCallback(()=>{t.clear()},[t]);return l.useEffect(()=>{const o=()=>{e.dispatchCommand(s.CAN_UNDO_COMMAND,t.undoStack.length>0),e.dispatchCommand(s.CAN_REDO_COMMAND,t.redoStack.length>0)};return t.on("stack-item-added",o),t.on("stack-item-popped",o),t.on("stack-cleared",o),()=>{t.off("stack-item-added",o),t.off("stack-item-popped",o),t.off("stack-cleared",o)}},[e,t]),o}function m(e,t){e.update(()=>{const o=s.$getRoot();if(o.isEmpty())if(t)switch(typeof t){case"string":{const o=e.parseEditorState(t);e.setEditorState(o,{tag:s.HISTORY_MERGE_TAG});break}case"object":e.setEditorState(t,{tag:s.HISTORY_MERGE_TAG});break;case"function":e.update(()=>{s.$getRoot().isEmpty()&&t(e)},{tag:s.HISTORY_MERGE_TAG})}else{const t=s.$createParagraphNode();o.append(t);const{activeElement:r}=document;(null!==s.$getSelection()||null!==r&&r===e.getRootElement())&&t.select()}},{tag:s.HISTORY_MERGE_TAG})}function g({editor:e,id:t,provider:n,yjsDocMap:s,name:a,color:c,shouldBootstrap:i,cursorsContainerRef:u,initialEditorState:l,awarenessData:f,collabContext:E,binding:C,setDoc:m,syncCursorPositionsFn:g}){const M=d(e,t,n,s,a,c,i,C,m,u,l,f,g);return function(e,t){_(e,r.useMemo(()=>o.createUndoManager(t,t.root.getSharedType()),[t]))}(e,C),p(e,n,a,c,f),M}const M=(e,t)=>{r.useEffect(()=>(e.isCollabActive=!0,()=>{null==t._parentEditor&&(e.isCollabActive=!1)}),[e,t])};exports.CollaborationPlugin=function({id:n,providerFactory:s,shouldBootstrap:a,username:c,cursorColor:u,cursorsContainerRef:l,initialEditorState:d,excludedProperties:f,awarenessData:E,syncCursorPositionsFn:C}){const p=r.useRef(!1),_=r.useRef(!1),m=e.useCollaborationContext(c,u),{yjsDocMap:R,name:O,color:D}=m,[T]=t.useLexicalComposerContext();M(m,T);const[A,S]=r.useState(),[I,N]=r.useState();r.useEffect(()=>{if(_.current)return;_.current=!0;const e=s(n,R);return S(e),N(R.get(n)),()=>{e.disconnect()}},[n,s,R]);const[h,L]=r.useState();return r.useEffect(()=>{if(!A)return;if(p.current)return;p.current=!0;const e=o.createBinding(T,A,n,I||R.get(n),R,f);return L(e),()=>{e.root.destroy(e)}},[T,A,n,R,I,f]),A&&h?i.jsx(g,{awarenessData:E,binding:h,collabContext:m,color:D,cursorsContainerRef:l,editor:T,id:n,initialEditorState:d,name:O,provider:A,setDoc:N,shouldBootstrap:a,yjsDocMap:R,syncCursorPositionsFn:C}):i.jsx(i.Fragment,{})},exports.CollaborationPluginV2__EXPERIMENTAL=function({id:a,doc:i,provider:u,__shouldBootstrapUnsafe:l,username:d,cursorColor:g,cursorsContainerRef:R,excludedProperties:O,awarenessData:D}){const T=e.useCollaborationContext(d,g),{yjsDocMap:A,name:S,color:I}=T,[N]=t.useLexicalComposerContext();M(T,N);const h=function(e,t,a,i,u,l,d,C={}){const{awarenessData:p,excludedProperties:_,rootName:g,__shouldBootstrapUnsafe:M}=C,R=r.useMemo(()=>({current:!1}),[]),O=r.useMemo(()=>o.createBindingV2__EXPERIMENTAL(e,t,a,u,{excludedProperties:_,rootName:g}),[e,t,a,u,_,g]);r.useEffect(()=>(u.set(t,a),()=>{u.delete(t)}),[a,u,t]);const D=r.useCallback(()=>{const{root:t}=O;M&&0===t._length&&m(e)},[O,e,M]),[T,A]=r.useState();return r.useEffect(()=>{n.mergeRegister(e.registerCommand(o.CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL,()=>(A(null),o.syncYjsStateToLexicalV2__EXPERIMENTAL(O,i),!0),s.COMMAND_PRIORITY_EDITOR),e.registerCommand(o.DIFF_VERSIONS_COMMAND__EXPERIMENTAL,({prevSnapshot:e,snapshot:t})=>(A({prevSnapshot:e,snapshot:t}),!0),s.COMMAND_PRIORITY_EDITOR))},[e,O,i]),r.useEffect(()=>{const{root:t}=O;if(T)return void o.renderSnapshot__EXPERIMENTAL(O,T.snapshot,T.prevSnapshot);const r=(e,t)=>{const r=t.origin;if(r!==O){const n=r instanceof c.UndoManager;o.syncYjsChangesToLexicalV2__EXPERIMENTAL(O,i,e,t,n)}};t.observeDeep(r);const n=e.registerUpdateListener(({prevEditorState:e,editorState:t,dirtyElements:r,normalizedNodes:n,tags:a})=>{a.has(s.SKIP_COLLAB_TAG)||o.syncLexicalUpdateToYjsV2__EXPERIMENTAL(O,i,e,t,r,n,a)});return()=>{t.unobserveDeep(r),n()}},[O,i,e,T]),f(e,i,l,d,R,p,D),E(O,i),O}(N,a,i,u,A,S,I,{__shouldBootstrapUnsafe:l,awarenessData:D,excludedProperties:O});return function(e,t){_(e,r.useMemo(()=>o.createUndoManager(t,t.root),[t]))}(N,h),p(N,u,S,I,D),C(h,R)};
@@ -6,4 +6,4 @@
6
6
  *
7
7
  */
8
8
 
9
- import{useCollaborationContext as t}from"@lexical/react/LexicalCollaborationContext";import{useLexicalComposerContext as e}from"@lexical/react/LexicalComposerContext";import{syncCursorPositions as o,syncLexicalUpdateToYjs as r,createUndoManager as n,setLocalStateFocus as s,createBindingV2__EXPERIMENTAL as a,CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL as c,syncYjsStateToLexicalV2__EXPERIMENTAL as i,DIFF_VERSIONS_COMMAND__EXPERIMENTAL as d,renderSnapshot__EXPERIMENTAL as u,syncLexicalUpdateToYjsV2__EXPERIMENTAL as l,syncYjsChangesToLexical as p,initLocalState as m,TOGGLE_CONNECT_COMMAND as f,syncYjsChangesToLexicalV2__EXPERIMENTAL as g,CONNECTED_COMMAND as C,createBinding as h}from"@lexical/yjs";import*as y from"react";import{useRef as v,useCallback as x,useEffect as E,useMemo as S,useState as b}from"react";import{mergeRegister as D}from"@lexical/utils";import{SKIP_COLLAB_TAG as k,FOCUS_COMMAND as _,COMMAND_PRIORITY_EDITOR as j,BLUR_COMMAND as w,$getRoot as P,HISTORY_MERGE_TAG as B,$createParagraphNode as R,$getSelection as L,UNDO_COMMAND as U,REDO_COMMAND as F,CAN_UNDO_COMMAND as M,CAN_REDO_COMMAND as N}from"lexical";import{createPortal as T}from"react-dom";import{UndoManager as A}from"yjs";import{jsx as z,Fragment as q}from"react/jsx-runtime";function G(t,e,n,s,a,c,i,d,u,l,m,f,g=o){const C=v(!1),h=x(()=>{const{root:e}=d;i&&e.isEmpty()&&0===e._xmlText._length&&Q(t,m)},[d,t,m,i]);return E(()=>{const{root:e}=d,o=(t,e)=>{const o=e.origin;if(o!==d){p(d,n,t,o instanceof A,g)}};e.getSharedType().observeDeep(o);const s=t.registerUpdateListener(({prevEditorState:t,editorState:e,dirtyLeaves:o,dirtyElements:s,normalizedNodes:a,tags:c})=>{c.has(k)||r(d,n,t,e,s,o,a,c)});return()=>{e.getSharedType().unobserveDeep(o),s()}},[d,n,t,u,s,e,g]),E(()=>{const o=o=>{!function(t,e){if(t.update(()=>{const t=P();t.clear(),t.select()},{tag:k}),null==e.cursors)return;const o=e.cursors;if(null==o)return;const r=e.cursorsContainer;if(null==r)return;const n=Array.from(o.values());for(let t=0;t<n.length;t++){const e=n[t].selection;if(e&&null!=e.selections){const o=e.selections;for(let e=0;e<o.length;e++)r.removeChild(o[t])}}}(t,d),u(o),s.set(e,o),C.current=!0},r=()=>{C.current=!1};return n.on("reload",o),n.on("sync",r),()=>{n.off("reload",o),n.off("sync",r)}},[d,n,t,u,s,e]),H(t,n,a,c,C,f,h),I(d,n),J(d,l)}function H(t,e,o,r,n,s,a){const c=x(()=>e.connect(),[e]),i=x(()=>{try{e.disconnect()}catch(t){}},[e]);E(()=>{const d=({status:e})=>{t.dispatchCommand(C,"connected"===e)},u=t=>{t&&!1===n.current&&a&&a()};m(e,o,r,document.activeElement===t.getRootElement(),s||{}),e.on("status",d),e.on("sync",u);const l=c();return()=>{!1===n.current&&(l?l.then(i):i()),e.off("sync",u),e.off("status",d)}},[t,e,o,r,n,s,a,c,i]),E(()=>t.registerCommand(f,t=>(t?(console.log("Collaboration connected!"),c()):(console.log("Collaboration disconnected!"),i()),!0),j),[c,i,t])}function I(t,e){E(()=>{const{awareness:r}=e,n=()=>{o(t,e)};return r.on("update",n),()=>{r.off("update",n)}},[t,e])}function J(t,e){return S(()=>T(z("div",{ref:e=>{t.cursorsContainer=e}}),e&&e.current||document.body),[t,e])}function K(t,e,o,r,n){E(()=>D(t.registerCommand(_,()=>(s(e,o,r,!0,n||{}),!1),j),t.registerCommand(w,()=>(s(e,o,r,!1,n||{}),!1),j)),[r,t,o,e,n])}function O(t,e){E(()=>D(t.registerCommand(U,()=>(e.undo(),!0),j),t.registerCommand(F,()=>(e.redo(),!0),j)));const o=x(()=>{e.clear()},[e]);return y.useEffect(()=>{const o=()=>{t.dispatchCommand(M,e.undoStack.length>0),t.dispatchCommand(N,e.redoStack.length>0)};return e.on("stack-item-added",o),e.on("stack-item-popped",o),e.on("stack-cleared",o),()=>{e.off("stack-item-added",o),e.off("stack-item-popped",o),e.off("stack-cleared",o)}},[t,e]),o}function Q(t,e){t.update(()=>{const o=P();if(o.isEmpty())if(e)switch(typeof e){case"string":{const o=t.parseEditorState(e);t.setEditorState(o,{tag:B});break}case"object":t.setEditorState(e,{tag:B});break;case"function":t.update(()=>{P().isEmpty()&&e(t)},{tag:B})}else{const e=R();o.append(e);const{activeElement:r}=document;(null!==L()||null!==r&&r===t.getRootElement())&&e.select()}},{tag:B})}function V({id:o,providerFactory:r,shouldBootstrap:n,username:s,cursorColor:a,cursorsContainerRef:c,initialEditorState:i,excludedProperties:d,awarenessData:u,syncCursorPositionsFn:l}){const p=v(!1),m=v(!1),f=t(s,a),{yjsDocMap:g,name:C,color:y}=f,[x]=e();Y(f,x);const[S,D]=b(),[k,_]=b();E(()=>{if(m.current)return;m.current=!0;const t=r(o,g);return D(t),_(g.get(o)),()=>{t.disconnect()}},[o,r,g]);const[j,w]=b();return E(()=>{if(!S)return;if(p.current)return;p.current=!0;const t=h(x,S,o,k||g.get(o),g,d);return w(t),()=>{t.root.destroy(t)}},[x,S,o,g,k,d]),S&&j?z(W,{awarenessData:u,binding:j,collabContext:f,color:y,cursorsContainerRef:c,editor:x,id:o,initialEditorState:i,name:C,provider:S,setDoc:_,shouldBootstrap:n,yjsDocMap:g,syncCursorPositionsFn:l}):z(q,{})}function W({editor:t,id:e,provider:o,yjsDocMap:r,name:s,color:a,shouldBootstrap:c,cursorsContainerRef:i,initialEditorState:d,awarenessData:u,collabContext:l,binding:p,setDoc:m,syncCursorPositionsFn:f}){const g=G(t,e,o,r,s,a,c,p,m,i,d,u,f);return function(t,e){O(t,S(()=>n(e,e.root.getSharedType()),[e]))}(t,p),K(t,o,s,a,u),g}function X({id:o,doc:r,provider:s,__shouldBootstrapUnsafe:p,username:m,cursorColor:f,cursorsContainerRef:C,excludedProperties:h,awarenessData:y}){const v=t(m,f),{yjsDocMap:_,name:w,color:P}=v,[B]=e();Y(v,B);const R=function(t,e,o,r,n,s,p,m={}){const{awarenessData:f,excludedProperties:C,rootName:h,__shouldBootstrapUnsafe:y}=m,v=S(()=>({current:!1}),[]),_=S(()=>a(t,e,o,n,{excludedProperties:C,rootName:h}),[t,e,o,n,C,h]);E(()=>(n.set(e,o),()=>{n.delete(e)}),[o,n,e]);const w=x(()=>{const{root:e}=_;y&&0===e._length&&Q(t)},[_,t,y]),[P,B]=b();return E(()=>{D(t.registerCommand(c,()=>(B(null),i(_,r),!0),j),t.registerCommand(d,({prevSnapshot:t,snapshot:e})=>(B({prevSnapshot:t,snapshot:e}),!0),j))},[t,_,r]),E(()=>{const{root:e}=_;if(P)return void u(_,P.snapshot,P.prevSnapshot);const o=(t,e)=>{const o=e.origin;o!==_&&g(_,r,t,e,o instanceof A)};e.observeDeep(o);const n=t.registerUpdateListener(({prevEditorState:t,editorState:e,dirtyElements:o,normalizedNodes:n,tags:s})=>{s.has(k)||l(_,r,t,e,o,n,s)});return()=>{e.unobserveDeep(o),n()}},[_,r,t,P]),H(t,r,s,p,v,f,w),I(_,r),_}(B,o,r,s,_,w,P,{__shouldBootstrapUnsafe:p,awarenessData:y,excludedProperties:h});return function(t,e){O(t,S(()=>n(e,e.root),[e]))}(B,R),K(B,s,w,P,y),J(R,C)}const Y=(t,e)=>{E(()=>(t.isCollabActive=!0,()=>{null==e._parentEditor&&(t.isCollabActive=!1)}),[t,e])};export{V as CollaborationPlugin,X as CollaborationPluginV2__EXPERIMENTAL};
9
+ import{useCollaborationContext as t}from"@lexical/react/LexicalCollaborationContext";import{useLexicalComposerContext as e}from"@lexical/react/LexicalComposerContext";import{syncCursorPositions as o,syncLexicalUpdateToYjs as r,createUndoManager as n,setLocalStateFocus as s,createBindingV2__EXPERIMENTAL as a,CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL as c,syncYjsStateToLexicalV2__EXPERIMENTAL as i,DIFF_VERSIONS_COMMAND__EXPERIMENTAL as d,renderSnapshot__EXPERIMENTAL as l,syncLexicalUpdateToYjsV2__EXPERIMENTAL as u,syncYjsChangesToLexical as p,initLocalState as m,TOGGLE_CONNECT_COMMAND as f,syncYjsChangesToLexicalV2__EXPERIMENTAL as g,CONNECTED_COMMAND as h,createBinding as C}from"@lexical/yjs";import*as y from"react";import{useRef as v,useCallback as E,useEffect as x,useMemo as b,useState as S}from"react";import{mergeRegister as w}from"@lexical/utils";import{SKIP_COLLAB_TAG as D,FOCUS_COMMAND as k,COMMAND_PRIORITY_EDITOR as L,BLUR_COMMAND as _,$getRoot as j,HISTORY_MERGE_TAG as P,$createParagraphNode as B,$getSelection as R,UNDO_COMMAND as U,REDO_COMMAND as F,CAN_UNDO_COMMAND as M,CAN_REDO_COMMAND as N}from"lexical";import{createPortal as T}from"react-dom";import{UndoManager as A}from"yjs";import{jsx as z,Fragment as q}from"react/jsx-runtime";function G(t,e,n,s,a,c,i,d,l,u,m,f,g=o){const h=v(!1),C=E(()=>{const{root:e}=d;i&&e.isEmpty()&&0===e._xmlText._length&&Q(t,m)},[d,t,m,i]);return x(()=>{const{root:e}=d,o=(t,e)=>{const o=e.origin;if(o!==d){p(d,n,t,o instanceof A,g)}};e.getSharedType().observeDeep(o);const s=t.registerUpdateListener(({prevEditorState:t,editorState:e,dirtyLeaves:o,dirtyElements:s,normalizedNodes:a,tags:c})=>{c.has(D)||r(d,n,t,e,s,o,a,c)});return()=>{e.getSharedType().unobserveDeep(o),s()}},[d,n,t,l,s,e,g]),x(()=>{const o=o=>{!function(t,e){if(t.update(()=>{const t=j();t.clear(),t.select()},{tag:D}),null==e.cursors)return;const o=e.cursors;if(null==o)return;const r=e.cursorsContainer;if(null==r)return;const n=Array.from(o.values());for(let t=0;t<n.length;t++){const e=n[t].selection;if(e&&null!=e.selections){const o=e.selections;for(let e=0;e<o.length;e++)r.removeChild(o[t])}}}(t,d),l(o),s.set(e,o),h.current=!0},r=()=>{h.current=!1};return n.on("reload",o),n.on("sync",r),()=>{n.off("reload",o),n.off("sync",r)}},[d,n,t,l,s,e]),H(t,n,a,c,h,f,C),I(d,n),J(d,u)}function H(t,e,o,r,n,s,a){const c=E(()=>e.connect(),[e]),i=E(()=>{try{e.disconnect()}catch(t){}},[e]);x(()=>{const d=({status:e})=>{t.dispatchCommand(h,"connected"===e)},l=t=>{t&&!1===n.current&&a&&a()};m(e,o,r,document.activeElement===t.getRootElement(),s||{}),e.on("status",d),e.on("sync",l);const u=c();return()=>{!1===n.current&&(u?u.then(i):i()),e.off("sync",l),e.off("status",d)}},[t,e,o,r,n,s,a,c,i]),x(()=>t.registerCommand(f,t=>(t?(console.log("Collaboration connected!"),c()):(console.log("Collaboration disconnected!"),i()),!0),L),[c,i,t]),x(()=>{const t=()=>{try{e.awareness.setLocalState(null)}catch(t){}};return window.addEventListener("beforeunload",t),window.addEventListener("pagehide",t),()=>{window.removeEventListener("beforeunload",t),window.removeEventListener("pagehide",t)}},[e])}function I(t,e){x(()=>{const{awareness:r}=e,n=()=>{o(t,e)};return r.on("update",n),()=>{r.off("update",n)}},[t,e])}function J(t,e){return b(()=>T(z("div",{ref:e=>{t.cursorsContainer=e}}),e&&e.current||document.body),[t,e])}function K(t,e,o,r,n){x(()=>w(t.registerCommand(k,()=>(s(e,o,r,!0,n||{}),!1),L),t.registerCommand(_,()=>(s(e,o,r,!1,n||{}),!1),L)),[r,t,o,e,n])}function O(t,e){x(()=>w(t.registerCommand(U,()=>(e.undo(),!0),L),t.registerCommand(F,()=>(e.redo(),!0),L)));const o=E(()=>{e.clear()},[e]);return y.useEffect(()=>{const o=()=>{t.dispatchCommand(M,e.undoStack.length>0),t.dispatchCommand(N,e.redoStack.length>0)};return e.on("stack-item-added",o),e.on("stack-item-popped",o),e.on("stack-cleared",o),()=>{e.off("stack-item-added",o),e.off("stack-item-popped",o),e.off("stack-cleared",o)}},[t,e]),o}function Q(t,e){t.update(()=>{const o=j();if(o.isEmpty())if(e)switch(typeof e){case"string":{const o=t.parseEditorState(e);t.setEditorState(o,{tag:P});break}case"object":t.setEditorState(e,{tag:P});break;case"function":t.update(()=>{j().isEmpty()&&e(t)},{tag:P})}else{const e=B();o.append(e);const{activeElement:r}=document;(null!==R()||null!==r&&r===t.getRootElement())&&e.select()}},{tag:P})}function V({id:o,providerFactory:r,shouldBootstrap:n,username:s,cursorColor:a,cursorsContainerRef:c,initialEditorState:i,excludedProperties:d,awarenessData:l,syncCursorPositionsFn:u}){const p=v(!1),m=v(!1),f=t(s,a),{yjsDocMap:g,name:h,color:y}=f,[E]=e();Y(f,E);const[b,w]=S(),[D,k]=S();x(()=>{if(m.current)return;m.current=!0;const t=r(o,g);return w(t),k(g.get(o)),()=>{t.disconnect()}},[o,r,g]);const[L,_]=S();return x(()=>{if(!b)return;if(p.current)return;p.current=!0;const t=C(E,b,o,D||g.get(o),g,d);return _(t),()=>{t.root.destroy(t)}},[E,b,o,g,D,d]),b&&L?z(W,{awarenessData:l,binding:L,collabContext:f,color:y,cursorsContainerRef:c,editor:E,id:o,initialEditorState:i,name:h,provider:b,setDoc:k,shouldBootstrap:n,yjsDocMap:g,syncCursorPositionsFn:u}):z(q,{})}function W({editor:t,id:e,provider:o,yjsDocMap:r,name:s,color:a,shouldBootstrap:c,cursorsContainerRef:i,initialEditorState:d,awarenessData:l,collabContext:u,binding:p,setDoc:m,syncCursorPositionsFn:f}){const g=G(t,e,o,r,s,a,c,p,m,i,d,l,f);return function(t,e){O(t,b(()=>n(e,e.root.getSharedType()),[e]))}(t,p),K(t,o,s,a,l),g}function X({id:o,doc:r,provider:s,__shouldBootstrapUnsafe:p,username:m,cursorColor:f,cursorsContainerRef:h,excludedProperties:C,awarenessData:y}){const v=t(m,f),{yjsDocMap:k,name:_,color:j}=v,[P]=e();Y(v,P);const B=function(t,e,o,r,n,s,p,m={}){const{awarenessData:f,excludedProperties:h,rootName:C,__shouldBootstrapUnsafe:y}=m,v=b(()=>({current:!1}),[]),k=b(()=>a(t,e,o,n,{excludedProperties:h,rootName:C}),[t,e,o,n,h,C]);x(()=>(n.set(e,o),()=>{n.delete(e)}),[o,n,e]);const _=E(()=>{const{root:e}=k;y&&0===e._length&&Q(t)},[k,t,y]),[j,P]=S();return x(()=>{w(t.registerCommand(c,()=>(P(null),i(k,r),!0),L),t.registerCommand(d,({prevSnapshot:t,snapshot:e})=>(P({prevSnapshot:t,snapshot:e}),!0),L))},[t,k,r]),x(()=>{const{root:e}=k;if(j)return void l(k,j.snapshot,j.prevSnapshot);const o=(t,e)=>{const o=e.origin;o!==k&&g(k,r,t,e,o instanceof A)};e.observeDeep(o);const n=t.registerUpdateListener(({prevEditorState:t,editorState:e,dirtyElements:o,normalizedNodes:n,tags:s})=>{s.has(D)||u(k,r,t,e,o,n,s)});return()=>{e.unobserveDeep(o),n()}},[k,r,t,j]),H(t,r,s,p,v,f,_),I(k,r),k}(P,o,r,s,k,_,j,{__shouldBootstrapUnsafe:p,awarenessData:y,excludedProperties:C});return function(t,e){O(t,b(()=>n(e,e.root),[e]))}(P,B),K(P,s,_,j,y),J(B,h)}const Y=(t,e)=>{x(()=>(t.isCollabActive=!0,()=>{null==e._parentEditor&&(t.isCollabActive=!1)}),[t,e])};export{V as CollaborationPlugin,X as CollaborationPluginV2__EXPERIMENTAL};
package/package.json CHANGED
@@ -8,26 +8,26 @@
8
8
  "rich-text"
9
9
  ],
10
10
  "license": "MIT",
11
- "version": "0.39.1-nightly.20260105.0",
11
+ "version": "0.39.1-nightly.20260107.0",
12
12
  "dependencies": {
13
13
  "@floating-ui/react": "^0.27.16",
14
- "@lexical/devtools-core": "0.39.1-nightly.20260105.0",
15
- "@lexical/dragon": "0.39.1-nightly.20260105.0",
16
- "@lexical/extension": "0.39.1-nightly.20260105.0",
17
- "@lexical/hashtag": "0.39.1-nightly.20260105.0",
18
- "@lexical/history": "0.39.1-nightly.20260105.0",
19
- "@lexical/link": "0.39.1-nightly.20260105.0",
20
- "@lexical/list": "0.39.1-nightly.20260105.0",
21
- "@lexical/mark": "0.39.1-nightly.20260105.0",
22
- "@lexical/markdown": "0.39.1-nightly.20260105.0",
23
- "@lexical/overflow": "0.39.1-nightly.20260105.0",
24
- "@lexical/plain-text": "0.39.1-nightly.20260105.0",
25
- "@lexical/rich-text": "0.39.1-nightly.20260105.0",
26
- "@lexical/table": "0.39.1-nightly.20260105.0",
27
- "@lexical/text": "0.39.1-nightly.20260105.0",
28
- "@lexical/utils": "0.39.1-nightly.20260105.0",
29
- "@lexical/yjs": "0.39.1-nightly.20260105.0",
30
- "lexical": "0.39.1-nightly.20260105.0",
14
+ "@lexical/devtools-core": "0.39.1-nightly.20260107.0",
15
+ "@lexical/dragon": "0.39.1-nightly.20260107.0",
16
+ "@lexical/extension": "0.39.1-nightly.20260107.0",
17
+ "@lexical/hashtag": "0.39.1-nightly.20260107.0",
18
+ "@lexical/history": "0.39.1-nightly.20260107.0",
19
+ "@lexical/link": "0.39.1-nightly.20260107.0",
20
+ "@lexical/list": "0.39.1-nightly.20260107.0",
21
+ "@lexical/mark": "0.39.1-nightly.20260107.0",
22
+ "@lexical/markdown": "0.39.1-nightly.20260107.0",
23
+ "@lexical/overflow": "0.39.1-nightly.20260107.0",
24
+ "@lexical/plain-text": "0.39.1-nightly.20260107.0",
25
+ "@lexical/rich-text": "0.39.1-nightly.20260107.0",
26
+ "@lexical/table": "0.39.1-nightly.20260107.0",
27
+ "@lexical/text": "0.39.1-nightly.20260107.0",
28
+ "@lexical/utils": "0.39.1-nightly.20260107.0",
29
+ "@lexical/yjs": "0.39.1-nightly.20260107.0",
30
+ "lexical": "0.39.1-nightly.20260107.0",
31
31
  "react-error-boundary": "^6.0.0"
32
32
  },
33
33
  "peerDependencies": {