@nodii/dev 0.6.138 → 0.6.139
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 +132 -3
- 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.139";
|
|
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 = 13;
|
|
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). */
|
|
@@ -536,7 +536,7 @@ export function emitSurface(tree, row, ds) {
|
|
|
536
536
|
used.add(state);
|
|
537
537
|
const fn = `${pascal(state)}View`;
|
|
538
538
|
const note = (path, kind, detail) => unmapped.push({ variant: variant.name, path, kind, detail });
|
|
539
|
-
const emitted = emitNode(pageBody(variant, row), { depth: 2, parent: null, path: variant.name, ds, imports, note, root: true, scopes, state, lists: new Map(), shapes }) ?? "";
|
|
539
|
+
const emitted = emitNode(pageBody(variant, row), { depth: 2, parent: null, path: variant.name, ds, imports, note, root: true, scopes, state, lists: new Map(), shapes, handlers: new Map() }) ?? "";
|
|
540
540
|
// A view the generator can't lay out is no page at all: generation fails rather than ship
|
|
541
541
|
// an empty view (NODII-DEV-CLI-81).
|
|
542
542
|
if (emitted.trimStart().startsWith("{/*")) {
|
|
@@ -914,6 +914,134 @@ function positionScope(node, ctx, kind) {
|
|
|
914
914
|
return camelName(`${ctx.parent?.name ?? kind} ${kind}${siblings.length > 1 ? ` ${siblings.indexOf(node) + 1}` : ""}`);
|
|
915
915
|
}
|
|
916
916
|
|
|
917
|
+
/**
|
|
918
|
+
* The parts whose template writes one generic handler, whatever the part does:
|
|
919
|
+
* a Button's and a DecisionButton's `onClick={onClick}`, an Empty State's
|
|
920
|
+
* `onAction={onAction}`. An Icon Button's template writes none.
|
|
921
|
+
*/
|
|
922
|
+
const HANDLER_ATTR = { Button: "onClick", DecisionButton: "onClick", IconButton: "onClick", EmptyState: "onAction" };
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* Every button gets its own handler, named by what it does (hub-464):
|
|
926
|
+
* `on<label>Click` from its own label (a Button's text, an Empty State's
|
|
927
|
+
* actionLabel, an aria-label), a DecisionButton's from its decision (Reject is
|
|
928
|
+
* `onRejectClick`, never Approve's), an Icon Button's from its layer name or
|
|
929
|
+
* icon, and an Icon Button gets the handler its template leaves out. The name
|
|
930
|
+
* is marked with the section it sits in (a button a template draws inside
|
|
931
|
+
* another part, with that part's scope), so two buttons with one label in two
|
|
932
|
+
* sections become `on<section><label>Click` and the same button in another
|
|
933
|
+
* view keeps its name (resolveScopes). A second button with one name in one
|
|
934
|
+
* section of a view is numbered.
|
|
935
|
+
*/
|
|
936
|
+
function nameHandlers(code, node, ctx, path, scope) {
|
|
937
|
+
const top = /^<([A-Za-z]+)/.exec(code)?.[1];
|
|
938
|
+
return rewriteTags(code, Object.keys(HANDLER_ATTR).join("|"), (attrs, tag, end, at) => {
|
|
939
|
+
const attr = HANDLER_ATTR[tag];
|
|
940
|
+
const generic = new RegExp(String.raw`\b${attr}=\{${attr}\}`).exec(attrs);
|
|
941
|
+
if (!generic && (tag !== "IconButton" || new RegExp(String.raw`\b${attr}=`).test(attrs))) return attrs;
|
|
942
|
+
const own = at === 0 && tag === top;
|
|
943
|
+
const label = handlerLabel(tag, attrs, code.slice(end), own ? node : null) ?? (own ? null : scope);
|
|
944
|
+
if (!label) return attrs;
|
|
945
|
+
const handler = handlerScope(node, ctx, path, own ? null : scope, label, at);
|
|
946
|
+
const ref = `${attr}={${mark(ctx, handler.scope, handler.name)}}`;
|
|
947
|
+
// An Icon Button's template draws no handler: the page's is added (hub-464).
|
|
948
|
+
return generic ? attrs.replace(generic[0], ref) : attrs.replace(/\s*\/?$/, (close) => ` ${ref}${close}`);
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/** Text as a handler's words: no figure, at most five words, in camelCase; null when nothing is left. */
|
|
953
|
+
function handlerWords(text) {
|
|
954
|
+
const words = (String(text ?? "").match(/[A-Za-z0-9]+/g) ?? []).filter((w) => /[A-Za-z]/.test(w)).slice(0, 5);
|
|
955
|
+
return words.length ? camelName(words.join(" ")) : null;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
/**
|
|
959
|
+
* What a button does, as a handler's words: a DecisionButton's decision; an
|
|
960
|
+
* Empty State's actionLabel; an aria-label; a Button's own text (`after` is the
|
|
961
|
+
* code after its opening tag); an Icon Button's layer name ("IconBtn · Reassign")
|
|
962
|
+
* or its icon. `node` is the instance the button is (null for a button a
|
|
963
|
+
* template draws inside another part).
|
|
964
|
+
*/
|
|
965
|
+
function handlerLabel(tag, attrs, after, node) {
|
|
966
|
+
const literal = (name) => {
|
|
967
|
+
const m = new RegExp(String.raw`(?:^|\s)${name}=(${LITERAL})`).exec(attrs);
|
|
968
|
+
return m ? literalText(m[1]) : null;
|
|
969
|
+
};
|
|
970
|
+
if (tag === "DecisionButton") return handlerWords((/\bdecision="([a-z_]+)"/.exec(attrs)?.[1] ?? "").replace(/_/g, " "));
|
|
971
|
+
// An Empty State whose action label the Surface doesn't draw is named by its title.
|
|
972
|
+
if (tag === "EmptyState") return handlerWords(literal("actionLabel") ?? literal("title"));
|
|
973
|
+
const aria = handlerWords(literal("aria-label"));
|
|
974
|
+
if (aria) return aria;
|
|
975
|
+
if (tag === "IconButton") {
|
|
976
|
+
const layer = node ? handlerWords(node.name.replace(/^\s*icon ?(?:btn|button)\b[\s·:\-–]*/i, "")) : null;
|
|
977
|
+
if (layer && layer !== camelName(node.instance?.set ?? "")) return layer;
|
|
978
|
+
const icon = /\bicon=\{<([A-Z][A-Za-z0-9]*?)(?:Icon)?[\s/>]/.exec(attrs)?.[1];
|
|
979
|
+
return handlerWords(literal("label")) ?? handlerWords(icon?.replace(/([a-z0-9])([A-Z])/g, "$1 $2"));
|
|
980
|
+
}
|
|
981
|
+
if (/\/\s*$/.test(attrs)) return null;
|
|
982
|
+
const close = after.indexOf(`</${tag}>`);
|
|
983
|
+
const inner = close === -1 ? "" : after.slice(0, close);
|
|
984
|
+
const strings = [...inner.matchAll(/\{("(?:[^"\\\n]|\\.)*")\}/g)].map((m) => JSON.parse(m[1]));
|
|
985
|
+
const bare = inner.replace(/<[^>]*>/g, " ").replace(/\{[^{}]*\}/g, " ").trim();
|
|
986
|
+
return handlerWords(strings.length ? strings.join(" ") : bare) ?? (node ? handlerWords(node.name.replace(/^\s*button\b[\s·:\-–]*/i, "")) : null);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
/**
|
|
990
|
+
* A button's handler `{ scope, name }`: `on<label>Click`, marked with `inner`
|
|
991
|
+
* (the part a template draws it in), else where it sits: the heading of its
|
|
992
|
+
* section, else the title of the part before it (a step's Section), else its
|
|
993
|
+
* nearest named frame (a Toolbar), else the page. A second button of one label
|
|
994
|
+
* in one place of one view is numbered (`onAdd2Click`); a node emitted again
|
|
995
|
+
* (a table's cells, a tab strip read twice) keeps its number.
|
|
996
|
+
*/
|
|
997
|
+
const UNNAMED_FRAME = /^(?:frame|group|auto ?layout|rectangle|container|slot)(?: \d+)?$/i;
|
|
998
|
+
function handlerScope(node, ctx, path, inner, label, at) {
|
|
999
|
+
const chain = ctx.chain ?? [];
|
|
1000
|
+
const frames = [...chain.slice(1)].reverse().filter((c) => c.type !== "SLOT" && !c.instance && /[A-Za-z]/.test(c.name ?? ""));
|
|
1001
|
+
// A frame named as the Surfaces name a place ("Row · Email domain allowlist") is that place.
|
|
1002
|
+
const place = frames.map((c) => /\s·\s*(.*[A-Za-z].*)$/.exec(c.name)?.[1]).find(Boolean);
|
|
1003
|
+
const named = frames.find((c) => !UNNAMED_FRAME.test(c.name.trim()))?.name;
|
|
1004
|
+
const scope = inner ?? camelName(place ?? sectionHeading(node, chain) ?? titleBefore(node, chain) ?? named ?? "page");
|
|
1005
|
+
const name = `on${cap(label)}Click`;
|
|
1006
|
+
if (!ctx.handlers || !ctx.scopes) return { scope, name };
|
|
1007
|
+
const key = `${scope}|${name}`;
|
|
1008
|
+
const seen = ctx.handlers.get(key) ?? ctx.handlers.set(key, []).get(key);
|
|
1009
|
+
// The probes (descriptionOf, tabOf) emit a node with no parent, so its place is read from the chain.
|
|
1010
|
+
const id = `${path}@${node.x},${node.y}:${(chain.at(-1)?.children ?? []).indexOf(node)}#${at}`;
|
|
1011
|
+
const n = seen.includes(id) ? seen.indexOf(id) : seen.push(id) - 1;
|
|
1012
|
+
return { scope, name: n ? `on${cap(label)}${n + 1}Click` : name };
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
/**
|
|
1016
|
+
* The title of the nearest DS part before `target` that has one (a step's
|
|
1017
|
+
* Section, in its "Card · Claim" frame), looking up its frames.
|
|
1018
|
+
*/
|
|
1019
|
+
function titleBefore(target, chain) {
|
|
1020
|
+
const titled = (node) => {
|
|
1021
|
+
if (node.visible === false) return null;
|
|
1022
|
+
if (node.instance?.remote) {
|
|
1023
|
+
// A Section names it `Title`, most parts `title`.
|
|
1024
|
+
const title = Object.entries(node.instance.props ?? {}).find(([k]) => /^title$/i.test(k))?.[1];
|
|
1025
|
+
return typeof title === "string" && /[A-Za-z]/.test(title) ? title : null;
|
|
1026
|
+
}
|
|
1027
|
+
for (const c of node.children ?? []) {
|
|
1028
|
+
const found = titled(c);
|
|
1029
|
+
if (found) return found;
|
|
1030
|
+
}
|
|
1031
|
+
return null;
|
|
1032
|
+
};
|
|
1033
|
+
let cur = target;
|
|
1034
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
1035
|
+
const shown = (chain[i].children ?? []).filter((c) => c.visible !== false);
|
|
1036
|
+
for (const sibling of shown.slice(0, Math.max(shown.indexOf(cur), 0)).reverse()) {
|
|
1037
|
+
const found = titled(sibling);
|
|
1038
|
+
if (found) return found;
|
|
1039
|
+
}
|
|
1040
|
+
cur = chain[i];
|
|
1041
|
+
}
|
|
1042
|
+
return null;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
917
1045
|
/** The form controls the page owns: a value it passes and a change it hears (NODII-DEV-CLI-82, -83). */
|
|
918
1046
|
const FIELD_CONTROLS = { Input: "onChange", Textarea: "onChange", SelectField: "onValueChange", RadioGroup: "onValueChange" };
|
|
919
1047
|
|
|
@@ -997,7 +1125,7 @@ function rewriteTags(code, names, fn) {
|
|
|
997
1125
|
if (m.index < last) continue;
|
|
998
1126
|
const from = m.index + m[0].length;
|
|
999
1127
|
const end = m.index + openTagEnd(code.slice(m.index));
|
|
1000
|
-
out += code.slice(last, from) + fn(code.slice(from, end), m[1]);
|
|
1128
|
+
out += code.slice(last, from) + fn(code.slice(from, end), m[1], end, m.index);
|
|
1001
1129
|
last = end;
|
|
1002
1130
|
}
|
|
1003
1131
|
return out + code.slice(last);
|
|
@@ -1412,6 +1540,7 @@ function emitNode(node, ctx) {
|
|
|
1412
1540
|
if (!ctx.tabProbe) own = own.replace(/<Tabs panelless(?: defaultValue="[^"]*")?>/g, CONTROLLED_TABS("tab", "onTabChange"));
|
|
1413
1541
|
const control = /^<(?:Input|Textarea|SelectField)\b/.test(own.replace(/^<Field\b[^>]*>\s*/, ""));
|
|
1414
1542
|
const scope = control ? fieldScope(node, ctx, own) : scopeName(node, own, ctx);
|
|
1543
|
+
own = nameHandlers(own, node, ctx, path, scope);
|
|
1415
1544
|
own = controlHalfControlled(liftRecordData(own, node, ctx, path, scope));
|
|
1416
1545
|
const code = ctx.scopes ? scopeIdentifiers(own, scope, ctx.scopes) : own;
|
|
1417
1546
|
// A Table part is valid only inside the DS Table. One the generator could not assemble
|