From cc3f4fabc2b49579f74d5d75dcb5d80ef1904d40 Mon Sep 17 00:00:00 2001 From: Sidharth Vinod Date: Sat, 5 Jun 2021 15:31:22 +0530 Subject: [PATCH] Loading Gist --- src/app.postcss | 4 +++ src/lib/components/actions.svelte | 43 +++++++++++++++++++++++++----- src/lib/types.d.ts | 4 +++ src/lib/util/fileLoaders/gist.ts | 12 +++++++-- src/lib/util/fileLoaders/loader.ts | 10 ++++--- src/lib/util/loading.ts | 19 +++++++++++++ src/lib/util/util.ts | 3 ++- src/routes/__layout.svelte | 42 +++++++++++++++++++++++++++++ 8 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 src/lib/util/loading.ts diff --git a/src/app.postcss b/src/app.postcss index ce6711bb..edf491bf 100644 --- a/src/app.postcss +++ b/src/app.postcss @@ -12,3 +12,7 @@ .action-btn { @apply rounded p-2 bg-indigo-400 shadow flex-auto text-white hover:bg-indigo-500; } + +.input { + @apply flex-1 border-indigo-400 border-solid border-2 rounded; +} diff --git a/src/lib/components/actions.svelte b/src/lib/components/actions.svelte index a119c4d5..ef3c03b4 100644 --- a/src/lib/components/actions.svelte +++ b/src/lib/components/actions.svelte @@ -1,7 +1,8 @@ - +
{#if isClipboardAvailable()} + +
+ +
+ +
diff --git a/src/lib/types.d.ts b/src/lib/types.d.ts index 3969b14d..e0a139b3 100644 --- a/src/lib/types.d.ts +++ b/src/lib/types.d.ts @@ -36,6 +36,10 @@ export interface GistLoaderConfig { url: string; } +export interface LoadingState { + loading: boolean; + message?: string; +} export interface FileLoaderConfig { codeURL: string; configURL?: string; diff --git a/src/lib/util/fileLoaders/gist.ts b/src/lib/util/fileLoaders/gist.ts index 6153b4b7..880032d9 100644 --- a/src/lib/util/fileLoaders/gist.ts +++ b/src/lib/util/fileLoaders/gist.ts @@ -55,9 +55,15 @@ const getGistData = async (gistURL: string): Promise => { }; const getStateFromGist = (gist: GistData): State => { - const state = { + const state: State = { ...defaultState, - code: gist.code + code: gist.code, + loader: { + type: 'gist', + config: { + url: gist.url + } + } }; gist.config && (state.mermaid = gist.config); return state; @@ -81,6 +87,8 @@ export const loadGistData = async (gistURL: string): Promise => { } gistHistory.reverse(); const state = getStateFromGist(gistHistory.slice(-1).pop()); + // @ts-ignore Ignore + state.loader.config.url = gistURL; for (const gist of gistHistory) { addHistoryEntry({ state: getStateFromGist(gist), diff --git a/src/lib/util/fileLoaders/loader.ts b/src/lib/util/fileLoaders/loader.ts index 5a04b2a7..c9a04184 100644 --- a/src/lib/util/fileLoaders/loader.ts +++ b/src/lib/util/fileLoaders/loader.ts @@ -7,7 +7,7 @@ const loaders: Record = { export const loadDataFromUrl = async (): Promise => { const searchParams = new URLSearchParams(window.location.search); - let state: State; + let state: State = defaultState; let code: string, config: string; const codeURL: string = searchParams.get('code'); const configURL: string = searchParams.get('config'); @@ -23,8 +23,12 @@ export const loadDataFromUrl = async (): Promise => { if (!code) { for (const [key, value] of searchParams.entries()) { if (key in loaders) { - state = await loaders[key](value); - break; + try { + state = await loaders[key](value); + break; + } catch (err) { + console.error(err); + } } } } else { diff --git a/src/lib/util/loading.ts b/src/lib/util/loading.ts new file mode 100644 index 00000000..84205ebf --- /dev/null +++ b/src/lib/util/loading.ts @@ -0,0 +1,19 @@ +import { writable, Writable } from 'svelte/store'; +import type { LoadingState } from '$lib/types'; + +const defaultLoading: LoadingState = { + loading: false +}; + +export const loadingStateStore: Writable = writable(defaultLoading); +export const initLoading = async (message: string, task: Promise): Promise => { + loadingStateStore.set({ + loading: true, + message + }); + const result: T = await task; + loadingStateStore.set({ + loading: false + }); + return result; +}; diff --git a/src/lib/util/util.ts b/src/lib/util/util.ts index f244f9ff..84e02992 100644 --- a/src/lib/util/util.ts +++ b/src/lib/util/util.ts @@ -2,6 +2,7 @@ import type { State } from '$lib/types'; import { initURLSubscription, loadState, updateCodeStore } from './state'; import { analytics, initAnalytics } from './stats'; import { loadDataFromUrl } from './fileLoaders/loader'; +import { initLoading } from './loading'; export const loadStateFromURL = (): void => { loadState(window.location.hash.slice(1)); @@ -15,7 +16,7 @@ export const syncDiagram = (): void => { export const initHandler = async (): Promise => { loadStateFromURL(); - await loadDataFromUrl().catch(console.error); + await initLoading('Loading Gist...', loadDataFromUrl().catch(console.error)); syncDiagram(); initURLSubscription(); await initAnalytics(); diff --git a/src/routes/__layout.svelte b/src/routes/__layout.svelte index f17b2f6e..92cf8996 100644 --- a/src/routes/__layout.svelte +++ b/src/routes/__layout.svelte @@ -2,6 +2,7 @@ import '../app.postcss'; import { base } from '$app/paths'; import { onMount } from 'svelte'; + import { loadingStateStore } from '$lib/util/loading'; // This can be removed once https://github.com/sveltejs/kit/issues/1612 is fixed. // Then move it into src and vite will bundle it automatically. onMount(() => { @@ -23,3 +24,44 @@
+ +{#if $loadingStateStore.loading} +
+
+
+
{$loadingStateStore.message}
+
+
+{/if} + +