add support for autoFocus options

This commit is contained in:
Philipp Kühn 2019-06-07 14:36:34 +02:00
parent d2e454dddd
commit 594e65ab97

View File

@ -25,7 +25,7 @@ export default class Editor extends Emitter {
this.defaultOptions = { this.defaultOptions = {
editorProps: {}, editorProps: {},
editable: true, editable: true,
autoFocus: false, autoFocus: null,
extensions: [], extensions: [],
content: '', content: '',
emptyDocument: { emptyDocument: {
@ -79,8 +79,8 @@ export default class Editor extends Emitter {
this.commands = this.createCommands() this.commands = this.createCommands()
this.setActiveNodesAndMarks() this.setActiveNodesAndMarks()
if (this.options.autoFocus) { if (this.options.autoFocus !== null) {
this.focus() this.focus(this.options.autoFocus)
} }
this.events.forEach(name => { this.events.forEach(name => {
@ -337,20 +337,22 @@ export default class Editor extends Emitter {
} }
focus(position = null) { focus(position = null) {
if (position !== null) { if (position === null || position === false) {
let pos = position return
if (position === 'start') {
pos = 0
} else if (position === 'end') {
pos = this.view.state.doc.nodeSize - 2
}
const selection = TextSelection.near(this.view.state.doc.resolve(pos))
const transaction = this.view.state.tr.setSelection(selection)
this.view.dispatch(transaction)
} }
let pos = position
if (position === 'start' || position === true) {
pos = 0
} else if (position === 'end') {
pos = this.view.state.doc.nodeSize - 2
}
const selection = TextSelection.near(this.view.state.doc.resolve(pos))
const transaction = this.view.state.tr.setSelection(selection)
this.view.dispatch(transaction)
setTimeout(() => { setTimeout(() => {
this.view.focus() this.view.focus()
}, 10) }, 10)