|
| 1 | +/** |
| 2 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import axios from '@nextcloud/axios' |
| 7 | +import { getRequestToken } from '@nextcloud/auth' |
| 8 | +import type { Connection } from '../composables/useConnection.js' |
| 9 | +import { unref, type ShallowRef } from 'vue' |
| 10 | +import { generateUrl } from '@nextcloud/router' |
| 11 | +import type { Document } from '../services/SyncService.ts' |
| 12 | + |
| 13 | +interface SaveData { |
| 14 | + version: number |
| 15 | + autosaveContent: string |
| 16 | + documentState: string |
| 17 | + force: boolean |
| 18 | + manualSave: boolean |
| 19 | +} |
| 20 | + |
| 21 | +interface SaveResponse { |
| 22 | + data: Document |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Save document |
| 27 | + * @param connection the active connection |
| 28 | + * @param data data save |
| 29 | + */ |
| 30 | +export function save( |
| 31 | + connection: ShallowRef<Connection> | Connection, |
| 32 | + data: SaveData, |
| 33 | +): Promise<SaveResponse> { |
| 34 | + const con = unref(connection) |
| 35 | + const pub = con.shareToken ? '/public' : '' |
| 36 | + const url = generateUrl(`apps/text${pub}/session/${con.documentId}/save`) |
| 37 | + |
| 38 | + return axios.post(url, { |
| 39 | + documentId: con.documentId, |
| 40 | + sessionId: con.sessionId, |
| 41 | + sessionToken: con.sessionToken, |
| 42 | + token: con.shareToken, |
| 43 | + baseVersionEtag: con.baseVersionEtag, |
| 44 | + filePath: con.filePath, |
| 45 | + version: data.version, |
| 46 | + autosaveContent: data.autosaveContent, |
| 47 | + documentState: data.documentState, |
| 48 | + force: data.force, |
| 49 | + manualSave: data.manualSave, |
| 50 | + }) |
| 51 | +} |
| 52 | + |
| 53 | +/** |
| 54 | + * Save document via `navigator.sendBeacon()` |
| 55 | + * @param connection the active connection |
| 56 | + * @param data data to save |
| 57 | + */ |
| 58 | +export function saveViaSendBeacon( |
| 59 | + connection: Connection, |
| 60 | + data: Omit<SaveData, 'force' | 'manualSave'>, |
| 61 | +): boolean { |
| 62 | + const con = unref(connection) |
| 63 | + const pub = con.shareToken ? '/public' : '' |
| 64 | + const url = generateUrl(`apps/text${pub}/session/${con.documentId}/save`) |
| 65 | + |
| 66 | + const blob = new Blob( |
| 67 | + [ |
| 68 | + JSON.stringify({ |
| 69 | + documentId: con.documentId, |
| 70 | + sessionId: con.sessionId, |
| 71 | + sessionToken: con.sessionToken, |
| 72 | + token: con.shareToken, |
| 73 | + baseVersionEtag: con.baseVersionEtag, |
| 74 | + filePath: con.filePath, |
| 75 | + version: data.version, |
| 76 | + autosaveContent: data.autosaveContent, |
| 77 | + documentState: data.documentState, |
| 78 | + force: false, |
| 79 | + manualSave: true, |
| 80 | + requesttoken: getRequestToken() ?? '', |
| 81 | + }), |
| 82 | + ], |
| 83 | + { |
| 84 | + type: 'application/json', |
| 85 | + }, |
| 86 | + ) |
| 87 | + return navigator.sendBeacon(url, blob) |
| 88 | +} |
0 commit comments