From 11b30fdd611dfc53fe9372b70ac111b53db859b5 Mon Sep 17 00:00:00 2001 From: nullkey Date: Mon, 6 Apr 2026 23:32:02 +0800 Subject: [PATCH] feat(frontend): tree view resource selector with cascading selection - Render resources as expandable tree with parent-child indentation - Cascade check/uncheck to all descendants - Add has_children to Resource API type - Add 'untitled' i18n key for en-US, zh-CN, ko-KR, ru-RU --- frontend/src/api/datasource/index.ts | 1 + frontend/src/i18n/locales/en-US.ts | 1 + frontend/src/i18n/locales/ko-KR.ts | 1 + frontend/src/i18n/locales/ru-RU.ts | 1 + frontend/src/i18n/locales/zh-CN.ts | 1 + .../settings/DataSourceEditorDialog.vue | 182 ++++++++++++++++-- 6 files changed, 175 insertions(+), 12 deletions(-) diff --git a/frontend/src/api/datasource/index.ts b/frontend/src/api/datasource/index.ts index 8681dc25..a5a47895 100644 --- a/frontend/src/api/datasource/index.ts +++ b/frontend/src/api/datasource/index.ts @@ -54,6 +54,7 @@ export interface Resource { description: string url: string parent_id?: string + has_children?: boolean } // --- API calls --- diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index 3d492e35..fa8f2b23 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -3449,6 +3449,7 @@ export default { connectionFailed: 'Connection failed', isRequired: 'is required', resourceHint: 'Select the spaces or folders to sync', + untitled: 'Untitled', resourceLoadFailed: 'Failed to load resources', noResources: 'No wiki spaces found', noResourcesDesc: 'The app needs wiki access via a group chat to fetch content', diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index 64ac1be1..ef66cfae 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -3501,6 +3501,7 @@ export default { connectionFailed: "연결 실패", isRequired: "은(는) 필수입니다", resourceHint: "동기화할 공간/폴더를 선택하세요", + untitled: "제목 없음", resourceLoadFailed: "리소스 목록 로드 실패", noResources: "동기화 가능한 위키 공간을 찾을 수 없습니다", noResourcesHint: "먼저 데이터 소스를 저장하고 권한 설정을 확인하세요", diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index e766094d..a1d153d3 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -3408,6 +3408,7 @@ export default { connectionFailed: 'Подключение не удалось', isRequired: 'обязательно для заполнения', resourceHint: 'Выберите пространства или папки для синхронизации', + untitled: 'Без названия', resourceLoadFailed: 'Не удалось загрузить список ресурсов', noResources: 'Пространства вики не найдены', noResourcesHint: 'Сначала сохраните источник данных и проверьте права доступа', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index 69f522c2..926d63ee 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -3448,6 +3448,7 @@ export default { connectionFailed: "连接失败", isRequired: "为必填项", resourceHint: "选择要同步的内容空间/文件夹", + untitled: "无标题", resourceLoadFailed: "加载资源列表失败", noResources: "未找到可同步的知识库空间", noResourcesDesc: "应用需要通过群聊获得知识库访问权限才能拉取内容", diff --git a/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue index b93a960a..919323b8 100644 --- a/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue +++ b/frontend/src/views/knowledge/settings/DataSourceEditorDialog.vue @@ -47,6 +47,78 @@ const form = ref({ const resources = ref([]) const loadingResources = ref(false) const selectedResourceIds = ref([]) +const expandedResourceIds = ref(new Set()) + +// Shared children/parent indexes — used by tree rendering and selection logic +const childrenMap = computed(() => { + const map = new Map() + for (const r of resources.value) { + if (r.parent_id) { + const siblings = map.get(r.parent_id) + if (siblings) siblings.push(r) + else map.set(r.parent_id, [r]) + } + } + return map +}) + +const parentMap = computed(() => { + const map = new Map() + for (const r of resources.value) { + if (r.parent_id) map.set(r.external_id, r.parent_id) + } + return map +}) + +// `selectedResourceIds` is a MINIMAL COVER SET: only the roots of fully-selected +// subtrees. Sending this to the backend gives "sync these IDs and all descendants" +// semantics — including any pages added later under a selected parent. +type CheckState = 'checked' | 'indeterminate' | 'unchecked' + +const checkStates = computed(() => { + const states = new Map() + const cover = new Set(selectedResourceIds.value) + + // Single post-order walk: a node is `checked` if itself or any ancestor is + // in the cover set; otherwise `indeterminate` if any descendant is checked; + // otherwise `unchecked`. Returns whether the subtree contains a checked node. + function walk(node: Resource, ancestorChecked: boolean): boolean { + const selfChecked = ancestorChecked || cover.has(node.external_id) + let descendantChecked = false + for (const c of childrenMap.value.get(node.external_id) || []) { + if (walk(c, selfChecked)) descendantChecked = true + } + if (selfChecked) states.set(node.external_id, 'checked') + else states.set(node.external_id, descendantChecked ? 'indeterminate' : 'unchecked') + return selfChecked || descendantChecked + } + for (const r of resources.value) { + if (!r.parent_id) walk(r, false) + } + return states +}) + +function toggleExpand(id: string) { + const next = new Set(expandedResourceIds.value) + if (next.has(id)) next.delete(id) + else next.add(id) + expandedResourceIds.value = next +} + +const visibleTree = computed(() => { + const roots = resources.value.filter(r => !r.parent_id) + const result: { resource: Resource; depth: number }[] = [] + function walk(items: Resource[], depth: number) { + for (const r of items) { + result.push({ resource: r, depth }) + if (r.has_children && expandedResourceIds.value.has(r.external_id)) { + walk(childrenMap.value.get(r.external_id) || [], depth + 1) + } + } + } + walk(roots, 0) + return result +}) // Connection test const testing = ref(false) @@ -100,7 +172,7 @@ const connectorDefs = computed(() => [ }, { type: 'notion', - available: false, + available: true, docUrl: 'https://www.notion.so/my-integrations', permissionDocUrl: '', permissionPageUrl: '', @@ -228,13 +300,74 @@ async function loadResources() { loadingResources.value = false } -function toggleResource(id: string) { - const idx = selectedResourceIds.value.indexOf(id) - if (idx >= 0) { - selectedResourceIds.value.splice(idx, 1) - } else { - selectedResourceIds.value.push(id) +function getDescendantIds(id: string): string[] { + const ids: string[] = [] + const children = childrenMap.value.get(id) || [] + for (const c of children) { + ids.push(c.external_id) + ids.push(...getDescendantIds(c.external_id)) } + return ids +} + +function getAncestorChain(id: string): string[] { + const chain = [id] + for (let p = parentMap.value.get(id); p; p = parentMap.value.get(p)) { + chain.push(p) + } + return chain +} + +function isCovered(id: string, cover: Set): boolean { + for (let cur: string | undefined = id; cur; cur = parentMap.value.get(cur)) { + if (cover.has(cur)) return true + } + return false +} + +function checkResource(id: string, cover: Set) { + if (isCovered(id, cover)) return + const descendants = new Set(getDescendantIds(id)) + for (const d of [...cover]) { + if (descendants.has(d)) cover.delete(d) + } + cover.add(id) +} + +// Removes id from the cover set. If id is covered transitively (an ancestor is +// in the cover set), the ancestor is replaced with explicit entries for each +// sibling along the path so the rest of the subtree stays selected. +function uncheckResource(id: string, cover: Set) { + const chain = getAncestorChain(id) // [id, parent, ..., root] + let highestIdx = -1 + for (let i = chain.length - 1; i >= 0; i--) { + if (cover.has(chain[i])) { highestIdx = i; break } + } + if (highestIdx > 0) { + cover.delete(chain[highestIdx]) + for (let i = highestIdx; i > 0; i--) { + const parent = chain[i] + const next = chain[i - 1] + for (const sib of childrenMap.value.get(parent) || []) { + if (sib.external_id !== next) cover.add(sib.external_id) + } + } + } + cover.delete(id) + const descendants = new Set(getDescendantIds(id)) + for (const d of [...cover]) { + if (descendants.has(d)) cover.delete(d) + } +} + +function toggleResource(id: string) { + const cover = new Set(selectedResourceIds.value) + if ((checkStates.value.get(id) || 'unchecked') === 'unchecked') { + checkResource(id, cover) + } else { + uncheckResource(id, cover) + } + selectedResourceIds.value = [...cover] } function validateStep1Fields(): boolean { @@ -469,18 +602,28 @@ const stepTitles = computed(() => [
+ + + +
-
{{ r.name }}
+
{{ r.name || t('datasource.untitled') }}
{{ resourceTypeLabel(r.type) }} {{ r.description }} @@ -766,7 +909,22 @@ const stepTitles = computed(() => [ .ds-dialog-footer { display: flex; justify-content: flex-end; gap: 8px; margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--td-border-level-2-color); } /* --- Step 2: resource list --- */ -.ds-resource-list { max-height: 320px; overflow-y: auto; display: flex; flex-direction: column; gap: 4px; } +.ds-resource-list { max-height: 400px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; } + +.ds-expand-btn { + display: flex; + align-items: center; + justify-content: center; + width: 20px; + height: 20px; + border-radius: 4px; + cursor: pointer; + color: var(--td-text-color-secondary); + flex-shrink: 0; + transition: background 0.15s; +} +.ds-expand-btn:hover { background: var(--td-bg-color-component-hover); } +.ds-expand-placeholder { width: 20px; flex-shrink: 0; } .ds-resource-row { display: flex;