Skip to content

Commit

Permalink
fix: Remember cursor position when autofocus on load
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Jul 21, 2023
1 parent 937e01c commit 950c671
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -493,6 +494,9 @@ export default {
})
},
extensions: [
Autofocus.configure({
fileId: this.fileId,
}),
Collaboration.configure({
document: this.$ydoc,
}),
Expand Down Expand Up @@ -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')
Expand Down
61 changes: 61 additions & 0 deletions src/extensions/Autofocus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* @copyright Copyright (c) 2023 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @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 <http://www.gnu.org/licenses/>.
*/

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')
},
}
},
})

0 comments on commit 950c671

Please sign in to comment.