mirror of
https://github.com/langgenius/dify.git
synced 2026-06-08 00:41:55 +08:00
Signed-off-by: Cocoon-Break <54054995+kuishou68@users.noreply.github.com> Co-authored-by: lingxiu58 <86288566+lingxiu58@users.noreply.github.com> Co-authored-by: pojian68 <232320289+pojian68@users.noreply.github.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com>
37 lines
1.6 KiB
TypeScript
37 lines
1.6 KiB
TypeScript
import type { EditorState } from 'lexical'
|
|
import type { NoteTheme } from './types'
|
|
import { useCallback } from 'react'
|
|
import { useSetLocalStorage } from '@/hooks/use-local-storage'
|
|
import { useNodeDataUpdate, useWorkflowHistory, WorkflowHistoryEvent } from '../hooks'
|
|
import { NOTE_SHOW_AUTHOR_STORAGE_KEY } from './constants'
|
|
|
|
export const useNote = (id: string) => {
|
|
const { handleNodeDataUpdateWithSyncDraft } = useNodeDataUpdate()
|
|
const { saveStateToHistory } = useWorkflowHistory()
|
|
const setShowAuthorStorage = useSetLocalStorage<string>(NOTE_SHOW_AUTHOR_STORAGE_KEY, { raw: true })
|
|
|
|
const handleThemeChange = useCallback((theme: NoteTheme) => {
|
|
handleNodeDataUpdateWithSyncDraft({ id, data: { theme } })
|
|
saveStateToHistory(WorkflowHistoryEvent.NoteChange, { nodeId: id })
|
|
}, [handleNodeDataUpdateWithSyncDraft, id, saveStateToHistory])
|
|
|
|
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) => {
|
|
setShowAuthorStorage(String(showAuthor))
|
|
handleNodeDataUpdateWithSyncDraft({ id, data: { showAuthor } })
|
|
saveStateToHistory(WorkflowHistoryEvent.NoteChange, { nodeId: id })
|
|
}, [handleNodeDataUpdateWithSyncDraft, id, saveStateToHistory, setShowAuthorStorage])
|
|
|
|
return {
|
|
handleThemeChange,
|
|
handleEditorChange,
|
|
handleShowAuthorChange,
|
|
}
|
|
}
|