Added more text functions

This commit is contained in:
2026-01-26 21:11:16 +01:00
parent c87212d54a
commit ca2020b9c6
7 changed files with 335 additions and 286 deletions

View File

@@ -1,4 +1,3 @@
// composables/useWikiTree.ts
export interface WikiPageItem {
id: string
parentId: string | null
@@ -12,12 +11,12 @@ export interface WikiPageItem {
export const useWikiTree = () => {
const { $api } = useNuxtApp()
// State
// STATE
const items = useState<WikiPageItem[]>('wiki-items', () => [])
const isLoading = useState<boolean>('wiki-loading', () => false)
const isSidebarOpen = useState<boolean>('wiki-sidebar-open', () => true)
// --- Computed: Tree Logic ---
// COMPUTED TREE
const tree = computed(() => {
const rawItems = items.value || []
if (!rawItems.length) return []
@@ -25,12 +24,12 @@ export const useWikiTree = () => {
const roots: WikiPageItem[] = []
const lookup: Record<string, WikiPageItem> = {}
// Clone & Init
// Init Lookup
rawItems.forEach(item => {
lookup[item.id] = { ...item, children: [] }
})
// Build Tree
// Build Hierarchy
rawItems.forEach(item => {
const node = lookup[item.id]
if (item.parentId && lookup[item.parentId]) {
@@ -40,7 +39,7 @@ export const useWikiTree = () => {
}
})
// Sort: Folders first, then Alphabet
// Sort: Folders first, then Alphabetical
const sortNodes = (nodes: WikiPageItem[]) => {
nodes.sort((a, b) => {
if (a.isFolder !== b.isFolder) return a.isFolder ? -1 : 1
@@ -55,47 +54,52 @@ export const useWikiTree = () => {
return roots
})
// --- Actions ---
// ACTIONS
const loadTree = async () => {
isLoading.value = true
try {
const data = await $api<WikiPageItem[]>('/api/wiki/tree', { method: 'GET' })
items.value = data
} catch (e) { console.error(e) }
finally { isLoading.value = false }
} catch (e) {
console.error('Wiki Tree Load Error', e)
} finally {
isLoading.value = false
}
}
// Zentralisierte Erstellen-Funktion
// Rein API-basierte Funktion ohne UI-Logik
const createItem = async (title: string, parentId: string | null, isFolder: boolean) => {
try {
const newItem = await $api('/api/wiki', {
method: 'POST',
body: { title, parentId, isFolder }
})
await loadTree() // Neu laden
await loadTree()
return newItem
} catch (e) {
alert('Fehler beim Erstellen')
return null
throw e // Fehler weiterwerfen, damit UI reagieren kann
}
}
// Löschen Funktion
// Rein API-basierte Funktion ohne UI-Logik
const deleteItem = async (id: string) => {
if (!confirm('Wirklich löschen? Alle Unterseiten werden ebenfalls gelöscht.')) return
try {
await $api(`/api/wiki/${id}`, { method: 'DELETE' })
await loadTree()
return true
} catch (e) {
alert('Fehler beim Löschen')
return false
throw e
}
}
return {
tree, items, isLoading, isSidebarOpen,
loadTree, createItem, deleteItem
tree,
items,
isLoading,
isSidebarOpen,
loadTree,
createItem,
deleteItem
}
}