From 594e65ab97691eaca005fa20aec89cc0abae95c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 7 Jun 2019 14:36:34 +0200 Subject: [PATCH] add support for autoFocus options --- packages/tiptap/src/Editor.js | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/tiptap/src/Editor.js b/packages/tiptap/src/Editor.js index ae43663ef..6a036db64 100644 --- a/packages/tiptap/src/Editor.js +++ b/packages/tiptap/src/Editor.js @@ -25,7 +25,7 @@ export default class Editor extends Emitter { this.defaultOptions = { editorProps: {}, editable: true, - autoFocus: false, + autoFocus: null, extensions: [], content: '', emptyDocument: { @@ -79,8 +79,8 @@ export default class Editor extends Emitter { this.commands = this.createCommands() this.setActiveNodesAndMarks() - if (this.options.autoFocus) { - this.focus() + if (this.options.autoFocus !== null) { + this.focus(this.options.autoFocus) } this.events.forEach(name => { @@ -337,20 +337,22 @@ export default class Editor extends Emitter { } focus(position = null) { - if (position !== null) { - let pos = position - - 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) + if (position === null || position === false) { + return } + 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(() => { this.view.focus() }, 10)