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 {EditorView} from "prosemirror-view"
import {Schema, DOMParser, DOMSerializer} from "prosemirror-model" import {Schema, DOMParser, DOMSerializer} from "prosemirror-model"
// @ts-ignore // @ts-ignore
@@ -77,7 +77,7 @@ export class Editor {
return this return this
} }
private createDocument(content: EditorContent): any { private createDocument(content: EditorContent, parseOptions: any = {}): any {
// if (content === null) { // if (content === null) {
// return this.schema.nodeFromJSON(this.options.emptyDocument) // return this.schema.nodeFromJSON(this.options.emptyDocument)
// } // }
@@ -94,19 +94,33 @@ export class Editor {
if (typeof content === 'string') { if (typeof content === 'string') {
return DOMParser return DOMParser
.fromSchema(this.schema) .fromSchema(this.schema)
.parse(elementFromString(content)) .parse(elementFromString(content), parseOptions)
} }
return false 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 { private dispatchTransaction(transaction: any): void {
const state = this.state.apply(transaction) const state = this.state.apply(transaction)
this.view.updateState(state) this.view.updateState(state)
const { from, to } = this.state.selection const { from, to } = this.state.selection
this.selection = { from, to } this.selection = { from, to }
// this.setActiveNodesAndMarks() // this.setActiveNodesAndMarks()
// this.emit('transaction', { // this.emit('transaction', {

View File

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