@niscorp/loom 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DESIGN.md +253 -0
- package/LICENSE +202 -0
- package/README.md +101 -0
- package/dist/chunk-A3UOFGL4.js +29 -0
- package/dist/chunk-A3UOFGL4.js.map +1 -0
- package/dist/chunk-D743DI5J.js +17 -0
- package/dist/chunk-D743DI5J.js.map +1 -0
- package/dist/chunk-DWQWPBBV.js +63 -0
- package/dist/chunk-DWQWPBBV.js.map +1 -0
- package/dist/chunk-LWY5TG7M.js +208 -0
- package/dist/chunk-LWY5TG7M.js.map +1 -0
- package/dist/chunk-MIRMQPUE.js +132 -0
- package/dist/chunk-MIRMQPUE.js.map +1 -0
- package/dist/chunk-PTXRAWJX.js +58 -0
- package/dist/chunk-PTXRAWJX.js.map +1 -0
- package/dist/chunk-TV4U22MY.js +176 -0
- package/dist/chunk-TV4U22MY.js.map +1 -0
- package/dist/chunk-X7PCPONB.js +380 -0
- package/dist/chunk-X7PCPONB.js.map +1 -0
- package/dist/chunk-XTY7XO5C.js +61 -0
- package/dist/chunk-XTY7XO5C.js.map +1 -0
- package/dist/index.cjs +630 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +40 -0
- package/dist/index.d.ts +40 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/loom-editor-CQ_GI4IA.d.cts +27 -0
- package/dist/loom-editor-D5SBQ4_F.d.ts +27 -0
- package/dist/plugins/nova/index.cjs +81 -0
- package/dist/plugins/nova/index.cjs.map +1 -0
- package/dist/plugins/nova/index.d.cts +14 -0
- package/dist/plugins/nova/index.d.ts +14 -0
- package/dist/plugins/nova/index.js +5 -0
- package/dist/plugins/nova/index.js.map +1 -0
- package/dist/plugins/nova/react/index.cjs +107 -0
- package/dist/plugins/nova/react/index.cjs.map +1 -0
- package/dist/plugins/nova/react/index.d.cts +17 -0
- package/dist/plugins/nova/react/index.d.ts +17 -0
- package/dist/plugins/nova/react/index.js +36 -0
- package/dist/plugins/nova/react/index.js.map +1 -0
- package/dist/plugins/prism/index.cjs +48 -0
- package/dist/plugins/prism/index.cjs.map +1 -0
- package/dist/plugins/prism/index.d.cts +18 -0
- package/dist/plugins/prism/index.d.ts +18 -0
- package/dist/plugins/prism/index.js +4 -0
- package/dist/plugins/prism/index.js.map +1 -0
- package/dist/plugins/prism/react/index.cjs +744 -0
- package/dist/plugins/prism/react/index.cjs.map +1 -0
- package/dist/plugins/prism/react/index.d.cts +12 -0
- package/dist/plugins/prism/react/index.d.ts +12 -0
- package/dist/plugins/prism/react/index.js +419 -0
- package/dist/plugins/prism/react/index.js.map +1 -0
- package/dist/plugins/vex/index.cjs +67 -0
- package/dist/plugins/vex/index.cjs.map +1 -0
- package/dist/plugins/vex/index.d.cts +25 -0
- package/dist/plugins/vex/index.d.ts +25 -0
- package/dist/plugins/vex/index.js +4 -0
- package/dist/plugins/vex/index.js.map +1 -0
- package/dist/plugins/vex/react/index.cjs +432 -0
- package/dist/plugins/vex/react/index.cjs.map +1 -0
- package/dist/plugins/vex/react/index.d.cts +13 -0
- package/dist/plugins/vex/react/index.d.ts +13 -0
- package/dist/plugins/vex/react/index.js +296 -0
- package/dist/plugins/vex/react/index.js.map +1 -0
- package/dist/react/index.cjs +1076 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +82 -0
- package/dist/react/index.d.ts +82 -0
- package/dist/react/index.js +120 -0
- package/dist/react/index.js.map +1 -0
- package/dist/schema--OhtatTX.d.cts +10 -0
- package/dist/schema--OhtatTX.d.ts +10 -0
- package/dist/types-BonjN3CB.d.cts +144 -0
- package/dist/types-BonjN3CB.d.ts +144 -0
- package/package.json +144 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
import { Query, DatabaseSchema } from '@niscorp/vex';
|
|
3
|
+
export { Query } from '@niscorp/vex';
|
|
4
|
+
import { h as LoomPlugin } from '../../types-BonjN3CB.cjs';
|
|
5
|
+
import '@niscorp/nova';
|
|
6
|
+
|
|
7
|
+
declare const PREVIEW = "vex:preview";
|
|
8
|
+
|
|
9
|
+
type VexRunResult = {
|
|
10
|
+
rows: unknown[];
|
|
11
|
+
warnings?: string[];
|
|
12
|
+
errors?: string[];
|
|
13
|
+
};
|
|
14
|
+
type VexPluginConfig = {
|
|
15
|
+
/** Run an edited query and return its rows (plus any warnings / errors). */
|
|
16
|
+
run: (query: Query) => Promise<VexRunResult>;
|
|
17
|
+
/** The introspected database schema — gives the field-path widgets their
|
|
18
|
+
* column list. Omit to fall back to plain inputs. */
|
|
19
|
+
db?: DatabaseSchema;
|
|
20
|
+
/** Override the edited schema; defaults to Vex's `QuerySchema`. */
|
|
21
|
+
schema?: ZodType;
|
|
22
|
+
};
|
|
23
|
+
declare const vexPlugin: (config: VexPluginConfig) => LoomPlugin;
|
|
24
|
+
|
|
25
|
+
export { PREVIEW, type VexPluginConfig, type VexRunResult, vexPlugin };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
import { Query, DatabaseSchema } from '@niscorp/vex';
|
|
3
|
+
export { Query } from '@niscorp/vex';
|
|
4
|
+
import { h as LoomPlugin } from '../../types-BonjN3CB.js';
|
|
5
|
+
import '@niscorp/nova';
|
|
6
|
+
|
|
7
|
+
declare const PREVIEW = "vex:preview";
|
|
8
|
+
|
|
9
|
+
type VexRunResult = {
|
|
10
|
+
rows: unknown[];
|
|
11
|
+
warnings?: string[];
|
|
12
|
+
errors?: string[];
|
|
13
|
+
};
|
|
14
|
+
type VexPluginConfig = {
|
|
15
|
+
/** Run an edited query and return its rows (plus any warnings / errors). */
|
|
16
|
+
run: (query: Query) => Promise<VexRunResult>;
|
|
17
|
+
/** The introspected database schema — gives the field-path widgets their
|
|
18
|
+
* column list. Omit to fall back to plain inputs. */
|
|
19
|
+
db?: DatabaseSchema;
|
|
20
|
+
/** Override the edited schema; defaults to Vex's `QuerySchema`. */
|
|
21
|
+
schema?: ZodType;
|
|
22
|
+
};
|
|
23
|
+
declare const vexPlugin: (config: VexPluginConfig) => LoomPlugin;
|
|
24
|
+
|
|
25
|
+
export { PREVIEW, type VexPluginConfig, type VexRunResult, vexPlugin };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var vex$1 = require('@niscorp/vex');
|
|
4
|
+
var react = require('react');
|
|
5
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
+
var react$1 = require('@niscorp/nova/adapters/react');
|
|
7
|
+
var nova = require('@niscorp/nova');
|
|
8
|
+
|
|
9
|
+
// src/plugins/vex/index.ts
|
|
10
|
+
|
|
11
|
+
// src/editor/view.ts
|
|
12
|
+
var mountView = (editor, action, read) => {
|
|
13
|
+
const { shell } = editor;
|
|
14
|
+
shell.registerAction(action);
|
|
15
|
+
shell.addCanvas({ id: action.id, initial: action.id });
|
|
16
|
+
const instance = shell.getCanvasState(action.id).active?.id;
|
|
17
|
+
if (instance === void 0) return;
|
|
18
|
+
const sync = () => {
|
|
19
|
+
shell.getRuntime(instance)?.setData(read(editor));
|
|
20
|
+
};
|
|
21
|
+
editor.on("change", sync);
|
|
22
|
+
sync();
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/plugins/vex/widgets.ts
|
|
26
|
+
var VEX_COMPARISON = "vex:comparison";
|
|
27
|
+
var VEX_FIELD_PATH = "vex:field-path";
|
|
28
|
+
var VEX_AGGREGATE = "vex:aggregate";
|
|
29
|
+
var VEX_COMPUTE = "vex:compute";
|
|
30
|
+
var COMPARISON_OPS = /* @__PURE__ */ new Set(["eq", "neq", "gt", "gte", "lt", "lte", "like", "ilike"]);
|
|
31
|
+
var FIELD_KEYS = /* @__PURE__ */ new Set(["field", "isNull", "isNotNull"]);
|
|
32
|
+
var leaf = (path) => path.slice(path.lastIndexOf(".") + 1);
|
|
33
|
+
var isColumnListItem = (path) => /(?:^|\.)(?:fields|groupBy)\.\*$/.test(path);
|
|
34
|
+
var vexWidgets = [
|
|
35
|
+
// A comparison `[left, right]`: a field-path picker + a typed operand.
|
|
36
|
+
{ role: VEX_COMPARISON, match: (field) => field.kind === "unknown" && COMPARISON_OPS.has(leaf(field.path)) },
|
|
37
|
+
// A standalone field path: a sort key, `isNull`/`isNotNull`, or a `fields` / `groupBy` item.
|
|
38
|
+
{ role: VEX_FIELD_PATH, match: (field) => field.kind === "string" && (FIELD_KEYS.has(leaf(field.path)) || isColumnListItem(field.path)) },
|
|
39
|
+
// `aggregate` / `compute` records (alias -> expression): keyed-entry editors.
|
|
40
|
+
{ role: VEX_AGGREGATE, match: (field) => field.kind === "unknown" && leaf(field.path) === "aggregate" },
|
|
41
|
+
{ role: VEX_COMPUTE, match: (field) => field.kind === "unknown" && leaf(field.path) === "compute" }
|
|
42
|
+
];
|
|
43
|
+
var catalogOf = (db) => {
|
|
44
|
+
const types = /* @__PURE__ */ new Map();
|
|
45
|
+
const columns = [];
|
|
46
|
+
for (const entity of db.entities) {
|
|
47
|
+
for (const field of entity.fields) {
|
|
48
|
+
const column = `${entity.name}.${field.name}`;
|
|
49
|
+
columns.push(column);
|
|
50
|
+
types.set(column, field.normalizedType);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return { columns, typeOf: (column) => types.get(column) };
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// src/plugins/vex/preview.layout.ts
|
|
57
|
+
var PREVIEW = "vex:preview";
|
|
58
|
+
var previewLayout = {
|
|
59
|
+
component: PREVIEW,
|
|
60
|
+
props: { query: "$.query" }
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
// src/plugins/vex/preview.action.ts
|
|
64
|
+
var previewAction = {
|
|
65
|
+
id: PREVIEW,
|
|
66
|
+
layout: previewLayout,
|
|
67
|
+
data: { query: null }
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// src/plugins/vex/index.ts
|
|
71
|
+
var vexPlugin = (config) => ({
|
|
72
|
+
name: "vex",
|
|
73
|
+
documents: { query: config.schema ?? vex$1.QuerySchema },
|
|
74
|
+
// Widgets only matter with a schema to scope columns against; without `db` the
|
|
75
|
+
// query edits as plain inputs.
|
|
76
|
+
...config.db !== void 0 ? { widgets: vexWidgets } : {},
|
|
77
|
+
mount: (editor) => mountView(editor, previewAction, (e) => ({ query: e.documents["query"] }))
|
|
78
|
+
});
|
|
79
|
+
var makePreview = (run) => {
|
|
80
|
+
const Preview = ({ query }) => {
|
|
81
|
+
const [state, setState] = react.useState({ status: "empty" });
|
|
82
|
+
const seq = react.useRef(0);
|
|
83
|
+
const key = query == null ? "" : JSON.stringify(query);
|
|
84
|
+
react.useEffect(() => {
|
|
85
|
+
if (query == null) {
|
|
86
|
+
setState({ status: "empty" });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const mine = ++seq.current;
|
|
90
|
+
setState({ status: "running" });
|
|
91
|
+
const handle = setTimeout(() => {
|
|
92
|
+
run(query).then((result) => {
|
|
93
|
+
if (mine === seq.current) setState({ status: "ok", result });
|
|
94
|
+
}).catch((err) => {
|
|
95
|
+
if (mine === seq.current) {
|
|
96
|
+
setState({ status: "error", message: err instanceof Error ? err.message : String(err) });
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
}, 250);
|
|
100
|
+
return () => clearTimeout(handle);
|
|
101
|
+
}, [key]);
|
|
102
|
+
return /* @__PURE__ */ jsxRuntime.jsx(PreviewView, { state });
|
|
103
|
+
};
|
|
104
|
+
return Preview;
|
|
105
|
+
};
|
|
106
|
+
var PreviewView = ({ state }) => {
|
|
107
|
+
if (state.status === "empty") return /* @__PURE__ */ jsxRuntime.jsx(Note, { children: "Edit the query to preview its results." });
|
|
108
|
+
if (state.status === "running") return /* @__PURE__ */ jsxRuntime.jsx(Note, { children: "Running\u2026" });
|
|
109
|
+
if (state.status === "error") return /* @__PURE__ */ jsxRuntime.jsx(Banner, { tone: "error", lines: [state.message] });
|
|
110
|
+
const { rows, warnings, errors } = state.result;
|
|
111
|
+
if (errors !== void 0 && errors.length > 0) return /* @__PURE__ */ jsxRuntime.jsx(Banner, { tone: "error", lines: errors });
|
|
112
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", flexDirection: "column", gap: 10 }, children: [
|
|
113
|
+
warnings !== void 0 && warnings.length > 0 ? /* @__PURE__ */ jsxRuntime.jsx(Banner, { tone: "warn", lines: warnings }) : null,
|
|
114
|
+
/* @__PURE__ */ jsxRuntime.jsx(RowsTable, { rows })
|
|
115
|
+
] });
|
|
116
|
+
};
|
|
117
|
+
var mono = "ui-monospace, SFMono-Regular, Menlo, monospace";
|
|
118
|
+
var Note = ({ children }) => /* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: 13, color: "#6b7280", padding: 12 }, children });
|
|
119
|
+
var TONES = {
|
|
120
|
+
error: { fg: "#991b1b", bg: "#fef2f2", border: "#fecaca" },
|
|
121
|
+
warn: { fg: "#92400e", bg: "#fffbeb", border: "#fde68a" }
|
|
122
|
+
};
|
|
123
|
+
var Banner = ({ tone, lines }) => {
|
|
124
|
+
const c = TONES[tone];
|
|
125
|
+
return /* @__PURE__ */ jsxRuntime.jsx("pre", { style: { margin: 0, padding: 12, background: c.bg, color: c.fg, border: `1px solid ${c.border}`, borderRadius: 6, fontSize: 12, fontFamily: mono, whiteSpace: "pre-wrap", wordBreak: "break-word" }, children: lines.join("\n") });
|
|
126
|
+
};
|
|
127
|
+
var th = { textAlign: "left", padding: "6px 10px", background: "#f9fafb", borderBottom: "1px solid #e5e7eb", color: "#6b7280", fontWeight: 600, whiteSpace: "nowrap" };
|
|
128
|
+
var td = { padding: "6px 10px", borderBottom: "1px solid #f1f5f9", fontFamily: mono, whiteSpace: "nowrap" };
|
|
129
|
+
var cell = (value) => value === null ? "\u2205" : typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
130
|
+
var RowsTable = ({ rows }) => {
|
|
131
|
+
if (rows.length === 0) return /* @__PURE__ */ jsxRuntime.jsx(Note, { children: "No rows." });
|
|
132
|
+
const first = rows[0];
|
|
133
|
+
if (first === null || typeof first !== "object") {
|
|
134
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Banner, { tone: "warn", lines: [JSON.stringify(rows, null, 2)] });
|
|
135
|
+
}
|
|
136
|
+
const cols = Object.keys(first);
|
|
137
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { style: { overflow: "auto", border: "1px solid #e5e7eb", borderRadius: 6 }, children: /* @__PURE__ */ jsxRuntime.jsxs("table", { style: { borderCollapse: "collapse", width: "100%", fontSize: 12 }, children: [
|
|
138
|
+
/* @__PURE__ */ jsxRuntime.jsx("thead", { children: /* @__PURE__ */ jsxRuntime.jsx("tr", { children: cols.map((c) => /* @__PURE__ */ jsxRuntime.jsx("th", { style: th, children: c }, c)) }) }),
|
|
139
|
+
/* @__PURE__ */ jsxRuntime.jsx("tbody", { children: rows.map((row, i) => /* @__PURE__ */ jsxRuntime.jsx("tr", { children: cols.map((c) => /* @__PURE__ */ jsxRuntime.jsx("td", { style: td, children: cell(row[c]) }, c)) }, i)) })
|
|
140
|
+
] }) });
|
|
141
|
+
};
|
|
142
|
+
var useModelWrite = (model) => {
|
|
143
|
+
const dispatch = react$1.useNovaDispatch();
|
|
144
|
+
return (value) => {
|
|
145
|
+
if (model !== void 0) dispatch({ type: "ui:model", ref: model.ref, payload: value });
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
var LoomDocumentContext = react.createContext(void 0);
|
|
149
|
+
var useLoomDocument = () => react.useContext(LoomDocumentContext);
|
|
150
|
+
|
|
151
|
+
// src/plugins/vex/react/columns.ts
|
|
152
|
+
var useScopedColumns = (catalog) => {
|
|
153
|
+
const document = useLoomDocument();
|
|
154
|
+
const from = document === void 0 ? void 0 : nova.getPath(document, "query.from");
|
|
155
|
+
const entities = Array.isArray(from) ? from.filter((source) => typeof source === "string") : [];
|
|
156
|
+
if (entities.length === 0) return catalog.columns;
|
|
157
|
+
const allowed = new Set(entities);
|
|
158
|
+
const scoped = catalog.columns.filter((column) => allowed.has(column.slice(0, column.indexOf("."))));
|
|
159
|
+
return scoped.length > 0 ? scoped : catalog.columns;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
// src/plugins/vex/react/styles.ts
|
|
163
|
+
var fieldStyle = { padding: "6px 10px", border: "1px solid #d1d5db", borderRadius: 6, fontSize: 14, boxSizing: "border-box" };
|
|
164
|
+
var controlBtn = { padding: "6px 10px", border: "1px solid #d1d5db", borderRadius: 6, background: "#f3f4f6", cursor: "pointer", fontSize: 13 };
|
|
165
|
+
var comboListStyle = { position: "absolute", top: "100%", left: 0, right: 0, zIndex: 20, marginTop: 2, maxHeight: 200, overflowY: "auto", background: "#fff", border: "1px solid #d1d5db", borderRadius: 6, boxShadow: "0 6px 16px rgba(0,0,0,0.12)" };
|
|
166
|
+
var comboItemStyle = { padding: "6px 10px", fontSize: 13, cursor: "pointer", fontFamily: "ui-monospace, Menlo, monospace" };
|
|
167
|
+
var Combo = ({ value, placeholder, options, onPick, style }) => {
|
|
168
|
+
const [open, setOpen] = react.useState(false);
|
|
169
|
+
const needle = value.toLowerCase();
|
|
170
|
+
const matches2 = options.filter((option) => option.toLowerCase().includes(needle));
|
|
171
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { position: "relative", ...style }, children: [
|
|
172
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
173
|
+
"input",
|
|
174
|
+
{
|
|
175
|
+
value,
|
|
176
|
+
placeholder,
|
|
177
|
+
onChange: (event) => onPick(event.target.value),
|
|
178
|
+
onFocus: () => setOpen(true),
|
|
179
|
+
onBlur: () => window.setTimeout(() => setOpen(false), 120),
|
|
180
|
+
style: { ...fieldStyle, width: "100%" }
|
|
181
|
+
}
|
|
182
|
+
),
|
|
183
|
+
open && matches2.length > 0 ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: comboListStyle, children: matches2.slice(0, 50).map((option) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
184
|
+
"div",
|
|
185
|
+
{
|
|
186
|
+
onMouseDown: (event) => {
|
|
187
|
+
event.preventDefault();
|
|
188
|
+
onPick(option);
|
|
189
|
+
setOpen(false);
|
|
190
|
+
},
|
|
191
|
+
style: comboItemStyle,
|
|
192
|
+
children: option
|
|
193
|
+
},
|
|
194
|
+
option
|
|
195
|
+
)) }) : null
|
|
196
|
+
] });
|
|
197
|
+
};
|
|
198
|
+
var FieldPath = ({ value, catalog, model }) => {
|
|
199
|
+
const set = useModelWrite(model);
|
|
200
|
+
const columns = useScopedColumns(catalog);
|
|
201
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Combo, { value: String(value ?? ""), placeholder: "entity.field", options: columns, onPick: set, style: { width: "100%" } });
|
|
202
|
+
};
|
|
203
|
+
var Operand = ({ type, value, onChange }) => {
|
|
204
|
+
const style = { ...fieldStyle, flex: 1, minWidth: 0 };
|
|
205
|
+
if (type === "number") {
|
|
206
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
207
|
+
"input",
|
|
208
|
+
{
|
|
209
|
+
type: "number",
|
|
210
|
+
value: typeof value === "number" ? value : "",
|
|
211
|
+
placeholder: "value",
|
|
212
|
+
onChange: (event) => onChange(event.target.value === "" ? null : Number(event.target.value)),
|
|
213
|
+
style
|
|
214
|
+
}
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
if (type === "boolean") {
|
|
218
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
219
|
+
"select",
|
|
220
|
+
{
|
|
221
|
+
value: value === true ? "true" : value === false ? "false" : "",
|
|
222
|
+
onChange: (event) => onChange(event.target.value === "" ? null : event.target.value === "true"),
|
|
223
|
+
style,
|
|
224
|
+
children: [
|
|
225
|
+
/* @__PURE__ */ jsxRuntime.jsx("option", { value: "", children: "\u2014" }),
|
|
226
|
+
/* @__PURE__ */ jsxRuntime.jsx("option", { value: "true", children: "true" }),
|
|
227
|
+
/* @__PURE__ */ jsxRuntime.jsx("option", { value: "false", children: "false" })
|
|
228
|
+
]
|
|
229
|
+
}
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
233
|
+
"input",
|
|
234
|
+
{
|
|
235
|
+
value: value === null || value === void 0 ? "" : String(value),
|
|
236
|
+
placeholder: "value",
|
|
237
|
+
onChange: (event) => onChange(event.target.value),
|
|
238
|
+
style
|
|
239
|
+
}
|
|
240
|
+
);
|
|
241
|
+
};
|
|
242
|
+
var Comparison = ({ value, catalog, model }) => {
|
|
243
|
+
const set = useModelWrite(model);
|
|
244
|
+
const columns = useScopedColumns(catalog);
|
|
245
|
+
const tuple = Array.isArray(value) ? value : [];
|
|
246
|
+
const left = tuple[0];
|
|
247
|
+
const right = tuple[1];
|
|
248
|
+
const type = typeof left === "string" ? catalog.typeOf(left) : void 0;
|
|
249
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", gap: 6, alignItems: "center" }, children: [
|
|
250
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
251
|
+
Combo,
|
|
252
|
+
{
|
|
253
|
+
value: String(left ?? ""),
|
|
254
|
+
placeholder: "entity.field",
|
|
255
|
+
options: columns,
|
|
256
|
+
onPick: (next) => set([next, right ?? null]),
|
|
257
|
+
style: { flex: 2, minWidth: 0 }
|
|
258
|
+
}
|
|
259
|
+
),
|
|
260
|
+
/* @__PURE__ */ jsxRuntime.jsx(Operand, { type, value: right, onChange: (next) => set([left ?? "", next]) })
|
|
261
|
+
] });
|
|
262
|
+
};
|
|
263
|
+
var fieldPathWidget = (catalog) => ({ value, novaModel }) => /* @__PURE__ */ jsxRuntime.jsx(FieldPath, { value, catalog, model: novaModel });
|
|
264
|
+
var comparisonWidget = (catalog) => ({ value, novaModel }) => /* @__PURE__ */ jsxRuntime.jsx(Comparison, { value, catalog, model: novaModel });
|
|
265
|
+
|
|
266
|
+
// src/compile/parse.ts
|
|
267
|
+
var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
268
|
+
|
|
269
|
+
// src/react/kit/shared.ts
|
|
270
|
+
var inputStyle = { padding: "6px 10px", border: "1px solid #d1d5db", borderRadius: 6, fontSize: 14, width: "100%", boxSizing: "border-box" };
|
|
271
|
+
var rawStyle = {
|
|
272
|
+
...inputStyle,
|
|
273
|
+
margin: 0,
|
|
274
|
+
fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace",
|
|
275
|
+
fontSize: 13,
|
|
276
|
+
minHeight: 64,
|
|
277
|
+
resize: "vertical",
|
|
278
|
+
width: "100%",
|
|
279
|
+
boxSizing: "border-box"
|
|
280
|
+
};
|
|
281
|
+
var showJson = (value) => JSON.stringify(value ?? null, null, 2);
|
|
282
|
+
var JsonEditor = ({ value, onChange }) => {
|
|
283
|
+
const [text, setText] = react.useState(() => showJson(value));
|
|
284
|
+
const [valid, setValid] = react.useState(true);
|
|
285
|
+
const serialized = JSON.stringify(value ?? null);
|
|
286
|
+
react.useEffect(() => {
|
|
287
|
+
let mine;
|
|
288
|
+
try {
|
|
289
|
+
mine = JSON.parse(text);
|
|
290
|
+
} catch {
|
|
291
|
+
setText(showJson(value));
|
|
292
|
+
setValid(true);
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (JSON.stringify(mine ?? null) !== serialized) {
|
|
296
|
+
setText(showJson(value));
|
|
297
|
+
setValid(true);
|
|
298
|
+
}
|
|
299
|
+
}, [serialized]);
|
|
300
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
301
|
+
"textarea",
|
|
302
|
+
{
|
|
303
|
+
value: text,
|
|
304
|
+
spellCheck: false,
|
|
305
|
+
onChange: (event) => {
|
|
306
|
+
const next = event.target.value;
|
|
307
|
+
setText(next);
|
|
308
|
+
try {
|
|
309
|
+
const parsed = JSON.parse(next);
|
|
310
|
+
setValid(true);
|
|
311
|
+
onChange(parsed);
|
|
312
|
+
} catch {
|
|
313
|
+
setValid(false);
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
style: valid ? rawStyle : { ...rawStyle, borderColor: "#dc2626", background: "#fef2f2" }
|
|
317
|
+
}
|
|
318
|
+
);
|
|
319
|
+
};
|
|
320
|
+
react.createContext(0);
|
|
321
|
+
react.createContext({ close: () => {
|
|
322
|
+
}, anchor: "right" });
|
|
323
|
+
var RecordRows = ({ value, model, newEntry, renderValue }) => {
|
|
324
|
+
const set = useModelWrite(model);
|
|
325
|
+
const entries = isRecord(value) ? Object.entries(value) : [];
|
|
326
|
+
const write = (next) => set(Object.fromEntries(next));
|
|
327
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", flexDirection: "column", gap: 6, padding: 12, border: "1px dashed #d1d5db", borderRadius: 6 }, children: [
|
|
328
|
+
entries.map(([key, val], index) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", gap: 6, alignItems: "flex-start" }, children: [
|
|
329
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
330
|
+
"input",
|
|
331
|
+
{
|
|
332
|
+
value: key,
|
|
333
|
+
placeholder: "alias",
|
|
334
|
+
onChange: (event) => write(entries.map((entry, i) => i === index ? [event.target.value, entry[1]] : entry)),
|
|
335
|
+
style: { ...fieldStyle, width: 110 }
|
|
336
|
+
}
|
|
337
|
+
),
|
|
338
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { style: { flex: 1, minWidth: 0 }, children: renderValue(val, (next) => write(entries.map((entry, i) => i === index ? [entry[0], next] : entry))) }),
|
|
339
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: () => write(entries.filter((_, i) => i !== index)), style: controlBtn, children: "\u2715" })
|
|
340
|
+
] }, index)),
|
|
341
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: () => write([...entries, [`field_${entries.length + 1}`, newEntry]]), style: { ...controlBtn, alignSelf: "flex-start" }, children: "Add" })
|
|
342
|
+
] });
|
|
343
|
+
};
|
|
344
|
+
var coerceLiteral = (text) => {
|
|
345
|
+
if (text === "") return "";
|
|
346
|
+
if (text === "true") return true;
|
|
347
|
+
if (text === "false") return false;
|
|
348
|
+
const n = Number(text);
|
|
349
|
+
return text.trim() !== "" && Number.isFinite(n) ? n : text;
|
|
350
|
+
};
|
|
351
|
+
var ValueInput = ({ value, columns, onChange }) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
352
|
+
Combo,
|
|
353
|
+
{
|
|
354
|
+
value: value === null || value === void 0 ? "" : String(value),
|
|
355
|
+
placeholder: "field or value",
|
|
356
|
+
options: columns,
|
|
357
|
+
onPick: (text) => onChange(coerceLiteral(text)),
|
|
358
|
+
style: { flex: 1, minWidth: 120 }
|
|
359
|
+
}
|
|
360
|
+
);
|
|
361
|
+
var AGG_OPS = ["count", "sum", "avg", "min", "max"];
|
|
362
|
+
var AggregateValue = ({ value, onChange, catalog }) => {
|
|
363
|
+
const columns = useScopedColumns(catalog);
|
|
364
|
+
const record = isRecord(value) ? value : {};
|
|
365
|
+
const op = AGG_OPS.find((candidate) => candidate in record) ?? "count";
|
|
366
|
+
const operand = typeof record[op] === "string" ? record[op] : "";
|
|
367
|
+
const options = op === "count" ? ["*", ...columns] : columns;
|
|
368
|
+
const switchOp = (next) => {
|
|
369
|
+
let carried = operand;
|
|
370
|
+
if (next === "count" && carried === "") carried = "*";
|
|
371
|
+
if (next !== "count" && carried === "*") carried = "";
|
|
372
|
+
onChange({ [next]: carried });
|
|
373
|
+
};
|
|
374
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", gap: 6, alignItems: "center" }, children: [
|
|
375
|
+
/* @__PURE__ */ jsxRuntime.jsx("select", { value: op, onChange: (event) => switchOp(event.target.value), style: { ...fieldStyle, width: 90 }, children: AGG_OPS.map((candidate) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: candidate, children: candidate }, candidate)) }),
|
|
376
|
+
/* @__PURE__ */ jsxRuntime.jsx(Combo, { value: operand, placeholder: "entity.field", options, onPick: (next) => onChange({ [op]: next }), style: { flex: 1, minWidth: 0 } })
|
|
377
|
+
] });
|
|
378
|
+
};
|
|
379
|
+
var ARITHMETIC = /* @__PURE__ */ new Set(["add", "subtract", "multiply", "divide"]);
|
|
380
|
+
var LISTS = /* @__PURE__ */ new Set(["concat", "coalesce"]);
|
|
381
|
+
var COMPUTE_OPS = ["add", "subtract", "multiply", "divide", "concat", "coalesce", "case"];
|
|
382
|
+
var ComputeValue = ({ value, onChange, catalog }) => {
|
|
383
|
+
const columns = useScopedColumns(catalog);
|
|
384
|
+
const record = isRecord(value) ? value : {};
|
|
385
|
+
const op = COMPUTE_OPS.find((candidate) => candidate in record) ?? "add";
|
|
386
|
+
const operands = Array.isArray(record[op]) ? record[op] : [];
|
|
387
|
+
const switchOp = (next) => {
|
|
388
|
+
if (next === "case") {
|
|
389
|
+
onChange({ case: { when: [{ condition: { eq: ["", ""] }, then: "" }], else: "" } });
|
|
390
|
+
} else if (ARITHMETIC.has(next)) {
|
|
391
|
+
onChange({ [next]: [operands[0] ?? "", operands[1] ?? ""] });
|
|
392
|
+
} else {
|
|
393
|
+
onChange({ [next]: operands.length >= 2 ? operands : ["", ""] });
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", gap: 6, alignItems: "flex-start", flexWrap: "wrap" }, children: [
|
|
397
|
+
/* @__PURE__ */ jsxRuntime.jsx("select", { value: op, onChange: (event) => switchOp(event.target.value), style: { ...fieldStyle, width: 90 }, children: COMPUTE_OPS.map((candidate) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: candidate, children: candidate }, candidate)) }),
|
|
398
|
+
ARITHMETIC.has(op) ? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
399
|
+
/* @__PURE__ */ jsxRuntime.jsx(ValueInput, { value: operands[0], columns, onChange: (next) => onChange({ [op]: [next, operands[1] ?? ""] }) }),
|
|
400
|
+
/* @__PURE__ */ jsxRuntime.jsx(ValueInput, { value: operands[1], columns, onChange: (next) => onChange({ [op]: [operands[0] ?? "", next] }) })
|
|
401
|
+
] }) : null,
|
|
402
|
+
LISTS.has(op) ? /* @__PURE__ */ jsxRuntime.jsx(OperandList, { values: operands, columns, onChange: (list) => onChange({ [op]: list }) }) : null,
|
|
403
|
+
op === "case" ? /* @__PURE__ */ jsxRuntime.jsx("div", { style: { flex: 1, minWidth: 200 }, children: /* @__PURE__ */ jsxRuntime.jsx(JsonEditor, { value: record.case, onChange: (next) => onChange({ case: next }) }) }) : null
|
|
404
|
+
] });
|
|
405
|
+
};
|
|
406
|
+
var OperandList = ({ values, columns, onChange }) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", flexDirection: "column", gap: 4, flex: 1, minWidth: 160 }, children: [
|
|
407
|
+
values.map((operand, index) => /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "flex", gap: 4 }, children: [
|
|
408
|
+
/* @__PURE__ */ jsxRuntime.jsx(ValueInput, { value: operand, columns, onChange: (next) => onChange(values.map((current, i) => i === index ? next : current)) }),
|
|
409
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", disabled: values.length <= 2, onClick: () => onChange(values.filter((_, i) => i !== index)), style: controlBtn, children: "\u2715" })
|
|
410
|
+
] }, index)),
|
|
411
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", onClick: () => onChange([...values, ""]), style: { ...controlBtn, alignSelf: "flex-start" }, children: "+" })
|
|
412
|
+
] });
|
|
413
|
+
var aggregateWidget = (catalog) => ({ value, novaModel }) => /* @__PURE__ */ jsxRuntime.jsx(RecordRows, { value, model: novaModel, newEntry: { count: "*" }, renderValue: (v, onChange) => /* @__PURE__ */ jsxRuntime.jsx(AggregateValue, { value: v, onChange, catalog }) });
|
|
414
|
+
var computeWidget = (catalog) => ({ value, novaModel }) => /* @__PURE__ */ jsxRuntime.jsx(RecordRows, { value, model: novaModel, newEntry: { add: ["", ""] }, renderValue: (v, onChange) => /* @__PURE__ */ jsxRuntime.jsx(ComputeValue, { value: v, onChange, catalog }) });
|
|
415
|
+
|
|
416
|
+
// src/plugins/vex/react/index.ts
|
|
417
|
+
var vex = (config) => {
|
|
418
|
+
const { name, documents, mount, widgets } = vexPlugin(config);
|
|
419
|
+
const components = { [PREVIEW]: makePreview(config.run) };
|
|
420
|
+
if (config.db !== void 0) {
|
|
421
|
+
const catalog = catalogOf(config.db);
|
|
422
|
+
components[VEX_COMPARISON] = comparisonWidget(catalog);
|
|
423
|
+
components[VEX_FIELD_PATH] = fieldPathWidget(catalog);
|
|
424
|
+
components[VEX_AGGREGATE] = aggregateWidget(catalog);
|
|
425
|
+
components[VEX_COMPUTE] = computeWidget(catalog);
|
|
426
|
+
}
|
|
427
|
+
return { name, documents, mount, widgets, components };
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
exports.vex = vex;
|
|
431
|
+
//# sourceMappingURL=index.cjs.map
|
|
432
|
+
//# sourceMappingURL=index.cjs.map
|