add autofocus option

This commit is contained in:
Philipp Kühn
2020-08-21 17:44:02 +02:00
parent 71c3927b28
commit a4a4ff31e5
2 changed files with 7 additions and 3 deletions

View File

@@ -54,6 +54,7 @@ interface EditorOptions {
content: EditorContent content: EditorContent
extensions: (Extension | Node | Mark)[] extensions: (Extension | Node | Mark)[]
injectCSS: Boolean, injectCSS: Boolean,
autoFocus: 'start' | 'end' | number | boolean | null
} }
@magicMethods @magicMethods
@@ -73,6 +74,7 @@ export class Editor extends EventEmitter {
content: '', content: '',
injectCSS: true, injectCSS: true,
extensions: [], extensions: [],
autoFocus: false,
} }
public isFocused = false public isFocused = false
public isEditable = true public isEditable = true
@@ -104,6 +106,8 @@ export class Editor extends EventEmitter {
if (this.options.injectCSS) { if (this.options.injectCSS) {
require('./style.css') require('./style.css')
} }
this.proxy.focus(this.options.autoFocus)
} }
private __get(name: string) { private __get(name: string) {

View File

@@ -16,14 +16,14 @@ interface ResolvedSelection {
to: number, to: number,
} }
type Position = 'start' | 'end' | number | null type Position = 'start' | 'end' | number | boolean | null
function resolveSelection(editor: Editor, position: Position = null): ResolvedSelection { function resolveSelection(editor: Editor, position: Position = null): ResolvedSelection {
if (position === null) { if (position === null) {
return editor.selection return editor.selection
} }
if (position === 'start') { if (position === 'start' || position === true) {
return { return {
from: 0, from: 0,
to: 0, to: 0,
@@ -48,7 +48,7 @@ function resolveSelection(editor: Editor, position: Position = null): ResolvedSe
export default (next: Function, editor: Editor) => async (position = null) => { export default (next: Function, editor: Editor) => async (position = null) => {
const { view, state } = editor const { view, state } = editor
if ((view.hasFocus() && position === null)) { if ((view.hasFocus() && position === null) || position === false) {
next() next()
return return
} }