@revisium/schema-toolkit 0.19.4 → 0.20.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 (29) hide show
  1. package/dist/{FormulaPathBuilder--okalwdl.d.cts → FormulaPathBuilder-D9LkBcSv.d.cts} +1 -1
  2. package/dist/{FormulaPathBuilder-rW6PJd-r.d.ts → FormulaPathBuilder-DwlKOvVB.d.ts} +1 -1
  3. package/dist/{chunk-O4QRUV7I.cjs → chunk-FZ2MS4Q5.cjs} +848 -73
  4. package/dist/chunk-FZ2MS4Q5.cjs.map +1 -0
  5. package/dist/{chunk-5BBFBSGI.js → chunk-U5YDA3LA.js} +850 -76
  6. package/dist/chunk-U5YDA3LA.js.map +1 -0
  7. package/dist/core/index.d.cts +2 -2
  8. package/dist/core/index.d.ts +2 -2
  9. package/dist/index.cjs +46 -42
  10. package/dist/index.d.cts +5 -5
  11. package/dist/index.d.ts +5 -5
  12. package/dist/index.js +1 -1
  13. package/dist/{json-string.store-DRmQFKrp.d.ts → json-string.store-C58zbl31.d.ts} +1 -1
  14. package/dist/{json-string.store-DOZn38CI.d.cts → json-string.store-DuI--IL2.d.cts} +1 -1
  15. package/dist/{json-value-patch.types-U2y_ugm1.d.ts → json-value-patch.types-CgiEpchp.d.ts} +1 -1
  16. package/dist/{json-value-patch.types-CjBpq5VF.d.cts → json-value-patch.types-LEJxcIWR.d.cts} +1 -1
  17. package/dist/{json.types-46Cq-WxU.d.cts → json.types-PrMmLhba.d.cts} +1 -1
  18. package/dist/{json.types-46Cq-WxU.d.ts → json.types-PrMmLhba.d.ts} +1 -1
  19. package/dist/lib/index.d.cts +2 -2
  20. package/dist/lib/index.d.ts +2 -2
  21. package/dist/model/index.cjs +46 -42
  22. package/dist/model/index.d.cts +130 -99
  23. package/dist/model/index.d.ts +130 -99
  24. package/dist/model/index.js +1 -1
  25. package/dist/types/index.d.cts +2 -2
  26. package/dist/types/index.d.ts +2 -2
  27. package/package.json +1 -1
  28. package/dist/chunk-5BBFBSGI.js.map +0 -1
  29. package/dist/chunk-O4QRUV7I.cjs.map +0 -1
@@ -1079,87 +1079,525 @@ var SchemaModelImpl = class {
1079
1079
  function createSchemaModel(schema, options) {
1080
1080
  return new SchemaModelImpl(schema, options);
1081
1081
  }
1082
-
1083
- // src/model/table/row/RowModelImpl.ts
1084
- var UNSET_INDEX = -1;
1085
- var RowModelImpl = class {
1086
- constructor(_rowId, _tree) {
1087
- this._rowId = _rowId;
1088
- this._tree = _tree;
1089
- chunk46ME2TAU_cjs.makeAutoObservable(this, {
1090
- _rowId: false,
1091
- _tree: false,
1092
- _tableModel: "observable.ref"
1082
+ var INDEX_REGEX = /^\[(-?\d+)\]/;
1083
+ var PROP_REGEX = /^\.?([a-zA-Z_]\w*)/;
1084
+ var FormulaCollector = class {
1085
+ root = null;
1086
+ collect(root) {
1087
+ this.root = root;
1088
+ const formulas = [];
1089
+ this.traverse(root, { parent: null, arrayLevels: [] }, formulas);
1090
+ return formulas;
1091
+ }
1092
+ traverse(node, context, formulas) {
1093
+ if (node.isPrimitive()) {
1094
+ this.collectPrimitive(node, context, formulas);
1095
+ } else if (node.isObject()) {
1096
+ for (const child of node.children) {
1097
+ this.traverse(child, { ...context, parent: node }, formulas);
1098
+ }
1099
+ } else if (node.isArray()) {
1100
+ for (let i = 0; i < node.length; i++) {
1101
+ const item = node.at(i);
1102
+ if (item) {
1103
+ this.traverse(
1104
+ item,
1105
+ {
1106
+ ...context,
1107
+ arrayLevels: [...context.arrayLevels, { array: node, index: i }]
1108
+ },
1109
+ formulas
1110
+ );
1111
+ }
1112
+ }
1113
+ }
1114
+ }
1115
+ collectPrimitive(node, context, formulas) {
1116
+ const formula = node.formula;
1117
+ if (!formula) {
1118
+ return;
1119
+ }
1120
+ const rawDependencies = this.extractDependencies(formula.expression);
1121
+ const dependencyNodes = this.resolveDependencies(
1122
+ rawDependencies,
1123
+ node,
1124
+ context
1125
+ );
1126
+ formulas.push({
1127
+ node,
1128
+ expression: formula.expression,
1129
+ parent: context.parent,
1130
+ dependencyNodes,
1131
+ arrayLevels: context.arrayLevels
1093
1132
  });
1094
1133
  }
1095
- _tableModel = null;
1096
- get rowId() {
1097
- return this._rowId;
1134
+ extractDependencies(expression) {
1135
+ try {
1136
+ const parsed = formula.parseExpression(expression);
1137
+ return parsed.dependencies;
1138
+ } catch {
1139
+ return [];
1140
+ }
1098
1141
  }
1099
- get tableModel() {
1100
- return this._tableModel;
1142
+ resolveDependencies(rawDeps, formulaNode, context) {
1143
+ const nodes = [];
1144
+ for (const dep of rawDeps) {
1145
+ const node = this.resolveOneDependency(dep, formulaNode, context);
1146
+ if (node) {
1147
+ this.collectPrimitiveNodes(node, nodes);
1148
+ }
1149
+ }
1150
+ return nodes;
1101
1151
  }
1102
- get tree() {
1103
- return this._tree;
1152
+ collectPrimitiveNodes(node, result) {
1153
+ if (node.isPrimitive()) {
1154
+ result.push(node);
1155
+ } else if (node.isArray()) {
1156
+ for (let i = 0; i < node.length; i++) {
1157
+ const item = node.at(i);
1158
+ if (item) {
1159
+ this.collectPrimitiveNodes(item, result);
1160
+ }
1161
+ }
1162
+ } else if (node.isObject()) {
1163
+ for (const child of node.children) {
1164
+ this.collectPrimitiveNodes(child, result);
1165
+ }
1166
+ }
1104
1167
  }
1105
- get index() {
1106
- if (!this._tableModel) {
1107
- return UNSET_INDEX;
1168
+ resolveOneDependency(dep, _formulaNode, context) {
1169
+ if (dep.startsWith("/")) {
1170
+ return this.resolveFromRoot(dep.slice(1));
1108
1171
  }
1109
- return this._tableModel.getRowIndex(this._rowId);
1172
+ if (dep.startsWith("../")) {
1173
+ return this.resolveRelative(dep, context);
1174
+ }
1175
+ if (context.parent) {
1176
+ return this.resolveFromNode(context.parent, dep);
1177
+ }
1178
+ return this.resolveFromRoot(dep);
1110
1179
  }
1111
- get prev() {
1112
- if (!this._tableModel) {
1180
+ resolveFromRoot(pathStr) {
1181
+ if (!this.root) {
1113
1182
  return null;
1114
1183
  }
1115
- const currentIndex = this.index;
1116
- if (currentIndex <= 0) {
1184
+ return this.resolveFromNode(this.root, pathStr);
1185
+ }
1186
+ resolveFromNode(startNode, pathStr) {
1187
+ const segments = this.parsePathSegments(pathStr);
1188
+ let current = startNode;
1189
+ for (const segment of segments) {
1190
+ if (!current) {
1191
+ return null;
1192
+ }
1193
+ if (segment.type === "property") {
1194
+ if (current.isObject()) {
1195
+ current = current.children.find((c) => c.name === segment.name) ?? null;
1196
+ } else {
1197
+ return null;
1198
+ }
1199
+ } else if (segment.type === "index") {
1200
+ if (current.isArray()) {
1201
+ current = current.at(segment.index) ?? null;
1202
+ } else {
1203
+ return null;
1204
+ }
1205
+ }
1206
+ }
1207
+ return current;
1208
+ }
1209
+ resolveRelative(dep, context) {
1210
+ let upLevels = 0;
1211
+ let remaining = dep;
1212
+ while (remaining.startsWith("../")) {
1213
+ upLevels++;
1214
+ remaining = remaining.slice(3);
1215
+ }
1216
+ let current = context.parent;
1217
+ for (let i = 0; i < upLevels && current; i++) {
1218
+ current = this.findParent(current);
1219
+ if (current?.isArray()) {
1220
+ current = this.findParent(current);
1221
+ }
1222
+ }
1223
+ if (!current) {
1117
1224
  return null;
1118
1225
  }
1119
- return this._tableModel.getRowAt(currentIndex - 1) ?? null;
1226
+ if (remaining) {
1227
+ return this.resolveFromNode(current, remaining);
1228
+ }
1229
+ return current;
1120
1230
  }
1121
- get next() {
1122
- if (!this._tableModel) {
1231
+ findParent(node) {
1232
+ if (!this.root || node === this.root) {
1123
1233
  return null;
1124
1234
  }
1125
- const currentIndex = this.index;
1126
- if (currentIndex === UNSET_INDEX || currentIndex >= this._tableModel.rowCount - 1) {
1235
+ return this.findParentRecursive(this.root, node);
1236
+ }
1237
+ findParentRecursive(current, target) {
1238
+ const children = this.getChildNodes(current);
1239
+ for (const child of children) {
1240
+ if (child === target) {
1241
+ return current;
1242
+ }
1243
+ const found = this.findParentRecursive(child, target);
1244
+ if (found) {
1245
+ return found;
1246
+ }
1247
+ }
1248
+ return null;
1249
+ }
1250
+ getChildNodes(node) {
1251
+ if (node.isObject()) {
1252
+ return [...node.children];
1253
+ }
1254
+ if (node.isArray()) {
1255
+ const items = [];
1256
+ for (let i = 0; i < node.length; i++) {
1257
+ const item = node.at(i);
1258
+ if (item) {
1259
+ items.push(item);
1260
+ }
1261
+ }
1262
+ return items;
1263
+ }
1264
+ return [];
1265
+ }
1266
+ parsePathSegments(pathStr) {
1267
+ const segments = [];
1268
+ let current = pathStr;
1269
+ while (current.length > 0) {
1270
+ const indexMatch = INDEX_REGEX.exec(current);
1271
+ if (indexMatch?.[1]) {
1272
+ segments.push({
1273
+ type: "index",
1274
+ index: Number.parseInt(indexMatch[1], 10)
1275
+ });
1276
+ current = current.slice(indexMatch[0].length);
1277
+ continue;
1278
+ }
1279
+ const propMatch = PROP_REGEX.exec(current);
1280
+ if (propMatch?.[1]) {
1281
+ segments.push({ type: "property", name: propMatch[1] });
1282
+ current = current.slice(propMatch[0].length);
1283
+ continue;
1284
+ }
1285
+ break;
1286
+ }
1287
+ return segments;
1288
+ }
1289
+ };
1290
+
1291
+ // src/model/value-formula/DependencyGraph.ts
1292
+ var DependencyGraph = class {
1293
+ buildDependencyMap(formulas) {
1294
+ const map = /* @__PURE__ */ new Map();
1295
+ for (const field of formulas) {
1296
+ for (const depNode of field.dependencyNodes) {
1297
+ let depSet = map.get(depNode);
1298
+ if (!depSet) {
1299
+ depSet = /* @__PURE__ */ new Set();
1300
+ map.set(depNode, depSet);
1301
+ }
1302
+ depSet.add(field);
1303
+ }
1304
+ }
1305
+ return map;
1306
+ }
1307
+ buildEvaluationOrder(formulas) {
1308
+ const formulaNodes = new Set(formulas.map((f) => f.node));
1309
+ const visited = /* @__PURE__ */ new Set();
1310
+ const order = [];
1311
+ const formulaByNode = /* @__PURE__ */ new Map();
1312
+ for (const field of formulas) {
1313
+ formulaByNode.set(field.node, field);
1314
+ }
1315
+ const visit = (field, stack) => {
1316
+ if (visited.has(field.node)) {
1317
+ return;
1318
+ }
1319
+ if (stack.has(field.node)) {
1320
+ return;
1321
+ }
1322
+ stack.add(field.node);
1323
+ for (const depNode of field.dependencyNodes) {
1324
+ if (formulaNodes.has(depNode)) {
1325
+ const depFormula = formulaByNode.get(depNode);
1326
+ if (depFormula) {
1327
+ visit(depFormula, stack);
1328
+ }
1329
+ }
1330
+ }
1331
+ stack.delete(field.node);
1332
+ visited.add(field.node);
1333
+ order.push(field);
1334
+ };
1335
+ for (const field of formulas) {
1336
+ visit(field, /* @__PURE__ */ new Set());
1337
+ }
1338
+ return order;
1339
+ }
1340
+ getAffectedFormulas(changedNode, dependencyMap, evaluationOrder) {
1341
+ const affected = /* @__PURE__ */ new Set();
1342
+ const queue = [changedNode];
1343
+ while (queue.length > 0) {
1344
+ const current = queue.shift();
1345
+ if (!current) {
1346
+ continue;
1347
+ }
1348
+ const dependents = dependencyMap.get(current);
1349
+ if (dependents) {
1350
+ for (const field of dependents) {
1351
+ if (!affected.has(field)) {
1352
+ affected.add(field);
1353
+ queue.push(field.node);
1354
+ }
1355
+ }
1356
+ }
1357
+ }
1358
+ return evaluationOrder.filter((field) => affected.has(field));
1359
+ }
1360
+ };
1361
+ var FormulaEvaluator = class {
1362
+ constructor(tree, options = {}) {
1363
+ this.tree = tree;
1364
+ this.options = options;
1365
+ }
1366
+ evaluate(field) {
1367
+ try {
1368
+ const context = this.buildContext(field);
1369
+ const result = formula.evaluateWithContext(field.expression, context);
1370
+ this.setNodeValue(field, result);
1371
+ this.checkForWarnings(field, result);
1372
+ } catch (error) {
1373
+ this.handleError(field, error);
1374
+ }
1375
+ }
1376
+ evaluateAll(fields) {
1377
+ for (const field of fields) {
1378
+ this.evaluate(field);
1379
+ }
1380
+ }
1381
+ buildContext(field) {
1382
+ const rootData = this.tree.getPlainValue();
1383
+ const context = { rootData };
1384
+ if (field.parent) {
1385
+ const itemData = this.getPlainObjectValue(field.parent);
1386
+ if (itemData) {
1387
+ context.itemData = itemData;
1388
+ }
1389
+ }
1390
+ if (field.arrayLevels.length > 0) {
1391
+ context.arrayContext = { levels: this.buildArrayLevels(field) };
1392
+ }
1393
+ return context;
1394
+ }
1395
+ buildArrayLevels(field) {
1396
+ const levels = [];
1397
+ for (let i = field.arrayLevels.length - 1; i >= 0; i--) {
1398
+ const level = field.arrayLevels[i];
1399
+ if (!level) {
1400
+ continue;
1401
+ }
1402
+ const { array, index } = level;
1403
+ levels.push({
1404
+ index,
1405
+ length: array.length,
1406
+ prev: this.getPrevItemValue(array, index),
1407
+ next: this.getNextItemValue(array, index)
1408
+ });
1409
+ }
1410
+ return levels;
1411
+ }
1412
+ getPrevItemValue(array, index) {
1413
+ if (index <= 0) {
1127
1414
  return null;
1128
1415
  }
1129
- return this._tableModel.getRowAt(currentIndex + 1) ?? null;
1416
+ const prevItem = array.at(index - 1);
1417
+ return prevItem ? this.getPlainNodeValue(prevItem) : null;
1130
1418
  }
1131
- get(path) {
1132
- return this._tree.get(path);
1419
+ getNextItemValue(array, index) {
1420
+ if (index >= array.length - 1) {
1421
+ return null;
1422
+ }
1423
+ const nextItem = array.at(index + 1);
1424
+ return nextItem ? this.getPlainNodeValue(nextItem) : null;
1133
1425
  }
1134
- getValue(path) {
1135
- return this._tree.getValue(path);
1426
+ getPlainObjectValue(node) {
1427
+ const result = {};
1428
+ for (const child of node.children) {
1429
+ result[child.name] = this.getPlainNodeValue(child);
1430
+ }
1431
+ return result;
1136
1432
  }
1137
- setValue(path, value) {
1138
- this._tree.setValue(path, value);
1433
+ getPlainNodeValue(node) {
1434
+ if (node.isPrimitive()) {
1435
+ return node.value;
1436
+ }
1437
+ if (node.isObject()) {
1438
+ return this.getPlainObjectValue(node);
1439
+ }
1440
+ if (node.isArray()) {
1441
+ const result = [];
1442
+ for (let i = 0; i < node.length; i++) {
1443
+ const item = node.at(i);
1444
+ if (item) {
1445
+ result.push(this.getPlainNodeValue(item));
1446
+ }
1447
+ }
1448
+ return result;
1449
+ }
1450
+ return null;
1139
1451
  }
1140
- getPlainValue() {
1141
- return this._tree.getPlainValue();
1452
+ setNodeValue(field, result) {
1453
+ const { node } = field;
1454
+ const defaultValue = node.defaultValue;
1455
+ if (result === null || result === void 0) {
1456
+ node.setValue(defaultValue, { internal: true });
1457
+ return;
1458
+ }
1459
+ if (typeof result === "number" && (Number.isNaN(result) || !Number.isFinite(result))) {
1460
+ node.setValue(defaultValue, { internal: true });
1461
+ return;
1462
+ }
1463
+ node.setValue(result, { internal: true });
1464
+ }
1465
+ checkForWarnings(field, result) {
1466
+ const { node } = field;
1467
+ if (typeof result === "number") {
1468
+ if (Number.isNaN(result)) {
1469
+ node.setFormulaWarning({
1470
+ type: "nan",
1471
+ message: "Formula result is NaN",
1472
+ expression: field.expression,
1473
+ computedValue: result
1474
+ });
1475
+ return;
1476
+ }
1477
+ if (!Number.isFinite(result)) {
1478
+ node.setFormulaWarning({
1479
+ type: "infinity",
1480
+ message: "Formula result is Infinity",
1481
+ expression: field.expression,
1482
+ computedValue: result
1483
+ });
1484
+ return;
1485
+ }
1486
+ }
1487
+ node.setFormulaWarning(null);
1142
1488
  }
1143
- get isDirty() {
1144
- return this._tree.isDirty;
1489
+ handleError(field, error) {
1490
+ const { node } = field;
1491
+ node.setValue(node.defaultValue, { internal: true });
1492
+ node.setFormulaWarning({
1493
+ type: "runtime-error",
1494
+ message: error.message,
1495
+ expression: field.expression,
1496
+ computedValue: void 0
1497
+ });
1498
+ if (this.options.onError) {
1499
+ this.options.onError(node, error);
1500
+ }
1145
1501
  }
1146
- get isValid() {
1147
- return this._tree.isValid;
1502
+ };
1503
+
1504
+ // src/model/value-formula/FormulaEngine.ts
1505
+ var FormulaEngine = class {
1506
+ constructor(tree, options = {}) {
1507
+ this.tree = tree;
1508
+ this.collector = new FormulaCollector();
1509
+ this.graph = new DependencyGraph();
1510
+ this.evaluator = new FormulaEvaluator(tree, options);
1511
+ this.initialize();
1512
+ }
1513
+ collector;
1514
+ graph;
1515
+ evaluator;
1516
+ formulas = [];
1517
+ dependencyMap = /* @__PURE__ */ new Map();
1518
+ evaluationOrder = [];
1519
+ disposers = [];
1520
+ reinitialize() {
1521
+ this.disposeReactions();
1522
+ this.initialize();
1148
1523
  }
1149
- get errors() {
1150
- return this._tree.errors;
1524
+ dispose() {
1525
+ this.disposeReactions();
1526
+ this.formulas = [];
1527
+ this.dependencyMap = /* @__PURE__ */ new Map();
1528
+ this.evaluationOrder = [];
1151
1529
  }
1152
- getPatches() {
1153
- return this._tree.getPatches();
1530
+ getFormulas() {
1531
+ return this.formulas;
1154
1532
  }
1155
- commit() {
1156
- this._tree.commit();
1533
+ getEvaluationOrder() {
1534
+ return this.evaluationOrder;
1157
1535
  }
1158
- revert() {
1159
- this._tree.revert();
1536
+ initialize() {
1537
+ this.formulas = this.collector.collect(this.tree.root);
1538
+ this.dependencyMap = this.graph.buildDependencyMap(this.formulas);
1539
+ this.evaluationOrder = this.graph.buildEvaluationOrder(this.formulas);
1540
+ this.evaluateAll();
1541
+ this.setupReactions();
1160
1542
  }
1161
- setTableModel(tableModel) {
1162
- this._tableModel = tableModel;
1543
+ evaluateAll() {
1544
+ chunk46ME2TAU_cjs.runInAction(() => {
1545
+ for (const field of this.evaluationOrder) {
1546
+ this.evaluator.evaluate(field);
1547
+ }
1548
+ });
1549
+ }
1550
+ setupReactions() {
1551
+ const watchedNodes = /* @__PURE__ */ new Set();
1552
+ for (const [depNode] of this.dependencyMap) {
1553
+ if (watchedNodes.has(depNode)) {
1554
+ continue;
1555
+ }
1556
+ watchedNodes.add(depNode);
1557
+ const disposer = chunk46ME2TAU_cjs.reaction(
1558
+ () => depNode.value,
1559
+ () => this.handleDependencyChange(depNode)
1560
+ );
1561
+ this.disposers.push(disposer);
1562
+ }
1563
+ this.setupArrayReactions(this.tree.root);
1564
+ }
1565
+ setupArrayReactions(node) {
1566
+ if (node.isArray()) {
1567
+ const disposer = chunk46ME2TAU_cjs.reaction(
1568
+ () => node.length,
1569
+ () => this.handleStructureChange()
1570
+ );
1571
+ this.disposers.push(disposer);
1572
+ for (const item of node.value) {
1573
+ this.setupArrayReactions(item);
1574
+ }
1575
+ } else if (node.isObject()) {
1576
+ for (const child of node.children) {
1577
+ this.setupArrayReactions(child);
1578
+ }
1579
+ }
1580
+ }
1581
+ handleDependencyChange(changedNode) {
1582
+ const affected = this.graph.getAffectedFormulas(
1583
+ changedNode,
1584
+ this.dependencyMap,
1585
+ this.evaluationOrder
1586
+ );
1587
+ chunk46ME2TAU_cjs.runInAction(() => {
1588
+ for (const field of affected) {
1589
+ this.evaluator.evaluate(field);
1590
+ }
1591
+ });
1592
+ }
1593
+ handleStructureChange() {
1594
+ this.reinitialize();
1595
+ }
1596
+ disposeReactions() {
1597
+ for (const disposer of this.disposers) {
1598
+ disposer();
1599
+ }
1600
+ this.disposers = [];
1163
1601
  }
1164
1602
  };
1165
1603
 
@@ -2107,6 +2545,20 @@ var ValuePathImpl = class _ValuePathImpl extends chunk46ME2TAU_cjs.AbstractBaseP
2107
2545
  }
2108
2546
  return parts.join("");
2109
2547
  }
2548
+ asJsonPointer() {
2549
+ if (this.segs.length === 0) {
2550
+ return "";
2551
+ }
2552
+ const parts = [];
2553
+ for (const seg of this.segs) {
2554
+ if (seg.isProperty()) {
2555
+ parts.push(seg.propertyName());
2556
+ } else if (seg.isIndex()) {
2557
+ parts.push(String(seg.indexValue()));
2558
+ }
2559
+ }
2560
+ return "/" + parts.join("/");
2561
+ }
2110
2562
  parent() {
2111
2563
  if (this.segs.length <= 1) {
2112
2564
  return EMPTY_VALUE_PATH;
@@ -2121,6 +2573,9 @@ var ValuePathImpl = class _ValuePathImpl extends chunk46ME2TAU_cjs.AbstractBaseP
2121
2573
  }
2122
2574
  };
2123
2575
  var EMPTY_VALUE_PATH = new ValuePathImpl([]);
2576
+ function createValuePath(segments) {
2577
+ return segments.length === 0 ? EMPTY_VALUE_PATH : new ValuePathImpl(segments);
2578
+ }
2124
2579
 
2125
2580
  // src/core/value-path/ValuePathParser.ts
2126
2581
  function parseIndexSegment(path, startIndex) {
@@ -2176,17 +2631,194 @@ function parseValuePath(path) {
2176
2631
  return segments;
2177
2632
  }
2178
2633
 
2634
+ // src/model/value-tree/ChangeTracker.ts
2635
+ var ChangeTracker = class {
2636
+ _changes = [];
2637
+ get changes() {
2638
+ return this._changes;
2639
+ }
2640
+ get hasChanges() {
2641
+ return this._changes.length > 0;
2642
+ }
2643
+ track(change) {
2644
+ this._changes.push(change);
2645
+ }
2646
+ clear() {
2647
+ this._changes = [];
2648
+ }
2649
+ toPatches() {
2650
+ const patches = [];
2651
+ for (const change of this._changes) {
2652
+ const converted = this.changeToPatch(change);
2653
+ patches.push(...converted);
2654
+ }
2655
+ return patches;
2656
+ }
2657
+ changeToPatch(change) {
2658
+ const path = change.path.asJsonPointer();
2659
+ switch (change.type) {
2660
+ case "setValue":
2661
+ return [{ op: "replace", path, value: change.value }];
2662
+ case "addProperty":
2663
+ return [{ op: "add", path, value: change.value }];
2664
+ case "removeProperty":
2665
+ return [{ op: "remove", path }];
2666
+ case "arrayPush":
2667
+ return [{ op: "add", path: `${path}/-`, value: change.value }];
2668
+ case "arrayInsert":
2669
+ return [
2670
+ { op: "add", path: `${path}/${change.index}`, value: change.value }
2671
+ ];
2672
+ case "arrayRemove":
2673
+ return [{ op: "remove", path: `${path}/${change.index}` }];
2674
+ case "arrayMove":
2675
+ return [
2676
+ {
2677
+ op: "move",
2678
+ from: `${path}/${change.fromIndex}`,
2679
+ path: `${path}/${change.toIndex}`
2680
+ }
2681
+ ];
2682
+ case "arrayReplace":
2683
+ return [
2684
+ {
2685
+ op: "replace",
2686
+ path: `${path}/${change.index}`,
2687
+ value: change.value
2688
+ }
2689
+ ];
2690
+ case "arrayClear":
2691
+ return [{ op: "replace", path, value: [] }];
2692
+ }
2693
+ }
2694
+ };
2695
+
2696
+ // src/model/value-tree/TreeIndex.ts
2697
+ var TreeIndex = class {
2698
+ constructor(root) {
2699
+ this.root = root;
2700
+ this.rebuild();
2701
+ }
2702
+ nodesById = /* @__PURE__ */ new Map();
2703
+ pathCache = /* @__PURE__ */ new Map();
2704
+ nodeById(id) {
2705
+ const node = this.nodesById.get(id);
2706
+ if (node) {
2707
+ return node;
2708
+ }
2709
+ this.rebuild();
2710
+ return this.nodesById.get(id);
2711
+ }
2712
+ pathOf(node) {
2713
+ if (this.isInsideArray(node)) {
2714
+ return this.computePath(node);
2715
+ }
2716
+ const cached = this.pathCache.get(node.id);
2717
+ if (cached) {
2718
+ return cached;
2719
+ }
2720
+ const path = this.computePath(node);
2721
+ this.pathCache.set(node.id, path);
2722
+ return path;
2723
+ }
2724
+ rebuild() {
2725
+ this.nodesById.clear();
2726
+ this.pathCache.clear();
2727
+ this.indexNode(this.root);
2728
+ }
2729
+ registerNode(node) {
2730
+ this.indexNode(node);
2731
+ }
2732
+ invalidatePathsUnder(node) {
2733
+ this.pathCache.delete(node.id);
2734
+ this.invalidateChildPaths(node);
2735
+ }
2736
+ isInsideArray(node) {
2737
+ let current = node.parent;
2738
+ while (current) {
2739
+ if (current.isArray()) {
2740
+ return true;
2741
+ }
2742
+ current = current.parent;
2743
+ }
2744
+ return false;
2745
+ }
2746
+ indexNode(node) {
2747
+ this.nodesById.set(node.id, node);
2748
+ if (node.isObject()) {
2749
+ for (const child of node.children) {
2750
+ this.indexNode(child);
2751
+ }
2752
+ } else if (node.isArray()) {
2753
+ for (const item of node.value) {
2754
+ this.indexNode(item);
2755
+ }
2756
+ }
2757
+ }
2758
+ computePath(node) {
2759
+ const segments = [];
2760
+ let current = node;
2761
+ while (current?.parent) {
2762
+ const parent = current.parent;
2763
+ if (parent.isObject()) {
2764
+ segments.unshift(new PropertySegment(current.name));
2765
+ } else if (parent.isArray()) {
2766
+ const index = parent.value.indexOf(current);
2767
+ if (index >= 0) {
2768
+ segments.unshift(new IndexSegment(index));
2769
+ }
2770
+ }
2771
+ current = parent;
2772
+ }
2773
+ if (segments.length === 0) {
2774
+ return EMPTY_VALUE_PATH;
2775
+ }
2776
+ return createValuePath(segments);
2777
+ }
2778
+ invalidateChildPaths(node) {
2779
+ if (node.isObject()) {
2780
+ for (const child of node.children) {
2781
+ this.pathCache.delete(child.id);
2782
+ this.invalidateChildPaths(child);
2783
+ }
2784
+ } else if (node.isArray()) {
2785
+ for (const item of node.value) {
2786
+ this.pathCache.delete(item.id);
2787
+ this.invalidateChildPaths(item);
2788
+ }
2789
+ }
2790
+ }
2791
+ };
2792
+
2179
2793
  // src/model/value-tree/ValueTree.ts
2180
2794
  var ValueTree = class {
2181
2795
  constructor(_root) {
2182
2796
  this._root = _root;
2797
+ this.index = new TreeIndex(_root);
2798
+ this.changeTracker = new ChangeTracker();
2183
2799
  chunk46ME2TAU_cjs.makeAutoObservable(this, {
2184
- _root: false
2800
+ _root: false,
2801
+ index: false,
2802
+ changeTracker: false,
2803
+ _formulaEngine: false
2185
2804
  });
2186
2805
  }
2806
+ index;
2807
+ changeTracker;
2808
+ _formulaEngine = null;
2187
2809
  get root() {
2188
2810
  return this._root;
2189
2811
  }
2812
+ nodeById(id) {
2813
+ return this.index.nodeById(id);
2814
+ }
2815
+ pathOf(nodeOrId) {
2816
+ const node = typeof nodeOrId === "string" ? this.index.nodeById(nodeOrId) : nodeOrId;
2817
+ if (!node) {
2818
+ return EMPTY_VALUE_PATH;
2819
+ }
2820
+ return this.index.pathOf(node);
2821
+ }
2190
2822
  get(path) {
2191
2823
  const segments = parseValuePath(path);
2192
2824
  if (segments.length === 0) {
@@ -2219,7 +2851,14 @@ var ValueTree = class {
2219
2851
  if (!node.isPrimitive()) {
2220
2852
  throw new Error(`Cannot set value on non-primitive node: ${path}`);
2221
2853
  }
2854
+ const oldValue = node.value;
2222
2855
  node.setValue(value);
2856
+ this.changeTracker.track({
2857
+ type: "setValue",
2858
+ path: this.index.pathOf(node),
2859
+ value,
2860
+ oldValue
2861
+ });
2223
2862
  }
2224
2863
  getPlainValue() {
2225
2864
  return this._root.getPlainValue();
@@ -2238,22 +2877,147 @@ var ValueTree = class {
2238
2877
  return this._root.errors;
2239
2878
  }
2240
2879
  getPatches() {
2241
- return [];
2880
+ return this.changeTracker.toPatches();
2881
+ }
2882
+ trackChange(change) {
2883
+ this.changeTracker.track(change);
2242
2884
  }
2243
2885
  commit() {
2244
2886
  const root = this._root;
2245
2887
  if ("commit" in root && typeof root.commit === "function") {
2246
2888
  root.commit();
2247
2889
  }
2890
+ this.changeTracker.clear();
2248
2891
  }
2249
2892
  revert() {
2250
2893
  const root = this._root;
2251
2894
  if ("revert" in root && typeof root.revert === "function") {
2252
2895
  root.revert();
2253
2896
  }
2897
+ this.changeTracker.clear();
2898
+ }
2899
+ rebuildIndex() {
2900
+ this.index.rebuild();
2901
+ }
2902
+ registerNode(node) {
2903
+ this.index.registerNode(node);
2904
+ }
2905
+ invalidatePathsUnder(node) {
2906
+ this.index.invalidatePathsUnder(node);
2907
+ }
2908
+ setFormulaEngine(engine) {
2909
+ this._formulaEngine = engine;
2910
+ }
2911
+ get formulaEngine() {
2912
+ return this._formulaEngine;
2913
+ }
2914
+ dispose() {
2915
+ this._formulaEngine?.dispose();
2916
+ this._formulaEngine = null;
2254
2917
  }
2255
2918
  };
2256
2919
 
2920
+ // src/model/table/row/RowModelImpl.ts
2921
+ var UNSET_INDEX = -1;
2922
+ var RowModelImpl = class {
2923
+ constructor(_rowId, _tree) {
2924
+ this._rowId = _rowId;
2925
+ this._tree = _tree;
2926
+ chunk46ME2TAU_cjs.makeAutoObservable(this, {
2927
+ _rowId: false,
2928
+ _tree: false,
2929
+ _tableModel: "observable.ref"
2930
+ });
2931
+ }
2932
+ _tableModel = null;
2933
+ get rowId() {
2934
+ return this._rowId;
2935
+ }
2936
+ get tableModel() {
2937
+ return this._tableModel;
2938
+ }
2939
+ get tree() {
2940
+ return this._tree;
2941
+ }
2942
+ get index() {
2943
+ if (!this._tableModel) {
2944
+ return UNSET_INDEX;
2945
+ }
2946
+ return this._tableModel.getRowIndex(this._rowId);
2947
+ }
2948
+ get prev() {
2949
+ if (!this._tableModel) {
2950
+ return null;
2951
+ }
2952
+ const currentIndex = this.index;
2953
+ if (currentIndex <= 0) {
2954
+ return null;
2955
+ }
2956
+ return this._tableModel.getRowAt(currentIndex - 1) ?? null;
2957
+ }
2958
+ get next() {
2959
+ if (!this._tableModel) {
2960
+ return null;
2961
+ }
2962
+ const currentIndex = this.index;
2963
+ if (currentIndex === UNSET_INDEX || currentIndex >= this._tableModel.rowCount - 1) {
2964
+ return null;
2965
+ }
2966
+ return this._tableModel.getRowAt(currentIndex + 1) ?? null;
2967
+ }
2968
+ get(path) {
2969
+ return this._tree.get(path);
2970
+ }
2971
+ getValue(path) {
2972
+ return this._tree.getValue(path);
2973
+ }
2974
+ setValue(path, value) {
2975
+ this._tree.setValue(path, value);
2976
+ }
2977
+ getPlainValue() {
2978
+ return this._tree.getPlainValue();
2979
+ }
2980
+ nodeById(id) {
2981
+ return this._tree.nodeById(id);
2982
+ }
2983
+ get isDirty() {
2984
+ return this._tree.isDirty;
2985
+ }
2986
+ get isValid() {
2987
+ return this._tree.isValid;
2988
+ }
2989
+ get errors() {
2990
+ return this._tree.errors;
2991
+ }
2992
+ getPatches() {
2993
+ return this._tree.getPatches();
2994
+ }
2995
+ commit() {
2996
+ this._tree.commit();
2997
+ }
2998
+ revert() {
2999
+ this._tree.revert();
3000
+ }
3001
+ dispose() {
3002
+ this._tree.dispose();
3003
+ }
3004
+ setTableModel(tableModel) {
3005
+ this._tableModel = tableModel;
3006
+ }
3007
+ };
3008
+ function createRowModel(options) {
3009
+ const factory = createNodeFactory({
3010
+ fkResolver: options.fkResolver,
3011
+ refSchemas: options.refSchemas
3012
+ });
3013
+ const rowData = options.data ?? generateDefaultValue(options.schema, { refSchemas: options.refSchemas });
3014
+ const rootNode = factory.createTree(options.schema, rowData);
3015
+ const valueTree = new ValueTree(rootNode);
3016
+ const formulaEngine = new FormulaEngine(valueTree);
3017
+ valueTree.setFormulaEngine(formulaEngine);
3018
+ return new RowModelImpl(options.rowId, valueTree);
3019
+ }
3020
+
2257
3021
  // src/model/table/TableModelImpl.ts
2258
3022
  var TableModelImpl = class {
2259
3023
  _tableId;
@@ -2273,7 +3037,7 @@ var TableModelImpl = class {
2273
3037
  this._rows = chunk46ME2TAU_cjs.observable.array();
2274
3038
  if (options.rows) {
2275
3039
  for (const row of options.rows) {
2276
- this._rows.push(this.createRowModel(row.rowId, row.data));
3040
+ this._rows.push(this.createRow(row.rowId, row.data));
2277
3041
  }
2278
3042
  }
2279
3043
  chunk46ME2TAU_cjs.makeAutoObservable(this, {
@@ -2315,19 +3079,24 @@ var TableModelImpl = class {
2315
3079
  if (this.getRow(rowId)) {
2316
3080
  throw new Error(`Row with id already exists: ${rowId}`);
2317
3081
  }
2318
- const rowModel = this.createRowModel(rowId, data);
3082
+ const rowModel = this.createRow(rowId, data);
2319
3083
  this._rows.push(rowModel);
2320
3084
  return rowModel;
2321
3085
  }
2322
3086
  removeRow(rowId) {
2323
- const index = this._rows.findIndex((row) => row.rowId === rowId);
2324
- if (index !== -1) {
2325
- const row = this._rows[index];
2326
- if (row instanceof RowModelImpl) {
2327
- row.setTableModel(null);
2328
- }
2329
- this._rows.splice(index, 1);
3087
+ const index = this._rows.findIndex((row2) => row2.rowId === rowId);
3088
+ if (index === -1) {
3089
+ return;
3090
+ }
3091
+ const row = this._rows[index];
3092
+ if (!row) {
3093
+ return;
2330
3094
  }
3095
+ row.dispose();
3096
+ if (row instanceof RowModelImpl) {
3097
+ row.setTableModel(null);
3098
+ }
3099
+ this._rows.splice(index, 1);
2331
3100
  }
2332
3101
  getRow(rowId) {
2333
3102
  return this._rows.find((row) => row.rowId === rowId);
@@ -2366,15 +3135,20 @@ var TableModelImpl = class {
2366
3135
  row.revert();
2367
3136
  }
2368
3137
  }
2369
- createRowModel(rowId, data) {
2370
- const factory = createNodeFactory({
3138
+ dispose() {
3139
+ for (const row of this._rows) {
3140
+ row.dispose();
3141
+ }
3142
+ this._rows.splice(0);
3143
+ }
3144
+ createRow(rowId, data) {
3145
+ const rowModel = createRowModel({
3146
+ rowId,
3147
+ schema: this._jsonSchema,
3148
+ data,
2371
3149
  fkResolver: this._fkResolver,
2372
3150
  refSchemas: this._refSchemas
2373
3151
  });
2374
- const rowData = data ?? generateDefaultValue(this._jsonSchema, { refSchemas: this._refSchemas });
2375
- const rootNode = factory.createTree(this._jsonSchema, rowData);
2376
- const valueTree = new ValueTree(rootNode);
2377
- const rowModel = new RowModelImpl(rowId, valueTree);
2378
3152
  rowModel.setTableModel(this);
2379
3153
  return rowModel;
2380
3154
  }
@@ -2808,6 +3582,7 @@ exports.createDataModel = createDataModel;
2808
3582
  exports.createDefaultRegistry = createDefaultRegistry;
2809
3583
  exports.createForeignKeyResolver = createForeignKeyResolver;
2810
3584
  exports.createNodeFactory = createNodeFactory;
3585
+ exports.createRowModel = createRowModel;
2811
3586
  exports.createSchemaModel = createSchemaModel;
2812
3587
  exports.createTableModel = createTableModel;
2813
3588
  exports.createTypeTransformChain = createTypeTransformChain;
@@ -2816,5 +3591,5 @@ exports.generateDefaultValue = generateDefaultValue;
2816
3591
  exports.generateNodeId = generateNodeId;
2817
3592
  exports.isForeignKeyValueNode = isForeignKeyValueNode;
2818
3593
  exports.resetNodeIdCounter = resetNodeIdCounter;
2819
- //# sourceMappingURL=chunk-O4QRUV7I.cjs.map
2820
- //# sourceMappingURL=chunk-O4QRUV7I.cjs.map
3594
+ //# sourceMappingURL=chunk-FZ2MS4Q5.cjs.map
3595
+ //# sourceMappingURL=chunk-FZ2MS4Q5.cjs.map