Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable26] fix: Remember cursor position when autofocus on load #4543

Merged
merged 3 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion cypress/e2e/sync.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Sync', () => {
}).as('sync')
cy.openTestFile()
cy.getContent().find('h2').should('contain', 'Hello world')
cy.getContent().type('* Saving the doc saves the doc state{enter}')
cy.getContent().type('{moveToEnd}* Saving the doc saves the doc state{enter}')
})

it('saves the actual file and document state', () => {
Expand Down
4 changes: 2 additions & 2 deletions js/editor.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions js/editor.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@
*
*/

/*
* @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/>.
*/

/*
* @copyright Copyright (c) 2023 Max <max@nextcloud.com>
*
Expand Down
2 changes: 1 addition & 1 deletion js/editor.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/files-modal.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/files-modal.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions js/text-editors.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-editors.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-files.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-files.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-public.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-public.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-text.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-text.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/text-viewer.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/text-viewer.js.map

Large diffs are not rendered by default.

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