diff --git a/packages/tiptap-core/src/Editor.ts b/packages/tiptap-core/src/Editor.ts index f629e1a2..b04e04b8 100644 --- a/packages/tiptap-core/src/Editor.ts +++ b/packages/tiptap-core/src/Editor.ts @@ -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', { diff --git a/src/templates/Post.vue b/src/templates/Post.vue index 2efbfab0..349938a9 100644 --- a/src/templates/Post.vue +++ b/src/templates/Post.vue @@ -27,6 +27,7 @@ export default { element: this.$refs.editor, content: '
test strong
', }) + // .setContent('hey
') // .registerCommand('lol', (next) => { // console.log('lol') // next()