hooks.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { useCallback } from 'react'
  2. import type { EditorState } from 'lexical'
  3. import { WorkflowHistoryEvent, useNodeDataUpdate, useWorkflowHistory } from '../hooks'
  4. import type { NoteTheme } from './types'
  5. export const useNote = (id: string) => {
  6. const { handleNodeDataUpdateWithSyncDraft } = useNodeDataUpdate()
  7. const { saveStateToHistory } = useWorkflowHistory()
  8. const handleThemeChange = useCallback((theme: NoteTheme) => {
  9. handleNodeDataUpdateWithSyncDraft({ id, data: { theme } })
  10. saveStateToHistory(WorkflowHistoryEvent.NoteChange)
  11. }, [handleNodeDataUpdateWithSyncDraft, id, saveStateToHistory])
  12. const handleEditorChange = useCallback((editorState: EditorState) => {
  13. if (!editorState?.isEmpty())
  14. handleNodeDataUpdateWithSyncDraft({ id, data: { text: JSON.stringify(editorState) } })
  15. else
  16. handleNodeDataUpdateWithSyncDraft({ id, data: { text: '' } })
  17. }, [handleNodeDataUpdateWithSyncDraft, id])
  18. const handleShowAuthorChange = useCallback((showAuthor: boolean) => {
  19. handleNodeDataUpdateWithSyncDraft({ id, data: { showAuthor } })
  20. saveStateToHistory(WorkflowHistoryEvent.NoteChange)
  21. }, [handleNodeDataUpdateWithSyncDraft, id, saveStateToHistory])
  22. return {
  23. handleThemeChange,
  24. handleEditorChange,
  25. handleShowAuthorChange,
  26. }
  27. }