From 83aca6e7ec5fbe6916fcd8d5935e5ed8f6f7014a Mon Sep 17 00:00:00 2001 From: yukineko <27853966+hideki0403@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:28:10 +0900 Subject: [PATCH] =?UTF-8?q?add:=20=E4=B8=8B=E6=9B=B8=E3=81=8D=E3=81=AB?= =?UTF-8?q?=E9=96=A2=E3=81=99=E3=82=8B=E9=96=A2=E6=95=B0=E7=AD=89=E3=81=AE?= =?UTF-8?q?=E5=88=87=E3=82=8A=E9=9B=A2=E3=81=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/frontend/src/scripts/note-drafts.ts | 44 ++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 packages/frontend/src/scripts/note-drafts.ts diff --git a/packages/frontend/src/scripts/note-drafts.ts b/packages/frontend/src/scripts/note-drafts.ts new file mode 100644 index 0000000000..0b886f912c --- /dev/null +++ b/packages/frontend/src/scripts/note-drafts.ts @@ -0,0 +1,44 @@ +import * as Misskey from 'misskey-js'; +import type { PollEditorModelValue } from '@/components/MkPollEditor.vue'; +import type { DeleteScheduleEditorModelValue } from '@/components/MkDeleteScheduleEditor.vue'; +import { miLocalStorage } from '@/local-storage.js'; + +export type NoteDraft = { + updatedAt: Date; + data: { + text: string; + useCw: boolean; + cw: string | null; + visibility: (typeof Misskey.noteVisibilities)[number]; + localOnly: boolean; + files: Misskey.entities.DriveFile[]; + poll: PollEditorModelValue | null; + scheduledNoteDelete: DeleteScheduleEditorModelValue | null; + }; +}; + +export function getAll() { + const drafts = miLocalStorage.getItem('drafts'); + if (!drafts) return {}; + return JSON.parse(drafts) as Record; +} + +export function get(key: string) { + const draft = getAll()[key]; + return draft ?? null; +} + +export function set(key: string, draft: NoteDraft['data']) { + const drafts = getAll(); + drafts[key] = { + updatedAt: new Date(), + data: draft, + }; + miLocalStorage.setItem('drafts', JSON.stringify(drafts)); +} + +export function remove(key: string) { + const drafts = getAll(); + delete drafts[key]; + miLocalStorage.setItem('drafts', JSON.stringify(drafts)); +}