mirror of
https://github.com/langgenius/dify.git
synced 2026-05-05 18:08:07 +08:00
add note node sync data
This commit is contained in:
@ -3,22 +3,73 @@
|
|||||||
import {
|
import {
|
||||||
createContext,
|
createContext,
|
||||||
memo,
|
memo,
|
||||||
|
useEffect,
|
||||||
useRef,
|
useRef,
|
||||||
} from 'react'
|
} from 'react'
|
||||||
import { LexicalComposer } from '@lexical/react/LexicalComposer'
|
import { LexicalComposer } from '@lexical/react/LexicalComposer'
|
||||||
|
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
|
||||||
import { LinkNode } from '@lexical/link'
|
import { LinkNode } from '@lexical/link'
|
||||||
import {
|
import {
|
||||||
ListItemNode,
|
ListItemNode,
|
||||||
ListNode,
|
ListNode,
|
||||||
} from '@lexical/list'
|
} from '@lexical/list'
|
||||||
|
import { $getRoot } from 'lexical'
|
||||||
import { createNoteEditorStore } from './store'
|
import { createNoteEditorStore } from './store'
|
||||||
import theme from './theme'
|
import theme from './theme'
|
||||||
|
|
||||||
|
const NoteEditorContentSynchronizer = ({ value }: { value?: string }) => {
|
||||||
|
const [editor] = useLexicalComposerContext()
|
||||||
|
const lastSyncedValueRef = useRef<string | null>(null)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const normalizedValue = normalizeEditorState(value)
|
||||||
|
if (normalizedValue === lastSyncedValueRef.current)
|
||||||
|
return
|
||||||
|
|
||||||
|
const currentSerializedState = JSON.stringify(editor.getEditorState().toJSON())
|
||||||
|
if (normalizedValue === currentSerializedState) {
|
||||||
|
lastSyncedValueRef.current = normalizedValue
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!normalizedValue) {
|
||||||
|
let hasContent = false
|
||||||
|
editor.getEditorState().read(() => {
|
||||||
|
hasContent = !$getRoot().isEmpty()
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!hasContent) {
|
||||||
|
lastSyncedValueRef.current = normalizedValue
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
editor.update(() => {
|
||||||
|
const root = $getRoot()
|
||||||
|
root.clear()
|
||||||
|
root.select()
|
||||||
|
})
|
||||||
|
lastSyncedValueRef.current = normalizedValue
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const nextState = editor.parseEditorState(normalizedValue)
|
||||||
|
editor.setEditorState(nextState)
|
||||||
|
lastSyncedValueRef.current = normalizedValue
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
lastSyncedValueRef.current = ''
|
||||||
|
}
|
||||||
|
}, [editor, value])
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
type NoteEditorStore = ReturnType<typeof createNoteEditorStore>
|
type NoteEditorStore = ReturnType<typeof createNoteEditorStore>
|
||||||
const NoteEditorContext = createContext<NoteEditorStore | null>(null)
|
const NoteEditorContext = createContext<NoteEditorStore | null>(null)
|
||||||
|
|
||||||
type NoteEditorContextProviderProps = {
|
type NoteEditorContextProviderProps = {
|
||||||
value: string
|
value?: string
|
||||||
children: React.JSX.Element | string | (React.JSX.Element | string)[]
|
children: React.JSX.Element | string | (React.JSX.Element | string)[]
|
||||||
editable?: boolean
|
editable?: boolean
|
||||||
}
|
}
|
||||||
@ -34,7 +85,8 @@ export const NoteEditorContextProvider = memo(({
|
|||||||
|
|
||||||
let initialValue = null
|
let initialValue = null
|
||||||
try {
|
try {
|
||||||
initialValue = JSON.parse(value)
|
if (value)
|
||||||
|
initialValue = JSON.parse(value)
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
|
|
||||||
@ -58,6 +110,7 @@ export const NoteEditorContextProvider = memo(({
|
|||||||
return (
|
return (
|
||||||
<NoteEditorContext.Provider value={storeRef.current}>
|
<NoteEditorContext.Provider value={storeRef.current}>
|
||||||
<LexicalComposer initialConfig={{ ...initialConfig }}>
|
<LexicalComposer initialConfig={{ ...initialConfig }}>
|
||||||
|
<NoteEditorContentSynchronizer value={value} />
|
||||||
{children}
|
{children}
|
||||||
</LexicalComposer>
|
</LexicalComposer>
|
||||||
</NoteEditorContext.Provider>
|
</NoteEditorContext.Provider>
|
||||||
@ -66,3 +119,19 @@ export const NoteEditorContextProvider = memo(({
|
|||||||
NoteEditorContextProvider.displayName = 'NoteEditorContextProvider'
|
NoteEditorContextProvider.displayName = 'NoteEditorContextProvider'
|
||||||
|
|
||||||
export default NoteEditorContext
|
export default NoteEditorContext
|
||||||
|
|
||||||
|
function normalizeEditorState(value?: string): string {
|
||||||
|
if (!value)
|
||||||
|
return ''
|
||||||
|
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(value)
|
||||||
|
if (!parsed || typeof parsed !== 'object' || !parsed.root)
|
||||||
|
return ''
|
||||||
|
|
||||||
|
return JSON.stringify(parsed)
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user