@tidal-ts/terminal 0.4.1 → 0.5.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.
@@ -31,12 +31,28 @@ function hidesPointer(context, toggledId, pointer) {
31
31
  : new Set([toggledId]);
32
32
  return cascade.has(pointer);
33
33
  }
34
- import { axisRangeIsEmpty, logAllowed, readsFrom, sourcesOf, } from '@tidal-ts/chart';
34
+ import { axisRangeIsEmpty, logAllowed, isStudySeries, readsFrom, sourcesOf, } from '@tidal-ts/chart';
35
35
  import { configAxisId, seriesAxisId, sharedAxisId, } from '@tidal-ts/chart';
36
36
  import { axisForAdd, canAddUnit } from './axisPolicy.js';
37
37
  export const PRESET_SLOTS = 5;
38
38
  /** Max viewport rows (the control-panel spec: 1–3, capped at 3 for now). */
39
39
  export const MAX_ROWS = 3;
40
+ /**
41
+ * Whether a new study of `op` on `target` opens on a row of its own
42
+ * (TDL-OSCROW): it reads in its own units (pond's `unit` rule — only `inherit`
43
+ * shares its source's scale), its target is a metric rather than a link in a
44
+ * study chain or a pair leg, and there is room for another row.
45
+ */
46
+ const ownsRow = (context, target, op) => !opSharesSourceAxis(op) &&
47
+ // Only off a METRIC — a raw one, a derived catalog metric (realized vol) or
48
+ // an unsplit pair's spread. A study of a STUDY, or of a pair leg, stays with
49
+ // its chain: a chain is one pipeline the panel reads top-down within a row,
50
+ // and a compare side is one too; split across rows, the panel showed a
51
+ // middle link as the tail and a leg's oscillator fell out of its pair (tidal
52
+ // #212 review). Its own axis still keeps it off its target's scale.
53
+ (!isStudySeries(target) || isCatalogColumn(context)(target.column)) &&
54
+ !target.group &&
55
+ context.rows.length < MAX_ROWS;
40
56
  /** Committed height (px) a freshly added row gets — a **fixed** strip, not a flex
41
57
  * remainder row (`height: 0`). Exactly one remainder row is kept (the seed's
42
58
  * top/vol panel, which absorbs slack); a second would make dragging any seam
@@ -64,6 +80,21 @@ function withRemainder(rows) {
64
80
  return rows;
65
81
  return rows.map((r, i) => (i === 0 ? { ...r, height: 0 } : r));
66
82
  }
83
+ /**
84
+ * Drop every row an edit EMPTIED (the control-panel spec: no empty rows), and
85
+ * keep exactly one remainder row. `after` is `before` with configs taken out
86
+ * or moved, row for row. A row that was already empty (a fresh `row.add`)
87
+ * stays: the user made it to fill. At least one row always survives.
88
+ *
89
+ * Shared by every edit that takes configs off rows — remove, group remove and a
90
+ * cross-row move — because an oscillator on its own row (TDL-OSCROW) means
91
+ * one edit can empty a row other than the one it started on.
92
+ */
93
+ function pruneEmptied(before, after) {
94
+ const had = new Set(before.filter((r) => r.configs.length > 0).map((r) => r.id));
95
+ const kept = after.filter((r) => r.configs.length > 0 || !had.has(r.id));
96
+ return withRemainder(kept.length > 0 ? kept : after.slice(0, 1));
97
+ }
67
98
  /**
68
99
  * Config identity. A seated config's `id` is minted here and never derived from
69
100
  * what it computes: `s-3` keys selection, expansion, the chart theme, axis
@@ -137,8 +168,10 @@ function derivedClosure(configs, rootId) {
137
168
  }
138
169
  /**
139
170
  * `rootId` plus the **studies layered on it** within this row — the travel set for
140
- * a cross-row move. `addStudy` co-locates a study with its target, so moving the
141
- * target takes its studies along rather than leaving `Price · SMA(20)` behind.
171
+ * a cross-row move. A study that shares its target's scale sits on its target's
172
+ * row, so moving the target takes its studies along rather than leaving
173
+ * `Price · SMA(20)` behind. A study on a row of its own (an RSI, TDL-OSCROW)
174
+ * stays where it is: its row is its place, not its target's.
142
175
  *
143
176
  * Only **studies** travel, never a derived **catalog** metric (`isCatalogId`): a
144
177
  * study exists solely as a layer on the series it was added to, whereas the
@@ -973,6 +1006,20 @@ function groupDoomed(rows, groupId) {
973
1006
  doomed.add(id);
974
1007
  return doomed;
975
1008
  }
1009
+ /** What `row.remove` takes: the row's configs, each with the same cascade a
1010
+ * single `series.remove` runs (`derivedClosure`), wherever the cascade lands.
1011
+ * So removing a row and removing its series one by one agree: a study of a
1012
+ * STUDY on the row goes with it on any row, and a study of a raw metric (an
1013
+ * RSI of the price on its own row) outlives it, because it reads the data,
1014
+ * not the config. */
1015
+ function rowDoomed(rows, rowId) {
1016
+ const all = flatConfigs(rows);
1017
+ const doomed = new Set();
1018
+ for (const c of rows.find((r) => r.id === rowId)?.configs ?? [])
1019
+ for (const id of derivedClosure(all, c.id))
1020
+ doomed.add(id);
1021
+ return doomed;
1022
+ }
976
1023
  export const groupMembers = (configs, groupId) => configs
977
1024
  .filter((c) => c.group?.id === groupId)
978
1025
  .sort((a, b) => (a.group.side === b.group.side ? 0 : a.group.side === 'A' ? -1 : 1));
@@ -1601,15 +1648,42 @@ export const terminalMachine = setup({
1601
1648
  // the id minted here and the one `expanded`/`cfgSeq` compute below all
1602
1649
  // agree without threading a value between them.
1603
1650
  const cfg = studyConfig(target, event.op, event.period, mintId(context.cfgSeq), unitOfColumn(context));
1604
- // Overlay on the target's row (own-row oscillators are a later step),
1605
- // inserted directly IN FRONT of its target — the list is front→back, so
1606
- // that's the target's own index. A study must read over its source (see
1607
- // `studyConfig`); appending would bury it at the very back.
1651
+ // A study in its OWN units (an RSI, a MACD, an ATR) opens on a row of
1652
+ // its own, directly under its target's row (TDL-OSCROW). On the target's
1653
+ // row it drew over the price on a second scale, which is what the
1654
+ // drawing plan measured for 63 of the addable studies. With the row cap
1655
+ // reached it overlays as before, on its own axis, and can be moved later.
1656
+ if (ownsRow(context, target, event.op)) {
1657
+ // Under the target's row — and under any own-row study of the same
1658
+ // target already there, so oscillators stack in the order added.
1659
+ const built = studiesOn(flatConfigs(context.rows), target.id, isCatalogColumn(context));
1660
+ const at = context.rows.reduce((last, r, i) => (r.configs.some((c) => built.has(c.id)) ? i : last), -1);
1661
+ const row = {
1662
+ id: mintRowId(context.rowSeq),
1663
+ height: NEW_ROW_HEIGHT,
1664
+ configs: [cfg],
1665
+ };
1666
+ return withRemainder([
1667
+ ...context.rows.slice(0, at + 1),
1668
+ row,
1669
+ ...context.rows.slice(at + 1),
1670
+ ]);
1671
+ }
1672
+ // Otherwise overlay on the target's row, inserted directly IN FRONT of
1673
+ // its target — the list is front→back, so that's the target's own
1674
+ // index. A study must read over its source (see `studyConfig`);
1675
+ // appending would bury it at the very back.
1608
1676
  return context.rows.map((r) => {
1609
1677
  const at = r.configs.findIndex((c) => c.id === target.id);
1610
1678
  return at < 0 ? r : { ...r, configs: reindex([...r.configs, cfg], r.configs.length, at) };
1611
1679
  });
1612
1680
  },
1681
+ rowSeq: ({ context, event }) => {
1682
+ if (event.type !== 'series.addStudy')
1683
+ return context.rowSeq;
1684
+ const target = flatConfigs(context.rows).find((c) => c.id === event.targetId);
1685
+ return target && ownsRow(context, target, event.op) ? context.rowSeq + 1 : context.rowSeq;
1686
+ },
1613
1687
  // Expand the new study so its controls (period, colour) open for tuning.
1614
1688
  expanded: ({ context, event }) => {
1615
1689
  if (event.type !== 'series.addStudy')
@@ -1782,9 +1856,7 @@ export const terminalMachine = setup({
1782
1856
  ...r,
1783
1857
  configs: r.configs.filter((c) => !doomed.has(c.id)),
1784
1858
  }));
1785
- return mapped.some((r) => r.configs.length === 0) && mapped.length > 1
1786
- ? mapped.filter((r) => r.configs.length > 0)
1787
- : mapped;
1859
+ return pruneEmptied(context.rows, mapped);
1788
1860
  },
1789
1861
  // The same invariant `removeSeries` states: neither pointer may point at a
1790
1862
  // removed series. Sibling assigners read the PRE-update context, so both
@@ -1869,13 +1941,9 @@ export const terminalMachine = setup({
1869
1941
  .filter((c) => !doomed.has(c.id))
1870
1942
  .map((c) => (c.group && orphaned.has(c.group.id) ? { ...c, group: undefined } : c)),
1871
1943
  }));
1872
- // Removing a row's last metric removes the row (the control-panel spec: no empty
1873
- // rows) — keep at least one.
1874
- const host = context.rows.find((r) => r.configs.some((c) => c.id === event.id));
1875
- const hostNow = host && mapped.find((r) => r.id === host.id);
1876
- return hostNow && hostNow.configs.length === 0 && mapped.length > 1
1877
- ? mapped.filter((r) => r.id !== host.id)
1878
- : mapped;
1944
+ // Not only the host row: an oscillator on its own row is in the
1945
+ // cascade, and its row must not stay behind as an empty pane.
1946
+ return pruneEmptied(context.rows, mapped);
1879
1947
  },
1880
1948
  // Neither pointer may point at a removed series (or a cascaded dependent).
1881
1949
  selected: ({ context, event }) => event.type === 'series.remove' &&
@@ -1941,10 +2009,8 @@ export const terminalMachine = setup({
1941
2009
  return { ...r, configs: [...placed, ...r.configs] };
1942
2010
  return r;
1943
2011
  });
1944
- // An emptied source row goes away (the control-panel spec: no empty rows) —
1945
- // keep at least one.
1946
- const emptied = mapped.find((r) => r.id === from.id && r.configs.length === 0);
1947
- return emptied && mapped.length > 1 ? mapped.filter((r) => r.id !== from.id) : mapped;
2012
+ // An emptied source row goes away; the remainder row is kept.
2013
+ return pruneEmptied(context.rows, mapped);
1948
2014
  },
1949
2015
  }),
1950
2016
  patchSeries: assign({
@@ -1981,10 +2047,9 @@ export const terminalMachine = setup({
1981
2047
  // trade from the pair eye, which keeps each leg's own state behind a
1982
2048
  // separate `group.hidden` flag. A pair's legs are peers; a study is
1983
2049
  // subordinate, so following its parent is the better default.
1984
- const row = context.rows.find((r) => r.configs.some((c) => c.id === event.id));
1985
- const cascade = row
1986
- ? studiesOn(row.configs, event.id, isCatalogColumn(context))
1987
- : new Set();
2050
+ // Across EVERY row: an oscillator on its own row is still built on
2051
+ // this series (TDL-OSCROW).
2052
+ const cascade = studiesOn(flatConfigs(context.rows), event.id, isCatalogColumn(context));
1988
2053
  return context.rows.map((r) => ({
1989
2054
  ...r,
1990
2055
  configs: r.configs.map((c) => (cascade.has(c.id) ? { ...c, visible: next } : c)),
@@ -2289,24 +2354,34 @@ export const terminalMachine = setup({
2289
2354
  },
2290
2355
  }),
2291
2356
  removeRow: assign({
2292
- rows: ({ context, event }) => event.type === 'row.remove' && context.rows.length > 1
2293
- ? // Removing the remainder row would leave the stack with none, so the
2294
- // first survivor takes over as the one that absorbs slack.
2295
- withRemainder(context.rows.filter((r) => r.id !== event.id))
2296
- : context.rows,
2357
+ // Removing a row is removing each of its series (`rowDoomed`), so a
2358
+ // study of one of its STUDIES goes too, on whatever row it sits.
2359
+ rows: ({ context, event }) => {
2360
+ if (event.type !== 'row.remove' || context.rows.length <= 1)
2361
+ return context.rows;
2362
+ const doomed = rowDoomed(context.rows, event.id);
2363
+ const after = context.rows
2364
+ .filter((r) => r.id !== event.id)
2365
+ .map((r) => ({ ...r, configs: r.configs.filter((c) => !doomed.has(c.id)) }));
2366
+ // Removing the remainder row would leave the stack with none, so the
2367
+ // first survivor takes over as the one that absorbs slack.
2368
+ return pruneEmptied(context.rows, after);
2369
+ },
2297
2370
  // Removing a row drops its series for good, so a pointer into it can't return
2298
2371
  // (unlike a ticker switch) — clear both (mirrors removeSeries).
2299
2372
  selected: ({ context, event }) => {
2300
2373
  if (event.type !== 'row.remove' || context.rows.length <= 1)
2301
2374
  return context.selected;
2302
- const row = context.rows.find((r) => r.id === event.id);
2303
- return row?.configs.some((c) => c.id === context.selected) ? null : context.selected;
2375
+ return context.selected != null && rowDoomed(context.rows, event.id).has(context.selected)
2376
+ ? null
2377
+ : context.selected;
2304
2378
  },
2305
2379
  expanded: ({ context, event }) => {
2306
2380
  if (event.type !== 'row.remove' || context.rows.length <= 1)
2307
2381
  return context.expanded;
2308
- const row = context.rows.find((r) => r.id === event.id);
2309
- return row?.configs.some((c) => c.id === context.expanded) ? null : context.expanded;
2382
+ return context.expanded != null && rowDoomed(context.rows, event.id).has(context.expanded)
2383
+ ? null
2384
+ : context.expanded;
2310
2385
  },
2311
2386
  }),
2312
2387
  },
@@ -2321,7 +2396,10 @@ export const terminalMachine = setup({
2321
2396
  palette: input.palette ?? [],
2322
2397
  spreadColor: input.spreadColor,
2323
2398
  axisRanges: {},
2324
- rowSeq: input.initialRows.length,
2399
+ // Past every seeded `row-N`, not just the row count: a host re-seeds from a
2400
+ // stored preset whose rows were minted in an earlier session, and a count
2401
+ // would mint one of their ids again.
2402
+ rowSeq: Math.max(input.initialRows.length, rowSeqAfter(input.initialRows)),
2325
2403
  cfgSeq: cfgSeqAfter(input.initialRows),
2326
2404
  selected: null,
2327
2405
  expanded: null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tidal-ts/terminal",
3
- "version": "0.4.1",
3
+ "version": "0.5.1",
4
4
  "private": false,
5
5
  "description": "The terminal machine — an XState v5 statechart that owns a chart's configuration (rows, series, studies, pairs, axes, presets) and never its data.",
6
6
  "license": "MIT",
@@ -36,8 +36,8 @@
36
36
  "xstate": "^5.28.0"
37
37
  },
38
38
  "dependencies": {
39
- "@tidal-ts/chart": "0.4.1",
40
- "@tidal-ts/core": "0.4.1"
39
+ "@tidal-ts/chart": "0.5.1",
40
+ "@tidal-ts/core": "0.5.1"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@pond-ts/charts": "^0.70.0",