@nodii/dev 0.6.145 → 0.6.147
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/dist/core.js +1 -1
- package/dist/surface.js +145 -25
- package/package.json +1 -1
package/dist/core.js
CHANGED
|
@@ -4,7 +4,7 @@ import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
|
4
4
|
import { homedir, platform } from "node:os";
|
|
5
5
|
import { dirname, join } from "node:path";
|
|
6
6
|
|
|
7
|
-
export const VERSION = "0.6.
|
|
7
|
+
export const VERSION = "0.6.147";
|
|
8
8
|
export const configPath = () =>
|
|
9
9
|
process.env.NODII_DEV_CONFIG_PATH ??
|
|
10
10
|
join(homedir(), ".config/nodii-dev/config.json");
|
package/dist/surface.js
CHANGED
|
@@ -32,7 +32,7 @@ import { CliError, VERSION, devRequest, readConfig } from "./core.js";
|
|
|
32
32
|
import { createRenderer } from "./vendor/render-templates.mjs";
|
|
33
33
|
|
|
34
34
|
/** Bumped whenever the emitted code changes shape for the same inputs. */
|
|
35
|
-
export const GENERATOR_FORMAT =
|
|
35
|
+
export const GENERATOR_FORMAT = 17;
|
|
36
36
|
export const TREE_DIR = "docs/figma-ux-ds/figma/surface-trees";
|
|
37
37
|
export const GENERATED_SUFFIX = ".surface.generated.tsx";
|
|
38
38
|
/** Where every generated product page lives, as a git glob (NODII-DEV-CLI-77). */
|
|
@@ -548,7 +548,7 @@ export function emitSurface(tree, row, ds) {
|
|
|
548
548
|
used.add(state);
|
|
549
549
|
const fn = `${pascal(state)}View`;
|
|
550
550
|
const note = (path, kind, detail) => unmapped.push({ variant: variant.name, path, kind, detail });
|
|
551
|
-
const emitted = emitNode(pageBody(variant, row), { depth: 2, parent: null, path: variant.name, row, ds, imports, note, root: true, scopes, layers, layer: "", state, lists: new Map(), shapes, handlers: new Map(), optionals }) ?? "";
|
|
551
|
+
const emitted = emitNode(pageBody(variant, row), { depth: 2, parent: null, path: variant.name, row, ds, imports, note, root: true, scopes, layers, layer: "", state, lists: new Map(), shapes, handlers: new Map(), shown: new Map(), optionals }) ?? "";
|
|
552
552
|
// A view the generator can't lay out is no page at all: generation fails rather than ship
|
|
553
553
|
// an empty view (NODII-DEV-CLI-81).
|
|
554
554
|
if (emitted.trimStart().startsWith("{/*")) {
|
|
@@ -906,7 +906,7 @@ const literalText = (lit) => JSON.parse(lit.startsWith("{") ? lit.slice(1, -1) :
|
|
|
906
906
|
function liftRecordData(code, node, ctx, path, scope = scopeName(node)) {
|
|
907
907
|
const lifted = [];
|
|
908
908
|
const lift = (at, name) => (lifted.push(name), mark(ctx, at, name));
|
|
909
|
-
let out = liftFieldControls(code, node, ctx, lift);
|
|
909
|
+
let out = liftFieldControls(code, node, ctx, lift, scope);
|
|
910
910
|
// An Alert's title or description that names a record's value: a date, an amount, a code,
|
|
911
911
|
// or any figure ("running at 4.2% this period", "3 lines still carry a flag"; hub-468).
|
|
912
912
|
out = out.replace(/<(Alert(Title|Description))>(\{"(?:[^"\\\n]|\\.)*"\})<\/\1>/g, (all, tag, part, lit) =>
|
|
@@ -1112,27 +1112,98 @@ function nameHandlers(code, node, ctx, path, scope) {
|
|
|
1112
1112
|
const GLYPH_WORDS = [[/^[‹«←⟨]+$/, "Previous"], [/^[›»→⟩]+$/, "Next"], [/^[↑]+$/, "Up"], [/^[↓]+$/, "Down"]];
|
|
1113
1113
|
|
|
1114
1114
|
/**
|
|
1115
|
-
*
|
|
1116
|
-
* state)
|
|
1117
|
-
*
|
|
1118
|
-
*
|
|
1119
|
-
*
|
|
1120
|
-
*
|
|
1121
|
-
*
|
|
1115
|
+
* When a part the page shows only sometimes (a permission, data being present,
|
|
1116
|
+
* a state) is drawn (hub-478), or null when it always is. A page that passes
|
|
1117
|
+
* nothing still draws it:
|
|
1118
|
+
* - a button, an Alert, a badge or the pager: `show<X>?: boolean`. A button's
|
|
1119
|
+
* X is its own handler's (`onStateEstablishmentSizeClick` →
|
|
1120
|
+
* `showStateEstablishmentSize`); the others' is their title or label, in at
|
|
1121
|
+
* most five words, with the place a button's handler would take, so two parts
|
|
1122
|
+
* never share one;
|
|
1123
|
+
* - a record's lone description row: its value is non-null, so a page passes
|
|
1124
|
+
* null for a field the record leaves out.
|
|
1125
|
+
* Null for a probe, and for a part a table row or another part's slot holds: a
|
|
1126
|
+
* row's parts are the row's, and an Alert's action hides with its Alert.
|
|
1127
|
+
* `code` is the part's code with its identifiers marked.
|
|
1122
1128
|
*/
|
|
1123
1129
|
const SHOWN_TAGS = new Set(["Button", "IconButton", "DecisionButton"]);
|
|
1124
|
-
|
|
1130
|
+
const FLAGGED_TAGS = new Set(["Alert", "Badge", "StatusPill", "Chip", "Pagination", "CursorPagination"]);
|
|
1131
|
+
const LONE_DESCRIPTION = /^<DescriptionList\b[^>]*>\s*<DescriptionItem\b[^>]*?\bvalue=\{(__s\d+_[a-z][A-Za-z0-9]*)\}[^>]*\/>\s*<\/DescriptionList>$/;
|
|
1132
|
+
function showCondition(code, node, ctx, path) {
|
|
1125
1133
|
if (!ctx.scopes || ctx.tabProbe || ctx.inTable || ctx.inSlot) return null;
|
|
1126
1134
|
const tag = /^<([A-Za-z]+)/.exec(code)?.[1];
|
|
1127
|
-
|
|
1135
|
+
const lone = LONE_DESCRIPTION.exec(code.trim());
|
|
1136
|
+
if (lone) return `${lone[1]} != null`;
|
|
1137
|
+
if (!SHOWN_TAGS.has(tag) && !FLAGGED_TAGS.has(tag)) return null;
|
|
1128
1138
|
const end = openTagEnd(code);
|
|
1129
1139
|
const attrs = code.slice(tag.length + 1, end);
|
|
1130
|
-
const own = new RegExp(String.raw`\b${HANDLER_ATTR[tag]}=\{(__s\d+)_on([A-Z]\w*)Click\}`).exec(attrs);
|
|
1131
|
-
if (own) return `${own[1]}_show${own[2]}`;
|
|
1132
|
-
// A template
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1140
|
+
const own = SHOWN_TAGS.has(tag) && new RegExp(String.raw`\b${HANDLER_ATTR[tag]}=\{(__s\d+)_on([A-Z]\w*)Click\}`).exec(attrs);
|
|
1141
|
+
if (own) return `${own[1]}_show${own[2]} !== false`;
|
|
1142
|
+
// A button whose template draws no handler takes the words and place nameHandlers would
|
|
1143
|
+
// give it; any other part, the title or label it draws as copy. A label lifted as the
|
|
1144
|
+
// record's (a status Badge's "Processed") is Figma's sample, never a name (hub-468): the
|
|
1145
|
+
// part is named by its kind. Numbered apart from the handlers.
|
|
1146
|
+
const props = node.instance?.props ?? {};
|
|
1147
|
+
const copy = [props.title, props.Title, props.label, props.Label].find((v) => typeof v === "string" && v.trim() && code.includes(v));
|
|
1148
|
+
const label = (SHOWN_TAGS.has(tag) ? handlerLabel(tag, attrs, code.slice(end), node) : handlerWords(copy)) ?? camelName(tag);
|
|
1149
|
+
const { scope, name } = handlerScope(node, { ...ctx, handlers: ctx.shown }, path, null, label, 0);
|
|
1150
|
+
return `${mark(ctx, scope, name.replace(/^on(\w+)Click$/, "show$1"))} !== false`;
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
/**
|
|
1154
|
+
* What a template draws inside one part that the page may still hide (hub-478),
|
|
1155
|
+
* in its marked code:
|
|
1156
|
+
* - an Alert's AlertAction: shown while its button's `show<X>` allows, the
|
|
1157
|
+
* whole AlertAction, so an Alert never keeps an empty one;
|
|
1158
|
+
* - a lone Tab's one trigger (the template's own strip): gated by the strip's
|
|
1159
|
+
* `visibleTabs`, its value its label (`Positions` → `positions`), as a run's
|
|
1160
|
+
* are (tabStrip).
|
|
1161
|
+
*/
|
|
1162
|
+
function hideable(code) {
|
|
1163
|
+
let out = code.replace(/<AlertAction>([\s\S]*?)<\/AlertAction>/g, (block, inner) => {
|
|
1164
|
+
const flagOf = (part) => {
|
|
1165
|
+
const own = /\bonClick=\{(__s\d+)_on([A-Z]\w*)Click\}/.exec(part);
|
|
1166
|
+
return own ? `${own[1]}_show${own[2]}` : null;
|
|
1167
|
+
};
|
|
1168
|
+
const buttons = buttonsIn(inner);
|
|
1169
|
+
if (buttons.length <= 1) {
|
|
1170
|
+
const flag = flagOf(inner);
|
|
1171
|
+
return flag ? `{${flag} !== false && (${block})}` : block;
|
|
1172
|
+
}
|
|
1173
|
+
// Two or more actions: each its own, inside the one AlertAction.
|
|
1174
|
+
let held = inner;
|
|
1175
|
+
for (const b of buttons.reverse()) {
|
|
1176
|
+
const flag = flagOf(b.code);
|
|
1177
|
+
if (flag) held = `${held.slice(0, b.at)}{${flag} !== false && (${b.code})}${held.slice(b.at + b.code.length)}`;
|
|
1178
|
+
}
|
|
1179
|
+
return `<AlertAction>${held}</AlertAction>`;
|
|
1180
|
+
});
|
|
1181
|
+
const strip = /<Tabs panelless value=\{(__s\d+)_tab\}/.exec(out);
|
|
1182
|
+
const triggers = out.match(/<TabsTrigger\b/g) ?? [];
|
|
1183
|
+
if (strip && triggers.length === 1) {
|
|
1184
|
+
out = out.replace(/^([ \t]*)(<TabsTrigger\b[\s\S]*?<\/TabsTrigger>)/m, (_, pad, trigger) => {
|
|
1185
|
+
const label = /\{("(?:[^"\\\n]|\\.)*")\}/.exec(trigger);
|
|
1186
|
+
const value = (label && slug(JSON.parse(label[1]))) || "tab";
|
|
1187
|
+
const body = trigger.replace(/\bvalue="tab"/, `value="${value}"`).split("\n").map((line, i) => (i === 0 ? `${pad} ${line}` : line ? ` ${line}` : line)).join("\n");
|
|
1188
|
+
return `${pad}{(${strip[1]}_visibleTabs?.includes(${JSON.stringify(value)}) ?? true) && (\n${body}\n${pad})}`;
|
|
1189
|
+
});
|
|
1190
|
+
}
|
|
1191
|
+
return out;
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/** Each Button or Icon Button element in `code`: [{ at, code }], its end found past braces (`icon={<X />}`). */
|
|
1195
|
+
function buttonsIn(code) {
|
|
1196
|
+
const found = [];
|
|
1197
|
+
for (const m of code.matchAll(/<(Button|IconButton)(?=[\s/>])/g)) {
|
|
1198
|
+
if (found.some((b) => m.index < b.at + b.code.length)) continue;
|
|
1199
|
+
// openTagEnd is the opening tag's `>`.
|
|
1200
|
+
const gt = m.index + openTagEnd(code.slice(m.index));
|
|
1201
|
+
const selfClosing = /\/\s*$/.test(code.slice(m.index, gt));
|
|
1202
|
+
const close = selfClosing ? gt : code.indexOf(`</${m[1]}>`, gt);
|
|
1203
|
+
if (close === -1) continue;
|
|
1204
|
+
found.push({ at: m.index, code: code.slice(m.index, selfClosing ? gt + 1 : close + m[1].length + 3) });
|
|
1205
|
+
}
|
|
1206
|
+
return found;
|
|
1136
1207
|
}
|
|
1137
1208
|
|
|
1138
1209
|
/** `drawn` (indented one deeper than `depth`) drawn only while `condition` holds. */
|
|
@@ -1285,6 +1356,9 @@ function textNextTo(node, ctx) {
|
|
|
1285
1356
|
/** The form controls the page owns: a value it passes and a change it hears (NODII-DEV-CLI-82, -83). */
|
|
1286
1357
|
const FIELD_CONTROLS = { Input: "onChange", Textarea: "onChange", SelectField: "onValueChange", RadioGroup: "onValueChange" };
|
|
1287
1358
|
|
|
1359
|
+
/** The form controls whose template may wire them with generic names, and the change each hears (hub-481). */
|
|
1360
|
+
const NAMED_CHANGE = { ...FIELD_CONTROLS, MultiSelect: "onValueChange", DateTimePicker: "onChange", DatePicker: "onValueChange", DateRangePicker: "onChange" };
|
|
1361
|
+
|
|
1288
1362
|
/**
|
|
1289
1363
|
* A control the template half-controls (`defaultValue="chart"` beside
|
|
1290
1364
|
* `onValueChange={setView}`, a Segmented Control's view switch), or that
|
|
@@ -1335,8 +1409,25 @@ function fieldScope(node, ctx, code) {
|
|
|
1335
1409
|
* and a change calls back. A SelectField's id is its own, not the template's
|
|
1336
1410
|
* shared `id="select"`. The scope also names the template's own identifiers
|
|
1337
1411
|
* (a select's options), so two filters never share one.
|
|
1412
|
+
*
|
|
1413
|
+
* A template that wires the control itself with generic names (an Input's
|
|
1414
|
+
* `value={value} onChange={onChange}`, a Date Time Picker's
|
|
1415
|
+
* `onChange={setValue}`, a Date Picker's `onValueChange={setDate}`) has them
|
|
1416
|
+
* named from its field, `<field>Value` and `on<Field>Change`, as a
|
|
1417
|
+
* SelectField's are; a date picker's sample or empty value (`value=""`) is the
|
|
1418
|
+
* page's too, in every view. Two fields of one label in one view are told
|
|
1419
|
+
* apart by their place (resolveScopes) (hub-481).
|
|
1338
1420
|
*/
|
|
1339
|
-
function liftFieldControls(code, node, ctx, lift) {
|
|
1421
|
+
function liftFieldControls(code, node, ctx, lift, scope) {
|
|
1422
|
+
code = rewriteTags(code, Object.keys(NAMED_CHANGE).join("|"), (attrs, tag) => {
|
|
1423
|
+
const handler = NAMED_CHANGE[tag];
|
|
1424
|
+
const change = new RegExp(String.raw`(^|\s)${handler}=\{(?:onChange|onValueChange|set(?:Value|Date|Range))\}`).exec(attrs);
|
|
1425
|
+
if (!change) return attrs;
|
|
1426
|
+
const named = attrs.replace(change[0], `${change[1]}${handler}={${lift(scope, `on${cap(scope)}Change`)}}`);
|
|
1427
|
+
const sample = String.raw`|"(?:[^"\\\n]|\\.)*"|\{\{[^{}]*\}\}|\{new Date\([^()]*\)\}`;
|
|
1428
|
+
const value = new RegExp(String.raw`(^|\s)value=(?:\{(?:value|date|range)\}${/^Date/.test(tag) ? sample : ""})`).exec(named);
|
|
1429
|
+
return value ? named.replace(value[0], `${value[1]}value={${lift(scope, `${scope}Value`)}}`) : named;
|
|
1430
|
+
});
|
|
1340
1431
|
// A field's placeholder is the page's too: in a Surface it is the value the frame shows
|
|
1341
1432
|
// ("Last 30 days", "K*** M***", "Sep 2026"), and a search box's copy names the list
|
|
1342
1433
|
// the page searches (hub-468). Named by the field, as its value is.
|
|
@@ -1971,7 +2062,7 @@ function emitNode(node, ctx) {
|
|
|
1971
2062
|
const scope = control ? fieldScope(node, ctx, own) : toggle ? camelName(toggle) : scopeName(node, own, ctx);
|
|
1972
2063
|
own = nameHandlers(own, node, ctx, path, scope);
|
|
1973
2064
|
own = controlHalfControlled(liftRecordData(own, node, ctx, path, scope));
|
|
1974
|
-
|
|
2065
|
+
let code = ctx.scopes ? scopeIdentifiers(own, scope, ctx.scopes, ctx.layer) : own;
|
|
1975
2066
|
if (ctx.optionals && ctx.scopes) {
|
|
1976
2067
|
// Each identifier is its drawing tag's: the outer template's own list, or, for a part it
|
|
1977
2068
|
// draws through executeTemplate (a Field's Input), that part's template's (DESIGN-SYSTEM-507).
|
|
@@ -1987,15 +2078,17 @@ function emitNode(node, ctx) {
|
|
|
1987
2078
|
(listed.has(m[1]) ? ctx.optionals.optional : ctx.optionals.required).add(m[0]);
|
|
1988
2079
|
}
|
|
1989
2080
|
}
|
|
2081
|
+
// What the page may still hide inside this part, after the optional props are read (hub-478).
|
|
2082
|
+
if (ctx.scopes && !ctx.tabProbe) code = hideable(code);
|
|
1990
2083
|
// A Table part is valid only inside the DS Table. One the generator could not assemble
|
|
1991
2084
|
// into a Table stays reported, never passed off as mapped (MW8-17.3).
|
|
1992
2085
|
const tag = /^<([A-Za-z]+)/.exec(code)?.[1];
|
|
1993
2086
|
if (!ctx.inTable && (tablePart(tag) || tag === SKELETON_ROWS)) note(path, "table", `${describe(node)} is a Table part outside any table the generator recognises`);
|
|
1994
|
-
const
|
|
1995
|
-
const at =
|
|
2087
|
+
const shown = showCondition(code, node, ctx, path);
|
|
2088
|
+
const at = shown ? depth + 1 : depth;
|
|
1996
2089
|
// Layout around a DS component is the page's; the component itself takes no className (U07-19).
|
|
1997
2090
|
const drawn = !sizing.length ? indent(code, at) : [indent(`<div className="${cls(["flex", "flex-col", ...sizing])}">`, at), indent(code, at + 1), indent("</div>", at)].join("\n");
|
|
1998
|
-
return
|
|
2091
|
+
return shown ? shownWhen(shown, drawn, depth) : drawn;
|
|
1999
2092
|
}
|
|
2000
2093
|
|
|
2001
2094
|
if (node.type === "TEXT") {
|
|
@@ -2049,10 +2142,37 @@ function emitNode(node, ctx) {
|
|
|
2049
2142
|
return indent(comment(`unmapped: ${node.type.toLowerCase()} ${JSON.stringify(node.name)} has no auto layout`), depth);
|
|
2050
2143
|
}
|
|
2051
2144
|
const classes = cls([...sizeClasses(node, parent, root), ...layoutClasses(node), node.clips ? "overflow-hidden" : null]);
|
|
2145
|
+
const shown = root ? null : sectionCondition(node, ctx, path);
|
|
2052
2146
|
const kids = emitChildren(node.children ?? [], child, { ...ctx, depth: depth + 1, parent: node, grand: parent, path, root: false, chain });
|
|
2053
2147
|
const open = `<div${classes ? ` className="${classes}"` : ""}`;
|
|
2054
|
-
|
|
2055
|
-
return
|
|
2148
|
+
const drawn = !kids.length ? indent(`${open} />`, depth) : [indent(`${open}>`, depth), ...kids, indent("</div>", depth)].join("\n");
|
|
2149
|
+
return shown ? shownWhen(shown, indent(drawn, 1), depth) : drawn;
|
|
2150
|
+
}
|
|
2151
|
+
|
|
2152
|
+
/**
|
|
2153
|
+
* When a section, card, rail or row the page shows only sometimes is drawn
|
|
2154
|
+
* (hub-478): a frame the Surfaces name as a place, `<Kind> · <Name>`
|
|
2155
|
+
* ("Card · Peer invitation", "Rail · Your decision", "Fact · Close note"),
|
|
2156
|
+
* takes `show<Name>?: boolean`, its name's words with the place a button's
|
|
2157
|
+
* handler would take, numbered apart from the handlers. So does a frame that
|
|
2158
|
+
* opens with its own heading ("Peer-feedback invitations" over its rows),
|
|
2159
|
+
* named by the heading; a heading with a figure is the record's, never a name.
|
|
2160
|
+
* A page that passes nothing draws it. Null for a frame of a table's own
|
|
2161
|
+
* anatomy, one a table row or another part's slot holds, and a probe.
|
|
2162
|
+
*/
|
|
2163
|
+
const PLACE_FRAME = /^\s*([A-Za-z][A-Za-z ]*?)\s*·\s*(.*[A-Za-z].*)$/;
|
|
2164
|
+
const TABLE_ANATOMY = new Set(["table", "cell"]);
|
|
2165
|
+
function sectionCondition(node, ctx, path) {
|
|
2166
|
+
if (!ctx.scopes || ctx.tabProbe || ctx.inTable || ctx.inSlot || node.instance) return null;
|
|
2167
|
+
const m = PLACE_FRAME.exec(node.name ?? "");
|
|
2168
|
+
// A head of only text (a title and its subtitle) is not the section: its body sits beside it.
|
|
2169
|
+
const [first, ...rest] = (node.children ?? []).filter((c) => c.visible !== false);
|
|
2170
|
+
const body = rest.some((c) => c.type !== "TEXT");
|
|
2171
|
+
const heading = body && first?.type === "TEXT" && HEADING_STYLE.test(first.textStyle ?? "") && !RECORD_TEXT(first.text ?? "") ? first.text : null;
|
|
2172
|
+
const words = m ? (TABLE_ANATOMY.has(m[1].trim().toLowerCase()) ? null : handlerWords(m[2])) : handlerWords(heading);
|
|
2173
|
+
if (!words) return null;
|
|
2174
|
+
const { scope, name } = handlerScope(node, { ...ctx, handlers: ctx.shown }, path, null, words, 0);
|
|
2175
|
+
return `${mark(ctx, scope, name.replace(/^on(\w+)Click$/, "show$1"))} !== false`;
|
|
2056
2176
|
}
|
|
2057
2177
|
|
|
2058
2178
|
/**
|