mirror of
https://github.com/ueberdosis/tiptap.git
synced 2025-01-01 06:37:51 +08:00
29 lines
845 B
TypeScript
29 lines
845 B
TypeScript
import { TextSelection } from 'prosemirror-state'
|
|
import { AnyObject, Command, RawCommands } from '../types'
|
|
|
|
declare module '@tiptap/core' {
|
|
interface Commands {
|
|
setContent: {
|
|
/**
|
|
* Replace the whole document with new content.
|
|
*/
|
|
setContent: (content: string, emitUpdate?: Boolean, parseOptions?: AnyObject) => Command,
|
|
}
|
|
}
|
|
}
|
|
|
|
export const setContent: RawCommands['setContent'] = (content, emitUpdate = false, parseOptions = {}) => ({ tr, editor, dispatch }) => {
|
|
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
|
|
}
|