@nodii/dev 0.6.137 → 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 +277 -18
- 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). */
|
|
@@ -528,13 +528,15 @@ export function emitSurface(tree, row, ds) {
|
|
|
528
528
|
}
|
|
529
529
|
const used = new Set();
|
|
530
530
|
const scopes = new Map();
|
|
531
|
+
// Each list's columns by name, across views (listScope).
|
|
532
|
+
const shapes = new Map();
|
|
531
533
|
const views = tree.variants.map((variant) => {
|
|
532
534
|
let state = variant.state;
|
|
533
535
|
while (used.has(state)) state += "-x";
|
|
534
536
|
used.add(state);
|
|
535
537
|
const fn = `${pascal(state)}View`;
|
|
536
538
|
const note = (path, kind, detail) => unmapped.push({ variant: variant.name, path, kind, detail });
|
|
537
|
-
const emitted = emitNode(pageBody(variant, row), { depth: 2, parent: null, path: variant.name, ds, imports, note, root: true, scopes, state }) ?? "";
|
|
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() }) ?? "";
|
|
538
540
|
// A view the generator can't lay out is no page at all: generation fails rather than ship
|
|
539
541
|
// an empty view (NODII-DEV-CLI-81).
|
|
540
542
|
if (emitted.trimStart().startsWith("{/*")) {
|
|
@@ -546,7 +548,7 @@ export function emitSurface(tree, row, ds) {
|
|
|
546
548
|
const stateType = `${component}State`;
|
|
547
549
|
const propsType = `${component}Props`;
|
|
548
550
|
resolveScopes(views, scopes);
|
|
549
|
-
const data = dataProps(views);
|
|
551
|
+
const data = dataProps(views, propsType);
|
|
550
552
|
const reactTypes = [data.size && "ComponentProps", [...data.values()].some((d) => /\bReactNode\b/.test(propType(d))) && "ReactNode"].filter(Boolean);
|
|
551
553
|
const drawn = [...data.values()].some((d) => [...d.attrs.values(), ...[...d.members.values()].flatMap((refs) => [...refs.values()])].some((r) => r.keys));
|
|
552
554
|
const header = [
|
|
@@ -568,7 +570,7 @@ export function emitSurface(tree, row, ds) {
|
|
|
568
570
|
? [
|
|
569
571
|
"/** The data and callbacks the Surface's Code Connect templates take (DS-BLK-1): each typed as the DS component it reaches. */",
|
|
570
572
|
`export interface ${propsType} {`,
|
|
571
|
-
...[...data.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([name, d]) => ` ${name}: ${propType(d)};`),
|
|
573
|
+
...[...data.entries()].sort(([a], [b]) => a.localeCompare(b)).map(([name, d]) => ` ${name}${d.optional ? "?" : ""}: ${d.raw ?? propType(d)};`),
|
|
572
574
|
"}",
|
|
573
575
|
"",
|
|
574
576
|
]
|
|
@@ -703,6 +705,61 @@ function textBeside(node, ctx) {
|
|
|
703
705
|
return before(ctx.parent, node) ?? (ctx.grand ? before(ctx.grand, ctx.parent) : null);
|
|
704
706
|
}
|
|
705
707
|
|
|
708
|
+
/** A section's own title: heading-styled text, or the title of a DS section or card header. */
|
|
709
|
+
const HEADING_STYLE = /heading|title/i;
|
|
710
|
+
const HEADER_SET = /\b(section|card|page) ?head(er)?\b/i;
|
|
711
|
+
function headingIn(node) {
|
|
712
|
+
if (node.visible === false) return null;
|
|
713
|
+
if (node.type === "TEXT") return HEADING_STYLE.test(node.textStyle ?? "") && /[A-Za-z0-9]/.test(node.text ?? "") ? node.text : null;
|
|
714
|
+
if (node.instance?.remote) {
|
|
715
|
+
const title = node.instance.props?.title;
|
|
716
|
+
return HEADER_SET.test(node.instance.set ?? "") && typeof title === "string" && /[A-Za-z0-9]/.test(title) ? title : null;
|
|
717
|
+
}
|
|
718
|
+
for (const c of node.children ?? []) {
|
|
719
|
+
const found = headingIn(c);
|
|
720
|
+
if (found) return found;
|
|
721
|
+
}
|
|
722
|
+
return null;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
/**
|
|
726
|
+
* The heading of the section a part sits in: the nearest heading drawn before
|
|
727
|
+
* it, looking back through its own frame, then its frame's frame, up to the view.
|
|
728
|
+
* `chain` is the part's ancestors, nearest last.
|
|
729
|
+
*/
|
|
730
|
+
function sectionHeading(target, chain) {
|
|
731
|
+
let cur = target;
|
|
732
|
+
for (let i = chain.length - 1; i >= 0; i--) {
|
|
733
|
+
const shown = (chain[i].children ?? []).filter((c) => c.visible !== false);
|
|
734
|
+
const at = shown.indexOf(cur);
|
|
735
|
+
for (const sibling of shown.slice(0, Math.max(at, 0)).reverse()) {
|
|
736
|
+
const found = headingIn(sibling);
|
|
737
|
+
if (found) return found;
|
|
738
|
+
}
|
|
739
|
+
cur = chain[i];
|
|
740
|
+
}
|
|
741
|
+
return null;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* The scope a list (a table's rows, a run of description lists) is named by:
|
|
746
|
+
* its section's heading, else `fallback`. Two lists in one view never share a
|
|
747
|
+
* scope, so never a prop (hub-454): a second list under the same
|
|
748
|
+
* heading is numbered. The same list in another view takes the same name; a
|
|
749
|
+
* list of another `shape` (a table's columns) in another view is numbered too,
|
|
750
|
+
* so one prop is never typed as two tables merged.
|
|
751
|
+
*/
|
|
752
|
+
function listScope(target, ctx, fallback, shape = null) {
|
|
753
|
+
const base = sectionHeading(target, ctx.chain ?? []) ?? fallback;
|
|
754
|
+
let name = camelName(base);
|
|
755
|
+
if (!ctx.lists) return name;
|
|
756
|
+
const taken = (n) => (ctx.lists.has(n) && ctx.lists.get(n) !== target) || (shape !== null && ctx.shapes?.has(n) && ctx.shapes.get(n) !== shape);
|
|
757
|
+
for (let n = 2; taken(name); n++) name = camelName(`${base} ${n}`);
|
|
758
|
+
ctx.lists.set(name, target);
|
|
759
|
+
if (shape !== null) ctx.shapes?.set(name, shape);
|
|
760
|
+
return name;
|
|
761
|
+
}
|
|
762
|
+
|
|
706
763
|
/** Text in camelCase as an identifier (`Subject kind` → `subjectKind`, `2FA` → `field2fa`). */
|
|
707
764
|
function camelName(text) {
|
|
708
765
|
const words = String(text ?? "").match(/[A-Za-z0-9]+/g) ?? [];
|
|
@@ -782,6 +839,16 @@ function liftRecordData(code, node, ctx, path, scope = scopeName(node)) {
|
|
|
782
839
|
next = tone ? next.replace(tone[0], `tone={${lift(scope, "tone")}}`) : next.replace(`label={`, `tone={${lift(scope, "tone")}} label={`);
|
|
783
840
|
return `<StatusPill${next}${close}>`;
|
|
784
841
|
});
|
|
842
|
+
// A Badge beside a record (outside a table) is its status: label and tone are the record's,
|
|
843
|
+
// as a StatusPill's (NODII-DEV-CLI-82). In a table, the lifted rows carry it.
|
|
844
|
+
if (!ctx.inTable) {
|
|
845
|
+
out = out.replace(/^<Badge\b([^>]*)>([^<>{}\n]*[A-Za-z0-9][^<>{}\n]*)<\/Badge>$/, (all, attrs) => {
|
|
846
|
+
const at = positionScope(node, ctx, "status");
|
|
847
|
+
const tone = new RegExp(String.raw`\btone=${LITERAL}`).exec(attrs);
|
|
848
|
+
const toned = tone ? attrs.replace(tone[0], `tone={${lift(at, "tone")}}`) : ` tone={${lift(at, "tone")}}${attrs}`;
|
|
849
|
+
return `<Badge${toned}>{${lift(at, "label")}}</Badge>`;
|
|
850
|
+
});
|
|
851
|
+
}
|
|
785
852
|
out = rewriteTags(out, String.raw`Metric(?!CardGroup\b)[A-Za-z]*`, (attrs, tag) => {
|
|
786
853
|
const name = new RegExp(String.raw`\b(?:title|label)=(${LITERAL})`).exec(attrs);
|
|
787
854
|
const at = name ? camelName(literalText(name[1])) : scope;
|
|
@@ -799,10 +866,11 @@ function liftRecordData(code, node, ctx, path, scope = scopeName(node)) {
|
|
|
799
866
|
return next.replace(/\s*\/?$/, (end) => ` ${selected}${end}`);
|
|
800
867
|
});
|
|
801
868
|
out = rewriteTags(out, "Pagination|CursorPagination", (attrs, tag) => {
|
|
802
|
-
if (tag === "Pagination" && !/\btype="cursor-bar"/.test(attrs)) return attrs;
|
|
803
869
|
let next = attrs;
|
|
870
|
+
// A numbered pager's summary ("Showing 1 – 5 of 5 results") is list state too.
|
|
804
871
|
const summary = new RegExp(String.raw`\bsummary=${LITERAL}`).exec(next);
|
|
805
872
|
if (summary) next = next.replace(summary[0], `summary={${lift(scope, "summary")}}`);
|
|
873
|
+
if (tag === "Pagination" && !/\btype="cursor-bar"/.test(attrs)) return next;
|
|
806
874
|
for (const attr of ["hasNext", "hasPrev"]) {
|
|
807
875
|
const value = `${attr}={${lift(scope, attr)}}`;
|
|
808
876
|
const bare = bareAttr(next, attr);
|
|
@@ -833,12 +901,147 @@ function liftRecordData(code, node, ctx, path, scope = scopeName(node)) {
|
|
|
833
901
|
*/
|
|
834
902
|
export const SAMPLE_VALUE = /₹\s?\d|\b\d{1,2} (?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b|[\w.+-]+@[a-z0-9-]+\.[a-z]|\b[a-z0-9-]+\.(?:com|in|io|co|org|net)\b|\b[A-Z]{2,5}-\d[\w-]*\b/;
|
|
835
903
|
|
|
904
|
+
/**
|
|
905
|
+
* Loose text that is a record's value: a sample value, or any text with a figure
|
|
906
|
+
* in it ("3 waiting on you", "v2 · published", "Showing 1 - 10 of 46"), which is a
|
|
907
|
+
* count, a version or a date, never fixed copy (NODII-DEV-CLI-82).
|
|
908
|
+
*/
|
|
909
|
+
const RECORD_TEXT = (text) => SAMPLE_VALUE.test(text) || /\d/.test(text);
|
|
910
|
+
|
|
836
911
|
/** A part named by where it sits: its parent's name, numbered when the parent holds several of the same set. */
|
|
837
912
|
function positionScope(node, ctx, kind) {
|
|
838
913
|
const siblings = (ctx.parent?.children ?? []).filter((c) => c.visible !== false && (node.instance ? c.instance?.set === node.instance.set : c.type === node.type));
|
|
839
914
|
return camelName(`${ctx.parent?.name ?? kind} ${kind}${siblings.length > 1 ? ` ${siblings.indexOf(node) + 1}` : ""}`);
|
|
840
915
|
}
|
|
841
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
|
+
|
|
842
1045
|
/** The form controls the page owns: a value it passes and a change it hears (NODII-DEV-CLI-82, -83). */
|
|
843
1046
|
const FIELD_CONTROLS = { Input: "onChange", Textarea: "onChange", SelectField: "onValueChange", RadioGroup: "onValueChange" };
|
|
844
1047
|
|
|
@@ -922,7 +1125,7 @@ function rewriteTags(code, names, fn) {
|
|
|
922
1125
|
if (m.index < last) continue;
|
|
923
1126
|
const from = m.index + m[0].length;
|
|
924
1127
|
const end = m.index + openTagEnd(code.slice(m.index));
|
|
925
|
-
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);
|
|
926
1129
|
last = end;
|
|
927
1130
|
}
|
|
928
1131
|
return out + code.slice(last);
|
|
@@ -972,9 +1175,14 @@ function resolveScopes(views, scopes) {
|
|
|
972
1175
|
for (const view of views) view.body = view.body.replace(marked, (_, n, name) => (users.get(name).size > 1 ? scoped(names[Number(n)], name) : name));
|
|
973
1176
|
}
|
|
974
1177
|
|
|
975
|
-
function dataProps(views) {
|
|
1178
|
+
function dataProps(views, propsType) {
|
|
976
1179
|
const data = new Map();
|
|
977
1180
|
const entry = (name) => data.get(name) ?? data.set(name, { attrs: new Map(), child: false, nested: false, members: new Map(), list: false }).get(name);
|
|
1181
|
+
// List state the generator itself writes (tableOf), optional: a page without a record
|
|
1182
|
+
// route or a server sort passes nothing (NODII-DEV-CLI-83).
|
|
1183
|
+
const own = (name, raw) => Object.assign(entry(name), { raw, optional: true });
|
|
1184
|
+
const keysOf = (name) => entry(name).sortKeys ?? (entry(name).sortKeys = new Set());
|
|
1185
|
+
const union = (keys) => [...keys].sort().map((k) => JSON.stringify(k)).join(" | ");
|
|
978
1186
|
const member = (d, name) => d.members.get(name) ?? d.members.set(name, new Map()).get(name);
|
|
979
1187
|
for (const view of views) {
|
|
980
1188
|
view.uses = new Set();
|
|
@@ -986,8 +1194,21 @@ function dataProps(views) {
|
|
|
986
1194
|
view.uses.add(o.list);
|
|
987
1195
|
}
|
|
988
1196
|
const itemList = (at, name) => (openers.some((o) => o.param === name) ? listAt(openers, at, name) : null);
|
|
1197
|
+
for (const m of body.matchAll(/onClick=\{([a-z][A-Za-z0-9]*) && \(\(\) => \1\(([a-z][A-Za-z0-9]*)\)\)\}/g)) {
|
|
1198
|
+
const list = listAt(openers, m.index, m[2]);
|
|
1199
|
+
if (!list) continue;
|
|
1200
|
+
own(m[1], `(row: ${propsType}[${JSON.stringify(list)}][number]) => void`);
|
|
1201
|
+
view.uses.add(m[1]);
|
|
1202
|
+
}
|
|
1203
|
+
for (const m of body.matchAll(/\bsort=\{([a-z][A-Za-z0-9]*) === ("(?:[^"\\\n]|\\.)*") \? ([a-z][A-Za-z0-9]*) : false\} onSort=\{([a-z][A-Za-z0-9]*) && /g)) {
|
|
1204
|
+
const [, by, key, dir, change] = m;
|
|
1205
|
+
keysOf(by).add(JSON.parse(key));
|
|
1206
|
+
keysOf(change).add(JSON.parse(key));
|
|
1207
|
+
own(dir, '"asc" | "desc"');
|
|
1208
|
+
for (const name of [by, dir, change]) view.uses.add(name);
|
|
1209
|
+
}
|
|
989
1210
|
for (const m of body.matchAll(/([A-Za-z][\w-]*)=\{([a-z][A-Za-z0-9]*)(?:\.([A-Za-z][A-Za-z0-9]*))?\}/g)) {
|
|
990
|
-
if (JS_LITERALS.has(m[2])) continue;
|
|
1211
|
+
if (JS_LITERALS.has(m[2]) || data.get(m[2])?.raw) continue;
|
|
991
1212
|
const list = m[3] ? itemList(m.index, m[2]) : null;
|
|
992
1213
|
// A list item's React key is its own: a string, never a DS prop.
|
|
993
1214
|
if (list && m[1] === "key") {
|
|
@@ -1018,6 +1239,14 @@ function dataProps(views) {
|
|
|
1018
1239
|
view.uses.add(m[1]);
|
|
1019
1240
|
}
|
|
1020
1241
|
}
|
|
1242
|
+
for (const d of data.values()) {
|
|
1243
|
+
if (!d.sortKeys) continue;
|
|
1244
|
+
const keys = union(d.sortKeys);
|
|
1245
|
+
d.optional = true;
|
|
1246
|
+
d.raw = d.raw ?? keys;
|
|
1247
|
+
}
|
|
1248
|
+
// The sort's change hears the column and the way, as the heads' keys type them.
|
|
1249
|
+
for (const [name, d] of data) if (d.sortKeys && /^on[A-Z]\w*SortChange$|^onSortChange$/.test(name)) d.raw = `(sortBy: ${union(d.sortKeys)}, sortDir: "asc" | "desc") => void`;
|
|
1021
1250
|
return data;
|
|
1022
1251
|
}
|
|
1023
1252
|
|
|
@@ -1261,7 +1490,9 @@ function emitNode(node, ctx) {
|
|
|
1261
1490
|
if (node.visible === false) return null;
|
|
1262
1491
|
const { ds, note, depth, parent, root } = ctx;
|
|
1263
1492
|
const path = root ? ctx.path : `${ctx.path} › ${node.name}`;
|
|
1264
|
-
|
|
1493
|
+
// Every node's ancestors, nearest last: a list is named by the section heading before it (hub-454).
|
|
1494
|
+
const chain = [...(ctx.chain ?? []), node];
|
|
1495
|
+
const child = (c) => emitNode(c, { ...ctx, depth: depth + 1, parent: node, grand: parent, path, root: false, chain });
|
|
1265
1496
|
const report = (detail) => note(path, "token", detail);
|
|
1266
1497
|
|
|
1267
1498
|
// A DS library instance: exactly what its Code Connect template emits for this instance's properties.
|
|
@@ -1291,7 +1522,7 @@ function emitNode(node, ctx) {
|
|
|
1291
1522
|
return comment(`unmapped: slot ${JSON.stringify(prop)} has no auto layout`);
|
|
1292
1523
|
}
|
|
1293
1524
|
const textOwner = TEXT_PROPERTY_PARTS.has(node.instance.set) ? node.instance.set : ctx.textOwner;
|
|
1294
|
-
const slotCtx = { ...ctx, depth: 0, parent: slotNode, grand: null, path: `${path} › ${prop}`, root: false, textOwner, inTable: false };
|
|
1525
|
+
const slotCtx = { ...ctx, depth: 0, parent: slotNode, grand: null, path: `${path} › ${prop}`, root: false, textOwner, inTable: false, chain: slotNode ? [...chain, slotNode] : chain };
|
|
1295
1526
|
const inner = emitChildren(slotNode?.children ?? [], (c) => emitNode(c, slotCtx), slotCtx);
|
|
1296
1527
|
return inner.length === 0 ? "<></>" : inner.length === 1 ? inner[0] : `<>\n${inner.map((x) => indent(x, 1)).join("\n")}\n</>`;
|
|
1297
1528
|
};
|
|
@@ -1309,6 +1540,7 @@ function emitNode(node, ctx) {
|
|
|
1309
1540
|
if (!ctx.tabProbe) own = own.replace(/<Tabs panelless(?: defaultValue="[^"]*")?>/g, CONTROLLED_TABS("tab", "onTabChange"));
|
|
1310
1541
|
const control = /^<(?:Input|Textarea|SelectField)\b/.test(own.replace(/^<Field\b[^>]*>\s*/, ""));
|
|
1311
1542
|
const scope = control ? fieldScope(node, ctx, own) : scopeName(node, own, ctx);
|
|
1543
|
+
own = nameHandlers(own, node, ctx, path, scope);
|
|
1312
1544
|
own = controlHalfControlled(liftRecordData(own, node, ctx, path, scope));
|
|
1313
1545
|
const code = ctx.scopes ? scopeIdentifiers(own, scope, ctx.scopes) : own;
|
|
1314
1546
|
// A Table part is valid only inside the DS Table. One the generator could not assemble
|
|
@@ -1340,7 +1572,7 @@ function emitNode(node, ctx) {
|
|
|
1340
1572
|
]);
|
|
1341
1573
|
// Text naming a record's value (a date, an amount, a code) is Figma's sample: the page's own (NODII-DEV-CLI-82).
|
|
1342
1574
|
let content = JSON.stringify(node.text);
|
|
1343
|
-
if (
|
|
1575
|
+
if (RECORD_TEXT(node.text) && !ctx.textOwner) {
|
|
1344
1576
|
const scope = positionScope(node, ctx, "text");
|
|
1345
1577
|
const name = mark(ctx, scope, scope);
|
|
1346
1578
|
note(path, LIFTED, `text ${shown} names a record's value: lifted from Figma's sample into the page's props (NODII-DEV-CLI-82)`);
|
|
@@ -1371,7 +1603,7 @@ function emitNode(node, ctx) {
|
|
|
1371
1603
|
return indent(comment(`unmapped: ${node.type.toLowerCase()} ${JSON.stringify(node.name)} has no auto layout`), depth);
|
|
1372
1604
|
}
|
|
1373
1605
|
const classes = cls([...sizeClasses(node, parent, root), ...layoutClasses(node), node.clips ? "overflow-hidden" : null]);
|
|
1374
|
-
const kids = emitChildren(node.children ?? [], child, { ...ctx, depth: depth + 1, parent: node, grand: parent, path, root: false });
|
|
1606
|
+
const kids = emitChildren(node.children ?? [], child, { ...ctx, depth: depth + 1, parent: node, grand: parent, path, root: false, chain });
|
|
1375
1607
|
const open = `<div${classes ? ` className="${classes}"` : ""}`;
|
|
1376
1608
|
if (!kids.length) return indent(`${open} />`, depth);
|
|
1377
1609
|
return [indent(`${open}>`, depth), ...kids, indent("</div>", depth)].join("\n");
|
|
@@ -1386,6 +1618,7 @@ function emitNode(node, ctx) {
|
|
|
1386
1618
|
*/
|
|
1387
1619
|
function emitChildren(children, emitOne, ctx) {
|
|
1388
1620
|
const out = [];
|
|
1621
|
+
const across = ctx.parent?.layout?.mode === "HORIZONTAL";
|
|
1389
1622
|
let run = [];
|
|
1390
1623
|
let runKind = null;
|
|
1391
1624
|
const flush = () => {
|
|
@@ -1397,7 +1630,9 @@ function emitChildren(children, emitOne, ctx) {
|
|
|
1397
1630
|
};
|
|
1398
1631
|
for (const c of children) {
|
|
1399
1632
|
if (c.visible === false) continue;
|
|
1400
|
-
|
|
1633
|
+
// Description lists side by side are columns of the layout, each its own list; only a run
|
|
1634
|
+
// down a column is one list of items (hub-454).
|
|
1635
|
+
const kind = tabOf(c, ctx) ? "tab" : !across && descriptionOf(c, ctx) ? `dl${descriptionOf(c, ctx).open}` : null;
|
|
1401
1636
|
if (kind && kind !== runKind) flush();
|
|
1402
1637
|
if (kind) {
|
|
1403
1638
|
run.push(c);
|
|
@@ -1433,7 +1668,7 @@ function descriptionRun(run, ctx) {
|
|
|
1433
1668
|
const { depth, path } = ctx;
|
|
1434
1669
|
const { open, imports } = descriptionOf(run[0], ctx);
|
|
1435
1670
|
ctx.imports.push(...imports);
|
|
1436
|
-
const items = mark(ctx, scopeName(ctx.parent ?? { name: "items" }), "items");
|
|
1671
|
+
const items = mark(ctx, listScope(run[0], ctx, scopeName(ctx.parent ?? { name: "items" })), "items");
|
|
1437
1672
|
ctx.note(path, LIFTED, `${run.length} sibling Description Lists lifted as one list of items (NODII-DEV-CLI-82)`);
|
|
1438
1673
|
const list = [
|
|
1439
1674
|
`<DescriptionList${open}>`,
|
|
@@ -1465,7 +1700,15 @@ function tabStrip(run, ctx) {
|
|
|
1465
1700
|
const { depth, path } = ctx;
|
|
1466
1701
|
const tabs = run.map((c) => tabOf(c, { ...ctx, path: `${path} › ${c.name}` }, { imports: ctx.imports, note: ctx.note }));
|
|
1467
1702
|
ctx.note(path, ASSEMBLED, `${run.length} sibling Tabs gathered into one DS Tab strip (MW8-17.3)`);
|
|
1468
|
-
|
|
1703
|
+
// Each tab's value is its label (`Pending revisions` → `pending-revisions`), so the page can
|
|
1704
|
+
// route by it; tab-<n> when it has none or shares it (NODII-DEV-CLI-83).
|
|
1705
|
+
const values = [];
|
|
1706
|
+
for (const [i, t] of tabs.entries()) {
|
|
1707
|
+
const label = /\{("(?:[^"\\\n]|\\.)*")\}/.exec(t.trigger);
|
|
1708
|
+
const value = label ? slug(JSON.parse(label[1])) : "";
|
|
1709
|
+
values.push(value && !values.includes(value) ? value : `tab-${i + 1}`);
|
|
1710
|
+
}
|
|
1711
|
+
const triggers = tabs.map((t, i) => indent(t.trigger.replace(/(<TabsTrigger\b[\s\S]*?)\bvalue="tab"/, `$1value="${values[i]}"`), 2));
|
|
1469
1712
|
return [
|
|
1470
1713
|
// The page controls which tab is shown (NODII-DEV-CLI-83); Figma's selected tab is only the drawn one.
|
|
1471
1714
|
indent(CONTROLLED_TABS(mark(ctx, scopeName(ctx.parent ?? { name: "tabs" }), "tab"), mark(ctx, scopeName(ctx.parent ?? { name: "tabs" }), "onTabChange")), depth),
|
|
@@ -1700,11 +1943,27 @@ function tableOf(node, ctx) {
|
|
|
1700
1943
|
return keys;
|
|
1701
1944
|
};
|
|
1702
1945
|
const headKeys = headRow ? columnKeys(headRow.cells.length) : [];
|
|
1946
|
+
// The table's list state is named by its section, so two tables never share it (hub-454).
|
|
1947
|
+
const listName = listScope(node, ctx, scopeName(node), (headRow ? headKeys : columnKeys(dataRows[0]?.cells.length ?? 0)).join(","));
|
|
1948
|
+
// A sortable head is one column of the list's one sort, which the page owns: which column
|
|
1949
|
+
// and which way, and a change calls back with both, as DataTableToolbarBlock's sortBy,
|
|
1950
|
+
// sortDir and onSortChange (NODII-DEV-CLI-83). The template's per-column onSort and a
|
|
1951
|
+
// drawn sort="asc" are Figma's sample.
|
|
1952
|
+
const controlSort = (code, key) => {
|
|
1953
|
+
if (!key || !/^\s*<TableSortHead\b/.test(code ?? "")) return code;
|
|
1954
|
+
const [by, dir, change] = ["sortBy", "sortDir", "onSortChange"].map((n) => mark(ctx, listName, n));
|
|
1955
|
+
const k = JSON.stringify(key);
|
|
1956
|
+
return code
|
|
1957
|
+
.replace(/(<TableSortHead\b[^>]*?) sort="(?:asc|desc)"/, "$1")
|
|
1958
|
+
.replace(/\bonSort=\{(?:__s\d+_)?onSort\}/, `sort={${by} === ${k} ? ${dir} : false} onSort={${change} && (() => ${change}(${k}, ${by} === ${k} && ${dir} === "asc" ? "desc" : "asc"))}`);
|
|
1959
|
+
};
|
|
1703
1960
|
const liftable = !skeleton && dataRows.length > 0 && dataRows.every((p) => !p.unrecognised && !p.framesOnly && p.cells.length === dataRows[0].cells.length) && (!headRow || headRow.cells.length === dataRows[0].cells.length);
|
|
1704
1961
|
const liftedRows = () => {
|
|
1705
1962
|
const first = dataRows[0];
|
|
1706
1963
|
const keys = columnKeys(first.cells.length);
|
|
1707
|
-
const rowsName = mark(ctx,
|
|
1964
|
+
const rowsName = mark(ctx, listName, "rows");
|
|
1965
|
+
// A row is a door to its record when the page passes a handler (U03-7; NODII-DEV-CLI-83).
|
|
1966
|
+
const rowClick = mark(ctx, listName, "onRowClick");
|
|
1708
1967
|
ctx.note(ctx.path, LIFTED, `${dataRows.length} body row${dataRows.length === 1 ? "" : "s"} lifted as data: rows of { id, ${keys.join(", ")} } (NODII-DEV-CLI-82)`);
|
|
1709
1968
|
const cells = first.cells.map((c, i) => {
|
|
1710
1969
|
const w = columnWidth(c.cell);
|
|
@@ -1721,7 +1980,7 @@ function tableOf(node, ctx) {
|
|
|
1721
1980
|
});
|
|
1722
1981
|
return [
|
|
1723
1982
|
indent(`{${rowsName}.map((row) => (`, depth + 2),
|
|
1724
|
-
indent(
|
|
1983
|
+
indent(`<TableRow key={row.id} onClick={${rowClick} && (() => ${rowClick}(row))}>`, depth + 3),
|
|
1725
1984
|
...cells,
|
|
1726
1985
|
indent("</TableRow>", depth + 3),
|
|
1727
1986
|
indent("))}", depth + 2),
|
|
@@ -1736,7 +1995,7 @@ function tableOf(node, ctx) {
|
|
|
1736
1995
|
? [skeletonRows()]
|
|
1737
1996
|
: kind === "body" && liftable
|
|
1738
1997
|
? liftedRows()
|
|
1739
|
-
: list.flatMap((p) => (p.unrecognised ? [indent(comment(p.unrecognised), depth + 2)] : [indent("<TableRow>", depth + 2), ...p.cells.map((c, i) => cellCode(c, p.row, depth + 3,
|
|
1998
|
+
: list.flatMap((p) => (p.unrecognised ? [indent(comment(p.unrecognised), depth + 2)] : [indent("<TableRow>", depth + 2), ...p.cells.map((c, i) => (p === headRow ? controlSort(cellCode(c, p.row, depth + 3, headKeys[i]), headKeys[i]) : cellCode(c, p.row, depth + 3, null))), indent("</TableRow>", depth + 2)]))),
|
|
1740
1999
|
indent(`</${tag}>`, depth + 1),
|
|
1741
2000
|
];
|
|
1742
2001
|
};
|