From 05650b66d27632063031172b2d4c91ac5f5206c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Ku=CC=88hn?= Date: Wed, 17 Apr 2019 23:29:45 +0200 Subject: [PATCH] add position option to focus method --- packages/tiptap/src/Editor.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/packages/tiptap/src/Editor.js b/packages/tiptap/src/Editor.js index 3d8f47b5..a2acb672 100644 --- a/packages/tiptap/src/Editor.js +++ b/packages/tiptap/src/Editor.js @@ -1,4 +1,9 @@ -import { EditorState, Plugin, PluginKey } from 'prosemirror-state' +import { + EditorState, + Plugin, + PluginKey, + TextSelection, +} from 'prosemirror-state' import { EditorView } from 'prosemirror-view' import { Schema, DOMParser, DOMSerializer } from 'prosemirror-model' import { dropCursor } from 'prosemirror-dropcursor' @@ -299,7 +304,21 @@ export default class Editor { }) } - focus() { + 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) + } + this.view.focus() }