From 14694f70822a928d6f22c330d64738962e772f98 Mon Sep 17 00:00:00 2001 From: pedrocx486 Date: Sun, 12 Feb 2023 04:11:26 -0300 Subject: [PATCH] Ability to load archive, remote posts, save archives and code improvements. --- src/App.vue | 162 ++++++++++++++++++++++++++++++++---- src/file-manager.service.ts | 8 +- src/http-helper.service.ts | 13 +-- src/types.ts | 6 +- 4 files changed, 163 insertions(+), 26 deletions(-) diff --git a/src/App.vue b/src/App.vue index 50bf55a..7ace5b1 100755 --- a/src/App.vue +++ b/src/App.vue @@ -7,10 +7,13 @@ import { generateTimestamp, parseTimestampToUTC, generatePostObj } from './post- import { getArchive, getPost } from './http-helper.service'; // TODO: -// Modal requesting blog install base url. -// Fetch archive from install and show posts in a list. -// Allow selection of posts form list and load them. -// Save post and archive. +// Ability to delete posts from the archive. + +let timestampUpdateInterval: number; + +const blogInstallLocation = ref(''); + +const archive = ref(); const postTitle = ref(''); const postContent = ref(''); @@ -20,21 +23,48 @@ const isDraft = ref(false); const isEditingExisting = ref(false); -let timestampUpdateInterval: number; +const onlyLoadArchive = ref(false); -const archive = ref(); +const openLoadModal = (archiveOnly = false): void => { + onlyLoadArchive.value = archiveOnly; + (document.getElementById('dialog') as HTMLDialogElement).showModal(); +} -const openLoadModal = (): void => { - alert('Not implemented, yet.'); +const closeLoadModal = (): void => { + (document.getElementById('dialog') as HTMLDialogElement).close(); +} + +const reset = (): void => { + archive.value = undefined; + postTitle.value = ''; + postContent.value = ''; + postTimestamp.value = ''; + editedTimestamp.value = ''; + isDraft.value = false; + isEditingExisting.value = false; + editedTimestamp.value = ''; + + clearInterval(timestampUpdateInterval); + + timestampUpdateInterval = setInterval(() => { + postTimestamp.value = generateTimestamp(); + }, 33); } const loadArchive = (baseUrl: string): void => { getArchive(baseUrl).then(res => { archive.value = res; + + if (onlyLoadArchive.value) { + onlyLoadArchive.value = false; + closeLoadModal(); + } }); } const loadPostFromFile = (file: any): void => { + archive.value = undefined; + loadFromFile(file.target.files[0]).then((res: Post) => { postTitle.value = res.postTitle; postContent.value = res.postContent; @@ -65,10 +95,12 @@ const loadPost = (baseUrl: string, filename: string): void => { timestampUpdateInterval = setInterval(() => { editedTimestamp.value = generateTimestamp(); }, 33); + + closeLoadModal(); }); } -const savePost = (): void => { +const savePost = (saveArchive?: boolean): void => { if (!postTitle.value) { postTitle.value = 'No title.'; } @@ -90,6 +122,17 @@ const savePost = (): void => { ), computedFilename.value ); + + if (saveArchive) { + if (!archive.value?.filter(post => post.filename === computedFilename.value) && !isDraft) { + archive.value?.push({ + postTitle: postTitle.value, + timestamp: postTimestamp.value, + filename: computedFilename.value + }); + } + downloadFile(archive.value, 'archive'); + } } onMounted(() => { @@ -105,6 +148,37 @@ const computedEditedTimestamp = computed(() => parseTimestampToUTC(editedTimesta