@vectoriox/iox-ui 5.20.0 → 5.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/design-core/index.d.ts +136 -2
- package/fesm2022/vectoriox-iox-ui-design-core.mjs +225 -5
- package/fesm2022/vectoriox-iox-ui-design-core.mjs.map +1 -1
- package/package.json +1 -1
- package/src/design-core/adopt.ts +250 -0
- package/src/design-core/element-styles.ts +17 -0
- package/src/design-core/fonts.ts +18 -4
- package/src/design-core/public-api.ts +1 -0
package/package.json
CHANGED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BUTTON_VARIANT_TO_STYLE, ElementStyle, ElementStyleTarget, OwnedTrait, defaultElementStyleFor,
|
|
3
|
+
elementStyleById, ownedTraitsOf, readOwnedTrait, writeOwnedTrait,
|
|
4
|
+
} from './element-styles';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* ADOPT (design-system DS7, decision D5): link the nodes of a page saved before element styles to
|
|
8
|
+
* the style a NEW node of their type is born wearing, so the page follows the look from then on.
|
|
9
|
+
* Opt-in per page, one click, one undo step — this module is only the pure tree pass; the builder
|
|
10
|
+
* owns the click, the history step and the save.
|
|
11
|
+
*
|
|
12
|
+
* The rule that keeps the author's work: a trait the style owns is rewritten to the style's
|
|
13
|
+
* reference ONLY when the node still holds what an untouched node of its type was saved with — the
|
|
14
|
+
* type's BASELINE (the unstyled constructor default the caller supplies), or a value a machine wrote
|
|
15
|
+
* into every new node before element styles existed (`PRE_DS2_FLAVOR_DEFAULTS`). Anything else is an
|
|
16
|
+
* author edit and is KEPT, and reported.
|
|
17
|
+
*
|
|
18
|
+
* A node that already wears a style is UPGRADED where it holds a value that style wrote in an OLDER
|
|
19
|
+
* contract (`SUPERSEDED_STYLE_VALUES`) — the same untouched-means-machine-written rule, one version on.
|
|
20
|
+
*
|
|
21
|
+
* Works on both node shapes element-styles.ts does: a builder node (values in `styleTraits`) or a
|
|
22
|
+
* serialized node (`styleProps`), so the server-side site build can run it too.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What a NEW node holds before its element style is written (C1.1): running text inherits, so a
|
|
27
|
+
* literal colour on any container would override the section's text. The factory
|
|
28
|
+
* (`ComponentRegistryService.createConfig`) writes it; adopt releases a baseline colour to it, so an
|
|
29
|
+
* adopted node is exactly what a new node of its type is.
|
|
30
|
+
*/
|
|
31
|
+
export const NEW_NODE_STYLE_DEFAULTS: Readonly<Record<string, string>> = { color: 'inherit' };
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Values a machine wrote into every NEW node of a type before element styles: the F1 Swiss pack's
|
|
35
|
+
* element defaults (`SWISS_FLAVOR.defaults`, iox-ui 5.3.0 `0a00761` + iox-builder 4.24.0 `85109eb`,
|
|
36
|
+
* `createConfig → applyFlavorDefaults`, resolved by `resolveFlavorDefaults` to
|
|
37
|
+
* `var(--iox-<token>, <pack value>)`; deleted in DS2). An untouched node created between F1 and DS2
|
|
38
|
+
* holds exactly these, so they count as untouched.
|
|
39
|
+
*/
|
|
40
|
+
export const PRE_DS2_FLAVOR_DEFAULTS: Readonly<Record<string, Readonly<Record<string, string>>>> = {
|
|
41
|
+
Button: { borderRadius: 'var(--iox-radius-control, 4px)', padding: 'var(--iox-space-control, 10px 18px)' },
|
|
42
|
+
ButtonBlock: { borderRadius: 'var(--iox-radius-control, 4px)', padding: 'var(--iox-space-control, 10px 18px)' },
|
|
43
|
+
Card: { borderRadius: 'var(--iox-radius-surface, 4px)' },
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** One owned trait whose value an element style CHANGED in the contract's history. */
|
|
47
|
+
export interface SupersededStyleValue extends OwnedTrait {
|
|
48
|
+
readonly style: string;
|
|
49
|
+
/** What the style wrote before. */
|
|
50
|
+
readonly values: ReadonlyArray<string>;
|
|
51
|
+
/** The style did not own the trait before, so an untouched node held its type's baseline. */
|
|
52
|
+
readonly wasUnowned?: boolean;
|
|
53
|
+
/** The iox-ai-guidance contract commit that changed it. */
|
|
54
|
+
readonly contract: string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Every element-style value the contract has superseded since element styles shipped (a diff of
|
|
59
|
+
* `flavor-spec.v1.json`'s `elementStyles` across its history — nothing else changed a value):
|
|
60
|
+
* - `b86c54f` (C1): body / lead `color: var(--iox-text)` → `inherit`;
|
|
61
|
+
* - `602d96e` (C1.1): box / card / panel / nav gained `color: inherit` — before, they owned no colour.
|
|
62
|
+
* A spec pins this list against the generated contract. Add an entry with every contract change.
|
|
63
|
+
*/
|
|
64
|
+
export const SUPERSEDED_STYLE_VALUES: ReadonlyArray<SupersededStyleValue> = [
|
|
65
|
+
{ style: 'body', trait: 'color', values: ['var(--iox-text)'], contract: 'b86c54f' },
|
|
66
|
+
{ style: 'lead', trait: 'color', values: ['var(--iox-text)'], contract: 'b86c54f' },
|
|
67
|
+
{ style: 'box', trait: 'color', values: [], wasUnowned: true, contract: '602d96e' },
|
|
68
|
+
{ style: 'card', trait: 'color', values: [], wasUnowned: true, contract: '602d96e' },
|
|
69
|
+
{ style: 'panel', trait: 'color', values: [], wasUnowned: true, contract: '602d96e' },
|
|
70
|
+
{ style: 'nav', trait: 'color', values: [], wasUnowned: true, contract: '602d96e' },
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
export enum AdoptOutcome {
|
|
74
|
+
/** The node wore no style; it now wears its type's default (or its legacy Button variant's). */
|
|
75
|
+
Linked = 'linked',
|
|
76
|
+
/** The node wore a style; values that style wrote in an older contract were brought current. */
|
|
77
|
+
Upgraded = 'upgraded',
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export enum AdoptSkipReason {
|
|
81
|
+
/** The type is born wearing no style (Gallery, SliderContainer, Spacer, …). */
|
|
82
|
+
NoStyle = 'no-style',
|
|
83
|
+
/** The node takes its look from a style preset — the author's own class. */
|
|
84
|
+
Preset = 'preset',
|
|
85
|
+
/** The node is (inside) a linked symbol instance: its look is the symbol's, shared across pages. */
|
|
86
|
+
Symbol = 'symbol',
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface AdoptContext {
|
|
90
|
+
/** Per type, the style map an untouched SAVED node of it holds — its unstyled constructor
|
|
91
|
+
* defaults (the builder's `ComponentRegistryService.adoptionBaseline`). */
|
|
92
|
+
baseline(type: string): Readonly<Record<string, unknown>>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The shapes this pass reads: an element-style target plus the fields that decide skips and styles. */
|
|
96
|
+
export interface AdoptableNode extends ElementStyleTarget {
|
|
97
|
+
id?: string;
|
|
98
|
+
props?: Readonly<Record<string, unknown>> | null;
|
|
99
|
+
traits?: ReadonlyArray<{ name: string; default?: unknown }> | null;
|
|
100
|
+
stylePresetId?: string | null;
|
|
101
|
+
symbolId?: string | null;
|
|
102
|
+
children?: AdoptableNode[] | null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** An owned trait the author had changed — kept, with the value kept. */
|
|
106
|
+
export interface KeptEdit extends OwnedTrait {
|
|
107
|
+
value: unknown;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export interface AdoptedNode {
|
|
111
|
+
id?: string;
|
|
112
|
+
type: string;
|
|
113
|
+
style: string;
|
|
114
|
+
outcome: AdoptOutcome;
|
|
115
|
+
/** Traits rewritten (the style's references, a current value, or a released colour). */
|
|
116
|
+
written: OwnedTrait[];
|
|
117
|
+
kept: KeptEdit[];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface SkippedNode {
|
|
121
|
+
id?: string;
|
|
122
|
+
type: string;
|
|
123
|
+
reason: AdoptSkipReason;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export interface AdoptReport {
|
|
127
|
+
adopted: AdoptedNode[];
|
|
128
|
+
skipped: SkippedNode[];
|
|
129
|
+
/** Author edits kept across every adopted node. */
|
|
130
|
+
kept: number;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const sameValue = (a: unknown, b: unknown): boolean =>
|
|
134
|
+
a === b || (a !== undefined && a !== null && b !== undefined && b !== null && String(a) === String(b));
|
|
135
|
+
|
|
136
|
+
function propOf(node: AdoptableNode, name: string): unknown {
|
|
137
|
+
const fromProps = node.props?.[name];
|
|
138
|
+
if (fromProps !== undefined) return fromProps;
|
|
139
|
+
return node.traits?.find(t => t?.name === name)?.default;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** The style an unstyled node adopts: a legacy Button by its `variant` (spec `buttonVariantToStyle`),
|
|
143
|
+
* every other type by its default (a Heading by its level), or null. */
|
|
144
|
+
export function adoptionStyleFor(node: AdoptableNode): string | null {
|
|
145
|
+
if (node.type === 'Button') {
|
|
146
|
+
const byVariant = BUTTON_VARIANT_TO_STYLE[String(propOf(node, 'variant') ?? '')];
|
|
147
|
+
if (byVariant) return byVariant;
|
|
148
|
+
}
|
|
149
|
+
const traits: Record<string, unknown> = { ...(node.props ?? {}) };
|
|
150
|
+
for (const t of node.traits ?? []) if (t?.name && !(t.name in traits)) traits[t.name] = t.default;
|
|
151
|
+
return defaultElementStyleFor(node.type, traits);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const owns = (style: ElementStyle, trait: string) => trait in style.base;
|
|
155
|
+
|
|
156
|
+
/** Release each new-node default the style does not own, where the node still holds its baseline. */
|
|
157
|
+
function releaseNewNodeDefaults(node: AdoptableNode, style: ElementStyle | null, baseline: Readonly<Record<string, unknown>>, written: OwnedTrait[]): void {
|
|
158
|
+
for (const [trait, value] of Object.entries(NEW_NODE_STYLE_DEFAULTS)) {
|
|
159
|
+
if (style && owns(style, trait)) continue;
|
|
160
|
+
const ref = { trait };
|
|
161
|
+
const current = readOwnedTrait(node, ref);
|
|
162
|
+
if (current === undefined || sameValue(current, value) || !sameValue(current, baseline[trait])) continue;
|
|
163
|
+
if (writeOwnedTrait(node, ref, value)) written.push(ref);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function untouched(node: AdoptableNode, trait: string, value: unknown, baseline: Readonly<Record<string, unknown>>): boolean {
|
|
168
|
+
return value === undefined || sameValue(value, baseline[trait]) || sameValue(value, PRE_DS2_FLAVOR_DEFAULTS[node.type]?.[trait]);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function link(node: AdoptableNode, style: ElementStyle, baseline: Readonly<Record<string, unknown>>): AdoptedNode {
|
|
172
|
+
const written: OwnedTrait[] = [];
|
|
173
|
+
const kept: KeptEdit[] = [];
|
|
174
|
+
for (const { value, ...ref } of ownedTraitsOf(style)) {
|
|
175
|
+
const current = readOwnedTrait(node, ref);
|
|
176
|
+
if (sameValue(current, value)) continue;
|
|
177
|
+
// A pseudo-state map has no baseline: a state value is always the author's.
|
|
178
|
+
const free = ref.state ? current === undefined : untouched(node, ref.trait, current, baseline);
|
|
179
|
+
if (!free) { kept.push({ ...ref, value: current }); continue; }
|
|
180
|
+
if (writeOwnedTrait(node, ref, value)) written.push(ref);
|
|
181
|
+
}
|
|
182
|
+
releaseNewNodeDefaults(node, style, baseline, written);
|
|
183
|
+
node.elementStyle = style.id;
|
|
184
|
+
return { id: node.id, type: node.type, style: style.id, outcome: AdoptOutcome.Linked, written, kept };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function upgrade(node: AdoptableNode, style: ElementStyle, baseline: Readonly<Record<string, unknown>>): AdoptedNode | null {
|
|
188
|
+
const written: OwnedTrait[] = [];
|
|
189
|
+
for (const old of SUPERSEDED_STYLE_VALUES) {
|
|
190
|
+
if (old.style !== style.id) continue;
|
|
191
|
+
const ref: OwnedTrait = old.state ? { trait: old.trait, state: old.state } : { trait: old.trait };
|
|
192
|
+
const now = old.state ? style[old.state]?.[old.trait] : style.base[old.trait];
|
|
193
|
+
if (now === undefined) continue;
|
|
194
|
+
const current = readOwnedTrait(node, ref);
|
|
195
|
+
const before = old.wasUnowned ? [...old.values, baseline[old.trait]] : old.values;
|
|
196
|
+
if (!before.some(v => sameValue(current, v))) continue;
|
|
197
|
+
if (writeOwnedTrait(node, ref, now)) written.push(ref);
|
|
198
|
+
}
|
|
199
|
+
releaseNewNodeDefaults(node, style, baseline, written);
|
|
200
|
+
return written.length ? { id: node.id, type: node.type, style: style.id, outcome: AdoptOutcome.Upgraded, written, kept: [] } : null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Adopt every node of `tree` in place (children included) and report what happened. Skipped, never
|
|
205
|
+
* touched: a node with a style preset, and every node of a linked symbol instance. A node whose type
|
|
206
|
+
* has no default style is not linked, but an untouched baseline colour on it is released to the
|
|
207
|
+
* new-node default all the same, so adopted text inside it is not painted the old literal.
|
|
208
|
+
*/
|
|
209
|
+
export function adoptElementStyles(tree: AdoptableNode[] | null | undefined, ctx: AdoptContext): AdoptReport {
|
|
210
|
+
const report: AdoptReport = { adopted: [], skipped: [], kept: 0 };
|
|
211
|
+
const visit = (nodes: AdoptableNode[] | null | undefined, inSymbol: boolean) => {
|
|
212
|
+
for (const node of nodes ?? []) {
|
|
213
|
+
if (!node?.type) continue;
|
|
214
|
+
const symbol = inSymbol || !!node.symbolId;
|
|
215
|
+
adoptNode(node, symbol, ctx, report);
|
|
216
|
+
visit(node.children, symbol);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
visit(tree, false);
|
|
220
|
+
report.kept = report.adopted.reduce((n, a) => n + a.kept.length, 0);
|
|
221
|
+
return report;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function adoptNode(node: AdoptableNode, inSymbol: boolean, ctx: AdoptContext, report: AdoptReport): void {
|
|
225
|
+
const worn = elementStyleById(node.elementStyle);
|
|
226
|
+
const target = worn ? null : elementStyleById(adoptionStyleFor(node));
|
|
227
|
+
const reason = inSymbol ? AdoptSkipReason.Symbol : node.stylePresetId ? AdoptSkipReason.Preset : null;
|
|
228
|
+
if (reason) {
|
|
229
|
+
if (target) report.skipped.push({ id: node.id, type: node.type, reason });
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
const baseline = ctx.baseline(node.type) ?? {};
|
|
233
|
+
if (worn) {
|
|
234
|
+
const upgraded = upgrade(node, worn, baseline);
|
|
235
|
+
if (upgraded) report.adopted.push(upgraded);
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
if (node.elementStyle) return;
|
|
239
|
+
if (!target) {
|
|
240
|
+
releaseNewNodeDefaults(node, null, baseline, []);
|
|
241
|
+
report.skipped.push({ id: node.id, type: node.type, reason: AdoptSkipReason.NoStyle });
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
report.adopted.push(link(node, target, baseline));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** What adopting `tree` WOULD do, without touching it (the banner's count). */
|
|
248
|
+
export function previewElementStyleAdoption(tree: AdoptableNode[] | null | undefined, ctx: AdoptContext): AdoptReport {
|
|
249
|
+
return adoptElementStyles(JSON.parse(JSON.stringify(tree ?? [])), ctx);
|
|
250
|
+
}
|
|
@@ -163,6 +163,23 @@ function ownedEntries(style: ElementStyle): Array<OwnedTrait & { value: string }
|
|
|
163
163
|
return out;
|
|
164
164
|
}
|
|
165
165
|
|
|
166
|
+
/** Every trait the style owns with the value it writes — base first, then hover/active/focus. */
|
|
167
|
+
export function ownedTraitsOf(style: ElementStyle): Array<OwnedTrait & { value: string }> {
|
|
168
|
+
return ownedEntries(style);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** The node's value of one owned trait (a base trait, or a trait inside a pseudo-state map). */
|
|
172
|
+
export function readOwnedTrait(target: ElementStyleTarget, owned: OwnedTrait): unknown {
|
|
173
|
+
return owned.state ? readState(target, owned.state, owned.trait) : readBase(target, owned.trait);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Write one owned trait. False when a builder node has no slot for a base trait (dropped). */
|
|
177
|
+
export function writeOwnedTrait(target: ElementStyleTarget, owned: OwnedTrait, value: string): boolean {
|
|
178
|
+
if (!owned.state) return writeBase(target, owned.trait, value);
|
|
179
|
+
writeState(target, owned.state, owned.trait, value);
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
|
|
166
183
|
const sameOwned = (a: OwnedTrait, b: OwnedTrait) => a.trait === b.trait && a.state === b.state;
|
|
167
184
|
|
|
168
185
|
function writeEntries(target: ElementStyleTarget, style: ElementStyle, skip: OwnedTrait[] = []): string[] {
|
package/src/design-core/fonts.ts
CHANGED
|
@@ -422,6 +422,13 @@ export interface FontsForTokensOptions {
|
|
|
422
422
|
* would otherwise download a monospace face for a site with no code on it.
|
|
423
423
|
*/
|
|
424
424
|
usedCss?: string;
|
|
425
|
+
/**
|
|
426
|
+
* The families the org UPLOADED. An uploaded family renders from the org's own files, so it is
|
|
427
|
+
* never fetched from Google — even when the catalog has a family of that name (an org that uploads
|
|
428
|
+
* Inter's variable file means THAT Inter: linking Google's too put its static cuts beside the
|
|
429
|
+
* upload, and the browser picked Google's — DS5.1).
|
|
430
|
+
*/
|
|
431
|
+
uploaded?: ReadonlyArray<string>;
|
|
425
432
|
}
|
|
426
433
|
|
|
427
434
|
/**
|
|
@@ -437,14 +444,21 @@ export function fontsForTokens(
|
|
|
437
444
|
const monoRead = MONO_REFERENCE.test(opts.usedCss ?? '')
|
|
438
445
|
|| rows.some(t => t.name !== MONO_ROLE && MONO_REFERENCE.test(`${t.value ?? ''} ${t.values?.dark ?? ''}`));
|
|
439
446
|
const roles = monoRead ? FONT_FAMILY_TOKENS : ALWAYS_LOADED_ROLES;
|
|
447
|
+
const uploaded = new Set((opts.uploaded ?? []).map(fontFamilyKey));
|
|
440
448
|
const seen = new Set<string>();
|
|
441
449
|
const ids: string[] = [];
|
|
442
450
|
for (const role of roles) {
|
|
443
451
|
const row = rows.find(t => t.name === role);
|
|
444
|
-
// The role's FIRST
|
|
445
|
-
//
|
|
446
|
-
//
|
|
447
|
-
|
|
452
|
+
// The role's FIRST family that renders decides: an uploaded one needs nothing from Google; the
|
|
453
|
+
// first catalog one is linked (it renders, or stands in for an uploaded family that is missing
|
|
454
|
+
// — `userFontStack` puts the look's font right after the author's). Nothing further down is
|
|
455
|
+
// ever reached, so nothing further down is loaded.
|
|
456
|
+
let id: string | null = null;
|
|
457
|
+
for (const family of parseFontFamilies(row?.value ?? '')) {
|
|
458
|
+
if (uploaded.has(fontFamilyKey(family))) break;
|
|
459
|
+
id = catalogFontId(family);
|
|
460
|
+
if (id) break;
|
|
461
|
+
}
|
|
448
462
|
if (id && !seen.has(id)) {
|
|
449
463
|
seen.add(id);
|
|
450
464
|
ids.push(id);
|