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
This commit is contained in:
parent
1f95d27018
commit
6d3f91bf2e
3 changed files with 27 additions and 6 deletions
|
|
@ -80,7 +80,8 @@ export type FeatureContext<Api> = {
|
||||||
/** A palette command under the area's name (counts towards `AREA_COMMAND_BUDGET`), removed on stop. */
|
/** A palette command under the area's name (counts towards `AREA_COMMAND_BUDGET`), removed on stop. */
|
||||||
command(definition: CommandDefinition): void;
|
command(definition: CommandDefinition): void;
|
||||||
/**
|
/**
|
||||||
* The feature's word `key`: `plugin.<area namespace>.<name>.<key>` in the app's catalog. (Not
|
* The feature's word `key`: `plugin.<area namespace>.<name>.<key>` in the app's catalog, or
|
||||||
|
* `plugin.<words>.<key>` for a module that names its own namespace (`words`). (Not
|
||||||
* named `t`: the i18n tools read `t('…')` as a whole catalog key.)
|
* named `t`: the i18n tools read `t('…')` as a whole catalog key.)
|
||||||
*/
|
*/
|
||||||
text(key: string, params?: Params): string;
|
text(key: string, params?: Params): string;
|
||||||
|
|
@ -99,6 +100,12 @@ export type FeatureModule<Api> = {
|
||||||
id: string;
|
id: string;
|
||||||
/** Selectors it depends on, by name (read them back with `ctx.selector(name)`). */
|
/** Selectors it depends on, by name (read them back with `ctx.selector(name)`). */
|
||||||
probes?: Readonly<Record<string, Probe>>;
|
probes?: Readonly<Record<string, Probe>>;
|
||||||
|
/**
|
||||||
|
* Where its words are in the catalog. Default: `plugin.<area namespace>.<name>.<key>`. A feature
|
||||||
|
* ported from a plugin names that plugin's namespace (`focus-mode`) and keeps its keys
|
||||||
|
* (`plugin.focus-mode.<key>`, its label `plugin.focus-mode.name`): nothing is retranslated.
|
||||||
|
*/
|
||||||
|
words?: string;
|
||||||
/** Storage namespace (default `<area namespace>.<name>`; a ported plugin keeps its old one). */
|
/** Storage namespace (default `<area namespace>.<name>`; a ported plugin keeps its old one). */
|
||||||
storage?: string;
|
storage?: string;
|
||||||
/** The key its settings are stored under (default `settings`). */
|
/** The key its settings are stored under (default `settings`). */
|
||||||
|
|
@ -201,8 +208,13 @@ export function createFeatureRegistry<Api extends RegistryApi>(options: FeatureR
|
||||||
const english = () => options.english?.() ?? /^en\b/i.test(doc.documentElement?.getAttribute?.('lang') ?? 'en');
|
const english = () => options.english?.() ?? /^en\b/i.test(doc.documentElement?.getAttribute?.('lang') ?? 'en');
|
||||||
const settled = () => options.settled?.() ?? !!doc.body?.firstElementChild;
|
const settled = () => options.settled?.() ?? !!doc.body?.firstElementChild;
|
||||||
const words = (area: AreaSpec) => api.i18n.namespace(area.namespace);
|
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<Api>, 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). */
|
/** 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 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 i18n = words(area);
|
||||||
const key = `feature.${featureName(spec.id)}.label`;
|
const key = `feature.${featureName(spec.id)}.label`;
|
||||||
return i18n.has(key) ? i18n.t(key) : null;
|
return i18n.has(key) ? i18n.t(key) : null;
|
||||||
|
|
@ -286,7 +298,7 @@ export function createFeatureRegistry<Api extends RegistryApi>(options: FeatureR
|
||||||
scope.commands.register(definition);
|
scope.commands.register(definition);
|
||||||
entry.commands += 1;
|
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}`); },
|
get storage() { return api.storage(module.storage ?? `${area.namespace}.${name}`); },
|
||||||
settings: entry.settings,
|
settings: entry.settings,
|
||||||
selector: probe => selectorOf(module, probe),
|
selector: probe => selectorOf(module, probe),
|
||||||
|
|
@ -378,8 +390,7 @@ export function createFeatureRegistry<Api extends RegistryApi>(options: FeatureR
|
||||||
const h = ui.h;
|
const h = ui.h;
|
||||||
const forms = entries.map(entry => {
|
const forms = entries.map(entry => {
|
||||||
const name = featureName(entry.module.id);
|
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 => wordOf(entry.module, area, key)) };
|
||||||
return { entry, title: featureLabel(entry.spec, area) ?? name, form: buildSettingsForm(h, entry.module.settings!, entry.settings.get(), key => i18n.t(`${name}.${key}`)) };
|
|
||||||
});
|
});
|
||||||
const single = forms.length === 1 && !notes.length ? forms[0]! : null;
|
const single = forms.length === 1 && !notes.length ? forms[0]! : null;
|
||||||
ui.dialog({
|
ui.dialog({
|
||||||
|
|
|
||||||
|
|
@ -272,14 +272,18 @@ test('registry: settings come from the schema, are stored under the module\'s na
|
||||||
const applied: unknown[] = [];
|
const applied: unknown[] = [];
|
||||||
const module: FeatureModule<CorePluginApi> = {
|
const module: FeatureModule<CorePluginApi> = {
|
||||||
id: 'navigation.focusMode',
|
id: 'navigation.focusMode',
|
||||||
|
// A feature ported from a plugin keeps that plugin's words and storage.
|
||||||
|
words: 'focus-mode',
|
||||||
storage: 'focus-mode',
|
storage: 'focus-mode',
|
||||||
settings: [{ key: 'enabled', type: 'toggle', default: true, label: 'enabled' }, { key: 'trends', type: 'toggle', default: true, label: 'trends' }],
|
settings: [{ key: 'enabled', type: 'toggle', default: true, label: 'enabled' }, { key: 'trends', type: 'toggle', default: true, label: 'trends' }],
|
||||||
start(ctx) {
|
start(ctx) {
|
||||||
applied.push(ctx.settings.get());
|
applied.push(ctx.settings.get());
|
||||||
ctx.settings.onChange(values => applied.push(values));
|
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 }));
|
env.store.set('demo-plugin:focus-mode:settings', JSON.stringify({ enabled: true, trends: false, gone: 1 }));
|
||||||
registry.define(module);
|
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/);
|
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.
|
// 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.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(':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.<area namespace>.<name>.<key>.
|
||||||
|
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');
|
env.api.commands.run('wb-navigation-plus.plugin.js:focusMode.toggle');
|
||||||
assert.deepEqual(applied.at(-1), { enabled: false, trends: false });
|
assert.deepEqual(applied.at(-1), { enabled: false, trends: false });
|
||||||
assert.equal(env.store.get('demo-plugin:focus-mode:settings'), '{"enabled":false,"trends":false}');
|
assert.equal(env.store.get('demo-plugin:focus-mode:settings'), '{"enabled":false,"trends":false}');
|
||||||
|
|
|
||||||
|
|
@ -137,7 +137,7 @@ function ModeSettingsOf({ table, notify }: { table: FeatureTable; notify: Notify
|
||||||
return <Section title={t('wb.modes.section')} hint={t('wb.modes.sectionHint')} id="modes">
|
return <Section title={t('wb.modes.section')} hint={t('wb.modes.sectionHint')} id="modes">
|
||||||
{table.modes.map(mode => {
|
{table.modes.map(mode => {
|
||||||
const current = state.mode === mode.id;
|
const current = state.mode === mode.id;
|
||||||
return <Row key={mode.id} label={name(mode.id)} hint={tx(mode.description, undefined, '')} keywords={`mode preset ${mode.id}`}
|
return <Row key={mode.id} label={name(mode.id)} hint={tx(mode.description, undefined, '')} keywords={`mode preset ${mode.id}`} wide={asking?.to === mode.id}
|
||||||
badge={<>{current && <Badge tone="accent">{t('wb.modes.current')}</Badge>}{mode.recommended && <Badge>{t('wb.modes.recommended')}</Badge>}</>}>
|
badge={<>{current && <Badge tone="accent">{t('wb.modes.current')}</Badge>}{mode.recommended && <Badge>{t('wb.modes.recommended')}</Badge>}</>}>
|
||||||
{asking?.to === mode.id
|
{asking?.to === mode.id
|
||||||
? <span className="wbf-ask" role="alertdialog">{t('wb.modes.keepQuestion')} <small>{t('wb.modes.keepHint', { count: asking.changes })}</small>
|
? <span className="wbf-ask" role="alertdialog">{t('wb.modes.keepQuestion')} <small>{t('wb.modes.keepHint', { count: asking.changes })}</small>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue