@azx-pbc/axiom-form-builder 0.0.0-stage → 0.0.1

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.
Files changed (100) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +231 -2
  3. package/dist/bridge/index.d.ts +12 -0
  4. package/dist/bridge/index.d.ts.map +1 -0
  5. package/dist/bridge/index.js +10 -0
  6. package/dist/bridge/index.js.map +1 -0
  7. package/dist/bridge/inspector-commands.d.ts +55 -0
  8. package/dist/bridge/inspector-commands.d.ts.map +1 -0
  9. package/dist/bridge/inspector-commands.js +532 -0
  10. package/dist/bridge/inspector-commands.js.map +1 -0
  11. package/dist/bridge/rule-context.d.ts +57 -0
  12. package/dist/bridge/rule-context.d.ts.map +1 -0
  13. package/dist/bridge/rule-context.js +302 -0
  14. package/dist/bridge/rule-context.js.map +1 -0
  15. package/dist/index.d.ts +32 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +28 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/inspector.d.ts +125 -0
  20. package/dist/inspector.d.ts.map +1 -0
  21. package/dist/inspector.js +2 -0
  22. package/dist/inspector.js.map +1 -0
  23. package/dist/reference/ReferencePicker.d.ts +59 -0
  24. package/dist/reference/ReferencePicker.d.ts.map +1 -0
  25. package/dist/reference/ReferencePicker.js +145 -0
  26. package/dist/reference/ReferencePicker.js.map +1 -0
  27. package/dist/reference/index.d.ts +13 -0
  28. package/dist/reference/index.d.ts.map +1 -0
  29. package/dist/reference/index.js +11 -0
  30. package/dist/reference/index.js.map +1 -0
  31. package/dist/reference/token.d.ts +44 -0
  32. package/dist/reference/token.d.ts.map +1 -0
  33. package/dist/reference/token.js +141 -0
  34. package/dist/reference/token.js.map +1 -0
  35. package/dist/reference/types.d.ts +49 -0
  36. package/dist/reference/types.d.ts.map +1 -0
  37. package/dist/reference/types.js +2 -0
  38. package/dist/reference/types.js.map +1 -0
  39. package/dist/rules/ConditionEditor.d.ts +38 -0
  40. package/dist/rules/ConditionEditor.d.ts.map +1 -0
  41. package/dist/rules/ConditionEditor.js +79 -0
  42. package/dist/rules/ConditionEditor.js.map +1 -0
  43. package/dist/rules/RuleBuilder.d.ts +76 -0
  44. package/dist/rules/RuleBuilder.d.ts.map +1 -0
  45. package/dist/rules/RuleBuilder.js +336 -0
  46. package/dist/rules/RuleBuilder.js.map +1 -0
  47. package/dist/rules/RuleList.d.ts +45 -0
  48. package/dist/rules/RuleList.d.ts.map +1 -0
  49. package/dist/rules/RuleList.js +62 -0
  50. package/dist/rules/RuleList.js.map +1 -0
  51. package/dist/rules/RuleMessageEditor.d.ts +38 -0
  52. package/dist/rules/RuleMessageEditor.d.ts.map +1 -0
  53. package/dist/rules/RuleMessageEditor.js +38 -0
  54. package/dist/rules/RuleMessageEditor.js.map +1 -0
  55. package/dist/rules/ValidationEditor.d.ts +60 -0
  56. package/dist/rules/ValidationEditor.d.ts.map +1 -0
  57. package/dist/rules/ValidationEditor.js +299 -0
  58. package/dist/rules/ValidationEditor.js.map +1 -0
  59. package/dist/rules/condition.d.ts +12 -0
  60. package/dist/rules/condition.d.ts.map +1 -0
  61. package/dist/rules/condition.js +46 -0
  62. package/dist/rules/condition.js.map +1 -0
  63. package/dist/rules/expression.d.ts +57 -0
  64. package/dist/rules/expression.d.ts.map +1 -0
  65. package/dist/rules/expression.js +286 -0
  66. package/dist/rules/expression.js.map +1 -0
  67. package/dist/rules/index.d.ts +28 -0
  68. package/dist/rules/index.d.ts.map +1 -0
  69. package/dist/rules/index.js +19 -0
  70. package/dist/rules/index.js.map +1 -0
  71. package/dist/rules/messages.d.ts +64 -0
  72. package/dist/rules/messages.d.ts.map +1 -0
  73. package/dist/rules/messages.js +114 -0
  74. package/dist/rules/messages.js.map +1 -0
  75. package/dist/rules/operators.d.ts +25 -0
  76. package/dist/rules/operators.d.ts.map +1 -0
  77. package/dist/rules/operators.js +128 -0
  78. package/dist/rules/operators.js.map +1 -0
  79. package/dist/rules/sentence.d.ts +151 -0
  80. package/dist/rules/sentence.d.ts.map +1 -0
  81. package/dist/rules/sentence.js +527 -0
  82. package/dist/rules/sentence.js.map +1 -0
  83. package/dist/rules/templates.d.ts +163 -0
  84. package/dist/rules/templates.d.ts.map +1 -0
  85. package/dist/rules/templates.js +560 -0
  86. package/dist/rules/templates.js.map +1 -0
  87. package/dist/rules/types.d.ts +209 -0
  88. package/dist/rules/types.d.ts.map +1 -0
  89. package/dist/rules/types.js +2 -0
  90. package/dist/rules/types.js.map +1 -0
  91. package/dist/rules/validation-editor-api.d.ts +12 -0
  92. package/dist/rules/validation-editor-api.d.ts.map +1 -0
  93. package/dist/rules/validation-editor-api.js +12 -0
  94. package/dist/rules/validation-editor-api.js.map +1 -0
  95. package/dist/rules/validation.d.ts +27 -0
  96. package/dist/rules/validation.d.ts.map +1 -0
  97. package/dist/rules/validation.js +184 -0
  98. package/dist/rules/validation.js.map +1 -0
  99. package/dist/styles.css +900 -0
  100. package/package.json +65 -3
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inspector.js","sourceRoot":"","sources":["../src/inspector.ts"],"names":[],"mappings":"","sourcesContent":["import type { FieldPrimitiveSpec } from '@azx-pbc/axiom-builder';\n\n/**\n * The vocabulary a host's inspector speaks.\n *\n * The inspector pane itself is the host's: this package ships the rule and reference surfaces and\n * the vocabulary around them, not a skin, so the selected-row rail in the reference Studio is a\n * host component and every edit it makes crosses into the store through the union below. Keeping\n * the union here — beside the rule editor that also edits a field's validation — is what lets two\n * different panes state one intent without either owning the store.\n *\n * Both draft types carry the `Palette` prefix because that is the name they were published under\n * while the palette was a component of this package; a host imports them by that name, so the\n * prefix stays even though the palette no longer ships. Renaming a published type is a break with\n * no benefit to the reader of a `change.draft`.\n */\n\n/** One table column a host's inspector holds before it states the declaration. */\nexport type PaletteColumnType = 'text' | 'number' | 'money' | 'date' | 'boolean';\n\n/** One table column the inspector holds. */\nexport interface PaletteColumnDraft {\n readonly name: string;\n readonly type: PaletteColumnType;\n}\n\n/** The comparison operators an inspector's validation row offers. */\nexport type PaletteValidationOperator =\n | 'is_empty'\n | 'is_not_empty'\n | 'equals'\n | 'not_equals'\n | 'greater_than'\n | 'less_than'\n | 'greater_or_equal'\n | 'less_or_equal'\n | 'contains'\n | 'starts_with'\n | 'ends_with'\n | 'one_of'\n | 'not_one_of'\n | 'matches_pattern'\n | 'between';\n\n/** A validation the inspector hands to its host, with one message per declared locale. */\nexport interface PaletteValidationDraft {\n readonly fieldId: string;\n readonly operator: PaletteValidationOperator;\n /** The first operand; empty for a none-arity operator. */\n readonly value: string;\n /** The second operand, used only by `between`. */\n readonly secondValue: string;\n /** One value per line for the list operators. */\n readonly values: string;\n /** The pattern text for `matches_pattern`. */\n readonly pattern: string;\n readonly severity: 'block' | 'warn';\n readonly messageByLocale: Readonly<Record<string, string>>;\n}\n\n/** Every intent an inspector can hand its host. */\nexport type InspectorChange =\n | {\n readonly kind: 'text';\n readonly fieldId: string;\n readonly member: 'label' | 'help' | 'placeholder';\n readonly locale: string;\n readonly text: string;\n /** The key to mint when the member has none. */\n readonly messageKey: string;\n }\n | {\n readonly kind: 'translations';\n readonly fieldId: string;\n readonly entries: readonly {\n readonly member: 'label' | 'help' | 'placeholder';\n readonly locale: string;\n readonly text: string;\n readonly messageKey: string;\n }[];\n }\n | {\n readonly kind: 'message';\n readonly messageKey: string;\n readonly locale: string;\n readonly text: string;\n }\n | {\n readonly kind: 'required';\n readonly fieldId: string;\n readonly required: boolean;\n /** The key the required rule raises; the host mints it when the field has none. */\n readonly messageKey: string;\n }\n | {\n readonly kind: 'options';\n readonly fieldId: string;\n /**\n * The option domain as the row shows it. A code is the option's identity and is the value a\n * record stores, so a field whose values are numbers (a rating's points) keeps numeric\n * codes rather than spelling them as text.\n */\n readonly options: readonly {\n readonly code: string | number;\n readonly label: string;\n }[];\n }\n | {\n /**\n * Reorders one option by one position. The code is the identity; only the list moves.\n */\n readonly kind: 'optionMove';\n readonly fieldId: string;\n readonly code: string | number;\n readonly direction: 'up' | 'down';\n }\n | {\n readonly kind: 'columns';\n readonly fieldId: string;\n readonly columns: readonly PaletteColumnDraft[];\n }\n | { readonly kind: 'covers'; readonly fieldId: string; readonly covers: readonly string[] }\n | {\n readonly kind: 'money';\n readonly fieldId: string;\n readonly currency: string;\n readonly scale: number;\n }\n | {\n readonly kind: 'bounds';\n readonly fieldId: string;\n readonly bounds: {\n readonly minLength?: number;\n readonly maxLength?: number;\n readonly minimum?: number;\n readonly maximum?: number;\n readonly minItems?: number;\n readonly maxItems?: number;\n };\n }\n | {\n readonly kind: 'primitive';\n readonly fieldId: string;\n readonly primitive: FieldPrimitiveSpec;\n }\n | {\n readonly kind: 'locales';\n readonly locales: readonly string[];\n readonly defaultLocale: string;\n }\n | { readonly kind: 'validation'; readonly draft: PaletteValidationDraft };\n"]}
@@ -0,0 +1,59 @@
1
+ import type { ReactElement } from 'react';
2
+ import type { FieldReference, ReferableField } from './types.js';
3
+ /**
4
+ * The English chrome the picker uses when a host supplies none.
5
+ *
6
+ * Every string is a fallback, never a claim about the document: a host passes its own labels (or
7
+ * its own message formatter) through `labels` to localise the picker. The picker itself carries
8
+ * no document text, because the field labels and the declared ids are the document's and arrive
9
+ * as props.
10
+ */
11
+ export interface ReferencePickerLabels {
12
+ /** The visible label above the search input. */
13
+ readonly label: string;
14
+ /** The search input's placeholder. */
15
+ readonly placeholder: string;
16
+ /** Shown when the host supplies no fields. */
17
+ readonly empty: string;
18
+ /** Shown when a search matches nothing. */
19
+ readonly noResults: string;
20
+ /** The accessible name for a field whose page names no group. */
21
+ readonly ungrouped: string;
22
+ /** The hint naming the short token, e.g. "Short token". */
23
+ readonly tokenHint: string;
24
+ /** The button label for one field, receiving the field's translated label. */
25
+ readonly choose: (label: string) => string;
26
+ }
27
+ /** The picker's props: a field view in, one picked reference out. */
28
+ export interface ReferencePickerProps {
29
+ /** The fields this picker offers; the caller may narrow them to the operands that fit. */
30
+ readonly fields: readonly ReferableField[];
31
+ /**
32
+ * The full field list tokens are drawn from, when `fields` is a filtered view.
33
+ *
34
+ * A token is a position in the document's field order, not in the current filter: the same field
35
+ * must read `$F7` in every picker, or the short handle an author learned would mean two fields.
36
+ * Omitted, it defaults to `fields`.
37
+ */
38
+ readonly tokenFields?: readonly ReferableField[] | undefined;
39
+ /** Called with the picked field's token, id and label. */
40
+ readonly onPick: (reference: FieldReference) => void;
41
+ readonly labels?: Partial<ReferencePickerLabels> | undefined;
42
+ readonly disabled?: boolean | undefined;
43
+ readonly autoFocus?: boolean | undefined;
44
+ readonly id?: string | undefined;
45
+ readonly className?: string | undefined;
46
+ /** Refuse to render more than this many matches; the search still reaches every field. */
47
+ readonly maxResults?: number | undefined;
48
+ }
49
+ /**
50
+ * A searchable field selector grouped by page and section.
51
+ *
52
+ * Each row shows the field's translated label and its short token; the declared id is what
53
+ * `onPick` carries and what an expression stores, so the author chooses by label without the id
54
+ * appearing as a second, competing name. Keyboard use is the WAI-ARIA combobox pattern: the input
55
+ * keeps focus, `aria-activedescendant` names the active row, and Arrow/Home/End/Enter/Escape move
56
+ * and choose without a second tab stop per option.
57
+ */
58
+ export declare function ReferencePicker(props: ReferencePickerProps): ReactElement;
59
+ //# sourceMappingURL=ReferencePicker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReferencePicker.d.ts","sourceRoot":"","sources":["../../src/reference/ReferencePicker.tsx"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAiB,YAAY,EAAE,MAAM,OAAO,CAAC;AAEzD,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjE;;;;;;;GAOG;AACH,MAAM,WAAW,qBAAqB;IACpC,gDAAgD;IAChD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,sCAAsC;IACtC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,8CAA8C;IAC9C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,2CAA2C;IAC3C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,iEAAiE;IACjE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,2DAA2D;IAC3D,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,8EAA8E;IAC9E,QAAQ,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;CAC5C;AAaD,qEAAqE;AACrE,MAAM,WAAW,oBAAoB;IACnC,0FAA0F;IAC1F,QAAQ,CAAC,MAAM,EAAE,SAAS,cAAc,EAAE,CAAC;IAC3C;;;;;;OAMG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,SAAS,cAAc,EAAE,GAAG,SAAS,CAAC;IAC7D,0DAA0D;IAC1D,QAAQ,CAAC,MAAM,EAAE,CAAC,SAAS,EAAE,cAAc,KAAK,IAAI,CAAC;IACrD,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,qBAAqB,CAAC,GAAG,SAAS,CAAC;IAC7D,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,SAAS,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACzC,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACjC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,0FAA0F;IAC1F,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC1C;AAsDD;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,GAAG,YAAY,CA+JzE"}
@@ -0,0 +1,145 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useId, useState } from 'react';
3
+ import { referenceIndex } from './token.js';
4
+ /** The overridable chrome, with the package's English defaults. */
5
+ const DEFAULT_REFERENCE_PICKER_LABELS = {
6
+ label: 'Insert a question',
7
+ placeholder: 'Search questions',
8
+ empty: 'This form has no questions yet.',
9
+ noResults: 'No question matches that search.',
10
+ ungrouped: 'Questions',
11
+ tokenHint: 'Short name',
12
+ choose: (label) => `Insert ${label}`,
13
+ };
14
+ function groupsOf(references, ungrouped) {
15
+ const groups = new Map();
16
+ for (const reference of references) {
17
+ const pageLabel = reference.pageLabel ?? ungrouped;
18
+ const sectionLabel = reference.sectionLabel;
19
+ const key = `${pageLabel}\u0000${sectionLabel ?? ''}`;
20
+ const group = groups.get(key);
21
+ if (group === undefined) {
22
+ groups.set(key, {
23
+ pageLabel,
24
+ ...(sectionLabel === undefined ? {} : { sectionLabel }),
25
+ references: [reference],
26
+ });
27
+ }
28
+ else {
29
+ group.references.push(reference);
30
+ }
31
+ }
32
+ return [...groups].map(([key, group]) => ({
33
+ key,
34
+ pageLabel: group.pageLabel,
35
+ sectionLabel: group.sectionLabel,
36
+ references: group.references,
37
+ }));
38
+ }
39
+ function matches(reference, query) {
40
+ if (query.length === 0)
41
+ return true;
42
+ // A locale-independent fold: the search must match the same fields on every machine, and the
43
+ // package states that it never reads a browser locale.
44
+ const needle = query.toLowerCase();
45
+ return (reference.label.toLowerCase().includes(needle) ||
46
+ reference.fieldId.toLowerCase().includes(needle) ||
47
+ reference.token.toLowerCase().includes(needle) ||
48
+ (reference.pageLabel?.toLowerCase().includes(needle) ?? false) ||
49
+ (reference.sectionLabel?.toLowerCase().includes(needle) ?? false));
50
+ }
51
+ /**
52
+ * A searchable field selector grouped by page and section.
53
+ *
54
+ * Each row shows the field's translated label and its short token; the declared id is what
55
+ * `onPick` carries and what an expression stores, so the author chooses by label without the id
56
+ * appearing as a second, competing name. Keyboard use is the WAI-ARIA combobox pattern: the input
57
+ * keeps focus, `aria-activedescendant` names the active row, and Arrow/Home/End/Enter/Escape move
58
+ * and choose without a second tab stop per option.
59
+ */
60
+ export function ReferencePicker(props) {
61
+ const labels = { ...DEFAULT_REFERENCE_PICKER_LABELS, ...props.labels };
62
+ const reactId = useId();
63
+ const baseId = props.id ?? `axb-reference-${reactId}`;
64
+ const listId = `${baseId}-list`;
65
+ const [query, setQuery] = useState('');
66
+ const [open, setOpen] = useState(false);
67
+ const [activeIndex, setActiveIndex] = useState(0);
68
+ const tokenFields = props.tokenFields ?? props.fields;
69
+ const extra = props.fields.filter((field) => !tokenFields.some((tokenField) => tokenField.fieldId === field.fieldId));
70
+ const index = referenceIndex([...tokenFields, ...extra]);
71
+ const offered = props.fields
72
+ .map((field) => index.byId.get(field.fieldId))
73
+ .filter((reference) => reference !== undefined);
74
+ const filtered = offered.filter((reference) => matches(reference, query));
75
+ const limit = props.maxResults === undefined ? filtered.length : Math.max(0, props.maxResults);
76
+ const visible = filtered.slice(0, limit);
77
+ const groups = groupsOf(visible, labels.ungrouped);
78
+ const active = visible[activeIndex];
79
+ const choose = (reference) => {
80
+ props.onPick(reference);
81
+ setQuery('');
82
+ setOpen(false);
83
+ setActiveIndex(0);
84
+ };
85
+ const onKeyDown = (event) => {
86
+ if (event.key === 'ArrowDown') {
87
+ event.preventDefault();
88
+ setOpen(true);
89
+ setActiveIndex((current) => visible.length === 0 ? 0 : Math.min(current + 1, visible.length - 1));
90
+ return;
91
+ }
92
+ if (event.key === 'ArrowUp') {
93
+ event.preventDefault();
94
+ setOpen(true);
95
+ setActiveIndex((current) => Math.max(current - 1, 0));
96
+ return;
97
+ }
98
+ if (event.key === 'Home') {
99
+ if (!open)
100
+ return;
101
+ event.preventDefault();
102
+ setActiveIndex(0);
103
+ return;
104
+ }
105
+ if (event.key === 'End') {
106
+ if (!open)
107
+ return;
108
+ event.preventDefault();
109
+ setActiveIndex(Math.max(visible.length - 1, 0));
110
+ return;
111
+ }
112
+ if (event.key === 'Enter') {
113
+ if (!open || active === undefined)
114
+ return;
115
+ event.preventDefault();
116
+ choose(active);
117
+ return;
118
+ }
119
+ if (event.key === 'Escape') {
120
+ if (!open)
121
+ return;
122
+ event.preventDefault();
123
+ setOpen(false);
124
+ }
125
+ };
126
+ const optionId = (position) => `${baseId}-option-${String(position)}`;
127
+ const positions = new Map(visible.map((reference, index) => [reference.fieldId, index]));
128
+ return (_jsxs("div", { className: ['axb-reference', props.className].filter(Boolean).join(' '), "data-axb-reference": "", onBlur: (event) => {
129
+ const next = event.relatedTarget;
130
+ if (next instanceof Node && event.currentTarget.contains(next))
131
+ return;
132
+ setOpen(false);
133
+ }, children: [_jsx("label", { className: "axb-reference__label", htmlFor: `${baseId}-input`, children: labels.label }), _jsx("input", { id: `${baseId}-input`, className: "axb-reference__input", type: "text", role: "combobox", "aria-expanded": open, "aria-controls": listId, "aria-autocomplete": "list", "aria-activedescendant": open && active !== undefined ? optionId(activeIndex) : undefined, placeholder: labels.placeholder, value: query, disabled: props.disabled === true, autoFocus: props.autoFocus === true, onChange: (event) => {
134
+ setQuery(event.currentTarget.value);
135
+ setOpen(true);
136
+ setActiveIndex(0);
137
+ }, onFocus: () => setOpen(true), onKeyDown: onKeyDown }), props.fields.length === 0 ? _jsx("p", { className: "axb-reference__empty", children: labels.empty }) : null, open && props.fields.length > 0 ? (_jsxs("ul", { id: listId, className: "axb-reference__list", role: "listbox", "aria-label": labels.label, children: [groups.map((group) => (_jsxs("li", { role: "group", "aria-label": group.sectionLabel ?? group.pageLabel, className: "axb-reference__group", children: [_jsx("div", { className: "axb-reference__page", children: group.pageLabel }), group.sectionLabel === undefined ? null : (_jsx("div", { className: "axb-reference__section", children: group.sectionLabel })), _jsx("ul", { className: "axb-reference__options", role: "presentation", children: group.references.map((reference) => {
138
+ const current = positions.get(reference.fieldId) ?? 0;
139
+ const selected = current === activeIndex;
140
+ return (_jsxs("li", { id: optionId(current), role: "option", "aria-selected": selected, className: selected
141
+ ? 'axb-reference__option axb-reference__option--active'
142
+ : 'axb-reference__option', onMouseDown: (event) => event.preventDefault(), onClick: () => choose(reference), title: labels.choose(reference.label), children: [_jsx("span", { className: "axb-reference__field-label", children: reference.label }), _jsx("span", { className: "axb-reference__token", title: labels.tokenHint, children: reference.token })] }, reference.fieldId));
143
+ }) })] }, group.key))), visible.length === 0 ? (_jsx("li", { role: "presentation", className: "axb-reference__no-results", children: labels.noResults })) : null] })) : null] }));
144
+ }
145
+ //# sourceMappingURL=ReferencePicker.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ReferencePicker.js","sourceRoot":"","sources":["../../src/reference/ReferencePicker.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAExC,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AA4B5C,mEAAmE;AACnE,MAAM,+BAA+B,GAA0B;IAC7D,KAAK,EAAE,mBAAmB;IAC1B,WAAW,EAAE,kBAAkB;IAC/B,KAAK,EAAE,iCAAiC;IACxC,SAAS,EAAE,kCAAkC;IAC7C,SAAS,EAAE,WAAW;IACtB,SAAS,EAAE,YAAY;IACvB,MAAM,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,UAAU,KAAK,EAAE;CAC7C,CAAC;AAgCF,SAAS,QAAQ,CACf,UAAqC,EACrC,SAAiB;IAEjB,MAAM,MAAM,GAAG,IAAI,GAAG,EAGnB,CAAC;IACJ,KAAK,MAAM,SAAS,IAAI,UAAU,EAAE,CAAC;QACnC,MAAM,SAAS,GAAG,SAAS,CAAC,SAAS,IAAI,SAAS,CAAC;QACnD,MAAM,YAAY,GAAG,SAAS,CAAC,YAAY,CAAC;QAC5C,MAAM,GAAG,GAAG,GAAG,SAAS,SAAS,YAAY,IAAI,EAAE,EAAE,CAAC;QACtD,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAC9B,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE;gBACd,SAAS;gBACT,GAAG,CAAC,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC;gB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SAAS,EAAC,sBAAsB,EAAC,KAAK,EAAE,MAAM,CAAC,SAAS,YAC3D,SAAS,CAAC,KAAK,GACX,KAhBF,SAAS,CAAC,OAAO,CAiBnB,CACN,CAAC;gCACJ,CAAC,CAAC,GACC,KAnCA,KAAK,CAAC,GAAG,CAoCX,CACN,CAAC,EACD,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,CACtB,aAAI,IAAI,EAAC,cAAc,EAAC,SAAS,EAAC,2BAA2B,YAC1D,MAAM,CAAC,SAAS,GACd,CACN,CAAC,CAAC,CAAC,IAAI,IACL,CACN,CAAC,CAAC,CAAC,IAAI,IACJ,CACP,CAAC;AACJ,CAAC","sourcesContent":["import { useId, useState } from 'react';\nimport type { KeyboardEvent, ReactElement } from 'react';\nimport { referenceIndex } from './token.js';\nimport type { FieldReference, ReferableField } from './types.js';\n\n/**\n * The English chrome the picker uses when a host supplies none.\n *\n * Every string is a fallback, never a claim about the document: a host passes its own labels (or\n * its own message formatter) through `labels` to localise the picker. The picker itself carries\n * no document text, because the field labels and the declared ids are the document's and arrive\n * as props.\n */\nexport interface ReferencePickerLabels {\n /** The visible label above the search input. */\n readonly label: string;\n /** The search input's placeholder. */\n readonly placeholder: string;\n /** Shown when the host supplies no fields. */\n readonly empty: string;\n /** Shown when a search matches nothing. */\n readonly noResults: string;\n /** The accessible name for a field whose page names no group. */\n readonly ungrouped: string;\n /** The hint naming the short token, e.g. \"Short token\". */\n readonly tokenHint: string;\n /** The button label for one field, receiving the field's translated label. */\n readonly choose: (label: string) => string;\n}\n\n/** The overridable chrome, with the package's English defaults. */\nconst DEFAULT_REFERENCE_PICKER_LABELS: ReferencePickerLabels = {\n label: 'Insert a question',\n placeholder: 'Search questions',\n empty: 'This form has no questions yet.',\n noResults: 'No question matches that search.',\n ungrouped: 'Questions',\n tokenHint: 'Short name',\n choose: (label: string) => `Insert ${label}`,\n};\n\n/** The picker's props: a field view in, one picked reference out. */\nexport interface ReferencePickerProps {\n /** The fields this picker offers; the caller may narrow them to the operands that fit. */\n readonly fields: readonly ReferableField[];\n /**\n * The full field list tokens are drawn from, when `fields` is a filtered view.\n *\n * A token is a position in the document's field order, not in the current filter: the same field\n * must read `$F7` in every picker, or the short handle an author learned would mean two fields.\n * Omitted, it defaults to `fields`.\n */\n readonly tokenFields?: readonly ReferableField[] | undefined;\n /** Called with the picked field's token, id and label. */\n readonly onPick: (reference: FieldReference) => void;\n readonly labels?: Partial<ReferencePickerLabels> | undefined;\n readonly disabled?: boolean | undefined;\n readonly autoFocus?: boolean | undefined;\n readonly id?: string | undefined;\n readonly className?: string | undefined;\n /** Refuse to render more than this many matches; the search still reaches every field. */\n readonly maxResults?: number | undefined;\n}\n\ninterface PickerGroup {\n readonly key: string;\n readonly pageLabel: string;\n readonly sectionLabel: string | undefined;\n readonly references: readonly FieldReference[];\n}\n\nfunction groupsOf(\n references: readonly FieldReference[],\n ungrouped: string,\n): readonly PickerGroup[] {\n const groups = new Map<\n string,\n { pageLabel: string; sectionLabel?: string; references: FieldReference[] }\n >();\n for (const reference of references) {\n const pageLabel = reference.pageLabel ?? ungrouped;\n const sectionLabel = reference.sectionLabel;\n const key = `${pageLabel}\\u0000${sectionLabel ?? ''}`;\n const group = groups.get(key);\n if (group === undefined) {\n groups.set(key, {\n pageLabel,\n ...(sectionLabel === undefined ? {} : { sectionLabel }),\n references: [reference],\n });\n } else {\n group.references.push(reference);\n }\n }\n return [...groups].map(([key, group]) => ({\n key,\n pageLabel: group.pageLabel,\n sectionLabel: group.sectionLabel,\n references: group.references,\n }));\n}\n\nfunction matches(reference: FieldReference, query: string): boolean {\n if (query.length === 0) return true;\n // A locale-independent fold: the search must match the same fields on every machine, and the\n // package states that it never reads a browser locale.\n const needle = query.toLowerCase();\n return (\n reference.label.toLowerCase().includes(needle) ||\n reference.fieldId.toLowerCase().includes(needle) ||\n reference.token.toLowerCase().includes(needle) ||\n (reference.pageLabel?.toLowerCase().includes(needle) ?? false) ||\n (reference.sectionLabel?.toLowerCase().includes(needle) ?? false)\n );\n}\n\n/**\n * A searchable field selector grouped by page and section.\n *\n * Each row shows the field's translated label and its short token; the declared id is what\n * `onPick` carries and what an expression stores, so the author chooses by label without the id\n * appearing as a second, competing name. Keyboard use is the WAI-ARIA combobox pattern: the input\n * keeps focus, `aria-activedescendant` names the active row, and Arrow/Home/End/Enter/Escape move\n * and choose without a second tab stop per option.\n */\nexport function ReferencePicker(props: ReferencePickerProps): ReactElement {\n const labels: ReferencePickerLabels = { ...DEFAULT_REFERENCE_PICKER_LABELS, ...props.labels };\n const reactId = useId();\n const baseId = props.id ?? `axb-reference-${reactId}`;\n const listId = `${baseId}-list`;\n const [query, setQuery] = useState('');\n const [open, setOpen] = useState(false);\n const [activeIndex, setActiveIndex] = useState(0);\n\n const tokenFields = props.tokenFields ?? props.fields;\n const extra = props.fields.filter(\n (field) => !tokenFields.some((tokenField) => tokenField.fieldId === field.fieldId),\n );\n const index = referenceIndex([...tokenFields, ...extra]);\n const offered = props.fields\n .map((field) => index.byId.get(field.fieldId))\n .filter((reference): reference is FieldReference => reference !== undefined);\n const filtered = offered.filter((reference) => matches(reference, query));\n const limit = props.maxResults === undefined ? filtered.length : Math.max(0, props.maxResults);\n const visible = filtered.slice(0, limit);\n const groups = groupsOf(visible, labels.ungrouped);\n const active = visible[activeIndex];\n\n const choose = (reference: FieldReference): void => {\n props.onPick(reference);\n setQuery('');\n setOpen(false);\n setActiveIndex(0);\n };\n\n const onKeyDown = (event: KeyboardEvent<HTMLInputElement>): void => {\n if (event.key === 'ArrowDown') {\n event.preventDefault();\n setOpen(true);\n setActiveIndex((current) =>\n visible.length === 0 ? 0 : Math.min(current + 1, visible.length - 1),\n );\n return;\n }\n if (event.key === 'ArrowUp') {\n event.preventDefault();\n setOpen(true);\n setActiveIndex((current) => Math.max(current - 1, 0));\n return;\n }\n if (event.key === 'Home') {\n if (!open) return;\n event.preventDefault();\n setActiveIndex(0);\n return;\n }\n if (event.key === 'End') {\n if (!open) return;\n event.preventDefault();\n setActiveIndex(Math.max(visible.length - 1, 0));\n return;\n }\n if (event.key === 'Enter') {\n if (!open || active === undefined) return;\n event.preventDefault();\n choose(active);\n return;\n }\n if (event.key === 'Escape') {\n if (!open) return;\n event.preventDefault();\n setOpen(false);\n }\n };\n\n const optionId = (position: number): string => `${baseId}-option-${String(position)}`;\n const positions = new Map(visible.map((reference, index) => [reference.fieldId, index]));\n\n return (\n <div\n className={['axb-reference', props.className].filter(Boolean).join(' ')}\n data-axb-reference=\"\"\n onBlur={(event) => {\n const next = event.relatedTarget;\n if (next instanceof Node && event.currentTarget.contains(next)) return;\n setOpen(false);\n }}\n >\n <label className=\"axb-reference__label\" htmlFor={`${baseId}-input`}>\n {labels.label}\n </label>\n <input\n id={`${baseId}-input`}\n className=\"axb-reference__input\"\n type=\"text\"\n role=\"combobox\"\n aria-expanded={open}\n aria-controls={listId}\n aria-autocomplete=\"list\"\n aria-activedescendant={open && active !== undefined ? optionId(activeIndex) : undefined}\n placeholder={labels.placeholder}\n value={query}\n disabled={props.disabled === true}\n autoFocus={props.autoFocus === true}\n onChange={(event) => {\n setQuery(event.currentTarget.value);\n setOpen(true);\n setActiveIndex(0);\n }}\n onFocus={() => setOpen(true)}\n onKeyDown={onKeyDown}\n />\n {props.fields.length === 0 ? <p className=\"axb-reference__empty\">{labels.empty}</p> : null}\n {open && props.fields.length > 0 ? (\n <ul id={listId} className=\"axb-reference__list\" role=\"listbox\" aria-label={labels.label}>\n {groups.map((group) => (\n <li\n key={group.key}\n role=\"group\"\n aria-label={group.sectionLabel ?? group.pageLabel}\n className=\"axb-reference__group\"\n >\n <div className=\"axb-reference__page\">{group.pageLabel}</div>\n {group.sectionLabel === undefined ? null : (\n <div className=\"axb-reference__section\">{group.sectionLabel}</div>\n )}\n <ul className=\"axb-reference__options\" role=\"presentation\">\n {group.references.map((reference) => {\n const current = positions.get(reference.fieldId) ?? 0;\n const selected = current === activeIndex;\n return (\n <li\n key={reference.fieldId}\n id={optionId(current)}\n role=\"option\"\n aria-selected={selected}\n className={\n selected\n ? 'axb-reference__option axb-reference__option--active'\n : 'axb-reference__option'\n }\n onMouseDown={(event) => event.preventDefault()}\n onClick={() => choose(reference)}\n title={labels.choose(reference.label)}\n >\n <span className=\"axb-reference__field-label\">{reference.label}</span>\n <span className=\"axb-reference__token\" title={labels.tokenHint}>\n {reference.token}\n </span>\n </li>\n );\n })}\n </ul>\n </li>\n ))}\n {visible.length === 0 ? (\n <li role=\"presentation\" className=\"axb-reference__no-results\">\n {labels.noResults}\n </li>\n ) : null}\n </ul>\n ) : null}\n </div>\n );\n}\n"]}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The reference module's door.
3
+ *
4
+ * A host stores a reference the way an expression does — by the declared field id — and shows the
5
+ * author a short token instead. This module is the one mapping between the two, plus the picker
6
+ * that inserts either into a source. Everything else the token walk needs stays internal, so the
7
+ * door is the mapping, the picker and their types.
8
+ */
9
+ export { ReferencePicker } from './ReferencePicker.js';
10
+ export type { ReferencePickerLabels, ReferencePickerProps } from './ReferencePicker.js';
11
+ export { insertReferenceAtCaret, insertTokenAtCaret, referenceIndex, referenceTokenOf, resolveReferenceField, resolveReferenceToken, resolveTokensInSource, } from './token.js';
12
+ export type { FieldReference, ReferableField } from './types.js';
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/reference/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,YAAY,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AACxF,OAAO,EACL,sBAAsB,EACtB,kBAAkB,EAClB,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The reference module's door.
3
+ *
4
+ * A host stores a reference the way an expression does — by the declared field id — and shows the
5
+ * author a short token instead. This module is the one mapping between the two, plus the picker
6
+ * that inserts either into a source. Everything else the token walk needs stays internal, so the
7
+ * door is the mapping, the picker and their types.
8
+ */
9
+ export { ReferencePicker } from './ReferencePicker.js';
10
+ export { insertReferenceAtCaret, insertTokenAtCaret, referenceIndex, referenceTokenOf, resolveReferenceField, resolveReferenceToken, resolveTokensInSource, } from './token.js';
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/reference/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,OAAO,EACL,sBAAsB,EACtB,kBAAkB,EAClB,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,YAAY,CAAC","sourcesContent":["/**\n * The reference module's door.\n *\n * A host stores a reference the way an expression does — by the declared field id — and shows the\n * author a short token instead. This module is the one mapping between the two, plus the picker\n * that inserts either into a source. Everything else the token walk needs stays internal, so the\n * door is the mapping, the picker and their types.\n */\nexport { ReferencePicker } from './ReferencePicker.js';\nexport type { ReferencePickerLabels, ReferencePickerProps } from './ReferencePicker.js';\nexport {\n insertReferenceAtCaret,\n insertTokenAtCaret,\n referenceIndex,\n referenceTokenOf,\n resolveReferenceField,\n resolveReferenceToken,\n resolveTokensInSource,\n} from './token.js';\nexport type { FieldReference, ReferableField } from './types.js';\n"]}
@@ -0,0 +1,44 @@
1
+ import type { FieldReference, ReferenceIndex, ReferenceInsertion, ReferableField } from './types.js';
2
+ /**
3
+ * The short token a field is shown by, `$F1` for the first field, `$F2` for the second.
4
+ *
5
+ * A token is positional by design: it is a pointing handle, not an identity. The field's declared
6
+ * id is what an expression stores, so reordering the picker changes the token and never changes
7
+ * the meaning of a saved expression.
8
+ *
9
+ * `field` is the field the token belongs to; `index` is its zero-based position in the ordered
10
+ * field list. The field is read only to refuse a token the caller could not have meant, and the
11
+ * returned token always names the position.
12
+ */
13
+ export declare function referenceTokenOf(field: ReferableField, index: number): string;
14
+ /** Builds the ordered token index for a list of fields, in the order the picker shows them. */
15
+ export declare function referenceIndex(fields: readonly ReferableField[]): ReferenceIndex;
16
+ /** The field a token names, or `undefined` when no field in the index carries the token. */
17
+ export declare function resolveReferenceToken(index: ReferenceIndex, token: string): FieldReference | undefined;
18
+ /** The reference a field id names, or `undefined` when the id is not in the index. */
19
+ export declare function resolveReferenceField(index: ReferenceIndex, fieldId: string): FieldReference | undefined;
20
+ /**
21
+ * Inserts a reference at the caret in an expression target.
22
+ *
23
+ * The inserted text is the field's **declared id**, which is what an expression compiles against;
24
+ * the token the author picks by is shown beside the label in the picker and never stored. A
25
+ * selection is replaced. A caret position outside the text is clamped, so a stale selection from
26
+ * another render cannot throw through the picker's callback.
27
+ */
28
+ export declare function insertReferenceAtCaret(text: string, selectionStart: number, selectionEnd: number, fieldId: string): ReferenceInsertion;
29
+ /**
30
+ * Inserts a token at the caret, for a target that is a token-aware preview rather than an
31
+ * expression target. `insertReferenceAtCaret` is the function an expression editor calls.
32
+ */
33
+ export declare function insertTokenAtCaret(text: string, selectionStart: number, selectionEnd: number, token: string): ReferenceInsertion;
34
+ /**
35
+ * Replaces every token in a token-aware draft with the declared id it stands for.
36
+ *
37
+ * A draft may be written with the short tokens the picker inserts; this is the one-way read that
38
+ * turns it into Expr v1 source. Tokens are matched longest-first and only at an identifier
39
+ * boundary, so `$F12` is not mistaken for `$F1` followed by `2`. A token inside a string literal
40
+ * is part of the value the author wrote and is left untouched, because rewriting it would change
41
+ * what the expression means.
42
+ */
43
+ export declare function resolveTokensInSource(text: string, index: ReferenceIndex): string;
44
+ //# sourceMappingURL=token.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/reference/token.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,cAAc,EACd,kBAAkB,EAClB,cAAc,EACf,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAQ7E;AAED,+FAA+F;AAC/F,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,cAAc,EAAE,GAAG,cAAc,CAkBhF;AAED,4FAA4F;AAC5F,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,cAAc,EACrB,KAAK,EAAE,MAAM,GACZ,cAAc,GAAG,SAAS,CAE5B;AAED,sFAAsF;AACtF,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,cAAc,EACrB,OAAO,EAAE,MAAM,GACd,cAAc,GAAG,SAAS,CAE5B;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,IAAI,EAAE,MAAM,EACZ,cAAc,EAAE,MAAM,EACtB,YAAY,EAAE,MAAM,EACpB,OAAO,EAAE,MAAM,GACd,kBAAkB,CAKpB;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,MAAM,EACZ,cAAc,EAAE,MAAM,EACtB,YAAY,EAAE,MAAM,EACpB,KAAK,EAAE,MAAM,GACZ,kBAAkB,CAKpB;AAED;;;;;;;;GAQG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,cAAc,GAAG,MAAM,CAuBjF"}
@@ -0,0 +1,141 @@
1
+ /**
2
+ * The short token a field is shown by, `$F1` for the first field, `$F2` for the second.
3
+ *
4
+ * A token is positional by design: it is a pointing handle, not an identity. The field's declared
5
+ * id is what an expression stores, so reordering the picker changes the token and never changes
6
+ * the meaning of a saved expression.
7
+ *
8
+ * `field` is the field the token belongs to; `index` is its zero-based position in the ordered
9
+ * field list. The field is read only to refuse a token the caller could not have meant, and the
10
+ * returned token always names the position.
11
+ */
12
+ export function referenceTokenOf(field, index) {
13
+ if (field.fieldId.length === 0) {
14
+ throw new Error('a reference token needs a field with a declared id');
15
+ }
16
+ if (!Number.isInteger(index) || index < 0) {
17
+ throw new Error('a reference token needs a non-negative whole index');
18
+ }
19
+ return `$F${String(index + 1)}`;
20
+ }
21
+ /** Builds the ordered token index for a list of fields, in the order the picker shows them. */
22
+ export function referenceIndex(fields) {
23
+ const ordered = [];
24
+ const byId = new Map();
25
+ const byToken = new Map();
26
+ fields.forEach((field, index) => {
27
+ const token = referenceTokenOf(field, index);
28
+ const reference = {
29
+ fieldId: field.fieldId,
30
+ label: field.label,
31
+ token,
32
+ ...(field.pageLabel === undefined ? {} : { pageLabel: field.pageLabel }),
33
+ ...(field.sectionLabel === undefined ? {} : { sectionLabel: field.sectionLabel }),
34
+ };
35
+ ordered.push(reference);
36
+ byId.set(reference.fieldId, reference);
37
+ byToken.set(reference.token, reference);
38
+ });
39
+ return { ordered, byId, byToken };
40
+ }
41
+ /** The field a token names, or `undefined` when no field in the index carries the token. */
42
+ export function resolveReferenceToken(index, token) {
43
+ return index.byToken.get(token);
44
+ }
45
+ /** The reference a field id names, or `undefined` when the id is not in the index. */
46
+ export function resolveReferenceField(index, fieldId) {
47
+ return index.byId.get(fieldId);
48
+ }
49
+ /**
50
+ * Inserts a reference at the caret in an expression target.
51
+ *
52
+ * The inserted text is the field's **declared id**, which is what an expression compiles against;
53
+ * the token the author picks by is shown beside the label in the picker and never stored. A
54
+ * selection is replaced. A caret position outside the text is clamped, so a stale selection from
55
+ * another render cannot throw through the picker's callback.
56
+ */
57
+ export function insertReferenceAtCaret(text, selectionStart, selectionEnd, fieldId) {
58
+ const start = Math.max(0, Math.min(selectionStart, text.length));
59
+ const end = Math.max(start, Math.min(selectionEnd, text.length));
60
+ const value = `${text.slice(0, start)}${fieldId}${text.slice(end)}`;
61
+ return { value, caret: start + fieldId.length };
62
+ }
63
+ /**
64
+ * Inserts a token at the caret, for a target that is a token-aware preview rather than an
65
+ * expression target. `insertReferenceAtCaret` is the function an expression editor calls.
66
+ */
67
+ export function insertTokenAtCaret(text, selectionStart, selectionEnd, token) {
68
+ const start = Math.max(0, Math.min(selectionStart, text.length));
69
+ const end = Math.max(start, Math.min(selectionEnd, text.length));
70
+ const value = `${text.slice(0, start)}${token}${text.slice(end)}`;
71
+ return { value, caret: start + token.length };
72
+ }
73
+ /**
74
+ * Replaces every token in a token-aware draft with the declared id it stands for.
75
+ *
76
+ * A draft may be written with the short tokens the picker inserts; this is the one-way read that
77
+ * turns it into Expr v1 source. Tokens are matched longest-first and only at an identifier
78
+ * boundary, so `$F12` is not mistaken for `$F1` followed by `2`. A token inside a string literal
79
+ * is part of the value the author wrote and is left untouched, because rewriting it would change
80
+ * what the expression means.
81
+ */
82
+ export function resolveTokensInSource(text, index) {
83
+ const tokens = [...index.byToken.keys()].sort((left, right) => right.length - left.length);
84
+ if (tokens.length === 0)
85
+ return text;
86
+ let result = '';
87
+ let position = 0;
88
+ while (position < text.length) {
89
+ const char = text[position];
90
+ if (char === '"' || char === "'") {
91
+ const end = stringEnd(text, position);
92
+ result += text.slice(position, end);
93
+ position = end;
94
+ continue;
95
+ }
96
+ const token = tokenAt(text, position, tokens);
97
+ if (token === undefined) {
98
+ result += char;
99
+ position += 1;
100
+ continue;
101
+ }
102
+ result += index.byToken.get(token)?.fieldId ?? token;
103
+ position += token.length;
104
+ }
105
+ return result;
106
+ }
107
+ /** The index after the closing quote of the literal that starts at `start`, or the text's end. */
108
+ function stringEnd(text, start) {
109
+ const quote = text[start];
110
+ let position = start + 1;
111
+ while (position < text.length) {
112
+ const char = text[position];
113
+ if (char === '\\') {
114
+ position += 2;
115
+ continue;
116
+ }
117
+ position += 1;
118
+ if (char === quote)
119
+ return position;
120
+ }
121
+ return text.length;
122
+ }
123
+ /** The token that starts at `position` and sits between identifier boundaries, if one does. */
124
+ function tokenAt(text, position, tokens) {
125
+ const before = text[position - 1];
126
+ if (before !== undefined && isIdentifierPart(before))
127
+ return undefined;
128
+ for (const token of tokens) {
129
+ if (!text.startsWith(token, position))
130
+ continue;
131
+ const after = text[position + token.length];
132
+ if (after !== undefined && isIdentifierPart(after))
133
+ continue;
134
+ return token;
135
+ }
136
+ return undefined;
137
+ }
138
+ function isIdentifierPart(char) {
139
+ return /[\w$]/.test(char);
140
+ }
141
+ //# sourceMappingURL=token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/reference/token.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,KAAqB,EAAE,KAAa;IACnE,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;IACxE,CAAC;IACD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAC;IACxE,CAAC;IACD,OAAO,KAAK,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC;AAClC,CAAC;AAED,+FAA+F;AAC/F,MAAM,UAAU,cAAc,CAAC,MAAiC;IAC9D,MAAM,OAAO,GAAqB,EAAE,CAAC;IACrC,MAAM,IAAI,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC/C,MAAM,OAAO,GAAG,IAAI,GAAG,EAA0B,CAAC;IAClD,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE;QAC9B,MAAM,KAAK,GAAG,gBAAgB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAC7C,MAAM,SAAS,GAAmB;YAChC,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,KAAK,EAAE,KAAK,CAAC,KAAK;YAClB,KAAK;YACL,GAAG,CAAC,KAAK,CAAC,SAA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CAAC;AAED,+FAA+F;AAC/F,SAAS,OAAO,CAAC,IAAY,EAAE,QAAgB,EAAE,MAAyB;IACxE,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC;IAClC,IAAI,MAAM,KAAK,SAAS,IAAI,gBAAgB,CAAC,MAAM,CAAC;QAAE,OAAO,SAAS,CAAC;IACvE,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC;YAAE,SAAS;QAChD,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC;QAC5C,IAAI,KAAK,KAAK,SAAS,IAAI,gBAAgB,CAAC,KAAK,CAAC;YAAE,SAAS;QAC7D,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,gBAAgB,CAAC,IAAY;IACpC,OAAO,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import type {\n FieldReference,\n ReferenceIndex,\n ReferenceInsertion,\n ReferableField,\n} from './types.js';\n\n/**\n * The short token a field is shown by, `$F1` for the first field, `$F2` for the second.\n *\n * A token is positional by design: it is a pointing handle, not an identity. The field's declared\n * id is what an expression stores, so reordering the picker changes the token and never changes\n * the meaning of a saved expression.\n *\n * `field` is the field the token belongs to; `index` is its zero-based position in the ordered\n * field list. The field is read only to refuse a token the caller could not have meant, and the\n * returned token always names the position.\n */\nexport function referenceTokenOf(field: ReferableField, index: number): string {\n if (field.fieldId.length === 0) {\n throw new Error('a reference token needs a field with a declared id');\n }\n if (!Number.isInteger(index) || index < 0) {\n throw new Error('a reference token needs a non-negative whole index');\n }\n return `$F${String(index + 1)}`;\n}\n\n/** Builds the ordered token index for a list of fields, in the order the picker shows them. */\nexport function referenceIndex(fields: readonly ReferableField[]): ReferenceIndex {\n const ordered: FieldReference[] = [];\n const byId = new Map<string, FieldReference>();\n const byToken = new Map<string, FieldReference>();\n fields.forEach((field, index) => {\n const token = referenceTokenOf(field, index);\n const reference: FieldReference = {\n fieldId: field.fieldId,\n label: field.label,\n token,\n ...(field.pageLabel === undefined ? {} : { pageLabel: field.pageLabel }),\n ...(field.sectionLabel === undefined ? {} : { sectionLabel: field.sectionLabel }),\n };\n ordered.push(reference);\n byId.set(reference.fieldId, reference);\n byToken.set(reference.token, reference);\n });\n return { ordered, byId, byToken };\n}\n\n/** The field a token names, or `undefined` when no field in the index carries the token. */\nexport function resolveReferenceToken(\n index: ReferenceIndex,\n token: string,\n): FieldReference | undefined {\n return index.byToken.get(token);\n}\n\n/** The reference a field id names, or `undefined` when the id is not in the index. */\nexport function resolveReferenceField(\n index: ReferenceIndex,\n fieldId: string,\n): FieldReference | undefined {\n return index.byId.get(fieldId);\n}\n\n/**\n * Inserts a reference at the caret in an expression target.\n *\n * The inserted text is the field's **declared id**, which is what an expression compiles against;\n * the token the author picks by is shown beside the label in the picker and never stored. A\n * selection is replaced. A caret position outside the text is clamped, so a stale selection from\n * another render cannot throw through the picker's callback.\n */\nexport function insertReferenceAtCaret(\n text: string,\n selectionStart: number,\n selectionEnd: number,\n fieldId: string,\n): ReferenceInsertion {\n const start = Math.max(0, Math.min(selectionStart, text.length));\n const end = Math.max(start, Math.min(selectionEnd, text.length));\n const value = `${text.slice(0, start)}${fieldId}${text.slice(end)}`;\n return { value, caret: start + fieldId.length };\n}\n\n/**\n * Inserts a token at the caret, for a target that is a token-aware preview rather than an\n * expression target. `insertReferenceAtCaret` is the function an expression editor calls.\n */\nexport function insertTokenAtCaret(\n text: string,\n selectionStart: number,\n selectionEnd: number,\n token: string,\n): ReferenceInsertion {\n const start = Math.max(0, Math.min(selectionStart, text.length));\n const end = Math.max(start, Math.min(selectionEnd, text.length));\n const value = `${text.slice(0, start)}${token}${text.slice(end)}`;\n return { value, caret: start + token.length };\n}\n\n/**\n * Replaces every token in a token-aware draft with the declared id it stands for.\n *\n * A draft may be written with the short tokens the picker inserts; this is the one-way read that\n * turns it into Expr v1 source. Tokens are matched longest-first and only at an identifier\n * boundary, so `$F12` is not mistaken for `$F1` followed by `2`. A token inside a string literal\n * is part of the value the author wrote and is left untouched, because rewriting it would change\n * what the expression means.\n */\nexport function resolveTokensInSource(text: string, index: ReferenceIndex): string {\n const tokens = [...index.byToken.keys()].sort((left, right) => right.length - left.length);\n if (tokens.length === 0) return text;\n let result = '';\n let position = 0;\n while (position < text.length) {\n const char = text[position];\n if (char === '\"' || char === \"'\") {\n const end = stringEnd(text, position);\n result += text.slice(position, end);\n position = end;\n continue;\n }\n const token = tokenAt(text, position, tokens);\n if (token === undefined) {\n result += char;\n position += 1;\n continue;\n }\n result += index.byToken.get(token)?.fieldId ?? token;\n position += token.length;\n }\n return result;\n}\n\n/** The index after the closing quote of the literal that starts at `start`, or the text's end. */\nfunction stringEnd(text: string, start: number): number {\n const quote = text[start];\n let position = start + 1;\n while (position < text.length) {\n const char = text[position];\n if (char === '\\\\') {\n position += 2;\n continue;\n }\n position += 1;\n if (char === quote) return position;\n }\n return text.length;\n}\n\n/** The token that starts at `position` and sits between identifier boundaries, if one does. */\nfunction tokenAt(text: string, position: number, tokens: readonly string[]): string | undefined {\n const before = text[position - 1];\n if (before !== undefined && isIdentifierPart(before)) return undefined;\n for (const token of tokens) {\n if (!text.startsWith(token, position)) continue;\n const after = text[position + token.length];\n if (after !== undefined && isIdentifierPart(after)) continue;\n return token;\n }\n return undefined;\n}\n\nfunction isIdentifierPart(char: string): boolean {\n return /[\\w$]/.test(char);\n}\n"]}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * The field view the reference picker reads.
3
+ *
4
+ * The picker is a controlled component over a host-built list: a form-builder host already holds
5
+ * the declared fields and their translated labels, so this module asks for the labels and the
6
+ * grouping rather than reading a store, a document or a message catalogue. A field outside a
7
+ * page or a section is shown in one ungrouped list.
8
+ */
9
+ export interface ReferableField {
10
+ /** The declared identifier an expression names, such as `f_total_area_sqm`. */
11
+ readonly fieldId: string;
12
+ /** The translated label the author recognises. */
13
+ readonly label: string;
14
+ /** A stable page identity; fields sharing it are grouped together. */
15
+ readonly pageId?: string;
16
+ /** The translated page name shown as the group heading. */
17
+ readonly pageLabel?: string;
18
+ /** A stable section identity within the page. */
19
+ readonly sectionId?: string;
20
+ /** The translated section name shown as the subgroup heading. */
21
+ readonly sectionLabel?: string;
22
+ }
23
+ /** One field as an expression reference: the token, the id, and the label both stand for. */
24
+ export interface FieldReference {
25
+ readonly fieldId: string;
26
+ readonly label: string;
27
+ readonly token: string;
28
+ readonly pageLabel?: string;
29
+ readonly sectionLabel?: string;
30
+ }
31
+ /**
32
+ * The two directions of the token mapping, built once from an ordered field list.
33
+ *
34
+ * `ordered` is the list the picker renders and the index every token is drawn from, so a token
35
+ * and a row can never disagree about which field they mean. Tokens are positional (`$F1`, `$F2`,
36
+ * …) because that is the short spelling an author points at; the declared id stays the value an
37
+ * expression stores and compiles.
38
+ */
39
+ export interface ReferenceIndex {
40
+ readonly ordered: readonly FieldReference[];
41
+ readonly byId: ReadonlyMap<string, FieldReference>;
42
+ readonly byToken: ReadonlyMap<string, FieldReference>;
43
+ }
44
+ /** A text edit a picked reference makes: the inserted text and the caret after it. */
45
+ export interface ReferenceInsertion {
46
+ readonly value: string;
47
+ readonly caret: number;
48
+ }
49
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/reference/types.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc;IAC7B,+EAA+E;IAC/E,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,kDAAkD;IAClD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,sEAAsE;IACtE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,2DAA2D;IAC3D,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,iDAAiD;IACjD,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,iEAAiE;IACjE,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;CAChC;AAED,6FAA6F;AAC7F,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;CAChC;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,CAAC;IAC5C,QAAQ,CAAC,IAAI,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACnD,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;CACvD;AAED,sFAAsF;AACtF,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/reference/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * The field view the reference picker reads.\n *\n * The picker is a controlled component over a host-built list: a form-builder host already holds\n * the declared fields and their translated labels, so this module asks for the labels and the\n * grouping rather than reading a store, a document or a message catalogue. A field outside a\n * page or a section is shown in one ungrouped list.\n */\nexport interface ReferableField {\n /** The declared identifier an expression names, such as `f_total_area_sqm`. */\n readonly fieldId: string;\n /** The translated label the author recognises. */\n readonly label: string;\n /** A stable page identity; fields sharing it are grouped together. */\n readonly pageId?: string;\n /** The translated page name shown as the group heading. */\n readonly pageLabel?: string;\n /** A stable section identity within the page. */\n readonly sectionId?: string;\n /** The translated section name shown as the subgroup heading. */\n readonly sectionLabel?: string;\n}\n\n/** One field as an expression reference: the token, the id, and the label both stand for. */\nexport interface FieldReference {\n readonly fieldId: string;\n readonly label: string;\n readonly token: string;\n readonly pageLabel?: string;\n readonly sectionLabel?: string;\n}\n\n/**\n * The two directions of the token mapping, built once from an ordered field list.\n *\n * `ordered` is the list the picker renders and the index every token is drawn from, so a token\n * and a row can never disagree about which field they mean. Tokens are positional (`$F1`, `$F2`,\n * …) because that is the short spelling an author points at; the declared id stays the value an\n * expression stores and compiles.\n */\nexport interface ReferenceIndex {\n readonly ordered: readonly FieldReference[];\n readonly byId: ReadonlyMap<string, FieldReference>;\n readonly byToken: ReadonlyMap<string, FieldReference>;\n}\n\n/** A text edit a picked reference makes: the inserted text and the caret after it. */\nexport interface ReferenceInsertion {\n readonly value: string;\n readonly caret: number;\n}\n"]}
@@ -0,0 +1,38 @@
1
+ import type { ReactElement } from 'react';
2
+ import type { ReferencePickerLabels } from '../reference/ReferencePicker.js';
3
+ import type { ConditionDraft, RuleContext } from './types.js';
4
+ import type { ValidationEditorLabels } from './ValidationEditor.js';
5
+ /** The overridable chrome a condition tree uses. */
6
+ export interface ConditionEditorLabels {
7
+ readonly condition: string;
8
+ readonly mode: string;
9
+ readonly predicateMode: string;
10
+ readonly andMode: string;
11
+ readonly orMode: string;
12
+ readonly notMode: string;
13
+ readonly first: string;
14
+ readonly second: string;
15
+ readonly nested: string;
16
+ }
17
+ /** The condition sub-builder's props. */
18
+ export interface ConditionEditorProps {
19
+ readonly value: ConditionDraft;
20
+ readonly context: RuleContext;
21
+ readonly onChange: (draft: ConditionDraft) => void;
22
+ readonly labels?: Partial<ConditionEditorLabels> | undefined;
23
+ readonly validationLabels?: Partial<ValidationEditorLabels> | undefined;
24
+ readonly referencePickerLabels?: Partial<ReferencePickerLabels> | undefined;
25
+ readonly disabled?: boolean | undefined;
26
+ readonly id?: string | undefined;
27
+ readonly className?: string | undefined;
28
+ }
29
+ /**
30
+ * A condition tree: a field check, `and`, `or`, or `not`.
31
+ *
32
+ * Switching a node's mode preserves what it meant where it can: `not` wraps the current node,
33
+ * and `and`/`or` keep it as the first operand and add a fresh predicate as the second. A predicate
34
+ * delegates to `ValidationEditor`, so the one operator vocabulary and the one expression builder
35
+ * serve both the validation editor and every condition in a rule.
36
+ */
37
+ export declare function ConditionEditor(props: ConditionEditorProps): ReactElement;
38
+ //# sourceMappingURL=ConditionEditor.d.ts.map