From a4a4ff31e537f885f630fd0ebab09406d19715f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Fri, 21 Aug 2020 17:44:02 +0200 Subject: [PATCH] add autofocus option --- packages/core/src/Editor.ts | 4 ++++ packages/core/src/commands/focus.ts | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/core/src/Editor.ts b/packages/core/src/Editor.ts index 8e469281..3bbc7260 100644 --- a/packages/core/src/Editor.ts +++ b/packages/core/src/Editor.ts @@ -54,6 +54,7 @@ interface EditorOptions { content: EditorContent extensions: (Extension | Node | Mark)[] injectCSS: Boolean, + autoFocus: 'start' | 'end' | number | boolean | null } @magicMethods @@ -73,6 +74,7 @@ export class Editor extends EventEmitter { content: '', injectCSS: true, extensions: [], + autoFocus: false, } public isFocused = false public isEditable = true @@ -104,6 +106,8 @@ export class Editor extends EventEmitter { if (this.options.injectCSS) { require('./style.css') } + + this.proxy.focus(this.options.autoFocus) } private __get(name: string) { diff --git a/packages/core/src/commands/focus.ts b/packages/core/src/commands/focus.ts index 357e9655..818dc3fd 100644 --- a/packages/core/src/commands/focus.ts +++ b/packages/core/src/commands/focus.ts @@ -16,14 +16,14 @@ interface ResolvedSelection { to: number, } -type Position = 'start' | 'end' | number | null +type Position = 'start' | 'end' | number | boolean | null function resolveSelection(editor: Editor, position: Position = null): ResolvedSelection { if (position === null) { return editor.selection } - if (position === 'start') { + if (position === 'start' || position === true) { return { from: 0, to: 0, @@ -48,7 +48,7 @@ function resolveSelection(editor: Editor, position: Position = null): ResolvedSe export default (next: Function, editor: Editor) => async (position = null) => { const { view, state } = editor - if ((view.hasFocus() && position === null)) { + if ((view.hasFocus() && position === null) || position === false) { next() return }