Foundry and discord bot
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
All checks were successful
Build and Deploy Nuxt / build (push) Successful in 1m51s
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script setup>
|
||||
import { ref, onMounted, watch } from 'vue';
|
||||
import { ref, onBeforeUnmount, onMounted, watch } from 'vue';
|
||||
import { emitter } from '~/services/Emitter';
|
||||
import Server from '~/services/Server';
|
||||
import { CreateWindow } from '~/services/Windows';
|
||||
@@ -40,8 +40,54 @@ watch(
|
||||
|
||||
onMounted(() => {
|
||||
loadExpandedFolders();
|
||||
emitter.on('note-created', handleNoteCreated);
|
||||
emitter.on('note-renamed', handleNoteRenamed);
|
||||
emitter.on('delete-note', handleNoteDeleted);
|
||||
emitter.on('folder-saved', handleFolderSaved);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
emitter.off('note-created', handleNoteCreated);
|
||||
emitter.off('note-renamed', handleNoteRenamed);
|
||||
emitter.off('delete-note', handleNoteDeleted);
|
||||
emitter.off('folder-saved', handleFolderSaved);
|
||||
});
|
||||
|
||||
// Keep this instance's folder content cache in sync with changes made anywhere in the app.
|
||||
// The cache entry for a folder only lives on the instance that renders it, so every
|
||||
// instance listens and updates whichever entries it owns.
|
||||
function handleNoteCreated(note) {
|
||||
if (!note) return;
|
||||
const folderId = note.folder?._id ?? note.folder ?? null;
|
||||
const entry = folderId ? folderContentCache.value[folderId] : null;
|
||||
if (entry) {
|
||||
entry.notes.unshift({ key: note._id, title: note.title, text: note.content || '' });
|
||||
}
|
||||
}
|
||||
|
||||
function handleNoteRenamed(data) {
|
||||
if (!data) return;
|
||||
for (const entry of Object.values(folderContentCache.value)) {
|
||||
const note = entry.notes.find((n) => n.key === data.key);
|
||||
if (note) note.title = data.title;
|
||||
}
|
||||
}
|
||||
|
||||
function handleNoteDeleted(noteKey) {
|
||||
for (const entry of Object.values(folderContentCache.value)) {
|
||||
entry.notes = entry.notes.filter((n) => n.key !== noteKey);
|
||||
}
|
||||
}
|
||||
|
||||
function handleFolderSaved(folder) {
|
||||
const folderId = folder?._id ?? folder?.id;
|
||||
if (!folderId) return;
|
||||
for (const entry of Object.values(folderContentCache.value)) {
|
||||
const subfolder = entry.subfolders.find((f) => (f._id ?? f.id) === folderId);
|
||||
if (subfolder) subfolder.name = folder.name;
|
||||
}
|
||||
}
|
||||
|
||||
function loadFolderContents(folderId) {
|
||||
if (loadingFolders.has(folderId)) return;
|
||||
loadingFolders.add(folderId);
|
||||
@@ -86,7 +132,7 @@ async function handleDropNoteOnFolder(folderId, event) {
|
||||
|
||||
try {
|
||||
await Server().post('/note/update', { id: noteKey, folder: folderId });
|
||||
emit('reload-notes');
|
||||
clearCacheAndReloadExpanded();
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
@@ -98,29 +144,23 @@ async function handleDropNoteOnRoot(event) {
|
||||
|
||||
try {
|
||||
await Server().post('/note/update', { id: noteKey, folder: null });
|
||||
emit('reload-notes');
|
||||
clearCacheAndReloadExpanded();
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
// Called on reload — clears cache so expanded folders refetch fresh content
|
||||
// Called on reload — clears cache and refetches what this instance is responsible for,
|
||||
// then bubbles the reload up so ancestors (and eventually the sidebar) refresh too
|
||||
function clearCacheAndReloadExpanded() {
|
||||
const folderIdsToReLoad = [];
|
||||
if (props.level === 0) {
|
||||
for (const id of props.expandedFolderIds) {
|
||||
if (folderContentCache.value[id]) folderIdsToReLoad.push(id);
|
||||
delete folderContentCache.value[id];
|
||||
}
|
||||
} else if (props.parentFolderId && folderContentCache.value[props.parentFolderId]) {
|
||||
folderIdsToReLoad.push(props.parentFolderId);
|
||||
delete folderContentCache.value[props.parentFolderId];
|
||||
}
|
||||
const loadedIds = Object.keys(folderContentCache.value);
|
||||
|
||||
// Force reactivity by creating a new object
|
||||
folderContentCache.value = {};
|
||||
|
||||
for (const id of folderIdsToReLoad) {
|
||||
for (const id of loadedIds) {
|
||||
loadFolderContents(id);
|
||||
}
|
||||
|
||||
emit('reload-notes');
|
||||
}
|
||||
|
||||
// Exposed to parent components
|
||||
@@ -180,7 +220,7 @@ function renameNote(note) { CreateWindow('rename_note', { title: note.title, key
|
||||
async function deleteNote(note) {
|
||||
try {
|
||||
const response = await Server().post('/note/delete', { id: note.key });
|
||||
if (response.data.status === 'ok') { emit('reload-notes'); emitter.emit('delete-note', note.key); }
|
||||
if (response.data.status === 'ok') emitter.emit('delete-note', note.key);
|
||||
} catch (error) {} finally {
|
||||
HideContextMenu();
|
||||
}
|
||||
@@ -190,7 +230,10 @@ function renameFolder(folderId, name) { CreateWindow('new_folder', { folderId, c
|
||||
|
||||
async function deleteFolder(folderId) {
|
||||
const response = await Server().post('/folder/delete', { id: folderId });
|
||||
if (response.data.status === 'ok') emit('reload-notes');
|
||||
if (response.data.status === 'ok') {
|
||||
emitter.emit('folder-deleted', folderId);
|
||||
clearCacheAndReloadExpanded();
|
||||
}
|
||||
HideContextMenu();
|
||||
}
|
||||
|
||||
@@ -198,16 +241,13 @@ async function moveNoteToFolder(note, folderId) {
|
||||
try {
|
||||
const response = await Server().post('/note/update', { id: note.key, folder: folderId || null });
|
||||
if (response.data.status !== 'ok') return;
|
||||
emit('reload-notes');
|
||||
clearCacheAndReloadExpanded();
|
||||
} catch (e) {}
|
||||
HideContextMenu();
|
||||
}
|
||||
|
||||
async function createNoteInFolder(folderId) {
|
||||
try {
|
||||
const response = await Server().post('/note/create', { title: 'New note', content: '', campaign: props.campaignId, folder: folderId });
|
||||
if (response.data.status === 'ok') emit('reload-notes');
|
||||
} catch (e) {}
|
||||
function createNoteInFolder(folderId) {
|
||||
CreateWindow('new_note', { campaign: props.campaignId, folder: folderId });
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -251,10 +291,6 @@ async function createNoteInFolder(folderId) {
|
||||
@reload-notes="clearCacheAndReloadExpanded"
|
||||
@toggle-expanded-folder="toggleExpanded"
|
||||
/>
|
||||
|
||||
<div v-if="expandedFolderIds.includes(folder._id) && folderContentCache[folder._id] !== undefined && (getFolderNotes(folder._id).length === 0 && getFolderSubfolders(folder._id).length === 0)" class="sidebar-state">
|
||||
Empty folder
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user