fix: preserve wiki folder path after creation
parent
25f3751138
commit
f7ef782d58
|
|
@ -772,6 +772,10 @@ import { hydrateProtectedFileImages, sanitizeMarkdownHTML } from '@/utils/securi
|
|||
import picturePreview from '@/components/picture-preview.vue'
|
||||
import WikiFolderActions from './WikiFolderActions.vue'
|
||||
import WikiRevisionDrawer from './WikiRevisionDrawer.vue'
|
||||
import {
|
||||
expandedWikiDirectoryPaths,
|
||||
expandWikiDirectoryPath,
|
||||
} from './wikiDirectoryState'
|
||||
import { getKnowledgeDetails } from '@/api/knowledge-base'
|
||||
import { createSessions } from '@/api/chat'
|
||||
import ChatView from '@/views/chat/index.vue'
|
||||
|
|
@ -2157,11 +2161,28 @@ async function loadCategoriesForType(type: string, opts: { reset?: boolean; pare
|
|||
// pages for one tab. Used after a structural mutation (move page, create /
|
||||
// rename / delete folder) so the tree reflects the new layout authoritatively
|
||||
// instead of guessing at the optimistic delta.
|
||||
async function reloadDirectoryForType(type: string) {
|
||||
async function reloadDirectoryForType(
|
||||
type: string,
|
||||
opts: { preserveDirectoryState?: boolean } = {},
|
||||
) {
|
||||
const refreshFlatList = sidebarViewMode.value === 'list' || ensureBucket(type).flatInitialized
|
||||
clearDirectoryStateForType(type)
|
||||
await loadPagesForType(type, { reset: true })
|
||||
const expandedPaths = opts.preserveDirectoryState
|
||||
? expandedWikiDirectoryPaths(type, collapsedDirectories.value, touchedDirectories.value)
|
||||
: []
|
||||
if (!opts.preserveDirectoryState) clearDirectoryStateForType(type)
|
||||
await loadPagesForType(type, {
|
||||
reset: true,
|
||||
preserveDirectoryState: opts.preserveDirectoryState,
|
||||
})
|
||||
await loadCategoriesForType(type, { reset: true })
|
||||
// Folder data is fetched one level at a time. Reload preserved paths in
|
||||
// parent-first order so each child request can resolve its parent folder id.
|
||||
for (const path of expandedPaths) {
|
||||
await Promise.all([
|
||||
loadPagesForType(type, { categoryPath: path }),
|
||||
loadCategoriesForType(type, { parentPath: path }),
|
||||
])
|
||||
}
|
||||
if (refreshFlatList) await loadFlatPagesForType(type, true)
|
||||
}
|
||||
|
||||
|
|
@ -2301,16 +2322,28 @@ async function confirmPendingMove() {
|
|||
// the API call, surface a toast, and reload the affected tab so the tree
|
||||
// reflects the new layout authoritatively.
|
||||
async function createFolder(parentId: string, parentPath: string[], name: string) {
|
||||
const type = activeTab.value
|
||||
const scrollTop = pageListRef.value?.scrollTop
|
||||
try {
|
||||
await createWikiFolder(props.knowledgeBaseId, parentId, name)
|
||||
MessagePlugin.success(t('knowledgeEditor.wikiBrowser.createFolderSuccess'))
|
||||
// Keep the parent expanded so the new child is visible.
|
||||
// Keep the current path expanded so refreshing the authoritative tree does
|
||||
// not collapse it and clamp the sidebar scroll position back to the root.
|
||||
if (parentPath.length > 0) {
|
||||
collapsedDirectories.value = new Set(
|
||||
[...collapsedDirectories.value].filter(k => k !== directoryPathKey(activeTab.value, parentPath)),
|
||||
const state = expandWikiDirectoryPath(
|
||||
type,
|
||||
parentPath,
|
||||
collapsedDirectories.value,
|
||||
touchedDirectories.value,
|
||||
)
|
||||
collapsedDirectories.value = state.collapsed
|
||||
touchedDirectories.value = state.touched
|
||||
}
|
||||
await reloadDirectoryForType(type, { preserveDirectoryState: true })
|
||||
await nextTick()
|
||||
if (activeTab.value === type && scrollTop !== undefined && pageListRef.value) {
|
||||
pageListRef.value.scrollTop = scrollTop
|
||||
}
|
||||
await reloadDirectoryForType(activeTab.value)
|
||||
} catch (e: any) {
|
||||
console.error('Failed to create wiki folder:', e)
|
||||
MessagePlugin.error(e?.message || t('knowledgeEditor.wikiBrowser.createFolderFailed'))
|
||||
|
|
@ -2397,7 +2430,10 @@ async function deleteFolder(folderId: string) {
|
|||
// checks work without another round-trip. Guard against concurrent
|
||||
// invocations for the same type (e.g. scroll event fires rapidly while
|
||||
// a network request is still in flight).
|
||||
async function loadPagesForType(type: string, opts: { reset?: boolean; categoryPath?: string[] } = {}) {
|
||||
async function loadPagesForType(
|
||||
type: string,
|
||||
opts: { reset?: boolean; categoryPath?: string[]; preserveDirectoryState?: boolean } = {},
|
||||
) {
|
||||
const bucket = ensureBucket(type)
|
||||
const categoryPath = opts.categoryPath || []
|
||||
const scopedToCategory = categoryPath.length > 0
|
||||
|
|
@ -2446,7 +2482,7 @@ async function loadPagesForType(type: string, opts: { reset?: boolean; categoryP
|
|||
bucket.items = batch
|
||||
bucket.nextPage = 2
|
||||
bucket.directoryPages = {}
|
||||
clearDirectoryStateForType(type)
|
||||
if (!opts.preserveDirectoryState) clearDirectoryStateForType(type)
|
||||
} else {
|
||||
const seenItems = new Set(bucket.items.map(p => p.id))
|
||||
for (const p of batch) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,70 @@
|
|||
import assert from 'node:assert/strict'
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { dirname, join } from 'node:path'
|
||||
import test from 'node:test'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
import {
|
||||
expandedWikiDirectoryPaths,
|
||||
expandWikiDirectoryPath,
|
||||
} from './wikiDirectoryState.ts'
|
||||
|
||||
test('keeps the current wiki directory path expanded without changing sibling state', () => {
|
||||
const collapsed = new Set([
|
||||
'knowledge:Guides',
|
||||
'knowledge:Guides/Setup',
|
||||
'knowledge:Reference',
|
||||
'summary:Guides',
|
||||
])
|
||||
const touched = new Set(['knowledge:Reference'])
|
||||
|
||||
const state = expandWikiDirectoryPath(
|
||||
'knowledge',
|
||||
['Guides', 'Setup'],
|
||||
collapsed,
|
||||
touched,
|
||||
)
|
||||
|
||||
assert.deepEqual([...state.collapsed].sort(), [
|
||||
'knowledge:Reference',
|
||||
'summary:Guides',
|
||||
])
|
||||
assert.deepEqual([...state.touched].sort(), [
|
||||
'knowledge:Guides',
|
||||
'knowledge:Guides/Setup',
|
||||
'knowledge:Reference',
|
||||
])
|
||||
assert.equal(collapsed.has('knowledge:Guides'), true)
|
||||
assert.equal(touched.has('knowledge:Guides'), false)
|
||||
})
|
||||
|
||||
test('returns expanded wiki paths in parent-first order for a refreshed tree', () => {
|
||||
const paths = expandedWikiDirectoryPaths(
|
||||
'knowledge',
|
||||
new Set(['knowledge:Reference']),
|
||||
new Set([
|
||||
'knowledge:Guides/Setup',
|
||||
'summary:Guides',
|
||||
'knowledge:Reference',
|
||||
'knowledge:Guides',
|
||||
]),
|
||||
)
|
||||
|
||||
assert.deepEqual(paths, [
|
||||
['Guides'],
|
||||
['Guides', 'Setup'],
|
||||
])
|
||||
})
|
||||
|
||||
test('folder creation refreshes data while preserving the current directory state', () => {
|
||||
const here = dirname(fileURLToPath(import.meta.url))
|
||||
const source = readFileSync(join(here, 'WikiBrowser.vue'), 'utf8')
|
||||
const createFolder = source.slice(
|
||||
source.indexOf('async function createFolder'),
|
||||
source.indexOf('// Inline rename:'),
|
||||
)
|
||||
|
||||
assert.match(createFolder, /expandWikiDirectoryPath/)
|
||||
assert.match(createFolder, /preserveDirectoryState:\s*true/)
|
||||
assert.match(createFolder, /scrollTop/)
|
||||
})
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
export interface WikiDirectoryState {
|
||||
collapsed: Set<string>
|
||||
touched: Set<string>
|
||||
}
|
||||
|
||||
function directoryPathKey(type: string, parts: string[]): string {
|
||||
return `${type}:${parts.join('/')}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Keep every directory in the current path expanded across a data refresh.
|
||||
* Marking the path as touched prevents default initialization from collapsing
|
||||
* it again when the refreshed folder and page batches arrive.
|
||||
*/
|
||||
export function expandWikiDirectoryPath(
|
||||
type: string,
|
||||
path: string[],
|
||||
collapsed: ReadonlySet<string>,
|
||||
touched: ReadonlySet<string>,
|
||||
): WikiDirectoryState {
|
||||
const nextCollapsed = new Set(collapsed)
|
||||
const nextTouched = new Set(touched)
|
||||
|
||||
for (let depth = 1; depth <= path.length; depth++) {
|
||||
const key = directoryPathKey(type, path.slice(0, depth))
|
||||
nextCollapsed.delete(key)
|
||||
nextTouched.add(key)
|
||||
}
|
||||
|
||||
return { collapsed: nextCollapsed, touched: nextTouched }
|
||||
}
|
||||
|
||||
/** Return expanded, user-touched paths in parent-first order for reloading. */
|
||||
export function expandedWikiDirectoryPaths(
|
||||
type: string,
|
||||
collapsed: ReadonlySet<string>,
|
||||
touched: ReadonlySet<string>,
|
||||
): string[][] {
|
||||
const prefix = `${type}:`
|
||||
return [...touched]
|
||||
.filter(key => key.startsWith(prefix) && !collapsed.has(key))
|
||||
.map(key => key.slice(prefix.length).split('/').filter(Boolean))
|
||||
.filter(path => path.length > 0)
|
||||
.sort((left, right) => left.length - right.length)
|
||||
}
|
||||
Loading…
Reference in New Issue