display links to the stacked note

This commit is contained in:
Julien Calixte
2021-06-06 10:47:36 +02:00
parent 4f13c18573
commit c6dec8769f
6 changed files with 66 additions and 12 deletions

View File

@@ -0,0 +1,7 @@
import { createEventBus } from 'retrobus'
interface EventBusParams {
fileSha: string
}
export const backlinkEventBus = createEventBus<EventBusParams>()

View File

@@ -7,4 +7,4 @@ interface EventBusParams {
currentNoteSHA?: string currentNoteSHA?: string
} }
export const noteEventBus = createEventBus<EventBusParams>('note') export const noteEventBus = createEventBus<EventBusParams>()

View File

@@ -1,19 +1,37 @@
<template> <template>
<div class="linked-notes"></div> <div v-if="(backlink?.links.length ?? 0) > 0" class="linked-notes">
<hr />
<h4 class="subtitle is-4">🔗 Links to this note</h4>
<ul>
<li v-for="link in backlink?.links" :key="link.sha">
{{ link.title }}
</li>
</ul>
</div>
</template> </template>
<script lang="ts"> <script lang="ts">
import { useBacklinks } from '@/hooks/useBacklinks.hook'
import { defineComponent } from 'vue' import { defineComponent } from 'vue'
export default defineComponent({ export default defineComponent({
name: 'LinkedNotes', name: 'LinkedNotes',
props: { props: {
sha: { type: String, required: true } sha: { type: String, required: true }
},
setup(props) {
const { backlink } = useBacklinks(props.sha)
return {
backlink: backlink.state
}
} }
}) })
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.linked-notes { .linked-notes {
.subtitle {
font-style: italic;
}
} }
</style> </style>

View File

@@ -0,0 +1,36 @@
import { backlinkEventBus } from '@/bus/backlinkEventBus'
import { data } from '@/data/data'
import { DataType } from '@/data/DataType.enum'
import { BacklinkNote } from '@/modules/note/models/BacklinkNote'
import { useAsyncState } from '@vueuse/core'
import { onUnmounted } from 'vue'
export const useBacklinks = (sha: string) => {
const backlink = useAsyncState(
data.get<DataType.BacklinkNote, BacklinkNote>(
data.generateId(DataType.BacklinkNote, sha)
),
null,
{
resetOnExecute: true
}
)
const unsubscribe = backlinkEventBus.addEventBusListener(
({ fileSha }) => {
if (fileSha !== sha) {
return
}
backlink.execute()
},
{
retro: true
}
)
onUnmounted(() => unsubscribe())
return {
backlink
}
}

View File

@@ -1,3 +1,4 @@
import { backlinkEventBus } from '@/bus/backlinkEventBus'
import { data } from '@/data/data' import { data } from '@/data/data'
import { DataType } from '@/data/DataType.enum' import { DataType } from '@/data/DataType.enum'
import { useFile } from '@/hooks/useFile.hook' import { useFile } from '@/hooks/useFile.hook'
@@ -80,15 +81,7 @@ export const useComputeBacklinks = () => {
} }
await data.add(backlinkNote) await data.add(backlinkNote)
backlinkEventBus.emit({ fileSha: sha })
} }
const backlinksInDb = await data.getAll<
DataType.BacklinkNote,
BacklinkNote
>({
prefix: DataType.BacklinkNote
})
console.log(backlinksInDb.filter((b) => b.links.length))
}) })
} }

View File

@@ -1,2 +1,2 @@
export const filenameToNoteTitle = (title: string) => export const filenameToNoteTitle = (title: string) =>
title.replaceAll('/', ' / ') title.replaceAll('/', ' / ').replaceAll('-', ' ')