refactoring
This commit is contained in:
@@ -68,6 +68,10 @@ export class Editor extends EventEmitter {
|
|||||||
return (...args: any) => command(...args)
|
return (...args: any) => command(...args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public get state() {
|
||||||
|
return this.view.state
|
||||||
|
}
|
||||||
|
|
||||||
public registerCommand(name: string, callback: Command): Editor {
|
public registerCommand(name: string, callback: Command): Editor {
|
||||||
if (this.commands[name]) {
|
if (this.commands[name]) {
|
||||||
throw new Error(`tiptap: command '${name}' is already defined.`)
|
throw new Error(`tiptap: command '${name}' is already defined.`)
|
||||||
@@ -125,12 +129,7 @@ export class Editor extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private createDocument(content: EditorContent, parseOptions: any = {}): any {
|
private createDocument(content: EditorContent, parseOptions: any = {}): any {
|
||||||
if (content === null) {
|
if (content && typeof content === 'object') {
|
||||||
// this.options.emptyDocument
|
|
||||||
return this.createDocument('')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof content === 'object') {
|
|
||||||
try {
|
try {
|
||||||
return this.schema.nodeFromJSON(content)
|
return this.schema.nodeFromJSON(content)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -145,15 +144,18 @@ export class Editor extends EventEmitter {
|
|||||||
.parse(elementFromString(content), parseOptions)
|
.parse(elementFromString(content), parseOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return this.createDocument('')
|
||||||
|
}
|
||||||
|
|
||||||
|
private storeSelection() {
|
||||||
|
const { from, to } = this.state.selection
|
||||||
|
this.selection = { from, to }
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
|
this.storeSelection()
|
||||||
const { from, to } = this.state.selection
|
|
||||||
this.selection = { from, to }
|
|
||||||
|
|
||||||
// this.setActiveNodesAndMarks()
|
// this.setActiveNodesAndMarks()
|
||||||
|
|
||||||
@@ -171,10 +173,6 @@ export class Editor extends EventEmitter {
|
|||||||
// this.emitUpdate(transaction)
|
// this.emitUpdate(transaction)
|
||||||
}
|
}
|
||||||
|
|
||||||
public get state() {
|
|
||||||
return this.view.state
|
|
||||||
}
|
|
||||||
|
|
||||||
public setContent(content: EditorContent = '', emitUpdate: Boolean = false, parseOptions: any = {}) {
|
public setContent(content: EditorContent = '', emitUpdate: Boolean = false, parseOptions: any = {}) {
|
||||||
const { doc, tr } = this.state
|
const { doc, tr } = this.state
|
||||||
const document = this.createDocument(content, parseOptions)
|
const document = this.createDocument(content, parseOptions)
|
||||||
|
|||||||
Reference in New Issue
Block a user