♻️ (scroll)

This commit is contained in:
Julien Calixte
2021-03-14 21:27:51 +01:00
parent 23a310f986
commit d624e064af
3 changed files with 33 additions and 18 deletions

View File

@@ -0,0 +1,27 @@
import { NOTE_WIDTH } from '@/constants/note-width'
import { useOverlay } from '@/hooks/useOverlay.hook'
import { nextTick } from 'vue'
import { LocationQueryValue } from 'vue-router'
export const useFocus = () => {
const { scrollToNote } = useOverlay(false)
const scrollToFocusedNote = (
stackedNotes: LocationQueryValue[],
sha?: string
) => {
if (!sha) {
return
}
nextTick(() => {
const index = stackedNotes.findIndex((noteSHA) => noteSHA === sha)
const left = index * NOTE_WIDTH
scrollToNote(left)
})
}
return {
scrollToFocusedNote
}
}

View File

@@ -12,7 +12,7 @@ import { noteEventBus } from '@/bus/noteBusEvent'
import { useLinks } from '@/hooks/useLinks.hook'
import { useRepo } from '@/hooks/useRepo.hook'
import { NOTE_WIDTH } from '@/constants/note-width'
import { useOverlay } from '@/hooks/useOverlay.hook'
import { useFocus } from '@/hooks/useFocus.hook'
const sanitizePath = (path: string) => {
if (path.startsWith('./')) {
@@ -28,7 +28,7 @@ export const useNote = (
) => {
const { push } = useRouter()
const { query } = useRoute()
const { scrollTo } = useOverlay(false)
const { scrollToFocusedNote } = useFocus()
const stackedNotes = ref(
query.stackedNotes
@@ -61,18 +61,6 @@ export const useNote = (
}, {})
)
const scrollToFocusedNote = (sha?: string) => {
if (!sha) {
return
}
nextTick(() => {
const index = stackedNotes.value.findIndex((noteSHA) => noteSHA === sha)
const left = index * NOTE_WIDTH
scrollTo(left)
})
}
const unsubscribe = noteEventBus.addEventBusListener(
({ path, currentNoteSHA }) => {
const currentFile = tree.value.find((file) => file.sha === currentNoteSHA)
@@ -88,7 +76,7 @@ export const useNote = (
const file = tree.value.find((file) => file.path === finalPath)
if (!file?.sha || stackedNotes.value.includes(file.sha)) {
scrollToFocusedNote(file?.sha)
scrollToFocusedNote(stackedNotes.value, file?.sha)
return
}
@@ -127,7 +115,7 @@ export const useNote = (
stackedNotes.value = newStackedNotes
scrollToFocusedNote(file.sha)
scrollToFocusedNote(stackedNotes.value, file.sha)
}
)

View File

@@ -21,7 +21,7 @@ export const useOverlay = (listen = true) => {
)
}
const scrollTo = (to: number) => {
const scrollToNote = (to: number) => {
body?.scroll({
left: to
})
@@ -29,6 +29,6 @@ export const useOverlay = (listen = true) => {
return {
x,
scrollTo
scrollToNote
}
}