From 00704bd88cfc191db457cc022057c0ab5ac0a919 Mon Sep 17 00:00:00 2001 From: solidsanek Date: Fri, 17 Feb 2023 13:56:01 +0100 Subject: [PATCH 1/2] Post: Add drafting feature --- .../post_status_form/post_status_form.js | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/src/components/post_status_form/post_status_form.js b/src/components/post_status_form/post_status_form.js index b7c66fc7..2d42d8cc 100644 --- a/src/components/post_status_form/post_status_form.js +++ b/src/components/post_status_form/post_status_form.js @@ -54,6 +54,14 @@ const pxStringToNumber = (str) => { return Number(str.substring(0, str.length - 2)) } +const deleteDraft = (draftKey) => { + const draftData = JSON.parse(localStorage.getItem('drafts') || '{}'); + + delete draftData[draftKey]; + + localStorage.setItem('drafts', JSON.stringify(draftData)); +} + const PostStatusForm = { props: [ 'statusId', @@ -161,6 +169,34 @@ const PostStatusForm = { } } + let draftKey = 'status'; + if (this.replyTo) { + draftKey = 'reply:' + this.replyTo; + } else if (this.quoteId) { + draftKey = 'quote:' + this.quoteId; + } + + const draft = JSON.parse(localStorage.getItem('drafts') || '{}')[draftKey]; + + if (draft) { + statusParams = { + spoilerText: draft.data.spoilerText, + status: draft.data.status, + sensitiveIfSubject, + nsfw: draft.data.nsfw, + files: draft.data.files, + poll: draft.data.poll, + mediaDescriptions: draft.data.mediaDescriptions, + visibility: draft.data.visibility, + language: draft.data.language, + contentType: draft.data.contentType + } + + if (draft.data.poll) { + this.togglePollForm(); + } + } + return { dropFiles: [], uploadingFiles: false, @@ -280,6 +316,7 @@ const PostStatusForm = { statusChanged () { this.autoPreview() this.updateIdempotencyKey() + this.saveDraft() }, clearStatus () { const newStatus = this.newStatus @@ -401,8 +438,50 @@ const PostStatusForm = { }).finally(() => { this.previewLoading = false }) + + let draftKey = 'status'; + if (this.replyTo) { + draftKey = 'reply:' + this.replyTo; + } else if (this.quoteId) { + draftKey = 'quote:' + this.quoteId; + } + deleteDraft(draftKey) }, debouncePreviewStatus: debounce(function () { this.previewStatus() }, 500), + saveDraft() { + const draftData = JSON.parse(localStorage.getItem('drafts') || '{}'); + + let draftKey = 'status'; + if (this.replyTo) { + draftKey = 'reply:' + this.replyTo; + } else if (this.quoteId) { + draftKey = 'quote:' + this.quoteId; + } + + if (this.newStatus.status || this.newStatus.spoilerText || this.newStatus.files.length > 0 || this.newStatus.poll.length > 0) { + if(this.newStatus.status) { + console.log(this.newStatus.status) + } + if(this.newStatus.spoilerText) { + console.log(this.newStatus.spoilerText) + } + if(this.newStatus.files) { + console.log(this.newStatus.files) + } + if(this.newStatus.poll) { + console.log(this.newStatus.poll) + } + console.log('not empty?'); + draftData[draftKey] = { + updatedAt: new Date(), + data: this.newStatus, + }; + + localStorage.setItem('drafts', JSON.stringify(draftData)); + } else { + deleteDraft(draftKey); + } + }, autoPreview () { if (!this.preview) return this.previewLoading = true From 2c007f06e35ffee25d55b2cc0bbf84202c473dbc Mon Sep 17 00:00:00 2001 From: solidsanek Date: Sun, 19 Feb 2023 18:58:53 +0100 Subject: [PATCH 2/2] Post: remove debug logs --- .../post_status_form/post_status_form.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/components/post_status_form/post_status_form.js b/src/components/post_status_form/post_status_form.js index 2d42d8cc..f7fef499 100644 --- a/src/components/post_status_form/post_status_form.js +++ b/src/components/post_status_form/post_status_form.js @@ -459,25 +459,13 @@ const PostStatusForm = { } if (this.newStatus.status || this.newStatus.spoilerText || this.newStatus.files.length > 0 || this.newStatus.poll.length > 0) { - if(this.newStatus.status) { - console.log(this.newStatus.status) - } - if(this.newStatus.spoilerText) { - console.log(this.newStatus.spoilerText) - } - if(this.newStatus.files) { - console.log(this.newStatus.files) - } - if(this.newStatus.poll) { - console.log(this.newStatus.poll) - } - console.log('not empty?'); - draftData[draftKey] = { + draftData[draftKey] = { updatedAt: new Date(), data: this.newStatus, }; localStorage.setItem('drafts', JSON.stringify(draftData)); + } else { deleteDraft(draftKey); }