hooks.ts 1.0 KB

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