Skip to content
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
13 changes: 12 additions & 1 deletion src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@ export default {
this.updateEditorWidth(newWidth)
},
},
dirty(val) {
if (val) {
window.addEventListener('beforeunload', this.saveBeforeUnload)
} else {
window.removeEventListener('beforeunload', this.saveBeforeUnload)
}
},
},
mounted() {
if (this.active && (this.hasDocumentParameters)) {
Expand Down Expand Up @@ -669,7 +676,7 @@ export default {
this.emit('ready')
}
if (Object.prototype.hasOwnProperty.call(state, 'dirty')) {
// ignore initial loading and other automated changes
// ignore initial loading and other automated changes before first user change
if (this.$editor
&& (this.$editor.can().undo() || this.$editor.can().redo())
) {
Expand Down Expand Up @@ -884,6 +891,10 @@ export default {
updateEditorWidth(newWidth) {
document.documentElement.style.setProperty('--text-editor-max-width', newWidth)
},

saveBeforeUnload() {
this.$syncService?.saveViaSendBeacon()
},
},
}
</script>
Expand Down
7 changes: 2 additions & 5 deletions src/components/Editor/Status.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,19 @@ export default {
return this.dirtyStateIndicator ? t('text', 'Saving …') : t('text', 'Saved')
},
dirtyStateIndicator() {
return this.dirty || this.hasUnsavedChanges
return this.dirty
},
lastSavedStatusTooltip() {
let message = t('text', 'Last saved {lastSave}', { lastSave: this.lastSavedString })
if (this.hasSyncCollission) {
message = t('text', 'The document has been changed outside of the editor. The changes cannot be applied.')
}
if (this.dirty || this.hasUnsavedChanges) {
if (this.dirty) {
message += ' - ' + t('text', 'Unsaved changes')
}
return message
},

hasUnsavedChanges() {
return this.document && this.document.lastSavedVersion < this.document.currentVersion
},
hasSyncCollission() {
return this.syncError && this.syncError.type === ERROR_TYPE.SAVE_COLLISSION
},
Expand Down
30 changes: 22 additions & 8 deletions src/services/SessionApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import axios from '@nextcloud/axios'
import { getRequestToken } from '@nextcloud/auth'
import { generateUrl } from '@nextcloud/router'

export class ConnectionClosedError extends Error {
Expand Down Expand Up @@ -106,17 +107,30 @@
})
}

save({ version, autosaveContent, documentState, force, manualSave }) {
return this.#post(this.#url(`session/${this.#document.id}/save`), {
save(data) {
const url = this.#url(`session/${this.#document.id}/save`)
const postData = {

Check warning on line 112 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L110-L112

Added lines #L110 - L112 were not covered by tests
...this.#defaultParams,
filePath: this.#options.filePath,
baseVersionEtag: this.#document.baseVersionEtag,
version,
autosaveContent,
documentState,
force,
manualSave,
})
...data,
}

Check warning on line 117 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L116-L117

Added lines #L116 - L117 were not covered by tests

return this.#post(url, postData)
}

Check warning on line 120 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L119-L120

Added lines #L119 - L120 were not covered by tests

saveViaSendBeacon(data) {
const url = this.#url(`session/${this.#document.id}/save`)
const postData = {
...this.#defaultParams,
filePath: this.#options.filePath,
baseVersionEtag: this.#document.baseVersionEtag,
...data,
requestToken: getRequestToken() ?? '',
}

Check warning on line 130 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L122-L130

Added lines #L122 - L130 were not covered by tests

const blob = new Blob([JSON.stringify(postData)], { type: 'application/json' })
return navigator.sendBeacon(url, blob)

Check warning on line 133 in src/services/SessionApi.js

View check run for this annotation

Codecov / codecov/patch

src/services/SessionApi.js#L132-L133

Added lines #L132 - L133 were not covered by tests
}

push({ steps, version, awareness }) {
Expand Down
12 changes: 12 additions & 0 deletions src/services/SyncService.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
this.emit('error', { type: ERROR_TYPE.PUSH_FORBIDDEN, data: {} })
}
// TODO: does response.data ever have a document? maybe for errors?
// TODO: `currentVersion` is always 0 nowadays. Check if this is still needed.

Check warning on line 221 in src/services/SyncService.js

View check run for this annotation

Codecov / codecov/patch

src/services/SyncService.js#L221

Added line #L221 was not covered by tests
// Only emit conflict event if we have synced until the latest version
if (response.data.document?.currentVersion === this.version) {
this.emit('error', { type: ERROR_TYPE.PUSH_FAILURE, data: {} })
Expand Down Expand Up @@ -299,6 +300,17 @@
}
}

saveViaSendBeacon() {
this.#connection.saveViaSendBeacon({
version: this.version,
autosaveContent: this._getContent(),
documentState: this.getDocumentState(),
force: false,
manualSave: true,
})
logger.debug('[SyncService] saved using sendBeacon')
}

Check warning on line 312 in src/services/SyncService.js

View check run for this annotation

Codecov / codecov/patch

src/services/SyncService.js#L303-L312

Added lines #L303 - L312 were not covered by tests

forceSave() {
return this.save({ force: true })
}
Expand Down
Loading