add setContent command
This commit is contained in:
@@ -53,6 +53,7 @@ export class Editor extends EventEmitter {
|
|||||||
this.registerCommand('focus', require('./commands/focus').default)
|
this.registerCommand('focus', require('./commands/focus').default)
|
||||||
this.registerCommand('insertText', require('./commands/insertText').default)
|
this.registerCommand('insertText', require('./commands/insertText').default)
|
||||||
this.registerCommand('insertHTML', require('./commands/insertHTML').default)
|
this.registerCommand('insertHTML', require('./commands/insertHTML').default)
|
||||||
|
this.registerCommand('setContent', require('./commands/setContent').default)
|
||||||
|
|
||||||
if (this.options.injectCSS) {
|
if (this.options.injectCSS) {
|
||||||
injectCSS(require('./style.css'))
|
injectCSS(require('./style.css'))
|
||||||
@@ -133,7 +134,7 @@ export class Editor extends EventEmitter {
|
|||||||
return this.proxy
|
return this.proxy
|
||||||
}
|
}
|
||||||
|
|
||||||
private createDocument(content: EditorContent, parseOptions: any = {}): any {
|
public createDocument = (content: EditorContent, parseOptions: any = {}): any => {
|
||||||
if (content && typeof content === 'object') {
|
if (content && typeof content === 'object') {
|
||||||
try {
|
try {
|
||||||
return this.schema.nodeFromJSON(content)
|
return this.schema.nodeFromJSON(content)
|
||||||
@@ -176,20 +177,6 @@ export class Editor extends EventEmitter {
|
|||||||
// TODO
|
// TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// public setParentComponent(component = null) {
|
// public setParentComponent(component = null) {
|
||||||
// if (!component) {
|
// if (!component) {
|
||||||
// return
|
// return
|
||||||
|
|||||||
22
packages/tiptap-core/src/commands/setContent.ts
Normal file
22
packages/tiptap-core/src/commands/setContent.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { Editor } from '../Editor'
|
||||||
|
import { TextSelection } from 'prosemirror-state'
|
||||||
|
|
||||||
|
declare module '../Editor' {
|
||||||
|
interface Editor {
|
||||||
|
setContent(content: string, emitUpdate: Boolean, parseOptions: any): Editor,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function setContent(next: Function, editor: Editor, content: string, emitUpdate: Boolean = true, parseOptions: any = {}): void {
|
||||||
|
const { view, state, createDocument } = editor
|
||||||
|
const { doc, tr } = state
|
||||||
|
const document = createDocument(content, parseOptions)
|
||||||
|
const selection = TextSelection.create(doc, 0, doc.content.size)
|
||||||
|
const transaction = tr
|
||||||
|
.setSelection(selection)
|
||||||
|
.replaceSelectionWith(document, false)
|
||||||
|
.setMeta('preventUpdate', !emitUpdate)
|
||||||
|
|
||||||
|
view.dispatch(transaction)
|
||||||
|
next()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user