| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | import { useCallback } from 'react' | 
					
						
							|  |  |  | import type { EditorState } from 'lexical' | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  | import { WorkflowHistoryEvent, useNodeDataUpdate, useWorkflowHistory } from '../hooks' | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | import type { NoteTheme } from './types' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const useNote = (id: string) => { | 
					
						
							|  |  |  |   const { handleNodeDataUpdateWithSyncDraft } = useNodeDataUpdate() | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  |   const { saveStateToHistory } = useWorkflowHistory() | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const handleThemeChange = useCallback((theme: NoteTheme) => { | 
					
						
							|  |  |  |     handleNodeDataUpdateWithSyncDraft({ id, data: { theme } }) | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  |     saveStateToHistory(WorkflowHistoryEvent.NoteChange) | 
					
						
							|  |  |  |   }, [handleNodeDataUpdateWithSyncDraft, id, saveStateToHistory]) | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const handleEditorChange = useCallback((editorState: EditorState) => { | 
					
						
							|  |  |  |     if (!editorState?.isEmpty()) | 
					
						
							|  |  |  |       handleNodeDataUpdateWithSyncDraft({ id, data: { text: JSON.stringify(editorState) } }) | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       handleNodeDataUpdateWithSyncDraft({ id, data: { text: '' } }) | 
					
						
							|  |  |  |   }, [handleNodeDataUpdateWithSyncDraft, id]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleShowAuthorChange = useCallback((showAuthor: boolean) => { | 
					
						
							|  |  |  |     handleNodeDataUpdateWithSyncDraft({ id, data: { showAuthor } }) | 
					
						
							| 
									
										
										
										
											2024-06-26 08:37:12 +02:00
										 |  |  |     saveStateToHistory(WorkflowHistoryEvent.NoteChange) | 
					
						
							|  |  |  |   }, [handleNodeDataUpdateWithSyncDraft, id, saveStateToHistory]) | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     handleThemeChange, | 
					
						
							|  |  |  |     handleEditorChange, | 
					
						
							|  |  |  |     handleShowAuthorChange, | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |