2020-03-30 04:24:51 +08:00
|
|
|
import { Editor } from '../Editor'
|
|
|
|
import { TextSelection } from 'prosemirror-state'
|
|
|
|
|
2020-08-17 22:38:13 +08:00
|
|
|
type SetContentCommand = (
|
2020-04-22 05:22:27 +08:00
|
|
|
content: string,
|
|
|
|
emitUpdate?: Boolean,
|
|
|
|
parseOptions?: any,
|
2020-08-17 22:38:13 +08:00
|
|
|
) => Editor
|
2020-04-22 05:22:27 +08:00
|
|
|
|
2020-03-30 04:24:51 +08:00
|
|
|
declare module '../Editor' {
|
|
|
|
interface Editor {
|
2020-08-17 22:38:13 +08:00
|
|
|
setContent: SetContentCommand,
|
2020-03-30 04:24:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:38:13 +08:00
|
|
|
export default (next: Function, editor: Editor) => (content: string, emitUpdate: Boolean = false, parseOptions = {}) => {
|
2020-03-30 05:06:37 +08:00
|
|
|
if (content === null) {
|
|
|
|
next()
|
|
|
|
return
|
|
|
|
}
|
2020-03-30 05:24:37 +08:00
|
|
|
|
2020-03-30 04:24:51 +08:00
|
|
|
const { view, state, createDocument } = editor
|
|
|
|
const { doc, tr } = state
|
|
|
|
const document = createDocument(content, parseOptions)
|
|
|
|
const selection = TextSelection.create(doc, 0, doc.content.size)
|
|
|
|
const transaction = tr
|
|
|
|
.setSelection(selection)
|
|
|
|
.replaceSelectionWith(document, false)
|
|
|
|
.setMeta('preventUpdate', !emitUpdate)
|
|
|
|
|
|
|
|
view.dispatch(transaction)
|
|
|
|
next()
|
|
|
|
}
|