Skip to content

Commit 9256ad7

Browse files
fix: Remember cursor position when autofocus on load
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 304369e commit 9256ad7

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

src/components/Editor.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import { loadState } from '@nextcloud/initial-state'
8383
import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'
8484
import { Collaboration } from '@tiptap/extension-collaboration'
8585
import { CollaborationCursor } from '@tiptap/extension-collaboration-cursor'
86+
import Autofocus from '../extensions/Autofocus.js'
8687
import { Doc } from 'yjs'
8788
8889
import {
@@ -493,6 +494,9 @@ export default {
493494
})
494495
},
495496
extensions: [
497+
Autofocus.configure({
498+
fileId: this.fileId,
499+
}),
496500
Collaboration.configure({
497501
document: this.$ydoc,
498502
}),
@@ -587,7 +591,7 @@ export default {
587591
this.contentLoaded = true
588592
if (this.autofocus && !this.readOnly) {
589593
this.$nextTick(() => {
590-
this.$editor.commands.focus()
594+
this.$editor.commands.autofocus()
591595
})
592596
}
593597
this.emit('ready')

src/extensions/Autofocus.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
3+
*
4+
* @author Julius Härtl <jus@bitgrid.net>
5+
*
6+
* @license GNU AGPL version 3 or any later version
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
import { Extension } from '@tiptap/core'
23+
24+
export default Extension.create({
25+
addOptions() {
26+
return {
27+
fileId: null,
28+
}
29+
},
30+
addStorage() {
31+
return {
32+
started: false,
33+
}
34+
},
35+
onCreate() {
36+
if (this.options.fileId === null) {
37+
throw new Error('fileId needs to be provided')
38+
}
39+
this.storage.started = true
40+
},
41+
onSelectionUpdate({ editor }) {
42+
if (!this.storage.started) {
43+
return
44+
}
45+
46+
const pos = editor.state.selection.$anchor.pos
47+
sessionStorage.setItem('text-lastPos-' + this.options.fileId, pos)
48+
},
49+
addCommands() {
50+
return {
51+
autofocus: () => ({ commands, editor }) => {
52+
const pos = sessionStorage.getItem('text-lastPos-' + this.options.fileId)
53+
if (pos) {
54+
return commands.focus(pos)
55+
}
56+
57+
return commands.focus('start')
58+
},
59+
}
60+
},
61+
})

0 commit comments

Comments
 (0)