update active note

This commit is contained in:
2023-05-13 07:29:27 +02:00
parent f96d139b55
commit 9b75843a9d
6 changed files with 19 additions and 15 deletions

View File

@@ -7,6 +7,7 @@ import {
deleteNote, deleteNote,
rootNote, rootNote,
setRootNote, setRootNote,
setActiveNote,
} from '@/composables/useNotes' } from '@/composables/useNotes'
const props = defineProps<{ const props = defineProps<{
@@ -39,7 +40,7 @@ const references = computed<Note[]>(() => {
const del = async (closeModal: () => Promise<Boolean>) => { const del = async (closeModal: () => Promise<Boolean>) => {
await closeModal() await closeModal()
activeNote.value = rootNote.value setActiveNote(rootNote.value?.id)
deleteNote(props.note.id) deleteNote(props.note.id)
} }

View File

@@ -13,7 +13,7 @@ import ListPlugin from '@ckeditor/ckeditor5-list/src/list'
import AutoformatPlugin from '@ckeditor/ckeditor5-autoformat/src/autoformat' import AutoformatPlugin from '@ckeditor/ckeditor5-autoformat/src/autoformat'
import ContextedPlugin from '@/ckeditor/ContextedPlugin' import ContextedPlugin from '@/ckeditor/ContextedPlugin'
import { mdToHtml } from '@/utils/markdown' import { mdToHtml } from '@/utils/markdown'
import { activeNote, getNoteByTitle } from '@/composables/useNotes' import { getNoteByTitle, setActiveNote } from '@/composables/useNotes'
import Autocomplete from '@/components/Autocomplete.vue' import Autocomplete from '@/components/Autocomplete.vue'
const props = defineProps<{ note: Note }>() const props = defineProps<{ note: Note }>()
@@ -58,7 +58,7 @@ const editorElement = ref<HTMLInputElement | null>(null)
const handleClick = ({ data }: { data: any }) => { const handleClick = ({ data }: { data: any }) => {
const noteTitle = data.domTarget.textContent as string const noteTitle = data.domTarget.textContent as string
const note = getNoteByTitle(noteTitle) const note = getNoteByTitle(noteTitle)
if (note) activeNote.value = note if (note) setActiveNote(note.id)
} }
const autocompleteRef = ref<InstanceType<typeof Autocomplete> | null>(null) const autocompleteRef = ref<InstanceType<typeof Autocomplete> | null>(null)

View File

@@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { activeNote } from '@/composables/useNotes' import { setActiveNote } from '@/composables/useNotes'
const props = defineProps<{ const props = defineProps<{
references: Note[] references: Note[]
}>() }>()
@@ -15,7 +15,7 @@ const props = defineProps<{
</span> </span>
</li> </li>
<li v-for="reference in props.references"> <li v-for="reference in props.references">
<a class="rounded-md" @click="activeNote = reference"> <a class="rounded-md" @click="setActiveNote(reference.id)">
<i class="far fa-file-alt fa-fw" /> <i class="far fa-file-alt fa-fw" />
{{ reference.title }} {{ reference.title }}
</a> </a>

View File

@@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { notes, findNotes, activeNote } from '@/composables/useNotes' import { notes, findNotes, setActiveNote } from '@/composables/useNotes'
const active = ref(false) const active = ref(false)
watch(active, () => { watch(active, () => {
@@ -15,7 +15,7 @@ const results = computed<Note[]>(() => {
}) })
const goToNote = (note: Note) => { const goToNote = (note: Note) => {
activeNote.value = note setActiveNote(note.id)
active.value = false active.value = false
if (queryElem.value) queryElem.value.blur() if (queryElem.value) queryElem.value.blur()
} }

View File

@@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { rootNote, notes, activeNote } from '@/composables/useNotes' import { rootNote, notes, setActiveNote } from '@/composables/useNotes'
const props = defineProps<{ const props = defineProps<{
viewModes: ViewMode[] viewModes: ViewMode[]
@@ -20,7 +20,7 @@ const emit = defineEmits<{
<template #items> <template #items>
<SideBarMenuItem <SideBarMenuItem
icon="fas fa-fw fa-home" icon="fas fa-fw fa-home"
@click="activeNote = rootNote" @click="setActiveNote(rootNote?.id)"
:title="rootNote?.title" :title="rootNote?.title"
> >
{{ rootNote?.title }} {{ rootNote?.title }}
@@ -49,7 +49,7 @@ const emit = defineEmits<{
<SideBarMenuItem <SideBarMenuItem
v-for="note in notes" v-for="note in notes"
icon="far fa-file-alt fa-fw" icon="far fa-file-alt fa-fw"
@click="activeNote = note" @click="setActiveNote(note.id)"
:title="rootNote?.title" :title="rootNote?.title"
> >
{{ note.title }} {{ note.title }}

View File

@@ -14,17 +14,20 @@ export const notes = computed<Note[]>(() => {
.sort((a, b) => b.modified - a.modified) as Note[] .sort((a, b) => b.modified - a.modified) as Note[]
}) })
watch(notes, () => { watch(notes, () => {
if (notes.value.length > 0 && !activeNote.value) activeNote.value = rootNote.value if (notes.value.length > 0 && !activeNote.value) setActiveNote(rootNote.value?.id)
}) })
export const activeNote = ref<Note>() const activeNoteId = ref<string>()
export const activeNote = computed(() =>
notes.value.find((note) => note.id === activeNoteId.value)
)
watch(activeNote, () => { watch(activeNote, () => {
if (activeNote.value) { if (activeNote.value) {
useTitle(`${activeNote.value.title} | Contexted`) useTitle(`${activeNote.value.title} | Contexted`)
} }
}) })
export const setActiveNote = (noteId: string) => { export const setActiveNote = (noteId: string | undefined) => {
activeNote.value = notes.value.find((note) => note.id === noteId) if (noteId) activeNoteId.value = noteId
} }
export const rootNote = computed<Note | undefined>(() => { export const rootNote = computed<Note | undefined>(() => {
@@ -95,7 +98,7 @@ export const addNote = (title: string, content: string, goToNote: boolean = fals
modified: new Date().getTime(), modified: new Date().getTime(),
} }
baseNotes[id] = newNote baseNotes[id] = newNote
if (goToNote) activeNote.value = notes.value.find((note) => note.id === newNote.id) if (goToNote) setActiveNote(id)
} }
export const deleteNote = (noteId: string) => { export const deleteNote = (noteId: string) => {