add setContent

This commit is contained in:
Philipp Kühn
2020-03-04 22:47:50 +01:00
parent 36198ed2ab
commit 560d453356
2 changed files with 19 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
import {EditorState, Plugin} from "prosemirror-state"
import {EditorState, TextSelection, Plugin} from "prosemirror-state"
import {EditorView} from "prosemirror-view"
import {Schema, DOMParser, DOMSerializer} from "prosemirror-model"
// @ts-ignore
@@ -77,7 +77,7 @@ export class Editor {
return this
}
private createDocument(content: EditorContent): any {
private createDocument(content: EditorContent, parseOptions: any = {}): any {
// if (content === null) {
// return this.schema.nodeFromJSON(this.options.emptyDocument)
// }
@@ -94,19 +94,33 @@ export class Editor {
if (typeof content === 'string') {
return DOMParser
.fromSchema(this.schema)
.parse(elementFromString(content))
.parse(elementFromString(content), parseOptions)
}
return false
}
public setContent(content: EditorContent = '', emitUpdate: Boolean = false, parseOptions: any = {}) {
const { doc, tr } = this.state
const document = this.createDocument(content, parseOptions)
const selection = TextSelection.create(doc, 0, doc.content.size)
const transaction = tr
.setSelection(selection)
.replaceSelectionWith(document, false)
.setMeta('preventUpdate', !emitUpdate)
this.view.dispatch(transaction)
return this
}
private dispatchTransaction(transaction: any): void {
const state = this.state.apply(transaction)
this.view.updateState(state)
const { from, to } = this.state.selection
this.selection = { from, to }
// this.setActiveNodesAndMarks()
// this.emit('transaction', {

View File

@@ -27,6 +27,7 @@ export default {
element: this.$refs.editor,
content: '<p>test <strong>strong</strong></p>',
})
// .setContent('<p>hey</p>')
// .registerCommand('lol', (next) => {
// console.log('lol')
// next()