Skip to content

Commit

Permalink
Supposedly we're saving which view you prefer per markwhen document. …
Browse files Browse the repository at this point in the history
…Get rid of global default view setting. Hide the active view business
  • Loading branch information
kochrt committed Mar 27, 2023
1 parent 8b4488f commit b754cf1
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/AppSettings/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const visualizationStore = useVisualizationStore();
<template>
<div class="gap-2 flex flex-col overflow-scroll">
<div class="flex flex-col px-2 gap-2">
<div class="ml-2 flex">
<!-- <div class="ml-2 flex">
<label for="defaultView" class="mr-auto">Default view</label>
<select
v-model="settingsStore.defaultView"
Expand All @@ -22,7 +22,7 @@ const visualizationStore = useVisualizationStore();
>
<option v-for="view in defaultViewOptions">{{ view }}</option>
</select>
</div>
</div> -->
<div class="ml-2 flex">
<label for="theme" class="mr-auto">Theme</label>
<select
Expand Down
26 changes: 12 additions & 14 deletions src/AppSettings/appSettingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ export const useAppSettingsStore = defineStore("appSettings", () => {
const defaultView = ref<typeof defaultViewOptions[number]>("Timeline");
const theme = ref<typeof themeOptions[number]>("System");

const visualizationStore = useVisualizationStore();

const savedSettings = getSettings();
if (savedSettings) {
if (
savedSettings.defaultView &&
defaultViewOptions.includes(savedSettings.defaultView)
) {
defaultView.value = savedSettings.defaultView;
const foundView = visualizationStore.activeViews.findIndex(
(v) => v.name === savedSettings.defaultView
);
if (foundView >= 0) {
visualizationStore.selectedViewIndex = foundView;
}
}
// if (
// savedSettings.defaultView &&
// defaultViewOptions.includes(savedSettings.defaultView)
// ) {
// defaultView.value = savedSettings.defaultView;
// const foundView = visualizationStore.activeViews.findIndex(
// (v) => v.name === savedSettings.defaultView
// );
// if (foundView >= 0) {
// visualizationStore.selectedViewIndex = foundView;
// }
// }
if (savedSettings.theme && themeOptions.includes(savedSettings.theme)) {
theme.value = savedSettings.theme;
}
Expand Down
14 changes: 11 additions & 3 deletions src/Drawer/VisualizationSwitcher/VisualizationIndicator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ import { useVisualizationStore } from "@/Views/visualizationStore";
const visualizationStore = useVisualizationStore();
const view = computed(() => visualizationStore.currentView);
const click = () => {
visualizationStore.showingWelcomeViewPicker =
!visualizationStore.showingWelcomeViewPicker;
};
</script>

<template>
<SettingsButton class="p-1 w-full justify-center" :hover-hint-left="-1">
<SettingsButton
class="p-1 w-full justify-center"
:hover-hint-left="-1"
@click="click"
>
<div
class="flex flex-row items-center gap-1"
v-if="!visualizationStore.showingWelcomeViewPicker"
Expand Down Expand Up @@ -42,8 +50,8 @@ const view = computed(() => visualizationStore.currentView);
d="M14 14m0 1a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v4a1 1 0 0 1 -1 1h-4a1 1 0 0 1 -1 -1z"
></path>
<path d="M14 7l6 0"></path>
<path d="M17 4l0 6"></path>
</svg><span class="text-sm">Views</span>
<path d="M17 4l0 6"></path></svg
><span class="text-sm">Views</span>
</div>
<template #hover="slotProps">
<HoverMenu :hovering="slotProps.hovering">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const selectView = (i: number) => {
<template>
<div class="flex flex-col">
<button
@click="visualizationStore.showWelcomeViewPicker"
@click.stop="visualizationStore.showWelcomeViewPicker"
class="flex flex-row px-3 py-1 items-center gap-2 hover:bg-indigo-600"
:class="{
'bg-indigo-600': visualizationStore.showingWelcomeViewPicker,
Expand Down Expand Up @@ -47,7 +47,7 @@ const selectView = (i: number) => {
></path>
<path d="M14 7l6 0"></path>
<path d="M17 4l0 6"></path></svg
><span class="text-sm">All views...</span>
><span class="text-sm">Views...</span>
</button>
<hr class="border-slate-700 my-[2px]" />
<button
Expand All @@ -58,7 +58,7 @@ const selectView = (i: number) => {
visualizationStore.currentView.name === view.name,
}"
class="flex flex-row px-3 py-1 items-center gap-2 hover:bg-indigo-600"
@click="selectView(i)"
@click.stop="selectView(i)"
>
<div class="w-4 h-4" v-html="view.iconSvg"></div>
<span class="text-sm">{{ view.name }}</span>
Expand Down
2 changes: 1 addition & 1 deletion src/Markwhen/composables/useParserWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const useParserWorker = (rawTimelineString: Ref<string>) => {

worker.addEventListener("message", (message) => {
const { timelines: fromWorker, cache: c } = message.data;
console.log("parse time", performance.now() - timeStart);
// console.log("parse time", performance.now() - timeStart);
timelines.value = fromWorker;
if (queuedString.value !== rawTimelineString.value) {
worker.postMessage({ rawTimelineString: queuedString.value });
Expand Down
124 changes: 101 additions & 23 deletions src/Views/visualizationStore.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import { useStorageStore } from "@/Storage/storageStore";
import type { ViewProvider } from "@/viewProvider";
import { defineStore } from "pinia";
import { computed, ref, watch, watchEffect } from "vue";
import { useViewProviders } from "./useViewProviders";
import { useViewOrchestrator } from "./ViewOrchestrator/useViewOrchestrator";

const viewAssociationsKey = "viewAssociations";

export const useVisualizationStore = defineStore("visualization", () => {
const activeFrame = ref<HTMLIFrameElement>();
const storageStore = useStorageStore();
const showingWelcomeViewPicker = ref(true);
const activeFrame = ref<HTMLIFrameElement>();
const selectedViewIndex = ref(-1);

const getViewOptions = () => {
const defaultOptions = useViewProviders();
if (typeof localStorage !== "undefined") {
const saved = localStorage.getItem("viewOptions");
if (saved) {
try {
const parsed = JSON.parse(saved);
return parsed as ViewProvider[];
} catch {
return defaultOptions;
}
} else {
return defaultOptions;
}
}
return defaultOptions;
};

Expand All @@ -39,28 +30,113 @@ export const useVisualizationStore = defineStore("visualization", () => {
activeFrame.value = frame;
};

watch(
viewOptions,
(vo) => {
if (typeof localStorage !== "undefined") {
localStorage.setItem(
"viewOptions",
JSON.stringify(vo.filter((v) => typeof v.url === "string"))
);
// watch(
// viewOptions,
// (vo) => {
// if (typeof localStorage !== "undefined") {
// for (const vp of useViewProviders()) {

// }
// localStorage.setItem(
// "viewOptions",
// JSON.stringify(vo.filter((v) => typeof v.url === "string"))
// );
// }
// },
// { deep: true }
// );

const getExistingViewAssociations = () => {
if (!localStorage || typeof localStorage === "undefined") {
return;
}
return JSON.parse(
localStorage.getItem(viewAssociationsKey) || "{}"
) as Record<string, string>;
};

const viewAssociationKeyForStorageType = computed(() => {
switch (storageStore.storageType) {
case "cloud":
return storageStore.cloudPath;
case "draft":
return storageStore.draftName || "draft";
}
});

const setViewAssociation = () => {
const existingViewAssociations = getExistingViewAssociations();
if (!existingViewAssociations) {
return;
}

if (viewAssociationKeyForStorageType.value && currentView.value) {
const updatedAssociations = {
...existingViewAssociations,
[viewAssociationKeyForStorageType.value]: currentView.value.id,
};
localStorage.setItem(
viewAssociationsKey,
JSON.stringify(updatedAssociations)
);
}
};

const getAndApplyViewAssociation = () => {
const existingViewAssociations = getExistingViewAssociations();
if (!existingViewAssociations) {
return;
}
if (existingViewAssociations && viewAssociationKeyForStorageType.value) {
const viewId =
existingViewAssociations[viewAssociationKeyForStorageType.value];
if (viewId) {
const index = viewOptions.value.findIndex((vp) => vp.id === viewId);
if (index !== -1) {
selectedViewIndex.value = index;
showingWelcomeViewPicker.value = false;
return true;
}
}
}
return false;
};

watch(
[
() => storageStore.storageType,
() => storageStore.draftName,
() => storageStore.cloudPath,
],
() => {
getAndApplyViewAssociation();
setViewAssociation();
},
{ deep: true }
{ immediate: true }
);

watch(selectedViewIndex, (val, oldVal) => {
if (val !== oldVal) {
setViewAssociation();
}
});

watchEffect(() => {
if (selectedViewIndex.value >= activeViews.value.length) {
selectedViewIndex.value = 0;
} else if (selectedViewIndex.value < 0) {
// Set default view
// Don't set view association here because we're just setting the default
selectedViewIndex.value = 2;
}
});

watch(showingWelcomeViewPicker, (showing) => {
if (!showing) {
setViewAssociation();
}
});

const showWelcomeViewPicker = () => {
showingWelcomeViewPicker.value = true;
};
Expand All @@ -76,6 +152,8 @@ export const useVisualizationStore = defineStore("visualization", () => {
currentView,
showingWelcomeViewPicker,

getAndApplyViewAssociation,
setViewAssociation,
showWelcomeViewPicker,
setActiveFrame,
jumpToRange,
Expand Down
9 changes: 9 additions & 0 deletions src/WelcomeViewPicker/CustomViewOption.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script setup lang="ts"></script>

<template>
<div
class="flex flex-col gap-2 p-2 group dark:hover:bg-slate-600 hover:bg-slate-200 rounded transition cursor-pointer outline outline-2 outline-slate-200 dark:outline-slate-600"
></div>
</template>

<style scoped></style>
12 changes: 12 additions & 0 deletions src/WelcomeViewPicker/ViewPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import VisualizationOption from "./VisualizationOption.vue";
import { useVisualizationStore } from "@/Views/visualizationStore";
import type { ViewProvider } from "@/viewProvider";
import { onMounted } from "vue";
import CustomViewOption from "./CustomViewOption.vue";
const visualizationStore = useVisualizationStore();
Expand All @@ -24,6 +25,15 @@ const removeView = (v: ViewProvider) => {
visualizationStore.viewOptions.splice(index, 1);
}
};
const select = (v: ViewProvider) => {
v.active = true;
const index = visualizationStore.activeViews.findIndex(
(vo) => v.name === vo.name && v.url === vo.url
);
visualizationStore.selectedViewIndex = index;
visualizationStore.showingWelcomeViewPicker = false
};
</script>

<template>
Expand All @@ -36,7 +46,9 @@ const removeView = (v: ViewProvider) => {
:is-active="!!view.active"
@toggle="toggleView(view)"
@remove="removeView(view)"
@click="select(view)"
></VisualizationOption>
<!-- <CustomViewOption></CustomViewOption> -->
</div>
</section>
</template>
Expand Down
7 changes: 4 additions & 3 deletions src/WelcomeViewPicker/VisualizationOption.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const events = canHover.value
<h1 class="text-xl font-bold flex flex-row items-center gap-2">
<div class="h-5 w-5" v-html="vp.iconSvg"></div>
{{ vp.name }}
<div class="ml-auto px-1 relative" v-on="events">
<!-- <div class="ml-auto px-1 relative" v-on="events">
<transition>
<input
class="transition"
Expand All @@ -44,13 +44,14 @@ const events = canHover.value
type="checkbox"
:checked="isActive"
:disabled="isActive && !canChange"
@change="emit('toggle')"
@click.stop=""
@change.prevent.stop="emit('toggle')"
/>
</transition>
<HoverHint :hovering="hovering" hover-position="top" :left="-3"
><span class="text-sm">Quick access</span></HoverHint
>
</div>
</div> -->
</h1>
<div class="py-1" v-if="vp.screenshots && vp.screenshots.length">
<img :src="vp.screenshots[0]" class="rounded" />
Expand Down
4 changes: 2 additions & 2 deletions src/WelcomeViewPicker/WelcomeViewPicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const close = () => {

<template>
<div class="w-full h-full flex justify-center @container">
<main class="p-6 flex flex-col gap-4 overflow-auto">
<main class="py-6 px-6 @lg:px-24 flex flex-col gap-4 overflow-auto">
<WelcomeBanner v-if="showingBanner" @close="close"></WelcomeBanner>
<ViewPicker></ViewPicker>
</main>
Expand All @@ -30,6 +30,6 @@ const close = () => {

<style scoped>
main {
width: min(96ch, 100%);
width: min(104ch, 100%);
}
</style>
3 changes: 2 additions & 1 deletion src/viewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export interface ViewSetting {
}

export interface ViewProvider {
url: string,
id: string,
url: string | any,
name: string,
iconSvg?: string,
settings?: (() => ViewSetting | any)[]
Expand Down

0 comments on commit b754cf1

Please sign in to comment.