Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: Remember cursor position when autofocus on load
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Jul 21, 2023
commit 950c6718e24c7f0ce6afcbe707a7dca87479eef2
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 <[email protected]>
*
* @author Julius Härtl <[email protected]>
*
* @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')
},
}
},
})