11import { useEffect , useRef , useMemo , useCallback } from "react" ;
22import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext" ;
3- import { $createParagraphNode , $createTextNode , $getRoot } from "lexical" ;
43import type * as Y from "yjs" ;
5- import { toast } from "sonner" ;
64import { debounce } from "@/renderer/main/utils/debounce" ;
7- import { useTranslation } from "react-i18next" ;
85
96interface YjsSyncPluginProps {
107 yText : Y . Text ;
@@ -15,11 +12,9 @@ export function YjsSyncPlugin({
1512 yText,
1613 onSyncStatusChange,
1714} : YjsSyncPluginProps ) : null {
18- const { t } = useTranslation ( ) ;
1915 const [ editor ] = useLexicalComposerContext ( ) ;
2016 const isUpdatingFromYjsRef = useRef ( false ) ;
2117 const isUpdatingFromLexicalRef = useRef ( false ) ;
22- const isMigratingRef = useRef ( false ) ;
2318 const hasPendingRef = useRef ( false ) ;
2419 const pendingJsonRef = useRef < string | null > ( null ) ;
2520 const onSyncStatusChangeRef = useRef ( onSyncStatusChange ) ;
@@ -31,7 +26,7 @@ export function YjsSyncPlugin({
3126
3227 const writeJsonToYjs = useCallback (
3328 ( jsonString : string ) => {
34- if ( isUpdatingFromYjsRef . current || isMigratingRef . current ) {
29+ if ( isUpdatingFromYjsRef . current ) {
3530 onSyncStatusChangeRef . current ?.( false ) ;
3631 return ;
3732 }
@@ -73,63 +68,20 @@ export function YjsSyncPlugin({
7368 }
7469 } ;
7570
76- const migrateLegacyText = ( legacyText : string ) => {
77- isMigratingRef . current = true ;
78- isUpdatingFromYjsRef . current = true ;
79-
80- editor . update ( ( ) => {
81- const root = $getRoot ( ) ;
82- root . clear ( ) ;
83-
84- const lines = legacyText . split ( / \r ? \n / ) ;
85- lines . forEach ( ( line ) => {
86- const paragraph = $createParagraphNode ( ) ;
87- if ( line . length > 0 ) {
88- paragraph . append ( $createTextNode ( line ) ) ;
89- }
90- root . append ( paragraph ) ;
91- } ) ;
92- } ) ;
93-
94- isUpdatingFromYjsRef . current = false ;
95-
96- const migratedJson = JSON . stringify ( editor . getEditorState ( ) . toJSON ( ) ) ;
97- const yDoc = yText . doc ;
98-
99- if ( yDoc ) {
100- isUpdatingFromLexicalRef . current = true ;
101- yDoc . transact ( ( ) => {
102- const oldLength = yText . length ;
103- yText . delete ( 0 , oldLength ) ;
104- yText . insert ( 0 , migratedJson ) ;
105- } , "migration" ) ;
106- isUpdatingFromLexicalRef . current = false ;
107- }
108-
109- toast . success ( t ( "settings.notes.toast.upgradedToRichNotes" ) ) ;
110-
111- isMigratingRef . current = false ;
112- } ;
113-
11471 // Set initial content from Yjs (stored as JSON)
11572 const storedContent = yText . toString ( ) ;
11673 if ( storedContent ) {
11774 try {
11875 setEditorStateFromJson ( storedContent ) ;
11976 } catch ( error ) {
120- // If parsing fails (e.g., old plain text content), migrate to Lexical JSON
121- console . warn (
122- "Failed to parse stored content as JSON, may be legacy format:" ,
123- error ,
124- ) ;
125- migrateLegacyText ( storedContent ) ;
77+ console . warn ( "Failed to parse stored content as editor state:" , error ) ;
12678 }
12779 }
12880 onSyncStatusChangeRef . current ?.( false ) ;
12981
13082 // Observer for Yjs changes -> Lexical
13183 const yjsObserver = ( ) => {
132- if ( isUpdatingFromLexicalRef . current || isMigratingRef . current ) return ;
84+ if ( isUpdatingFromLexicalRef . current ) return ;
13385
13486 const newContent = yText . toString ( ) ;
13587
@@ -153,7 +105,6 @@ export function YjsSyncPlugin({
153105 // Skip if triggered by Yjs sync or no actual changes
154106 if (
155107 isUpdatingFromYjsRef . current ||
156- isMigratingRef . current ||
157108 ( dirtyElements . size === 0 && dirtyLeaves . size === 0 )
158109 ) {
159110 return ;
0 commit comments