From 2891fd02552dfd50ebc844580274841b33e198e1 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 24 Feb 2022 16:25:34 +0100 Subject: fix: do not try to send steps to a read only doc Cypress tests were failing because they triggered editor.focus on a read only doc. Focus seems to cause sendable steps - maybe because the current cursor changes. Prevent this on various levels: * Do not autofocus read only docs. * Do not send steps to read only docs. * Handle server response with 403 without content gracefully when sending steps. The last originated here: https://github.com/nextcloud/text/blob/master/lib/Service/ApiService.php#L158 So wither the session was not valid or the document read only. Not entirely sure what best to do in this situation. Logging to console.error for now. Signed-off-by: Max --- src/components/EditorWrapper.vue | 2 +- src/extensions/Collaboration.js | 2 +- src/services/PollingBackend.js | 18 +++++++++++++----- 3 files changed, 15 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/components/EditorWrapper.vue b/src/components/EditorWrapper.vue index 97e61ac20..512d6bc84 100644 --- a/src/components/EditorWrapper.vue +++ b/src/components/EditorWrapper.vue @@ -438,7 +438,7 @@ export default { .on('stateChange', (state) => { if (state.initialLoading && !this.initialLoading) { this.initialLoading = true - if (this.autofocus) { + if (this.autofocus && !this.readOnly) { this.tiptap.commands.focus() } this.$emit('ready') diff --git a/src/extensions/Collaboration.js b/src/extensions/Collaboration.js index 5d82e95e4..2e5ae78a4 100644 --- a/src/extensions/Collaboration.js +++ b/src/extensions/Collaboration.js @@ -24,7 +24,7 @@ const Collaboration = Extension.create({ this.getSendableSteps = debounce(state => { const sendable = sendableSteps(state) - if (sendable) { + if (sendable && this.editor.options.editable) { this.options.onSendable({ editor: this.editor, sendable: { diff --git a/src/services/PollingBackend.js b/src/services/PollingBackend.js index 2237a394c..798b20191 100644 --- a/src/services/PollingBackend.js +++ b/src/services/PollingBackend.js @@ -228,16 +228,24 @@ class PollingBackend { this.carefulRetryReset() this.lock = false this.fetchSteps() - }).catch((e) => { + }).catch(({ response, code }) => { console.error('failed to apply steps due to collission, retrying') this.lock = false - if (!e.response || e.code === 'ECONNABORTED') { + if (!response || code === 'ECONNABORTED') { this._authority.emit('error', ERROR_TYPE.CONNECTION_FAILED, {}) return - } else if (e.response.status === 403 && e.response.data.document.currentVersion === this._authority.document.currentVersion) { + } + const { status, data } = response + if (status === 403) { + if (!data.document) { + // either the session is invalid or the document is read only. + console.error('failed to write to document - not allowed') + } // Only emit conflict event if we have synced until the latest version - this._authority.emit('error', ERROR_TYPE.PUSH_FAILURE, {}) - OC.Notification.showTemporary('Changes could not be sent yet') + if (data.document?.currentVersion === this._authority.document.currentVersion) { + this._authority.emit('error', ERROR_TYPE.PUSH_FAILURE, {}) + OC.Notification.showTemporary('Changes could not be sent yet') + } } this.fetchSteps() -- cgit v1.2.3