tiptap/packages/core/src/commands/setContent.ts
2020-09-20 23:44:38 +02:00

32 lines
775 B
TypeScript

import { Command } from '../Editor'
import { TextSelection } from 'prosemirror-state'
type SetContentCommand = (
content: string,
emitUpdate?: Boolean,
parseOptions?: any,
) => Command
declare module '../Editor' {
interface Editor {
setContent: SetContentCommand,
}
}
export const setContent: SetContentCommand = (content = null, emitUpdate = false, parseOptions = {}) => ({ tr, editor }) => {
if (content === null) {
return false
}
const { createDocument } = editor
const { doc } = tr
const document = createDocument(content, parseOptions)
const selection = TextSelection.create(doc, 0, doc.content.size)
tr.setSelection(selection)
.replaceSelectionWith(document, false)
.setMeta('preventUpdate', !emitUpdate)
return true
}