@trebco/treb 28.7.0 → 28.10.5

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 (37) hide show
  1. package/dist/treb-spreadsheet-light.mjs +14 -14
  2. package/dist/treb-spreadsheet.mjs +12 -12
  3. package/dist/treb.d.ts +39 -6
  4. package/notes/connected-elements.md +37 -0
  5. package/package.json +1 -1
  6. package/treb-base-types/src/gradient.ts +1 -1
  7. package/treb-base-types/src/localization.ts +6 -0
  8. package/treb-calculator/src/calculator.ts +72 -30
  9. package/treb-calculator/src/dag/calculation_leaf_vertex.ts +7 -0
  10. package/treb-calculator/src/dag/graph.ts +8 -0
  11. package/treb-calculator/src/functions/base-functions.ts +30 -1
  12. package/treb-calculator/src/index.ts +1 -1
  13. package/treb-charts/src/chart-functions.ts +14 -0
  14. package/treb-charts/src/chart-types.ts +25 -1
  15. package/treb-charts/src/chart-utils.ts +195 -9
  16. package/treb-charts/src/chart.ts +4 -0
  17. package/treb-charts/src/default-chart-renderer.ts +17 -1
  18. package/treb-charts/src/renderer.ts +182 -9
  19. package/treb-charts/style/charts.scss +39 -0
  20. package/treb-embed/markup/toolbar.html +35 -34
  21. package/treb-embed/src/custom-element/treb-global.ts +10 -2
  22. package/treb-embed/src/embedded-spreadsheet.ts +209 -106
  23. package/treb-embed/src/options.ts +7 -0
  24. package/treb-embed/style/layout.scss +4 -0
  25. package/treb-embed/style/toolbar.scss +37 -0
  26. package/treb-grid/src/index.ts +1 -1
  27. package/treb-grid/src/types/conditional_format.ts +1 -1
  28. package/treb-grid/src/types/data_model.ts +32 -0
  29. package/treb-grid/src/types/grid.ts +11 -1
  30. package/treb-grid/src/types/grid_base.ts +161 -5
  31. package/treb-grid/src/types/grid_command.ts +32 -0
  32. package/treb-grid/src/types/grid_events.ts +7 -0
  33. package/treb-grid/src/types/grid_options.ts +8 -0
  34. package/treb-grid/src/types/sheet.ts +0 -56
  35. package/treb-grid/src/types/update_flags.ts +1 -0
  36. package/treb-parser/src/parser-types.ts +6 -0
  37. package/treb-parser/src/parser.ts +48 -1
package/dist/treb.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- /*! API v28.7. Copyright 2018-2024 trebco, llc. All rights reserved. LGPL: https://treb.app/license */
1
+ /*! API v28.10. Copyright 2018-2024 trebco, llc. All rights reserved. LGPL: https://treb.app/license */
2
2
 
3
3
  /**
4
4
  * add our tag to the map
@@ -19,12 +19,20 @@ declare global {
19
19
  export declare class TREBGlobal {
20
20
 
21
21
  /**
22
- * build version
22
+ * Package version
23
23
  */
24
24
  version: string;
25
25
 
26
26
  /**
27
- * create a spreadsheet instance
27
+ * Create a spreadsheet. The `USER_DATA_TYPE` template parameter is the type
28
+ * assigned to the `user_data` field of the spreadsheet instance -- it can
29
+ * help simplify typing if you are storing extra data in spreadsheet
30
+ * files.
31
+ *
32
+ * Just ignore this parameter if you don't need it.
33
+ *
34
+ * @typeParam USER_DATA_TYPE - type for the `user_data` field in the
35
+ * spreadsheet instance
28
36
  */
29
37
  CreateSpreadsheet<USER_DATA_TYPE = unknown>(options: EmbeddedSpreadsheetOptions): EmbeddedSpreadsheet<USER_DATA_TYPE>;
30
38
  }
@@ -256,6 +264,13 @@ export interface EmbeddedSpreadsheetOptions {
256
264
  * top-left of the spreadsheet when a network document has local changes.
257
265
  */
258
266
  revert_indicator?: boolean;
267
+
268
+ /**
269
+ * handle the F9 key and recalculate the spreadsheet. for compatibility.
270
+ * we're leaving this option to default `false` for now, but that may
271
+ * change in the future. key modifiers have no effect.
272
+ */
273
+ recalculate_on_f9?: boolean;
259
274
  }
260
275
 
261
276
  /**
@@ -291,11 +306,17 @@ export declare class EmbeddedSpreadsheet<USER_DATA_TYPE = unknown> {
291
306
  /** document name (metadata) */
292
307
  set document_name(name: string | undefined);
293
308
 
294
- /** opaque user data (metadata) */
309
+ /**
310
+ * opaque user data (metadata). `USER_DATA_TYPE` is a template
311
+ * parameter you can set when creating the spreadsheet.
312
+ */
295
313
  get user_data(): USER_DATA_TYPE | undefined;
296
314
 
297
- /** opaque user data (metadata) */
298
- set user_data(data: USER_DATA_TYPE);
315
+ /**
316
+ * opaque user data (metadata). `USER_DATA_TYPE` is a template
317
+ * parameter you can set when creating the spreadsheet.
318
+ */
319
+ set user_data(data: USER_DATA_TYPE | undefined);
299
320
 
300
321
  /** current grid scale */
301
322
  get scale(): number;
@@ -417,6 +438,8 @@ export declare class EmbeddedSpreadsheet<USER_DATA_TYPE = unknown> {
417
438
  * Add a sheet, optionally named.
418
439
  */
419
440
  AddSheet(name?: string): number;
441
+ RemoveConnectedChart(id: number): void;
442
+ UpdateConnectedChart(id: number, formula: string): void;
420
443
 
421
444
  /**
422
445
  * Insert an annotation node. Usually this means inserting a chart. Regarding
@@ -849,6 +872,16 @@ export declare class EmbeddedSpreadsheet<USER_DATA_TYPE = unknown> {
849
872
  */
850
873
  FormatNumber(value: number, format?: string): string;
851
874
 
875
+ /**
876
+ * convert a javascript date (or timestamp) to a spreadsheet date
877
+ */
878
+ SpreadsheetDate(javascript_date: number | Date): number;
879
+
880
+ /**
881
+ * convert a spreadsheet date to a javascript date
882
+ */
883
+ JavascriptDate(spreadsheet_date: number): number;
884
+
852
885
  /**
853
886
  * Apply borders to range.
854
887
  *
@@ -0,0 +1,37 @@
1
+
2
+
3
+ "Connected elements" refers to a new API that's intended to link things
4
+ outside of the spreadsheet to the spreadsheet's graph and calculation events.
5
+
6
+ The canonical example is a chart that lives outside of the spreadsheet.
7
+ It should be updated any time the spreadsheet data changes. It should
8
+ also gracefully handle layout modifications (change sheet name, insert/delete
9
+ rows/columns) as if it were inside the spreadsheet.
10
+
11
+ One thing about these is that they're ephemeral; they have no persistent
12
+ representation in the data model. So you create them when you lay out your
13
+ web page, and they only exist for the lifetime of that page.
14
+
15
+ Still TODO:
16
+
17
+ - clean up junk (when?)
18
+ As long as we remove the leaf nodes from the graph, it should be
19
+ clean.
20
+
21
+ - deal with model rebuild (elements are getting orphaned here)
22
+ I think this is handled? still maybe an issue on Reset()
23
+
24
+ - API to update elements (e.g. change formula w/o removing)
25
+ that last one could be implemented as remove/add, possibly
26
+ reusing the generated ID. at least as a first cut.
27
+
28
+ Done:
29
+
30
+ - API to remove elements
31
+
32
+ - rewrite formula on layout changes
33
+
34
+ Open Qs:
35
+
36
+ - what happens if you completely flush the data model (reset, load new file?)
37
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trebco/treb",
3
- "version": "28.7.0",
3
+ "version": "28.10.5",
4
4
  "license": "LGPL-3.0-or-later",
5
5
  "homepage": "https://treb.app",
6
6
  "repository": {
@@ -15,7 +15,7 @@ export class Gradient {
15
15
 
16
16
  public mapped: Array<GradientStop & { resolved: number[] }>;
17
17
 
18
- constructor(stops: GradientStop[], theme: Theme, public color_space: ColorSpace = 'HSL') {
18
+ constructor(stops: GradientStop[], theme: Theme, public color_space: ColorSpace = 'RGB') {
19
19
 
20
20
  this.mapped = stops.map(stop => {
21
21
 
@@ -98,6 +98,12 @@ export class Localization {
98
98
 
99
99
  }
100
100
 
101
+ // don't allow the "C" locale
102
+ if (this.locale === 'C') {
103
+ console.warn('Locale not set, defaulting to en-us');
104
+ this.locale = 'en-us';
105
+ }
106
+
101
107
  const decimal_separator = new Intl.NumberFormat(this.locale,
102
108
  {minimumFractionDigits: 1}).format(3.3).replace(/\d/g, '');
103
109
 
@@ -55,6 +55,7 @@ import type { LeafVertex } from './dag/graph';
55
55
  import { ArgumentError, ReferenceError, UnknownError, ValueError, ExpressionError, NAError, DivideByZeroError } from './function-error';
56
56
  import { StateLeafVertex } from './dag/state_leaf_vertex';
57
57
  import { CalculationLeafVertex } from './dag/calculation_leaf_vertex';
58
+ import type { ConnectedElementType } from 'treb-grid';
58
59
 
59
60
  /**
60
61
  * breaking this out so we can use it for export (TODO)
@@ -1291,6 +1292,7 @@ export class Calculator extends Graph {
1291
1292
  subset = undefined;
1292
1293
  this.UpdateAnnotations();
1293
1294
  this.UpdateConditionals();
1295
+ this.UpdateConnectedElements();
1294
1296
  // this.UpdateNotifiers();
1295
1297
  this.full_rebuild_required = false; // unset
1296
1298
  }
@@ -1488,6 +1490,7 @@ export class Calculator extends Graph {
1488
1490
 
1489
1491
  this.UpdateAnnotations(); // all
1490
1492
  this.UpdateConditionals();
1493
+ this.UpdateConnectedElements();
1491
1494
 
1492
1495
  // and notifiers
1493
1496
 
@@ -1767,9 +1770,73 @@ export class Calculator extends Graph {
1767
1770
  }
1768
1771
  }
1769
1772
 
1773
+ public RemoveConnectedELement(element: ConnectedElementType) {
1774
+ let internal = element.internal as { vertex: StateLeafVertex };
1775
+ if (internal?.vertex) {
1776
+ this.RemoveLeafVertex(internal.vertex);
1777
+ return true;
1778
+ }
1779
+ return false;
1780
+ }
1781
+
1782
+ public UpdateConnectedElements(context?: Sheet, element?: ConnectedElementType) {
1783
+
1784
+ // we have a problem here in that these elements are not bound
1785
+ // to sheets, so we might have no context. for now we'll
1786
+ // just grab the first sheet, although that's not necessarily
1787
+ // what you want. we should enforce that these have hard sheet
1788
+ // references when created.
1789
+
1790
+ if (!context) {
1791
+ context = this.model.sheets.list[0];
1792
+ }
1793
+
1794
+ if (element) {
1795
+ let internal = element.internal as { vertex: StateLeafVertex };
1796
+ if (internal?.vertex) {
1797
+ this.RemoveLeafVertex(internal.vertex);
1798
+ }
1799
+ }
1800
+
1801
+ const elements = element ? [element] : this.model.connected_elements.values();
1802
+
1803
+ for (const element of elements) {
1804
+ let internal = element.internal as { vertex: StateLeafVertex };
1805
+ if (!internal) {
1806
+ internal = {
1807
+ vertex: new StateLeafVertex(),
1808
+ };
1809
+ element.internal = internal;
1810
+ }
1811
+
1812
+ const vertex = internal.vertex as LeafVertex;
1813
+ this.AddLeafVertex(vertex);
1814
+ this.UpdateLeafVertex(vertex, element.formula, context);
1815
+
1816
+ }
1817
+ }
1818
+
1770
1819
  public UpdateConditionals(list?: ConditionalFormat|ConditionalFormat[], context?: Sheet): void {
1771
1820
 
1821
+ // this method is (1) relying on the leaf vertex Set to avoid duplication,
1822
+ // and (2) leaving orphansed conditionals in place. we should look to
1823
+ // cleaning things up.
1824
+
1825
+ // is it also (3) adding unecessary calculations (building the expression,
1826
+ // below)?
1827
+
1772
1828
  if (!list) {
1829
+
1830
+ // we could in theory remove all of the leaves (the ones we know to
1831
+ // be used for conditionals), because they will be added back below.
1832
+ // how wasteful is that?
1833
+
1834
+ // or maybe we could change the mark, and then use invalid marks
1835
+ // to check?
1836
+
1837
+ // the alternative is just to leave them as orphans until the graph
1838
+ // is rebuilt. which is lazy, but probably not that bad...
1839
+
1773
1840
  for (const sheet of this.model.sheets.list) {
1774
1841
  if (sheet.conditional_formats?.length) {
1775
1842
  this.UpdateConditionals(sheet.conditional_formats, sheet);
@@ -1833,7 +1900,11 @@ export class Calculator extends Graph {
1833
1900
  entry.internal = {};
1834
1901
  }
1835
1902
  if (!entry.internal.vertex) {
1836
- entry.internal.vertex = new CalculationLeafVertex();
1903
+
1904
+ const vertex = new CalculationLeafVertex();
1905
+ vertex.use = 'conditional';
1906
+
1907
+ entry.internal.vertex = vertex;
1837
1908
 
1838
1909
  let options: EvaluateOptions|undefined;
1839
1910
  if (entry.type !== 'gradient' && entry.type !== 'duplicate-values') {
@@ -1851,35 +1922,6 @@ export class Calculator extends Graph {
1851
1922
  this.AddLeafVertex(vertex);
1852
1923
  this.UpdateLeafVertex(vertex, expression, context);
1853
1924
 
1854
- /*
1855
- if (entry.type === 'cell-match') {
1856
- if (!entry.internal) {
1857
- entry.internal = {};
1858
- }
1859
- if (!entry.internal.vertex) {
1860
- entry.internal.vertex = new CalculationLeafVertex();
1861
- }
1862
- const vertex = entry.internal.vertex as LeafVertex;
1863
- this.AddLeafVertex(vertex);
1864
- this.UpdateLeafVertex(vertex, entry.expression, context);
1865
- }
1866
- else if (entry.type === 'expression') {
1867
- if (!entry.internal) {
1868
- entry.internal = {};
1869
- }
1870
- if (!entry.internal.vertex) {
1871
- entry.internal.vertex = new CalculationLeafVertex();
1872
-
1873
- // set initial state based on current state
1874
- entry.internal.vertex.result = { type: ValueType.boolean, value: !!entry.applied };
1875
-
1876
- }
1877
- const vertex = entry.internal.vertex as LeafVertex;
1878
- this.AddLeafVertex(vertex);
1879
- this.UpdateLeafVertex(vertex, entry.expression, context);
1880
- }
1881
- */
1882
-
1883
1925
  }
1884
1926
 
1885
1927
  }
@@ -42,6 +42,13 @@ export class CalculationLeafVertex extends SpreadsheetVertex {
42
42
 
43
43
  public address = { row: -1, column: -1 }; // fake address
44
44
 
45
+ /**
46
+ * this type is currently only used for conditional formatting.
47
+ * but that might change in the future. I want to identify what
48
+ * it's used for so we can selectively prune them when necessary.
49
+ */
50
+ public use?: string;
51
+
45
52
  /**
46
53
  * flag, to reduce unecessary application. work in progress. this
47
54
  * indicates that we reached the calculation step. that means either
@@ -31,6 +31,7 @@ import type { DataModel } from 'treb-grid';
31
31
  import { CalculationLeafVertex } from './calculation_leaf_vertex';
32
32
 
33
33
  export type LeafVertex = StateLeafVertex|CalculationLeafVertex;
34
+ export type { StateLeafVertex };
34
35
 
35
36
  // FIXME: this is a bad habit if you're testing on falsy for OK.
36
37
 
@@ -816,6 +817,13 @@ export abstract class Graph implements GraphCallbacks {
816
817
  * managing and maintaining these vertices: we only need references.
817
818
  */
818
819
  public AddLeafVertex(vertex: LeafVertex): void {
820
+
821
+ /*
822
+ if (this.leaf_vertices.has(vertex)) {
823
+ console.info("TLV already has", vertex);
824
+ }
825
+ */
826
+
819
827
  this.leaf_vertices.add(vertex);
820
828
  }
821
829
 
@@ -848,6 +848,17 @@ export const BaseFunctionLibrary: FunctionMap = {
848
848
  }),
849
849
  },
850
850
 
851
+ RoundUp: {
852
+ fn: Utils.ApplyAsArray2((a, digits = 0) => {
853
+ const m = Math.pow(10, digits);
854
+ const positive = a >= 0;
855
+ return {
856
+ type: ValueType.number,
857
+ value: positive ? Math.ceil(m * a) / m : Math.floor(m * a) / m,
858
+ };
859
+ }),
860
+ },
861
+
851
862
  /*
852
863
 
853
864
  Round: {
@@ -1259,13 +1270,31 @@ export const BaseFunctionLibrary: FunctionMap = {
1259
1270
  columns = area.value[0]?.length || 0;
1260
1271
 
1261
1272
  const result: UnionValue[][] = [];
1273
+
1262
1274
  for (let r = 0; r < rows; r++) {
1263
1275
  const row: UnionValue[] = [];
1264
1276
  for (let c = 0; c < columns; c++) {
1265
1277
  const src = area.value[r][c];
1266
1278
  if (src.type === ValueType.number) {
1267
1279
  let calc = 0;
1268
- if (range > 0) {
1280
+
1281
+ // special case: max === min. this can be used to do binary
1282
+ // coloring over a set of data (ignoring the pivot).
1283
+
1284
+ // FIXME: use a separate loop?
1285
+
1286
+ if (max === min) {
1287
+ if (src.value > max) {
1288
+ calc = 1;
1289
+ }
1290
+ else if (src.value < max) {
1291
+ calc = 0;
1292
+ }
1293
+ else {
1294
+ calc = 0.5
1295
+ }
1296
+ }
1297
+ else if (range > 0) {
1269
1298
  calc = (src.value - min) / range;
1270
1299
  }
1271
1300
  row.push({ type: ValueType.number, value: calc });
@@ -24,4 +24,4 @@ export * from './calculator';
24
24
 
25
25
  // for annotations that have dependencies
26
26
 
27
- export type { LeafVertex } from './dag/graph';
27
+ export type { LeafVertex, StateLeafVertex } from './dag/graph';
@@ -60,14 +60,20 @@ export const ChartFunctions: FunctionMap = {
60
60
  * more general "group" if you just want to group things.
61
61
  *
62
62
  * boxing properly as "extended" type
63
+ *
64
+ * this is getting too specific to bubble charts, which have a lot
65
+ * of requirements that other charts don't have. can we split?
66
+ *
63
67
  */
64
68
  Series: {
65
69
  arguments: [
66
70
  { name: 'Label' }, // , metadata: true, },
67
71
  { name: 'X', metadata: true, },
68
72
  { name: 'Y', metadata: true, },
73
+ { name: 'Z', metadata: true, },
69
74
  { name: 'index', },
70
75
  { name: 'subtype', },
76
+ { name: 'Labels', description: 'Labels for bubble charts only (atm)' },
71
77
  ],
72
78
  fn: (...args: any) => {
73
79
  return {
@@ -153,4 +159,12 @@ export const ChartFunctions: FunctionMap = {
153
159
  fn: Identity,
154
160
  },
155
161
 
162
+ 'Bubble.Chart': {
163
+ arguments: [
164
+ { name: 'Data', metadata: true, },
165
+ { name: 'Chart Title' },
166
+ ],
167
+ fn: Identity,
168
+ },
169
+
156
170
  };
@@ -107,6 +107,27 @@ export interface ScatterData2 extends ChartDataBaseType {
107
107
 
108
108
  }
109
109
 
110
+ export interface BubbleChartData extends ChartDataBaseType {
111
+
112
+ type: 'bubble';
113
+
114
+ /*
115
+ x?: SubSeries;
116
+ y?: SubSeries;
117
+ z?: SubSeries;
118
+ c?: any[];
119
+ */
120
+ series: SeriesType[];
121
+
122
+
123
+ x_scale: RangeScale;
124
+ y_scale: RangeScale;
125
+
126
+ x_labels?: string[];
127
+ y_labels?: string[];
128
+
129
+ }
130
+
110
131
  /** base for column types (FIXME: probably common to scatter/line/area also) */
111
132
  export interface ColumnDataBaseType extends ChartDataBaseType {
112
133
  column_width: number;
@@ -190,6 +211,7 @@ export type ChartData
190
211
  | AreaData
191
212
  | ColumnData
192
213
  | BarData
214
+ | BubbleChartData
193
215
  ;
194
216
 
195
217
  export enum LegendLayout {
@@ -201,7 +223,7 @@ export enum LegendPosition {
201
223
  }
202
224
 
203
225
  export enum LegendStyle {
204
- line, marker
226
+ line, marker, bubble
205
227
  }
206
228
 
207
229
  export interface LegendOptions {
@@ -225,6 +247,8 @@ export interface SeriesType {
225
247
  subtype?: string;
226
248
  x: SubSeries;
227
249
  y: SubSeries;
250
+ z?: SubSeries;
228
251
  index?: number;
252
+ labels?: string[];
229
253
  }
230
254