(backlinks) implement backlinks in notes

This commit is contained in:
Julien Calixte
2021-06-06 11:11:15 +02:00
parent c6dec8769f
commit bff90e6ef5
7 changed files with 117 additions and 103 deletions

View File

@@ -61,6 +61,10 @@ export const useComputeBacklinks = () => {
}
const previousBacklinks = backlinks.get(backlinkFile.sha) ?? []
if (previousBacklinks.find((bl) => bl.sha === file.sha)) {
continue
}
backlinks.set(backlinkFile.sha, [
...previousBacklinks,
{

View File

@@ -1,42 +0,0 @@
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 { stackedNotes } = useQueryStackedNotes()
const scrollToFocusedNote = (sha?: string, backToTop?: boolean) => {
if (backToTop) {
scrollToNote(0)
return
}
if (!sha) {
return
}
nextTick(() => {
const index = stackedNotes.value.findIndex((noteSHA) => noteSHA === sha)
const hasOneStackedNote = stackedNotes.value.length === 1
if (isMobile.value) {
const element = document.querySelector(`.note-${sha}`) as HTMLElement
const top = (index + 1) * (element?.clientHeight ?? height.value)
scrollToNote(top, hasOneStackedNote)
} else {
const margin = index * 44
const left = (index + 1) * NOTE_WIDTH - margin
scrollToNote(left, hasOneStackedNote)
}
})
}
return {
scrollToFocusedNote
}
}

View File

@@ -2,19 +2,15 @@ import { computed, onMounted, onUnmounted, watch } from '@vue/runtime-core'
import { NOTE_WIDTH } from '@/constants/note-width'
import { noteEventBus } from '@/bus/noteEventBus'
import { useFocus } from '@/hooks/useFocus.hook'
import { useOverlay } from '@/hooks/useOverlay.hook'
import { useQueryStackedNotes } from '@/hooks/useQueryStackedNotes.hook'
import { useRouter } from 'vue-router'
import { resolvePath } from '@/modules/repo/services/resolvePath'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
export const useNote = (containerClass: string) => {
const store = useUserRepoStore()
const { push, currentRoute } = useRouter()
const { isMobile } = useOverlay(false)
const { scrollToFocusedNote } = useFocus()
const { stackedNotes, updateQueryStackedNotes } = useQueryStackedNotes()
const { stackedNotes, addStackedNote } = useQueryStackedNotes()
const titles = computed(() =>
stackedNotes.value?.reduce((obj: Record<string, string>, note) => {
@@ -38,7 +34,7 @@ export const useNote = (containerClass: string) => {
)
const unsubscribeLink = noteEventBus.addEventBusListener(
({ user, repo, path, currentNoteSHA }) => {
({ path, currentNoteSHA }) => {
const currentFile = store.files.find(
(file) => file.sha === currentNoteSHA
)
@@ -46,47 +42,11 @@ export const useNote = (containerClass: string) => {
const finalPath = resolvePath(currentFile?.path ?? '', path)
const file = store.files.find((file) => file.path === finalPath)
if (!file?.sha || stackedNotes.value.includes(file.sha)) {
scrollToFocusedNote(file?.sha)
if (!file?.sha) {
return
}
const getStackedNotes = () => {
if (!file?.sha) {
return []
}
if (!currentNoteSHA) {
return [file.sha]
}
const [splittedStackedNotes] = stackedNotes.value
.join(';')
.split(currentNoteSHA)
return [
...splittedStackedNotes.replaceAll(';;', ';').split(';'),
currentNoteSHA,
file.sha
].filter((sha) => !!sha)
}
const newStackedNotes = getStackedNotes()
push({
name: currentRoute.value.name ?? 'Home',
params: {
user,
repo
},
query: {
stackedNotes: newStackedNotes
}
})
updateQueryStackedNotes(newStackedNotes)
scrollToFocusedNote(file.sha)
addStackedNote(currentNoteSHA ?? '', file.sha)
}
)

View File

@@ -1,6 +1,11 @@
import { NOTE_WIDTH } from '@/constants/note-width'
import { useOverlay } from '@/hooks/useOverlay.hook'
import { useUserRepoStore } from '@/modules/repo/store/userRepo.store'
import { readonly, ref } from '@vue/reactivity'
import { useWindowSize } from '@vueuse/core'
import { nextTick } from 'vue'
import { useRoute } from 'vue-router'
import { useRoute, useRouter } from 'vue-router'
const stackedNotes = ref<string[]>([])
@@ -8,24 +13,102 @@ let initial = true
export const useQueryStackedNotes = () => {
const { query } = useRoute()
const { push, currentRoute } = useRouter()
const store = useUserRepoStore()
const { height } = useWindowSize()
const { scrollToNote, isMobile } = useOverlay(false)
const initResetStackedNote = () =>
(stackedNotes.value = (Array.isArray(query.stackedNotes)
? query.stackedNotes
: [query.stackedNotes]
)
.map((n) => n?.toString())
.filter((n) => !!n) as string[])
const scrollToFocusedNote = (sha?: string, backToTop?: boolean) => {
if (backToTop) {
scrollToNote(0)
return
}
if (!sha) {
return
}
nextTick(() => {
const index = stackedNotes.value.findIndex((noteSHA) => noteSHA === sha)
const hasOneStackedNote = stackedNotes.value.length === 1
if (isMobile.value) {
const element = document.querySelector(`.note-${sha}`) as HTMLElement
const top = (index + 1) * (element?.clientHeight ?? height.value)
scrollToNote(top, hasOneStackedNote)
} else {
const margin = index * 44
const left = (index + 1) * NOTE_WIDTH - margin
scrollToNote(left, hasOneStackedNote)
}
})
}
const initResetStackedNote = () => {
stackedNotes.value = Array.isArray(query.stackedNotes)
? (query.stackedNotes as string[])
: ([query.stackedNotes]
.map((n) => n?.toString())
.filter((n) => !!n) as string[])
}
if (initial) {
initial = false
initResetStackedNote()
}
const updateQueryStackedNotes = (newStackedNotes: string[]) =>
(stackedNotes.value = newStackedNotes)
const addStackedNote = (currentSHA: string, sha: string) => {
if (stackedNotes.value.includes(sha)) {
scrollToFocusedNote(sha)
return
}
const getStackedNotes = () => {
if (!sha) {
return []
}
if (!currentSHA) {
return [sha]
}
const [splittedStackedNotes] = stackedNotes.value
.join(';')
.split(currentSHA)
return [
...splittedStackedNotes.replaceAll(';;', ';').split(';'),
currentSHA,
sha
].filter((sha) => !!sha)
}
const newStackedNotes = getStackedNotes()
push({
name: currentRoute.value.name ?? 'Home',
params: {
user: store.user,
repo: store.repo
},
query: {
stackedNotes: newStackedNotes
}
})
updateQueryStackedNotes(newStackedNotes)
scrollToFocusedNote(sha)
}
return {
stackedNotes: readonly(stackedNotes),
updateQueryStackedNotes: (newStackedNotes: string[]) =>
(stackedNotes.value = newStackedNotes),
resetStackedNotes: () => initResetStackedNote()
updateQueryStackedNotes,
addStackedNote,
resetStackedNotes: () => initResetStackedNote(),
scrollToFocusedNote
}
}