tiptap/packages/core/src/commands/setContent.ts

21 lines
620 B
TypeScript
Raw Normal View History

2020-11-05 05:38:52 +08:00
import { TextSelection } from 'prosemirror-state'
2020-11-17 04:42:35 +08:00
import { Command } from '../types'
2020-11-05 05:38:52 +08:00
2020-11-18 23:43:27 +08:00
/**
* Replace the whole document with new content.
*/
export const setContent = (content: string, emitUpdate: Boolean = false, parseOptions = {}): Command => ({ tr, editor, dispatch }) => {
2020-11-05 05:38:52 +08:00
const { createDocument } = editor
const { doc } = tr
const document = createDocument(content, parseOptions)
const selection = TextSelection.create(doc, 0, doc.content.size)
if (dispatch) {
tr.setSelection(selection)
.replaceSelectionWith(document, false)
.setMeta('preventUpdate', !emitUpdate)
}
return true
}