add more commands

This commit is contained in:
Philipp Kühn
2020-09-20 23:44:38 +02:00
parent fbdc156981
commit f6270c0e0c
6 changed files with 27 additions and 27 deletions

View File

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