From 950c6718e24c7f0ce6afcbe707a7dca87479eef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 17 Jul 2023 13:19:56 +0200 Subject: [PATCH] fix: Remember cursor position when autofocus on load MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- src/components/Editor.vue | 6 +++- src/extensions/Autofocus.js | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/extensions/Autofocus.js diff --git a/src/components/Editor.vue b/src/components/Editor.vue index 8183618c14..96c4f00caa 100644 --- a/src/components/Editor.vue +++ b/src/components/Editor.vue @@ -83,6 +83,7 @@ import { loadState } from '@nextcloud/initial-state' import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus' import { Collaboration } from '@tiptap/extension-collaboration' import { CollaborationCursor } from '@tiptap/extension-collaboration-cursor' +import Autofocus from '../extensions/Autofocus.js' import { Doc } from 'yjs' import { @@ -493,6 +494,9 @@ export default { }) }, extensions: [ + Autofocus.configure({ + fileId: this.fileId, + }), Collaboration.configure({ document: this.$ydoc, }), @@ -587,7 +591,7 @@ export default { this.contentLoaded = true if (this.autofocus && !this.readOnly) { this.$nextTick(() => { - this.$editor.commands.focus() + this.$editor.commands.autofocus() }) } this.emit('ready') diff --git a/src/extensions/Autofocus.js b/src/extensions/Autofocus.js new file mode 100644 index 0000000000..6dd7bdec81 --- /dev/null +++ b/src/extensions/Autofocus.js @@ -0,0 +1,61 @@ +/* + * @copyright Copyright (c) 2023 Julius Härtl + * + * @author Julius Härtl + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import { Extension } from '@tiptap/core' + +export default Extension.create({ + addOptions() { + return { + fileId: null, + } + }, + addStorage() { + return { + started: false, + } + }, + onCreate() { + if (this.options.fileId === null) { + throw new Error('fileId needs to be provided') + } + this.storage.started = true + }, + onSelectionUpdate({ editor }) { + if (!this.storage.started) { + return + } + + const pos = editor.state.selection.$anchor.pos + sessionStorage.setItem('text-lastPos-' + this.options.fileId, pos) + }, + addCommands() { + return { + autofocus: () => ({ commands, editor }) => { + const pos = sessionStorage.getItem('text-lastPos-' + this.options.fileId) + if (pos) { + return commands.focus(pos) + } + + return commands.focus('start') + }, + } + }, +})