| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | 'use client' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   createContext, | 
					
						
							|  |  |  |   memo, | 
					
						
							|  |  |  |   useRef, | 
					
						
							|  |  |  | } from 'react' | 
					
						
							|  |  |  | import { LexicalComposer } from '@lexical/react/LexicalComposer' | 
					
						
							|  |  |  | import { LinkNode } from '@lexical/link' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   ListItemNode, | 
					
						
							|  |  |  |   ListNode, | 
					
						
							|  |  |  | } from '@lexical/list' | 
					
						
							|  |  |  | import { createNoteEditorStore } from './store' | 
					
						
							|  |  |  | import theme from './theme' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type NoteEditorStore = ReturnType<typeof createNoteEditorStore> | 
					
						
							|  |  |  | const NoteEditorContext = createContext<NoteEditorStore | null>(null) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type NoteEditorContextProviderProps = { | 
					
						
							|  |  |  |   value: string | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |   children: React.JSX.Element | string | (React.JSX.Element | string)[] | 
					
						
							| 
									
										
										
										
											2025-05-14 18:02:22 +08:00
										 |  |  |   editable?: boolean | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | export const NoteEditorContextProvider = memo(({ | 
					
						
							|  |  |  |   value, | 
					
						
							|  |  |  |   children, | 
					
						
							| 
									
										
										
										
											2025-05-14 18:02:22 +08:00
										 |  |  |   editable = true, | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | }: NoteEditorContextProviderProps) => { | 
					
						
							| 
									
										
										
										
											2025-03-21 17:41:03 +08:00
										 |  |  |   const storeRef = useRef<NoteEditorStore | undefined>(undefined) | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   if (!storeRef.current) | 
					
						
							|  |  |  |     storeRef.current = createNoteEditorStore() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   let initialValue = null | 
					
						
							|  |  |  |   try { | 
					
						
							|  |  |  |     initialValue = JSON.parse(value) | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2025-04-14 11:27:14 +08:00
										 |  |  |   catch { | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const initialConfig = { | 
					
						
							|  |  |  |     namespace: 'note-editor', | 
					
						
							|  |  |  |     nodes: [ | 
					
						
							|  |  |  |       LinkNode, | 
					
						
							|  |  |  |       ListNode, | 
					
						
							|  |  |  |       ListItemNode, | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     editorState: !initialValue?.root.children.length ? null : JSON.stringify(initialValue), | 
					
						
							|  |  |  |     onError: (error: Error) => { | 
					
						
							|  |  |  |       throw error | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     theme, | 
					
						
							| 
									
										
										
										
											2025-05-14 18:02:22 +08:00
										 |  |  |     editable, | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return ( | 
					
						
							|  |  |  |     <NoteEditorContext.Provider value={storeRef.current}> | 
					
						
							|  |  |  |       <LexicalComposer initialConfig={{ ...initialConfig }}> | 
					
						
							|  |  |  |         {children} | 
					
						
							|  |  |  |       </LexicalComposer> | 
					
						
							|  |  |  |     </NoteEditorContext.Provider> | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | NoteEditorContextProvider.displayName = 'NoteEditorContextProvider' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default NoteEditorContext |