From 01763ccdbafd1bfb91838e054ec05566c522d8b1 Mon Sep 17 00:00:00 2001 From: CRT Date: Wed, 30 Sep 2026 12:53:11 +0000 Subject: [PATCH] Feature modules: a feature ported from a plugin keeps that plugin's words (words), so nothing is retranslated; the keep-my-changes question takes its own line --- packages/runtime/src/featureModules.ts | 19 +++++++++++++++---- packages/runtime/tests/features.test.ts | 12 +++++++++++- packages/shell/src/panel/Modes.tsx | 2 +- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/packages/runtime/src/featureModules.ts b/packages/runtime/src/featureModules.ts index b4a2b80..11f7b00 100644 --- a/packages/runtime/src/featureModules.ts +++ b/packages/runtime/src/featureModules.ts @@ -80,7 +80,8 @@ export type FeatureContext = { /** A palette command under the area's name (counts towards `AREA_COMMAND_BUDGET`), removed on stop. */ command(definition: CommandDefinition): void; /** - * The feature's word `key`: `plugin...` in the app's catalog. (Not + * The feature's word `key`: `plugin...` in the app's catalog, or + * `plugin..` for a module that names its own namespace (`words`). (Not * named `t`: the i18n tools read `t('…')` as a whole catalog key.) */ text(key: string, params?: Params): string; @@ -99,6 +100,12 @@ export type FeatureModule = { id: string; /** Selectors it depends on, by name (read them back with `ctx.selector(name)`). */ probes?: Readonly>; + /** + * Where its words are in the catalog. Default: `plugin...`. A feature + * ported from a plugin names that plugin's namespace (`focus-mode`) and keeps its keys + * (`plugin.focus-mode.`, its label `plugin.focus-mode.name`): nothing is retranslated. + */ + words?: string; /** Storage namespace (default `.`; a ported plugin keeps its old one). */ storage?: string; /** The key its settings are stored under (default `settings`). */ @@ -201,8 +208,13 @@ export function createFeatureRegistry(options: FeatureR const english = () => options.english?.() ?? /^en\b/i.test(doc.documentElement?.getAttribute?.('lang') ?? 'en'); const settled = () => options.settled?.() ?? !!doc.body?.firstElementChild; const words = (area: AreaSpec) => api.i18n.namespace(area.namespace); + /** A feature's word `key`: under its own namespace when it names one (`words`), else under its area's. */ + const wordOf = (module: FeatureModule, area: AreaSpec, key: string, params?: Params) => + (module.words ? api.i18n.namespace(module.words).t(key, params) : words(area).t(`${featureName(module.id)}.${key}`, params)); /** A feature's label in the page, when the page has it (`plugin.*` words only reach a site tab). */ const featureLabel = (spec: FeatureSpec, area: AreaSpec) => { + const legacy = modules.get(spec.id)?.words; + if (legacy) { const own = api.i18n.namespace(legacy); return own.has('name') ? own.t('name') : null; } const i18n = words(area); const key = `feature.${featureName(spec.id)}.label`; return i18n.has(key) ? i18n.t(key) : null; @@ -286,7 +298,7 @@ export function createFeatureRegistry(options: FeatureR scope.commands.register(definition); entry.commands += 1; }, - text: (key, params) => words(area).t(`${name}.${key}`, params), + text: (key, params) => wordOf(module, area, key, params), get storage() { return api.storage(module.storage ?? `${area.namespace}.${name}`); }, settings: entry.settings, selector: probe => selectorOf(module, probe), @@ -378,8 +390,7 @@ export function createFeatureRegistry(options: FeatureR const h = ui.h; const forms = entries.map(entry => { const name = featureName(entry.module.id); - const i18n = words(area); - return { entry, title: featureLabel(entry.spec, area) ?? name, form: buildSettingsForm(h, entry.module.settings!, entry.settings.get(), key => i18n.t(`${name}.${key}`)) }; + return { entry, title: featureLabel(entry.spec, area) ?? name, form: buildSettingsForm(h, entry.module.settings!, entry.settings.get(), key => wordOf(entry.module, area, key)) }; }); const single = forms.length === 1 && !notes.length ? forms[0]! : null; ui.dialog({ diff --git a/packages/runtime/tests/features.test.ts b/packages/runtime/tests/features.test.ts index 89c004c..b64ba5b 100644 --- a/packages/runtime/tests/features.test.ts +++ b/packages/runtime/tests/features.test.ts @@ -272,14 +272,18 @@ test('registry: settings come from the schema, are stored under the module\'s na const applied: unknown[] = []; const module: FeatureModule = { id: 'navigation.focusMode', + // A feature ported from a plugin keeps that plugin's words and storage. + words: 'focus-mode', storage: 'focus-mode', settings: [{ key: 'enabled', type: 'toggle', default: true, label: 'enabled' }, { key: 'trends', type: 'toggle', default: true, label: 'trends' }], start(ctx) { applied.push(ctx.settings.get()); ctx.settings.onChange(values => applied.push(values)); - ctx.command({ id: 'focusMode.toggle', title: 'Toggle Focus Mode', run: () => { ctx.settings.set({ enabled: !ctx.settings.get().enabled }); } }); + ctx.command({ id: 'focusMode.toggle', title: ctx.text('toggle'), run: () => { ctx.settings.set({ enabled: !ctx.settings.get().enabled }); } }); }, }; + env.api.i18n.namespace('focus-mode').register({ en: { toggle: 'Toggle Focus Mode' } }); + env.api.i18n.namespace('navigation-plus').register({ en: { 'keys.hello': 'Hello {name}' } }); env.store.set('demo-plugin:focus-mode:settings', JSON.stringify({ enabled: true, trends: false, gone: 1 })); registry.define(module); assert.throws(() => registryFor(env).define({ id: 'navigation.keys', settings: [{ key: 'a', type: 'toggle', default: true, label: 'a' }], start() {} }), /the table's "settings" and the module's schema disagree/); @@ -288,6 +292,12 @@ test('registry: settings come from the schema, are stored under the module\'s na // The area got its one settings command next to the feature's own; both belong to the area's owner. assert.deepEqual(env.api.commands.list().map(command => command.id).sort(), ['wb-navigation-plus.plugin.js:focusMode.toggle', 'wb-navigation-plus.plugin.js:settings']); assert.equal(env.api.commands.list().find(command => command.id.endsWith(':settings'))!.title, 'Navigation+ settings'); + assert.equal(env.api.commands.list().find(command => command.id.endsWith('focusMode.toggle'))!.title, 'Toggle Focus Mode', 'its words are under the namespace of the plugin it came from'); + // Without `words`, a feature's words are under its area: plugin.... + let greeting = ''; + registry.define({ id: 'navigation.keys', start(ctx) { greeting = ctx.text('hello', { name: 'you' }); } }); + registry.setFlags({ 'navigation.focusMode': true, 'navigation.keys': true }); + assert.equal(greeting, 'Hello you'); env.api.commands.run('wb-navigation-plus.plugin.js:focusMode.toggle'); assert.deepEqual(applied.at(-1), { enabled: false, trends: false }); assert.equal(env.store.get('demo-plugin:focus-mode:settings'), '{"enabled":false,"trends":false}'); diff --git a/packages/shell/src/panel/Modes.tsx b/packages/shell/src/panel/Modes.tsx index ec6aca4..23f91e9 100644 --- a/packages/shell/src/panel/Modes.tsx +++ b/packages/shell/src/panel/Modes.tsx @@ -137,7 +137,7 @@ function ModeSettingsOf({ table, notify }: { table: FeatureTable; notify: Notify return
{table.modes.map(mode => { const current = state.mode === mode.id; - return {current && {t('wb.modes.current')}}{mode.recommended && {t('wb.modes.recommended')}}}> {asking?.to === mode.id ? {t('wb.modes.keepQuestion')} {t('wb.modes.keepHint', { count: asking.changes })}