Merge PR #662: fix(native): 文件 tab 图标不再依赖插件侧记录(Fixes #660)
# Conflicts: # src/client/native/index.ts
This commit is contained in:
commit
4e02951fb5
3 files changed
+197
-4
No files matched your search
@@ -269,7 +269,17 @@ export function registerNativeSurface(deps: NativeSurfaceDeps): () => void {
|
||||
disposers.push(ctx.slots.inject('sidebar.right.pane.tab.title', () => ctx.slots.register({
|
||||
name: 'sidebar.right.pane.tab.title',
|
||||
key: id,
|
||||
inject: () => ({ records, service, descriptorId: injected.descriptorId }),
|
||||
// The chip draws a FILE tab's own glyph, so it needs the same
|
||||
// address-derived seed the body gets: a file tab's address names the
|
||||
// file it shows, and the chip must be able to draw it BEFORE — and
|
||||
// without — the plugin-side record, which exists only after the body
|
||||
// has rendered (and is gone again once the body unmounts).
|
||||
inject: () => ({
|
||||
records,
|
||||
service,
|
||||
descriptorId: injected.descriptorId,
|
||||
...(params.paramsOf === undefined ? {} : { paramsOf: params.paramsOf }),
|
||||
}),
|
||||
}, NativeTabTitle)))
|
||||
} catch (error) {
|
||||
for (const dispose of disposers) disposeSafely(dispose, `native tab slot "${id}"`)
|
||||
|
||||
@@ -407,6 +407,17 @@ export interface NativeTitleInjected {
|
||||
readonly service: BetterSidebarService
|
||||
/** The descriptor id this title belongs to (one registration per descriptor). */
|
||||
readonly descriptorId: string
|
||||
/**
|
||||
* The seed fields the native RECORD itself carries, when the type declares
|
||||
* them (a file address names the file the tab shows).
|
||||
*
|
||||
* The chip must read the file's path from here and not only from
|
||||
* {@link NativeTabRecords}: the plugin-side record is minted by the tab
|
||||
* BODY's render — one commit after the chip first draws — and it is dropped
|
||||
* again when the body unmounts, so a selected file tab used to fall back to
|
||||
* its descriptor's generic glyph every time it was activated.
|
||||
*/
|
||||
readonly paramsOf?: (info: NativeTabInfo) => NativeTabParams | undefined
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -424,8 +435,9 @@ export interface NativeTitleInjected {
|
||||
* cannot drift apart.
|
||||
*/
|
||||
export function NativeTabTitle(props: NativeTitleInjected & NativeBodyFrameworkProps): ReactNode {
|
||||
const { records, service, descriptorId, useTabInfo } = props
|
||||
const nativeTab = useTabInfo().tab
|
||||
const { records, service, descriptorId, paramsOf, useTabInfo } = props
|
||||
const info = useTabInfo()
|
||||
const nativeTab = info.tab
|
||||
const version = useSyncExternalStore(
|
||||
listener => records.subscribe(listener),
|
||||
() => records.versionOf(nativeTab.id),
|
||||
@@ -436,7 +448,12 @@ export function NativeTabTitle(props: NativeTitleInjected & NativeBodyFrameworkP
|
||||
// icon itself is derived from the record, never stored.
|
||||
void version
|
||||
const descriptor = service.getTab(descriptorId) ?? service.getTab(record?.tab.type ?? nativeTab.kind)
|
||||
const path = record?.tab.path
|
||||
// The record's own path wins (an in-place switch stores it there, and the
|
||||
// native address of a page kind cannot carry it); the address-derived path
|
||||
// is what keeps the glyph correct when the record is not there YET (the tab
|
||||
// was just activated and the body has not rendered) or has already been
|
||||
// dropped (the body unmounted when the tab was switched away).
|
||||
const path = record?.tab.path ?? paramsOf?.(info)?.path
|
||||
const icon = path !== undefined && descriptorId === EDITOR_KIND
|
||||
? service.fileIcon(path, CHIP_ICON_SIZE)
|
||||
: undefined
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
// @vitest-environment jsdom
|
||||
/**
|
||||
* The native tab CHIP's file glyph (`NativeTabTitle`): a file tab must draw
|
||||
* the FILE's own icon from its very first frame — including while the
|
||||
* plugin-side record does not exist yet.
|
||||
*
|
||||
* That record is minted by the tab BODY's render (one commit after the chip
|
||||
* first draws) and dropped again when the body unmounts, so a chip that
|
||||
* derived the glyph from the record alone fell back to its descriptor's
|
||||
* generic glyph every time a file tab was activated: the selected tab showed
|
||||
* the folder artwork while the inactive ones (rendered later, with the record
|
||||
* in place) showed the right file icon.
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { createElement } from 'react'
|
||||
import { createRoot, type Root } from 'react-dom/client'
|
||||
import { act } from 'react-dom/test-utils'
|
||||
import {
|
||||
NativeTabTitle,
|
||||
createNativeTabRecords,
|
||||
type NativeTabInfo,
|
||||
type NativeTabParams,
|
||||
} from '../src/client/native/tab-adapter.tsx'
|
||||
import { createBetterSidebarService } from '../src/client/service.ts'
|
||||
import { createSidebarStore } from '../src/client/state.ts'
|
||||
|
||||
const scope = { sessionId: 's1', cwd: '/work' }
|
||||
|
||||
/** One native tab record, as the host hands it to a title registration. */
|
||||
function nativeInfo(id: string, kind: string, title: string, contentId: string): NativeTabInfo {
|
||||
return {
|
||||
tab: {
|
||||
id,
|
||||
kind,
|
||||
title,
|
||||
contentId,
|
||||
visible: true,
|
||||
navigation: { address: contentId, params: undefined, revision: 0 },
|
||||
signal: new AbortController().signal,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** A service whose editor descriptor has a marker glyph, plus two file icons. */
|
||||
function createService(): ReturnType<typeof createBetterSidebarService> {
|
||||
const service = createBetterSidebarService(createSidebarStore())
|
||||
service.registerTab({
|
||||
id: 'editor',
|
||||
title: () => 'Files',
|
||||
icon: (size: number) => createElement('i', { 'data-type-icon': size }),
|
||||
component: () => createElement('div'),
|
||||
})
|
||||
service.registerFileIcon({
|
||||
id: 'test:md',
|
||||
exts: ['md'],
|
||||
icon: () => createElement('i', { 'data-file-glyph': 'md' }),
|
||||
})
|
||||
service.registerFileIcon({
|
||||
id: 'test:txt',
|
||||
exts: ['txt'],
|
||||
icon: () => createElement('i', { 'data-file-glyph': 'txt' }),
|
||||
})
|
||||
return service
|
||||
}
|
||||
|
||||
/** Render one chip title and hand back its host element. */
|
||||
function renderTitle(props: {
|
||||
records: ReturnType<typeof createNativeTabRecords>
|
||||
service: ReturnType<typeof createBetterSidebarService>
|
||||
info: NativeTabInfo
|
||||
descriptorId: string
|
||||
paramsOf?: (info: NativeTabInfo) => NativeTabParams | undefined
|
||||
}): { host: HTMLDivElement; unmount: () => void } {
|
||||
const host = document.createElement('div')
|
||||
document.body.appendChild(host)
|
||||
let root: Root | undefined
|
||||
act(() => {
|
||||
root = createRoot(host)
|
||||
root.render(createElement(NativeTabTitle, {
|
||||
records: props.records,
|
||||
service: props.service,
|
||||
descriptorId: props.descriptorId,
|
||||
paramsOf: props.paramsOf,
|
||||
useTabInfo: () => props.info,
|
||||
// The registration's injected face is validated by the adapter, not by
|
||||
// this fixture.
|
||||
} as never))
|
||||
})
|
||||
return {
|
||||
host,
|
||||
unmount: () => {
|
||||
act(() => { root?.unmount() })
|
||||
host.remove()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
describe('NativeTabTitle file glyph', () => {
|
||||
it('draws the file glyph from the tab ADDRESS while no record exists yet', () => {
|
||||
const records = createNativeTabRecords()
|
||||
const service = createService()
|
||||
const info = nativeInfo('tab1', 'editor', 'notes.md', 'dsh-resource://file/session/s1/work/notes.md')
|
||||
|
||||
const { host, unmount } = renderTitle({
|
||||
records,
|
||||
service,
|
||||
info,
|
||||
descriptorId: 'editor',
|
||||
paramsOf: () => ({ path: '/work/notes.md' }),
|
||||
})
|
||||
expect(
|
||||
host.querySelector('[data-file-glyph="md"]'),
|
||||
'the file glyph must come from the address, not from the record',
|
||||
).not.toBeNull()
|
||||
expect(
|
||||
host.querySelector('[data-type-icon]'),
|
||||
'the descriptor glyph must not stand in for a file tab',
|
||||
).toBeNull()
|
||||
unmount()
|
||||
})
|
||||
|
||||
it('keeps the record’s own path when an in-place switch put it there', () => {
|
||||
const records = createNativeTabRecords()
|
||||
const service = createService()
|
||||
// An in-place switch (the merged files window) lives in the record only:
|
||||
// the tab's native kind is the `files` page, whose address carries no path.
|
||||
records.ensure({
|
||||
id: 'tab2',
|
||||
kind: 'files',
|
||||
title: 'notes.md',
|
||||
params: { path: '/work/notes.md' },
|
||||
scope,
|
||||
} as never)
|
||||
const info = nativeInfo('tab2', 'files', 'notes.md', 'sidebar://files')
|
||||
|
||||
const { host, unmount } = renderTitle({
|
||||
records,
|
||||
service,
|
||||
info,
|
||||
descriptorId: 'editor',
|
||||
paramsOf: () => undefined,
|
||||
})
|
||||
expect(
|
||||
host.querySelector('[data-file-glyph="md"]'),
|
||||
'the record’s in-place path still titles the chip',
|
||||
).not.toBeNull()
|
||||
unmount()
|
||||
})
|
||||
|
||||
it('keeps the descriptor glyph for a path-less page tab', () => {
|
||||
const records = createNativeTabRecords()
|
||||
const service = createService()
|
||||
const info = nativeInfo('tab3', 'files', 'Files', 'sidebar://files')
|
||||
|
||||
const { host, unmount } = renderTitle({
|
||||
records,
|
||||
service,
|
||||
info,
|
||||
descriptorId: 'editor',
|
||||
paramsOf: () => undefined,
|
||||
})
|
||||
expect(host.querySelector('[data-type-icon]'), 'a path-less page keeps its type glyph').not.toBeNull()
|
||||
expect(host.querySelector('[data-file-glyph]'), 'no file glyph without a file').toBeNull()
|
||||
unmount()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user