diff --git a/docs/external-plugin-guide.md b/docs/external-plugin-guide.md index 0a339eaa..ac15d199 100644 --- a/docs/external-plugin-guide.md +++ b/docs/external-plugin-guide.md @@ -1064,7 +1064,7 @@ interface SettingsDescriptor { 四条必须记住的新规则: -1. **命名空间 = 插件 Loader 行的 `entry.options.id`**,不是插件自选的字符串。聚合包会用别的 id 挂同一个包,所以**运行时自发现**(本插件 `src/index.ts` 的 `ownEntryId(ctx)`:按 `entry.options.name === '<包名>'` + `entry.fiber === ctx.fiber` 匹配,回退到第一个启用的同名行),**不要硬编码** id。 +1. **命名空间 = 插件 Loader 行的 `entry.options.id`**,不是插件自选的字符串。聚合包会用别的 id 挂同一个包,所以**运行时自发现**(本插件 `src/index.ts` 的 `ownEntryId(ctx)`:有效 id 的行优先按 `entry.fiber === ctx.fiber` 匹配,支持别名与 `file:` 挂载;未找到自己的 fiber 才回退到第一个启用的同包名行),**不要硬编码** id。 2. **schema 来自插件模块导出的 `Config`**(`entry.fiber.runtime.Config`)。用户可见的字段必须**并进这个 `Config`**——本插件把 `PrefsSchema` 合并进 `Config`(`src/config.ts`),并把每个偏好字段标 `meta.volatile = true`;**这一个标记就是「改设置实时生效、不重挂插件」的全部机制**(Loader 的 `equalExceptVolatile` 跳过 volatile 字段,纯 volatile 变更提交进运行中的引用并发 `loader/volatile-update`,普通字段仍是重挂语义)。 3. **自带设置页的插件要主动退出自动表单**:`ctx.effect(() => ctx.settings.configure({ auto: false }, ctx.fiber))`,否则 Settings 会把同一批字段渲染两遍(策略不影响读写)。 4. **`settings/document-updated` 你监听不到,别写**:事件签名是 `'settings/document-updated'(ns, revision)`(语义是「该条目的表单值 / 可用性 / 页面策略变了,表单客户端重新读取 schema、解析值与 revision」),但它在 **settings 服务自己的 context 上 `emit`**,而 cordis 事件只向该 ctx 的**祖先**冒泡——插件 fiber 是兄弟,`ctx.on(...)` 永远不会触发。**替代做法:每次重新读取表单时重新求值**(本插件的 `settingsFace.get()` 在返回当前值前跑一遍门控同步;客户端侧另经 `remote` 服务的 `$on('settings/document-updated')` 镜像触发重新拉取)。**旧文档里的 `settings/updated` 事件在 0.1.7 已不存在**。 diff --git a/src/index.ts b/src/index.ts index d72ddb61..917f084f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1176,9 +1176,8 @@ const LEGACY_SETTINGS_FILE = 'settings.yaml' * DSH 0.1.7 addresses settings forms by profile entry id, and that id is a * mount choice rather than a package property: this bundle's patch uses * `better-sidebar`, while an aggregate bundle mounts the same package under - * its own id. The row is therefore identified by the package name plus fiber - * identity, with an enabled same-name row as the fallback for the moment - * before the fiber is attached. + * its own id. Fiber identity also covers aliases and absolute file URLs; + * an enabled same-package row is the fallback before the fiber is attached. * @param ctx - the plugin's own context. * @returns the row's configured id, or undefined when no row can be identified. */ @@ -1187,9 +1186,9 @@ function ownEntryId(ctx: Context): string | undefined { try { for (const entry of ctx.loader.entries()) { const id = entry.options.id - if (entry.options.name !== SIDEBAR_PACKAGE_NAME || typeof id !== 'string' || id === '') continue + if (typeof id !== 'string' || id === '') continue if (entry.fiber === ctx.fiber) return id - if (entry.disabled !== true && fallback === undefined) fallback = id + if (entry.options.name === SIDEBAR_PACKAGE_NAME && entry.disabled !== true && fallback === undefined) fallback = id } } catch { // A loader that does not expose its entries leaves the settings face diff --git a/tests/smoke.spec.ts b/tests/smoke.spec.ts index 7f0d9a60..6d293460 100644 --- a/tests/smoke.spec.ts +++ b/tests/smoke.spec.ts @@ -16,7 +16,7 @@ import { encodeText } from '../src/text-encoding.ts' import { downloadUrl, htmlUrl } from '../src/client/api.ts' import * as git from '../src/git.ts' import { listDirectory } from '../src/fs-tree.ts' -import type { SidebarWebRoute, SidebarWebUpgradeRoute } from '../src/context-types.ts' +import type { SidebarLoaderEntry, SidebarWebRoute, SidebarWebUpgradeRoute } from '../src/context-types.ts' /** Symlink creation may require elevated privileges on Windows. */ const canCreateSymlink = (() => { @@ -1324,7 +1324,9 @@ const createFakeSettings = (pre?: Record>) => { * @param home - a harness home holding a retired `settings.yaml`, when the test wants the legacy import to run. * @returns the mounted `/sidebar/api` route. */ -const mountWithSettings = (settings?: unknown, home?: string): SidebarWebRoute => { +const mountWithSettings = (settings?: unknown, home?: string, entries: SidebarLoaderEntry[] = [ + { options: { id: ENTRY_ID, name: 'dsh-better-sidebar' }, fiber: PLUGIN_FIBER }, +]): SidebarWebRoute => { const routes: SidebarWebRoute[] = [] lastMountedRoutes = routes const ctx = { @@ -1348,7 +1350,7 @@ const mountWithSettings = (settings?: unknown, home?: string): SidebarWebRoute = // The plugin discovers its own settings entry id from the loader, so a // fake without these entries has NO settings face at all. loader: { - entries: () => [{ options: { id: ENTRY_ID, name: 'dsh-better-sidebar' }, fiber: PLUGIN_FIBER }], + entries: () => entries, // The real loader settles before a form is addressable; the fake // resolves immediately so the import runs on the same tick. await: () => Promise.resolve(), @@ -1437,6 +1439,28 @@ describe('side card settings routes', () => { expect(after.revision).toBe(1) }) + it('uses its exact fiber for file/alias mounts before the enabled bare-package fallback', async () => { + const aliasId = 'sidebar-dev-file' + const entries: SidebarLoaderEntry[] = [ + { options: { id: 'disabled-sidebar', name: 'dsh-better-sidebar' }, disabled: true }, + { options: { id: ENTRY_ID, name: 'dsh-better-sidebar' }, fiber: {} }, + { options: { id: '', name: 'file:///plugins/sidebar.js' }, fiber: PLUGIN_FIBER }, + { options: { id: aliasId, name: 'file:///plugins/sidebar.js' }, fiber: PLUGIN_FIBER }, + ] + const settings = createFakeSettings({ [aliasId]: { editorExplorer: true } }) + const route = mountWithSettings(settings, undefined, entries) + expect(await invoke(route, 'settings.get', {})).toMatchObject({ + ok: true, value: { value: { editorExplorer: true }, revision: 0 }, + }) + expect(await invoke(route, 'settings.update', { patch: { autoOpenJobs: false }, expectedRevision: 0 })) + .toMatchObject({ ok: true, value: { value: { autoOpenJobs: false }, revision: 1 } }) + expect(settings.describe().find(row => row.ns === ENTRY_ID)?.value.autoOpenJobs).toBe(true) + const fallbackRoute = mountWithSettings(settings, undefined, entries.slice(0, 2)) + expect(await invoke(fallbackRoute, 'settings.get', {})).toMatchObject({ + ok: true, value: { value: { editorExplorer: false, autoOpenJobs: true }, revision: 0 }, + }) + }) + it('imports a pre-0.1.7 settings.yaml section once, keeping only declared fields', async () => { const home = mkdtempSync(join(tmpdir(), 'dsh-sidebar-legacy-prefs-')) // DSH renames the retired document to `.imported` before importing any