update note display

This commit is contained in:
2023-04-29 19:10:25 +02:00
parent 4cc7c01365
commit 96e3bed77d
11 changed files with 250 additions and 61 deletions

View File

@@ -1,11 +1,24 @@
import { ref, computed, watch } from 'vue'
import { ref, reactive, computed, watch } from 'vue'
import { useTitle } from '@vueuse/core'
import { mdToHtml } from '@/utils/markdown'
import { getAllMatches } from '@/utils/helpers'
import shortid from 'shortid'
const contextedPrefix = 'c@'
const baseNotes = reactive<{ [noteId: string]: BaseNote }>({})
const baseNotes = ref<BaseNote[]>([])
export const notes = computed<Note[]>(() => {
// extract links and add word count
return baseNotes.value as Note[]
return Object.entries(baseNotes)
.map(([_, note]) => ({
...note,
wordCount: note.content.split(' ').length,
}))
.sort((a, b) => b.modified - a.modified) as Note[]
})
watch(notes, () => {
if (notes.value.length > 0 && !activeNote.value)
activeNote.value = rootNote.value
})
export const activeNote = ref<Note>()
@@ -16,16 +29,11 @@ watch(activeNote, () => {
export const rootNote = computed<Note | undefined>(() =>
notes.value.find((note: Note) => note.isRoot)
)
watch(
rootNote,
() => {
if (rootNote.value) activeNote.value = rootNote.value
},
{ immediate: true }
)
export const setDefaultNotes = (defaultNotes: BaseNote[]) => {
baseNotes.value = defaultNotes
defaultNotes.forEach((defaultNote) => {
baseNotes[defaultNote.id] = defaultNote
})
}
export const getNoteById = (noteId: string) => {
@@ -35,7 +43,7 @@ export const getNoteById = (noteId: string) => {
export const findNotes = (query: string): Note[] => {
const removeMdFromText = (mdText: string): string => {
const div = document.createElement('div')
div.innerHTML = mdToHtml(mdText, 'c@', getNoteById)
div.innerHTML = mdToHtml(mdText, contextedPrefix, getNoteById)
const textWithoutMd = div.textContent || div.innerText || ''
return textWithoutMd
}
@@ -47,3 +55,80 @@ export const findNotes = (query: string): Note[] => {
return matchTitle || matchContent
})
}
export const updateNote = (noteId: string, note: Note) => {
const updatedNote: Note = {
...note,
modified: new Date().getTime(),
}
baseNotes[noteId] = updatedNote
}
export const addNote = (
title: string,
content: string,
goToNote: boolean = false
) => {
const id = shortid.generate()
const newNote: BaseNote = {
id,
title,
content,
created: new Date().getTime(),
modified: new Date().getTime(),
}
baseNotes[id] = newNote
if (goToNote)
activeNote.value = notes.value.find((note) => note.id === newNote.id)
}
const getNoteLinksByNoteId = (noteId: string): string[] => {
const note = baseNotes[noteId]
const regex = /\[\[(.*?)\]\]/g
const links = getAllMatches(regex, note.content || '')
.map((to) => notes.value.find((note) => note.title === to[1])?.id || '')
.filter((noteId) => Object.keys(baseNotes).includes(noteId))
return [...links]
}
interface NoteRelations {
id: string
to: string[]
from: string[]
}
interface NotesRelations {
[noteId: string]: {
to: string[]
from: string[]
}
}
export const notesRelations = computed(() => {
const noteIds = Object.keys(baseNotes)
const relations = noteIds
.filter((id) => id !== undefined)
.map((id) => {
const to = getNoteLinksByNoteId(id)
return { id, to }
})
.map((noteRelations, _, notesRelations): NoteRelations => {
const from = [...notesRelations]
.map((noteRelation) =>
noteRelation.to
.filter((toId) => toId === noteRelations.id)
.map(() => noteRelation.id)
)
.reduce((arr, elem) => arr.concat(elem), [])
.filter((value, index, self) => self.indexOf(value) === index)
return {
id: noteRelations.id,
to: noteRelations.to,
from,
}
})
.reduce((notes, { id, to, from }) => {
notes[id] = { to, from }
return notes
}, {} as NotesRelations)
return relations
})