diff --git a/src/hooks/useFocus.hook.ts b/src/hooks/useFocus.hook.ts index 68e215d..9dee770 100644 --- a/src/hooks/useFocus.hook.ts +++ b/src/hooks/useFocus.hook.ts @@ -1,32 +1,21 @@ -import { LocationQueryValue, useRoute } from 'vue-router' -import { computed, nextTick } from 'vue' - import { NOTE_WIDTH } from '@/constants/note-width' +import { nextTick } from 'vue' import { useOverlay } from '@/hooks/useOverlay.hook' +import { useQueryStackedNotes } from '@/hooks/useQueryStackedNotes.hook' import { useWindowSize } from '@vueuse/core' export const useFocus = () => { const { height } = useWindowSize() const { scrollToNote, isMobile } = useOverlay(false) - const { query } = useRoute() + const { stackedNotes } = useQueryStackedNotes() - const initialStackedNotes = computed(() => - query.stackedNotes - ? Array.isArray(query.stackedNotes) - ? query.stackedNotes - : [query.stackedNotes] - : [] - ) - - const scrollToFocusedNote = ( - sha?: string, - stackedNotes: LocationQueryValue[] = initialStackedNotes.value - ) => { + const scrollToFocusedNote = (sha?: string) => { if (!sha) { return } nextTick(() => { - const index = stackedNotes.findIndex((noteSHA) => noteSHA === sha) + const index = stackedNotes.value.findIndex((noteSHA) => noteSHA === sha) + if (isMobile.value) { const element = document.querySelector(`.note-${sha}`) as HTMLElement const top = (index + 1) * (element?.clientHeight ?? height.value) diff --git a/src/hooks/useNote.hook.ts b/src/hooks/useNote.hook.ts index 478a558..e78428c 100644 --- a/src/hooks/useNote.hook.ts +++ b/src/hooks/useNote.hook.ts @@ -1,4 +1,3 @@ -import { Ref, ref } from '@vue/reactivity' import { computed, nextTick, @@ -6,14 +5,16 @@ import { onUnmounted, watch } from '@vue/runtime-core' -import { useRoute, useRouter } from 'vue-router' import { NOTE_WIDTH } from '@/constants/note-width' +import { Ref } from '@vue/reactivity' import { noteEventBus } from '@/bus/noteBusEvent' import { useFocus } from '@/hooks/useFocus.hook' import { useLinks } from '@/hooks/useLinks.hook' import { useOverlay } from '@/hooks/useOverlay.hook' +import { useQueryStackedNotes } from '@/hooks/useQueryStackedNotes.hook' import { useRepo } from '@/hooks/useRepo.hook' +import { useRouter } from 'vue-router' const sanitizePath = (path: string) => { if (path.startsWith('./')) { @@ -28,23 +29,15 @@ export const useNote = ( repo: Ref ) => { const { push } = useRouter() - const { query } = useRoute() const { isMobile } = useOverlay(false) const { scrollToFocusedNote } = useFocus() - - const stackedNotes = ref( - query.stackedNotes - ? Array.isArray(query.stackedNotes) - ? query.stackedNotes - : [query.stackedNotes] - : [] - ) + const { stackedNotes, updateQueryStackedNotes } = useQueryStackedNotes() const { readme, notFound, tree } = useRepo(user, repo) const { listenToClick } = useLinks('note-display') const titles = computed(() => - stackedNotes.value.reduce((obj: Record, note) => { + stackedNotes.value?.reduce((obj: Record, note) => { if (!note) { return obj } @@ -82,7 +75,7 @@ export const useNote = ( ) if (!file?.sha || stackedNotes.value.includes(file.sha)) { - scrollToFocusedNote(file?.sha, stackedNotes.value) + scrollToFocusedNote(file?.sha) return } @@ -119,9 +112,8 @@ export const useNote = ( } }) - stackedNotes.value = newStackedNotes - - scrollToFocusedNote(file.sha, stackedNotes.value) + updateQueryStackedNotes(newStackedNotes) + scrollToFocusedNote(file.sha) } ) @@ -145,10 +137,6 @@ export const useNote = ( } } - const resetStackedNotes = () => { - stackedNotes.value = [] - } - onMounted(() => { resizeContainer() }) @@ -164,8 +152,6 @@ export const useNote = ( return { titles, readme, - notFound, - stackedNotes, - resetStackedNotes + notFound } } diff --git a/src/hooks/useQueryStackedNotes.hook.ts b/src/hooks/useQueryStackedNotes.hook.ts new file mode 100644 index 0000000..90f2419 --- /dev/null +++ b/src/hooks/useQueryStackedNotes.hook.ts @@ -0,0 +1,30 @@ +import { readonly, ref } from '@vue/reactivity' + +import { useRoute } from 'vue-router' + +const stackedNotes = ref([]) + +let initial = true + +export const useQueryStackedNotes = () => { + const { query } = useRoute() + if (initial) { + initial = false + stackedNotes.value = query.stackedNotes + ? Array.isArray(query.stackedNotes) + ? (query.stackedNotes + .map((n) => n?.toString()) + .filter((n) => !!n) as string[]) + : ([query.stackedNotes] + .map((n) => n?.toString()) + .filter((n) => !!n) as string[]) + : ([] as string[]) + } + + return { + stackedNotes: readonly(stackedNotes), + updateQueryStackedNotes: (newStackedNotes: string[]) => + (stackedNotes.value = newStackedNotes), + resetStackedNotes: () => (stackedNotes.value = []) + } +} diff --git a/src/views/Home.vue b/src/views/Home.vue index ae658ad..3d1f1a4 100644 --- a/src/views/Home.vue +++ b/src/views/Home.vue @@ -43,6 +43,7 @@