From 2e07477398a2b0e2a1cc9302c421cbf0d5a33dc1 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Sun, 2 Oct 2022 15:52:39 +0200 Subject: [PATCH] client: discard drafts that are just the default state Changelog: Changed --- packages/client/src/components/post-form.vue | 65 +++++++++++++++----- 1 file changed, 50 insertions(+), 15 deletions(-) diff --git a/packages/client/src/components/post-form.vue b/packages/client/src/components/post-form.vue index 3340f22ae..8821641f6 100644 --- a/packages/client/src/components/post-form.vue +++ b/packages/client/src/components/post-form.vue @@ -121,9 +121,9 @@ const cwInputEl = $ref(null); const hashtagsInputEl = $ref(null); const visibilityButton = $ref(null); -let posting = $ref(false); -let text = $ref(props.initialText ?? ''); -let files = $ref(props.initialFiles ?? []); +let posting: boolean = $ref(false); +let text: string = $ref(props.initialText ?? ''); +let files: foundkey.entities.DriveFile[] = $ref(props.initialFiles ?? []); let poll = $ref<{ choices: string[]; multiple: boolean; @@ -502,21 +502,56 @@ function onDrop(ev): void { //#endregion } +// Save a copy of the initial data to detect whether it has been changed. +// Cloning the data to make sure there are no remaining references. +const initialDraftData = JSON.parse(JSON.stringify({ + text, + useCw, + cw, + visibility, + localOnly, + files, + poll, +})); + function saveDraft() { const draftData = JSON.parse(localStorage.getItem('drafts') || '{}'); - draftData[draftKey] = { - updatedAt: new Date(), - data: { - text, - useCw, - cw, - visibility, - localOnly, - files, - poll, - }, - }; + if ( + initialDraftData.text === text + && initialDraftData.useCw === useCw + // don't compare cw for equality if it is disabled, since it won't be sent + && (!useCw || initialDraftData.cw === cw) + && initialDraftData.visibility === visibility + && initialDraftData.localOnly === localOnly + && initialDraftData.files.each((file, i) => file.id === files[i].id) + && ( + (initialDraftData.poll == null && poll == null) + || ( + initialDraftData.poll.choices.each((choice, i) => choice === choices[i]) + && initialDraftData.poll.multiple === poll.multiple + && initialDraftData.poll.expiresAt === poll.expiresAt + && initialDraftData.poll.expiredAfter === poll.expiredAfter + ) + ) + ) { + // This is the same as the initial draft data, no need to save it. + // If it was saved before, delete it. + delete draftData[draftKey]; + } else { + draftData[draftKey] = { + updatedAt: new Date(), + data: { + text, + useCw, + cw, + visibility, + localOnly, + files, + poll, + }, + }; + } localStorage.setItem('drafts', JSON.stringify(draftData)); }