From 456e410d58a151232a6a712ae5de1a3aa539935e Mon Sep 17 00:00:00 2001 From: tenlee2012 Date: Thu, 10 Sep 2026 23:58:54 +0800 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AE=B6=E8=B0=B1?= =?UTF-8?q?=E6=A0=91=E6=90=9C=E7=B4=A2=E5=B9=B6=E6=B7=BB=E5=8A=A0=E6=97=A5?= =?UTF-8?q?=E5=B8=B8=E4=BA=B2=E5=B1=9E=E7=A7=B0=E5=91=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + THIRD_PARTY_NOTICES.md | 29 ++- package.json | 1 + .../desktop/e2e/tree-search-kinship.spec.ts | 58 +++++ packages/desktop/src/env.d.ts | 8 + .../src/features/tree/TreeView.test.ts | 166 ++++++++++++- .../features/tree/components/FamilyGraph.vue | 62 ++++- .../features/tree/components/TreeToolbar.vue | 59 ++++- .../tree/composables/useFamilyGraph.ts | 33 +++ .../src/features/tree/views/TreeView.vue | 78 +++++-- .../desktop/src/shared/domain/kinship.test.ts | 162 +++++++++++++ packages/desktop/src/shared/domain/kinship.ts | 220 ++++++++++++++++++ pnpm-lock.yaml | 9 + 13 files changed, 859 insertions(+), 27 deletions(-) create mode 100644 packages/desktop/e2e/tree-search-kinship.spec.ts create mode 100644 packages/desktop/src/shared/domain/kinship.test.ts create mode 100644 packages/desktop/src/shared/domain/kinship.ts diff --git a/README.md b/README.md index 927b7f4..c120edd 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ Android 端目前不提供 CLI、Codex Skill 安装或“AI 工具”页面。 - 记录人物的多种姓名、生卒信息、生平、职业、称谓、备注和头像。 - 整理亲生、收养、继亲、监护,以及婚姻、订婚、事实伴侣、分居、离异等关系。 - 从家谱树、人物列表和时间线浏览家庭资料。 +- 桌面家谱树支持部分姓名、字号搜索,高亮匹配人物并点击定位;选中人物后,在卡片和“人物与称呼”列表查看其对其他人物的日常称呼,如爸爸、妈妈、爷爷、奶奶、太奶奶。长幼不明时保留“兄弟”“姐妹”等合称;上四代及更远的直系祖辈显示“老祖宗”及代数。称呼仅根据已载入资料的最短关系路径计算,收养、继亲、监护、已结束的伴侣关系及信息不足的情况保留关系说明,不写入家谱资料。 - 管理地点、家庭事件、史料来源、引文与本地附件。 ### 核对、备份与迁移 diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 0af4310..076a1be 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -12,8 +12,33 @@ The desktop application uses: Branchloom does not relicense `elkjs`. Recipients may obtain its source and license from the upstream project above. -## Release requirement +## Kinship calculator + +The family tree's local kinship calculator uses `relationship.js` 1.2.9 +([source](https://github.com/mumuy/relationship)) under the MIT License: + +> Copyright (c) 2016 Haole Zheng +> +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in all +> copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +> SOFTWARE. + +## Release inventory Before publishing a binary release, generate and review a complete third-party license inventory from the exact npm and Cargo lockfiles used for that release. Include all required license texts, copyright notices, attributions, and source-availability statements with the distributed application or installer. -This file highlights a dependency with reciprocal redistribution requirements; it is not a substitute for the complete per-release inventory. +This file highlights selected runtime dependencies; it is not a substitute for the complete per-release inventory. diff --git a/package.json b/package.json index ea99ad7..8780a9b 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "cytoscape-elk": "2.3.0", "elkjs": "0.11.1", "pinia": "4.0.2", + "relationship.js": "1.2.9", "vue": "3.5.40", "vue-router": "5.2.0" }, diff --git a/packages/desktop/e2e/tree-search-kinship.spec.ts b/packages/desktop/e2e/tree-search-kinship.spec.ts new file mode 100644 index 0000000..3f8afa4 --- /dev/null +++ b/packages/desktop/e2e/tree-search-kinship.spec.ts @@ -0,0 +1,58 @@ +import { expect, test } from '@playwright/test' +import { expectNoRuntimeErrors, resetDemo, watchRuntimeErrors } from './helpers/demo' + +test.beforeEach(async ({ page }) => resetDemo(page)) + +for (const width of [1440, 900]) { + test(`partial-name search highlights and locates a person at ${width}px`, async ({ page }, testInfo) => { + const errors = watchRuntimeErrors(page) + await page.setViewportSize({ width, height: 1000 }) + const search = page.getByRole('searchbox', { name: '搜索跳转人物' }) + await search.fill('梅兰') + const results = page.getByRole('region', { name: '人物搜索结果' }) + await expect(results).toContainText('找到 1 位人物') + await expect(results.getByRole('button', { name: '定位王梅兰' })).toBeVisible() + await search.press('ArrowDown') + await expect(results.getByRole('button', { name: '定位王梅兰' })).toBeFocused() + await page.keyboard.press('Enter') + await expect(page.getByText('王梅兰对其他人物的称呼', { exact: true })).toBeVisible() + await expect(page.getByRole('complementary', { name: '为王梅兰添加关系' })).toHaveCount(0) + await expect(page.getByTestId('family-graph').locator('canvas').first()).toBeVisible() + await page.locator('.family-graph__directory summary').click() + const selected = page.getByRole('button', { name: '选择人物:王梅兰', exact: true }) + await expect(selected).toHaveClass(/is-search-match/) + await expect(selected).toHaveAttribute('aria-pressed', 'true') + await expect(selected).toContainText('本人') + await expect(page.locator('.app-topbar')).toBeVisible() + expect(await page.locator('html').evaluate((element) => element.scrollWidth)).toBeLessThanOrEqual(width) + await page.screenshot({ path: testInfo.outputPath('search-and-kinship.png') }) + + await search.fill('') + await expect(selected).not.toHaveClass(/is-search-match/) + await expect(results).toHaveCount(0) + await search.fill('查无此人的测试关键词') + await expect(results).toContainText('未找到匹配人物') + expectNoRuntimeErrors(errors) + }) +} + +test('keyboard selection changes the perspective and exposes everyday kinship terms', async ({ page }, testInfo) => { + const errors = watchRuntimeErrors(page) + await page.setViewportSize({ width: 1440, height: 1000 }) + await page.locator('.family-graph__directory summary').click() + const daughter = page.getByRole('button', { name: '选择人物:林晨', exact: true }) + await daughter.focus() + await page.keyboard.press('Enter') + await expect(page.getByText('林晨对其他人物的称呼', { exact: true })).toBeVisible() + await expect(page.getByRole('button', { name: '选择人物:林海', exact: true })).toContainText('爸爸') + await expect(page.getByRole('button', { name: '选择人物:陈芳', exact: true })).toContainText('妈妈') + await expect(page.getByRole('button', { name: '选择人物:林国强', exact: true })).toContainText('爷爷') + await expect(page.getByRole('button', { name: '选择人物:周素琴', exact: true })).toContainText('奶奶') + await page.screenshot({ path: testInfo.outputPath('everyday-kinship.png') }) + await page.getByRole('button', { name: '选择人物:林海', exact: true }).click() + await expect(daughter).toContainText('女儿') + await page.keyboard.press('Escape') + await expect(page.getByText('林海对其他人物的称呼', { exact: true })).toHaveCount(0) + await expect(daughter).not.toContainText('女儿') + expectNoRuntimeErrors(errors) +}) diff --git a/packages/desktop/src/env.d.ts b/packages/desktop/src/env.d.ts index 41c65b2..6f5f8cc 100644 --- a/packages/desktop/src/env.d.ts +++ b/packages/desktop/src/env.d.ts @@ -5,3 +5,11 @@ declare module 'cytoscape-elk' { const register: Ext export default register } + +declare module 'relationship.js' { + export default function relationship(options: { + text: string + sex: -1 | 0 | 1 + optimal: boolean + }): string[] +} diff --git a/packages/desktop/src/features/tree/TreeView.test.ts b/packages/desktop/src/features/tree/TreeView.test.ts index fe6fa5e..0fb7ccc 100644 --- a/packages/desktop/src/features/tree/TreeView.test.ts +++ b/packages/desktop/src/features/tree/TreeView.test.ts @@ -1,15 +1,18 @@ import { flushPromises, mount } from '@vue/test-utils' import { createPinia } from 'pinia' import { afterEach, describe, expect, it, vi } from 'vitest' +import cytoscape from 'cytoscape' import { createAppRouter } from '../../app/router' import { BrowserPrototypeRepository } from '../../shared/repository/BrowserPrototypeRepository' import { branchloomRepositoryKey } from '../../shared/repository/injection' import type { BranchloomRepository } from '../../shared/domain/types' import type { PrototypeStorage } from '../../shared/repository/storage' import { createDemoState } from '../../shared/fixtures/demoState' +import { buildKinshipDescriptions } from '../../shared/domain/kinship' import { buildVisibleGraph } from './model/buildVisibleGraph' import { buildFamilyAwarePositions, + applyGraphAnnotations, buildCytoscapeElements, createNodeTapController, familyGraphStylesheet, @@ -48,6 +51,7 @@ async function mountTree( const zoomIn = vi.fn() const zoomOut = vi.fn() const zoomTo = vi.fn() + const locate = vi.fn() const wrapper = mount(TreeView, { attachTo: document.body, props: { warningThreshold }, @@ -57,7 +61,7 @@ async function mountTree( stubs: { FamilyGraph: { name: 'FamilyGraph', - props: ['graph', 'density', 'selectedPersonId'], + props: ['graph', 'density', 'selectedPersonId', 'annotations'], emits: ['node-click', 'node-double-click', 'canvas-click', 'selected-node-anchor-change', 'zoom-change'], template: `
@@ -65,14 +69,14 @@ async function mountTree(
`, - methods: { fit, relayout, zoomIn, zoomOut, zoomTo }, + methods: { fit, relayout, zoomIn, zoomOut, zoomTo, locate }, }, }, }, }) wrappers.push(wrapper) await flushPromises() - return { wrapper, router, repository, fit, relayout, zoomIn, zoomOut, zoomTo } + return { wrapper, router, repository, fit, relayout, zoomIn, zoomOut, zoomTo, locate } } describe('TreeView', () => { @@ -295,6 +299,112 @@ describe('TreeView', () => { sort: 'name', }) expect(wrapper.get('select[name="personJump"]').text()).toContain('远房人物') + expect(wrapper.get('[aria-label="定位远房人物"]').isVisible()).toBe(true) + }) + + it('shows partial-name matches, highlights them without resetting the graph, and locates the result', async () => { + const { wrapper, router, locate } = await mountTree() + const canvas = wrapper.getComponent({ name: 'FamilyGraph' }) + const originalGraph = canvas.props('graph') + await wrapper.get('input[aria-label="搜索跳转人物"]').setValue(' 梅兰 ') + await flushPromises() + expect(wrapper.get('[aria-label="人物搜索结果"]').text()).toContain('找到 1 位人物') + expect(wrapper.get('[aria-label="定位王梅兰"]').isVisible()).toBe(true) + expect(canvas.props('annotations').searchPersonIds.has('person-wang-meilan')).toBe(true) + expect(canvas.props('graph')).toBe(originalGraph) + + await wrapper.get('[aria-label="定位王梅兰"]').trigger('click') + await flushPromises() + expect(canvas.props('selectedPersonId')).toBe('person-wang-meilan') + expect(locate).toHaveBeenCalledWith('person-wang-meilan') + if (!originalGraph.nodes.some(({ id }: { id: string }) => id === 'person-wang-meilan')) { + expect(router.currentRoute.value.query.personId).toBe('person-wang-meilan') + } + await wrapper.get('input[aria-label="搜索跳转人物"]').setValue('') + await flushPromises() + expect(canvas.props('annotations').searchPersonIds.size).toBe(0) + expect(wrapper.find('[aria-label="人物搜索结果"]').exists()).toBe(false) + }) + + it('locates an existing visible match with Enter and retains the current center', async () => { + const { wrapper, router, locate } = await mountTree() + const search = wrapper.get('input[aria-label="搜索跳转人物"]') + await search.setValue('晨晨') + await flushPromises() + await search.trigger('keydown', { key: 'Enter' }) + await flushPromises() + expect(locate).toHaveBeenCalledWith('person-lin-chen') + expect(wrapper.text()).toContain('中心人物:林海') + expect(router.currentRoute.value.query.personId).toBeUndefined() + }) + + it('waits for an off-screen search target’s new family slice before locating it', async () => { + const { wrapper, repository, locate } = await mountTree() + await wrapper.get('select[name="treeMode"]').setValue('ancestors') + await wrapper.get('input[name="generationsUp"]').setValue('0') + await flushPromises() + const graph = wrapper.getComponent({ name: 'FamilyGraph' }) + expect(graph.props('graph').nodes.some(({ id }: { id: string }) => id === 'person-wang-meilan')).toBe(false) + const slice = await repository.getTreeFamilySlice('project-demo-family', 'person-wang-meilan', { generationsUp: 0, generationsDown: 0 }) + let finishSlice!: (value: typeof slice) => void + vi.spyOn(repository, 'getTreeFamilySlice').mockReturnValueOnce(new Promise((resolve) => { finishSlice = resolve })) + await wrapper.get('input[aria-label="搜索跳转人物"]').setValue('梅兰') + await flushPromises() + await wrapper.get('[aria-label="定位王梅兰"]').trigger('click') + await flushPromises() + expect(locate).not.toHaveBeenCalled() + finishSlice(slice) + await flushPromises() + expect(locate).toHaveBeenCalledOnce() + expect(locate).toHaveBeenCalledWith('person-wang-meilan') + expect(graph.props('selectedPersonId')).toBe('person-wang-meilan') + }) + + it('keeps the graph available while search fails, allows retry, and reports empty results', async () => { + const { wrapper, repository } = await mountTree() + const listPeople = vi.spyOn(repository, 'listPeople').mockRejectedValueOnce(new Error('搜索暂时不可用')) + const search = wrapper.get('input[aria-label="搜索跳转人物"]') + await search.setValue('梅兰') + await flushPromises() + expect(wrapper.get('[aria-label="人物搜索结果"]').text()).toContain('搜索暂时不可用') + expect(wrapper.find('[data-testid="family-graph"]').exists()).toBe(true) + await wrapper.get('[aria-label="人物搜索结果"] button').trigger('click') + await flushPromises() + expect(wrapper.find('[aria-label="定位王梅兰"]').exists()).toBe(true) + expect(listPeople).toHaveBeenCalledTimes(2) + await search.setValue('不存在的测试人物') + await flushPromises() + expect(wrapper.get('[aria-label="人物搜索结果"]').text()).toContain('未找到匹配人物') + await search.trigger('keydown', { key: 'Escape' }) + expect(wrapper.find('[aria-label="人物搜索结果"]').exists()).toBe(false) + }) + + it('ignores stale search responses instead of replacing the newest matches', async () => { + const { wrapper, repository } = await mountTree() + const originalList = repository.listPeople.bind(repository) + let finishOldSearch!: (value: Awaited>) => void + vi.spyOn(repository, 'listPeople').mockImplementation((projectId, query) => query.search === '梅' + ? new Promise((resolve) => { finishOldSearch = resolve }) : originalList(projectId, query)) + const search = wrapper.get('input[aria-label="搜索跳转人物"]') + await search.setValue('梅') + expect(wrapper.get('[aria-label="人物搜索结果"]').text()).toContain('正在搜索') + await search.setValue('林晨') + await flushPromises() + finishOldSearch(await originalList('project-demo-family', { search: '梅', page: 1, pageSize: 100, sort: 'name' })) + await flushPromises() + expect(wrapper.find('[aria-label="定位林晨"]').exists()).toBe(true) + expect(wrapper.find('[aria-label="定位王梅兰"]').exists()).toBe(false) + }) + + it('shows the selected person’s kinship terms and clears them on deselection', async () => { + const { wrapper } = await mountTree() + await wrapper.get('[aria-label="选择林晨"]').trigger('click') + const canvas = wrapper.getComponent({ name: 'FamilyGraph' }) + expect(canvas.props('annotations').kinships.get('person-lin-hai')?.label).toBe('爸爸') + expect(canvas.props('annotations').kinships.get('person-chen-fang')?.label).toBe('妈妈') + expect(canvas.props('annotations').kinships.get('person-lin-guoqiang')?.label).toBe('爷爷') + await wrapper.get('[aria-label="点击画布空白处"]').trigger('click') + expect(canvas.props('annotations').kinships.size).toBe(0) }) it('controls mode, generations, density, collapse, fit and relayout accessibly', async () => { @@ -445,7 +555,7 @@ describe('FamilyGraph lifecycle', () => { }) it('wires graph callbacks, updates a live runtime, and destroys it on unmount', async () => { - const runtime = { destroy: vi.fn(), update: vi.fn(), focus: vi.fn(), fit: vi.fn(), relayout: vi.fn() } + const runtime = { destroy: vi.fn(), update: vi.fn(), focus: vi.fn(), annotate: vi.fn(), locate: vi.fn(), fit: vi.fn(), relayout: vi.fn() } const create = vi.fn().mockResolvedValue(runtime) const graph = buildVisibleGraph(createDemoState(), { centerPersonId: 'person-lin-hai', mode: 'combined', generationsUp: 2, generationsDown: 2, @@ -480,12 +590,24 @@ describe('FamilyGraph lifecycle', () => { expect(runtime.update).toHaveBeenCalled() await wrapper.setProps({ selectedPersonId: 'person-lin-chen' }) expect(runtime.focus).toHaveBeenLastCalledWith('person-lin-chen') + const state = createDemoState() + const annotations = { + searchPersonIds: new Set(['person-lin-hai']), + kinships: buildKinshipDescriptions(state.people, state.relationships, 'person-lin-chen'), + } + runtime.update.mockClear() + await wrapper.setProps({ annotations }) + expect(runtime.annotate).toHaveBeenLastCalledWith(annotations) + expect(runtime.update).not.toHaveBeenCalled() + expect(wrapper.get('[aria-label="选择人物:林海"]').text()).toContain('爸爸') + wrapper.vm.locate('person-lin-hai') + expect(runtime.locate).toHaveBeenLastCalledWith('person-lin-hai') wrapper.unmount() expect(runtime.destroy).toHaveBeenCalledOnce() }) it('applies the latest graph and density after an async adapter finishes creating', async () => { - const runtime = { destroy: vi.fn(), update: vi.fn(), fit: vi.fn(), relayout: vi.fn() } + const runtime = { destroy: vi.fn(), update: vi.fn(), annotate: vi.fn(), locate: vi.fn(), fit: vi.fn(), relayout: vi.fn() } let resolveRuntime!: (value: typeof runtime) => void const create = vi.fn(() => new Promise((resolve) => { resolveRuntime = resolve })) const first = buildVisibleGraph(createDemoState(), { @@ -504,11 +626,15 @@ describe('FamilyGraph lifecycle', () => { adapter: { create }, }, }) - await wrapper.setProps({ graph: latest, density: latestDensity }) + const latestAnnotations = { searchPersonIds: new Set(['person-lin-chen']), kinships: new Map() } + await wrapper.setProps({ graph: latest, density: latestDensity, annotations: latestAnnotations }) + wrapper.vm.locate('person-lin-chen') expect(runtime.update).not.toHaveBeenCalled() resolveRuntime(runtime) await flushPromises() expect(runtime.update).toHaveBeenCalledWith(latest, latestDensity) + expect(runtime.annotate).toHaveBeenLastCalledWith(latestAnnotations) + expect(runtime.locate).toHaveBeenLastCalledWith('person-lin-chen') wrapper.unmount() }) @@ -564,6 +690,34 @@ describe('FamilyGraph lifecycle', () => { }) describe('Cytoscape graph presentation', () => { + it('updates real node highlights and kinship labels without moving or zooming the graph', () => { + const state = createDemoState() + const graph = buildVisibleGraph(state, { + centerPersonId: 'person-lin-chen', mode: 'combined', generationsUp: 2, generationsDown: 2, collapsedPersonIds: new Set(), + }) + const density = { avatars: true, dates: true, places: true, relationships: true } + const cy = cytoscape({ headless: true, elements: buildCytoscapeElements(graph, density) }) + try { + cy.zoom(0.75) + cy.pan({ x: 45, y: 60 }) + const node = cy.getElementById('person-lin-hai') + const position = { ...node.position() } + applyGraphAnnotations(cy, graph, density, { + searchPersonIds: new Set(['person-lin-hai']), + kinships: buildKinshipDescriptions(state.people, state.relationships, 'person-lin-chen'), + }) + expect(node.hasClass('is-search-match')).toBe(true) + expect(node.data('label')).toContain('\n爸爸') + expect(node.data('kinship')).toBe('爸爸') + expect(cy.zoom()).toBe(0.75) + expect(cy.pan()).toEqual({ x: 45, y: 60 }) + expect(node.position()).toEqual(position) + applyGraphAnnotations(cy, graph, density, { searchPersonIds: new Set(), kinships: new Map() }) + expect(node.hasClass('is-search-match')).toBe(false) + expect(node.data('label')).not.toContain('爸爸') + } finally { cy.destroy() } + }) + it('keeps partner groups adjacent instead of allowing marriage lines to cross unrelated people', () => { const source = buildVisibleGraph(createDemoState(), { centerPersonId: 'person-lin-hai', mode: 'combined', generationsUp: 2, generationsDown: 2, diff --git a/packages/desktop/src/features/tree/components/FamilyGraph.vue b/packages/desktop/src/features/tree/components/FamilyGraph.vue index d6bc566..a8b5ebb 100644 --- a/packages/desktop/src/features/tree/components/FamilyGraph.vue +++ b/packages/desktop/src/features/tree/components/FamilyGraph.vue @@ -1,7 +1,13 @@