add state getter
This commit is contained in:
@@ -71,7 +71,6 @@ export default class Editor extends Emitter {
|
|||||||
this.keymaps = this.createKeymaps()
|
this.keymaps = this.createKeymaps()
|
||||||
this.inputRules = this.createInputRules()
|
this.inputRules = this.createInputRules()
|
||||||
this.pasteRules = this.createPasteRules()
|
this.pasteRules = this.createPasteRules()
|
||||||
this.state = this.createState()
|
|
||||||
this.view = this.createView()
|
this.view = this.createView()
|
||||||
this.commands = this.createCommands()
|
this.commands = this.createCommands()
|
||||||
this.setActiveNodesAndMarks()
|
this.setActiveNodesAndMarks()
|
||||||
@@ -118,6 +117,10 @@ export default class Editor extends Emitter {
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get state() {
|
||||||
|
return this.view ? this.view.state : null
|
||||||
|
}
|
||||||
|
|
||||||
createExtensions() {
|
createExtensions() {
|
||||||
return new ExtensionManager([
|
return new ExtensionManager([
|
||||||
...this.builtInExtensions,
|
...this.builtInExtensions,
|
||||||
@@ -233,7 +236,7 @@ export default class Editor extends Emitter {
|
|||||||
|
|
||||||
createView() {
|
createView() {
|
||||||
const view = new EditorView(this.element, {
|
const view = new EditorView(this.element, {
|
||||||
state: this.state,
|
state: this.createState(),
|
||||||
handlePaste: (...args) => { this.emit('paste', ...args) },
|
handlePaste: (...args) => { this.emit('paste', ...args) },
|
||||||
handleDrop: (...args) => { this.emit('drop', ...args) },
|
handleDrop: (...args) => { this.emit('drop', ...args) },
|
||||||
dispatchTransaction: this.dispatchTransaction.bind(this),
|
dispatchTransaction: this.dispatchTransaction.bind(this),
|
||||||
@@ -300,8 +303,8 @@ export default class Editor extends Emitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dispatchTransaction(transaction) {
|
dispatchTransaction(transaction) {
|
||||||
this.state = this.state.apply(transaction)
|
const newState = this.state.apply(transaction)
|
||||||
this.view.updateState(this.state)
|
this.view.updateState(newState)
|
||||||
this.setActiveNodesAndMarks()
|
this.setActiveNodesAndMarks()
|
||||||
|
|
||||||
if (!transaction.docChanged) {
|
if (!transaction.docChanged) {
|
||||||
@@ -365,13 +368,13 @@ export default class Editor extends Emitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setContent(content = {}, emitUpdate = false, parseOptions) {
|
setContent(content = {}, emitUpdate = false, parseOptions) {
|
||||||
this.state = EditorState.create({
|
const newState = EditorState.create({
|
||||||
schema: this.state.schema,
|
schema: this.state.schema,
|
||||||
doc: this.createDocument(content, parseOptions),
|
doc: this.createDocument(content, parseOptions),
|
||||||
plugins: this.state.plugins,
|
plugins: this.state.plugins,
|
||||||
})
|
})
|
||||||
|
|
||||||
this.view.updateState(this.state)
|
this.view.updateState(newState)
|
||||||
|
|
||||||
if (emitUpdate) {
|
if (emitUpdate) {
|
||||||
this.emitUpdate()
|
this.emitUpdate()
|
||||||
@@ -426,10 +429,10 @@ export default class Editor extends Emitter {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.state = this.state.reconfigure({
|
const newState = this.state.reconfigure({
|
||||||
plugins: this.state.plugins.concat([plugin]),
|
plugins: this.state.plugins.concat([plugin]),
|
||||||
})
|
})
|
||||||
this.view.updateState(this.state)
|
this.view.updateState(newState)
|
||||||
}
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
|
|||||||
Reference in New Issue
Block a user