minor fixes
This commit is contained in:
parent
9350aee23f
commit
c39eaf6e93
|
@ -55,60 +55,57 @@ const createEditorState = (
|
||||||
}
|
}
|
||||||
|
|
||||||
export const ProseMirror = (props: Props) => {
|
export const ProseMirror = (props: Props) => {
|
||||||
const editorRef = {} as HTMLDivElement
|
let editorRef: HTMLDivElement
|
||||||
const editorView = () => untrack(() => unwrap(props.editorView))
|
const editorView = () => untrack(() => unwrap(props.editorView))
|
||||||
|
|
||||||
const dispatchTransaction = (tr: Transaction) => {
|
const dispatchTransaction = (tr: Transaction) => {
|
||||||
if (!editorView()) return
|
if (!editorView()) return
|
||||||
|
|
||||||
const newState = editorView().state.apply(tr)
|
const newState = editorView().state.apply(tr)
|
||||||
|
|
||||||
editorView().updateState(newState)
|
editorView().updateState(newState)
|
||||||
|
|
||||||
if (!tr.docChanged) return
|
if (!tr.docChanged) return
|
||||||
|
|
||||||
props.onChange(newState)
|
props.onChange(newState)
|
||||||
}
|
}
|
||||||
|
|
||||||
const rerender = (state: [any, ProseMirrorExtension[]]) => {
|
|
||||||
const [prevText, prevExtensions] = state
|
|
||||||
const text = unwrap(props.text) as EditorState
|
|
||||||
const extensions: ProseMirrorExtension[] = unwrap(props.extensions)
|
|
||||||
|
|
||||||
if (!text || !extensions?.length) return [text, extensions]
|
|
||||||
|
|
||||||
if (!props.editorView) {
|
|
||||||
const { editorState, nodeViews } = createEditorState(text, extensions)
|
|
||||||
const view = new EditorView(editorRef, {
|
|
||||||
state: editorState,
|
|
||||||
nodeViews,
|
|
||||||
dispatchTransaction
|
|
||||||
})
|
|
||||||
|
|
||||||
view.focus()
|
|
||||||
props.onInit(editorState, view)
|
|
||||||
|
|
||||||
return [editorState, extensions]
|
|
||||||
}
|
|
||||||
|
|
||||||
if (extensions !== prevExtensions || (!(text instanceof EditorState) && text !== prevText)) {
|
|
||||||
const { editorState, nodeViews } = createEditorState(text, extensions, prevText)
|
|
||||||
|
|
||||||
if (!editorState) return
|
|
||||||
|
|
||||||
editorView().updateState(editorState)
|
|
||||||
editorView().setProps({ nodeViews, dispatchTransaction })
|
|
||||||
props.onReconfigure(editorState)
|
|
||||||
editorView().focus()
|
|
||||||
|
|
||||||
return [editorState, extensions]
|
|
||||||
}
|
|
||||||
|
|
||||||
return [text, extensions]
|
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line solid/reactivity
|
// eslint-disable-next-line solid/reactivity
|
||||||
createEffect(rerender as any, [props.text, props.extensions])
|
createEffect(
|
||||||
|
(state: [EditorState, ProseMirrorExtension[]]) => {
|
||||||
|
const [prevText, prevExtensions] = state
|
||||||
|
const text = unwrap(props.text) as EditorState
|
||||||
|
const extensions: ProseMirrorExtension[] = unwrap(props.extensions)
|
||||||
|
|
||||||
|
if (!text || !extensions?.length) return [text, extensions]
|
||||||
|
|
||||||
|
if (!props.editorView) {
|
||||||
|
const { editorState, nodeViews } = createEditorState(text, extensions)
|
||||||
|
const view = new EditorView(editorRef, {
|
||||||
|
state: editorState,
|
||||||
|
nodeViews,
|
||||||
|
dispatchTransaction
|
||||||
|
})
|
||||||
|
|
||||||
|
view.focus()
|
||||||
|
props.onInit(editorState, view)
|
||||||
|
|
||||||
|
return [editorState, extensions]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extensions !== prevExtensions || (!(text instanceof EditorState) && text !== prevText)) {
|
||||||
|
const { editorState, nodeViews } = createEditorState(text, extensions, prevText)
|
||||||
|
|
||||||
|
if (!editorState) return
|
||||||
|
|
||||||
|
editorView().updateState(editorState)
|
||||||
|
editorView().setProps({ nodeViews, dispatchTransaction })
|
||||||
|
props.onReconfigure(editorState)
|
||||||
|
editorView().focus()
|
||||||
|
|
||||||
|
return [editorState, extensions]
|
||||||
|
}
|
||||||
|
|
||||||
|
return [text, extensions]
|
||||||
|
},
|
||||||
|
[props.text, props.extensions]
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
|
Loading…
Reference in New Issue
Block a user