@dxos/plugin-sheet 0.6.8-staging.77f93a3 → 0.6.8-staging.dec6b33

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 (76) hide show
  1. package/dist/lib/browser/SheetContainer-6K3XOBZ7.mjs +1765 -0
  2. package/dist/lib/browser/SheetContainer-6K3XOBZ7.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-6VPEAUG6.mjs → chunk-D3PUKBH6.mjs} +1 -1
  4. package/dist/lib/browser/{chunk-6VPEAUG6.mjs.map → chunk-D3PUKBH6.mjs.map} +1 -1
  5. package/dist/lib/browser/chunk-MJOFSXLS.mjs +3251 -0
  6. package/dist/lib/browser/chunk-MJOFSXLS.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +86 -12
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/types.mjs +1 -1
  11. package/dist/lib/node/SheetContainer-QWNOHX7P.cjs +1759 -0
  12. package/dist/lib/node/SheetContainer-QWNOHX7P.cjs.map +7 -0
  13. package/dist/lib/node/chunk-3AINF7RX.cjs +3275 -0
  14. package/dist/lib/node/chunk-3AINF7RX.cjs.map +7 -0
  15. package/dist/lib/node/{chunk-4CE6FK5Z.cjs → chunk-3R3J7IZR.cjs} +4 -4
  16. package/dist/lib/node/{chunk-4CE6FK5Z.cjs.map → chunk-3R3J7IZR.cjs.map} +1 -1
  17. package/dist/lib/node/index.cjs +87 -20
  18. package/dist/lib/node/index.cjs.map +4 -4
  19. package/dist/lib/node/meta.json +1 -1
  20. package/dist/lib/node/types.cjs +9 -9
  21. package/dist/lib/node/types.cjs.map +1 -1
  22. package/dist/types/src/SheetPlugin.d.ts.map +1 -1
  23. package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts.map +1 -1
  24. package/dist/types/src/components/CellEditor/extension.d.ts +2 -1
  25. package/dist/types/src/components/CellEditor/extension.d.ts.map +1 -1
  26. package/dist/types/src/components/ComputeGraph/async-function.d.ts +4 -2
  27. package/dist/types/src/components/ComputeGraph/async-function.d.ts.map +1 -1
  28. package/dist/types/src/components/ComputeGraph/edge-function.d.ts.map +1 -1
  29. package/dist/types/src/components/ComputeGraph/graph-context.d.ts +2 -1
  30. package/dist/types/src/components/ComputeGraph/graph-context.d.ts.map +1 -1
  31. package/dist/types/src/components/ComputeGraph/graph.d.ts +9 -4
  32. package/dist/types/src/components/ComputeGraph/graph.d.ts.map +1 -1
  33. package/dist/types/src/components/ComputeGraph/index.d.ts +1 -1
  34. package/dist/types/src/components/ComputeGraph/index.d.ts.map +1 -1
  35. package/dist/types/src/components/Sheet/Sheet.d.ts.map +1 -1
  36. package/dist/types/src/components/Sheet/Sheet.stories.d.ts.map +1 -1
  37. package/dist/types/src/components/Sheet/sheet-context.d.ts +3 -2
  38. package/dist/types/src/components/Sheet/sheet-context.d.ts.map +1 -1
  39. package/dist/types/src/model/functions.d.ts +11 -0
  40. package/dist/types/src/model/functions.d.ts.map +1 -0
  41. package/dist/types/src/model/index.d.ts +1 -0
  42. package/dist/types/src/model/index.d.ts.map +1 -1
  43. package/dist/types/src/model/model.d.ts +25 -2
  44. package/dist/types/src/model/model.d.ts.map +1 -1
  45. package/dist/types/src/types.d.ts +1 -1
  46. package/dist/types/src/types.d.ts.map +1 -1
  47. package/package.json +28 -30
  48. package/src/SheetPlugin.tsx +27 -9
  49. package/src/components/CellEditor/CellEditor.stories.tsx +5 -2
  50. package/src/components/CellEditor/extension.test.ts +3 -1
  51. package/src/components/CellEditor/extension.ts +6 -13
  52. package/src/components/ComputeGraph/async-function.ts +10 -1
  53. package/src/components/ComputeGraph/edge-function.ts +8 -9
  54. package/src/components/ComputeGraph/graph-context.tsx +6 -2
  55. package/src/components/ComputeGraph/graph.browser.test.ts +3 -2
  56. package/src/components/ComputeGraph/graph.ts +22 -12
  57. package/src/components/ComputeGraph/index.ts +1 -1
  58. package/src/components/Sheet/Sheet.stories.tsx +3 -7
  59. package/src/components/Sheet/Sheet.tsx +7 -5
  60. package/src/components/Sheet/sheet-context.tsx +55 -6
  61. package/src/model/functions.ts +2427 -0
  62. package/src/model/index.ts +1 -0
  63. package/src/model/model.browser.test.ts +1 -1
  64. package/src/model/model.ts +77 -6
  65. package/src/types.ts +1 -1
  66. package/dist/lib/browser/SheetContainer-H22IDJ43.mjs +0 -3740
  67. package/dist/lib/browser/SheetContainer-H22IDJ43.mjs.map +0 -7
  68. package/dist/lib/browser/chunk-AT2FJXQX.mjs +0 -861
  69. package/dist/lib/browser/chunk-AT2FJXQX.mjs.map +0 -7
  70. package/dist/lib/node/SheetContainer-S32KTNZ6.cjs +0 -3731
  71. package/dist/lib/node/SheetContainer-S32KTNZ6.cjs.map +0 -7
  72. package/dist/lib/node/chunk-FCKJ4QRM.cjs +0 -881
  73. package/dist/lib/node/chunk-FCKJ4QRM.cjs.map +0 -7
  74. package/dist/types/src/components/CellEditor/functions.d.ts +0 -66
  75. package/dist/types/src/components/CellEditor/functions.d.ts.map +0 -1
  76. package/src/components/CellEditor/functions.ts +0 -2017
@@ -2,5 +2,6 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
+ export * from './functions';
5
6
  export * from './model';
6
7
  export * from './types';
@@ -20,7 +20,7 @@ describe('model', () => {
20
20
  const createModel = async () => {
21
21
  const graph = createComputeGraph();
22
22
  const sheet = createSheet();
23
- const model = new SheetModel(graph, sheet, { rows: 5, columns: 5 });
23
+ const model = new SheetModel(graph, sheet, undefined, { rows: 5, columns: 5 });
24
24
  await model.initialize();
25
25
  return model;
26
26
  };
@@ -8,11 +8,14 @@ import { type SimpleCellAddress } from 'hyperformula/typings/Cell';
8
8
  import { type SimpleDate, type SimpleDateTime } from 'hyperformula/typings/DateTimeHelper';
9
9
 
10
10
  import { Event } from '@dxos/async';
11
+ import { type Space } from '@dxos/client/echo';
11
12
  import { Context } from '@dxos/context';
12
13
  import { invariant } from '@dxos/invariant';
13
14
  import { PublicKey } from '@dxos/keys';
14
15
  import { log } from '@dxos/log';
16
+ import { type FunctionType } from '@dxos/plugin-script/types';
15
17
 
18
+ import { defaultFunctions, type FunctionDefinition } from './functions';
16
19
  import { addressFromA1Notation, addressToA1Notation, type CellAddress, type CellRange } from './types';
17
20
  import { createIndices, RangeException, ReadonlyException } from './util';
18
21
  import { type ComputeGraph } from '../components';
@@ -30,6 +33,8 @@ export type SheetModelOptions = {
30
33
  readonly?: boolean;
31
34
  rows: number;
32
35
  columns: number;
36
+ mapFormulaBindingToId: (functions: FunctionType[]) => (formula: string) => string;
37
+ mapFormulaBindingFromId: (functions: FunctionType[]) => (formula: string) => string;
33
38
  };
34
39
 
35
40
  const typeMap: Record<string, ValueTypeEnum> = {
@@ -45,6 +50,8 @@ const typeMap: Record<string, ValueTypeEnum> = {
45
50
  export const defaultOptions: SheetModelOptions = {
46
51
  rows: 50,
47
52
  columns: 26,
53
+ mapFormulaBindingFromId: () => (formula) => formula,
54
+ mapFormulaBindingToId: () => (formula) => formula,
48
55
  };
49
56
 
50
57
  const getTopLeft = (range: CellRange) => {
@@ -78,12 +85,14 @@ export class SheetModel {
78
85
  */
79
86
  private readonly _sheetId: number;
80
87
  private readonly _options: SheetModelOptions;
88
+ private _functions: FunctionType[] = [];
81
89
 
82
90
  public readonly update = new Event();
83
91
 
84
92
  constructor(
85
93
  private readonly _graph: ComputeGraph,
86
94
  private readonly _sheet: SheetType,
95
+ private readonly _space?: Space,
87
96
  options: Partial<SheetModelOptions> = {},
88
97
  ) {
89
98
  // Sheet for this object.
@@ -115,8 +124,12 @@ export class SheetModel {
115
124
  };
116
125
  }
117
126
 
118
- get functions(): string[] {
119
- return this._graph.hf.getRegisteredFunctionNames();
127
+ get functions(): FunctionDefinition[] {
128
+ const hfFunctions = this._graph.hf
129
+ .getRegisteredFunctionNames()
130
+ .map((name) => defaultFunctions.find((fn) => fn.name === name) ?? { name });
131
+ const echoFunctions = this._functions.map((fn) => ({ name: fn.binding! }));
132
+ return [...hfFunctions, ...echoFunctions];
120
133
  }
121
134
 
122
135
  get initialized(): boolean {
@@ -142,6 +155,19 @@ export class SheetModel {
142
155
  const unsubscribe = this._graph.update.on(() => this.update.emit());
143
156
  this._ctx.onDispose(unsubscribe);
144
157
 
158
+ if (this._space) {
159
+ const { Filter } = await import('@dxos/client/echo');
160
+ const { FunctionType } = await import('@dxos/plugin-script/types');
161
+
162
+ // Listen for function changes.
163
+ const query = this._space?.db.query(Filter.schema(FunctionType));
164
+ const unsubscribe = query.subscribe(({ objects }) => {
165
+ this._functions = objects.filter((fn) => fn.binding);
166
+ this.update.emit();
167
+ });
168
+ this._ctx.onDispose(unsubscribe);
169
+ }
170
+
145
171
  return this;
146
172
  }
147
173
 
@@ -163,7 +189,7 @@ export class SheetModel {
163
189
  Object.entries(this._sheet.cells).forEach(([key, { value }]) => {
164
190
  const { column, row } = this.addressFromIndex(key);
165
191
  if (typeof value === 'string' && value.charAt(0) === '=') {
166
- value = this.mapFormulaIndicesToRefs(value);
192
+ value = this.mapFormulaBindingToFormula(this.mapFormulaBindingFromId(this.mapFormulaIndicesToRefs(value)));
167
193
  }
168
194
 
169
195
  this._graph.hf.setCellContents({ sheet: this._sheetId, row, col: column }, value);
@@ -267,7 +293,7 @@ export class SheetModel {
267
293
  }
268
294
 
269
295
  if (typeof value === 'string' && value.charAt(0) === '=') {
270
- return this.mapFormulaIndicesToRefs(value);
296
+ return this.mapFormulaBindingFromId(this.mapFormulaIndicesToRefs(value));
271
297
  } else {
272
298
  return String(value);
273
299
  }
@@ -326,7 +352,9 @@ export class SheetModel {
326
352
  }
327
353
 
328
354
  // Insert into engine.
329
- this._graph.hf.setCellContents({ sheet: this._sheetId, row: cell.row, col: cell.column }, [[value]]);
355
+ this._graph.hf.setCellContents({ sheet: this._sheetId, row: cell.row, col: cell.column }, [
356
+ [typeof value === 'string' && value.charAt(0) === '=' ? this.mapFormulaBindingToFormula(value) : value],
357
+ ]);
330
358
 
331
359
  // Insert into sheet.
332
360
  const idx = this.addressToIndex(cell);
@@ -334,7 +362,7 @@ export class SheetModel {
334
362
  delete this._sheet.cells[idx];
335
363
  } else {
336
364
  if (typeof value === 'string' && value.charAt(0) === '=') {
337
- value = this.mapFormulaRefsToIndices(value);
365
+ value = this.mapFormulaBindingToId(this.mapFormulaRefsToIndices(value));
338
366
  }
339
367
 
340
368
  this._sheet.cells[idx] = { value };
@@ -432,6 +460,49 @@ export class SheetModel {
432
460
  return { from, to };
433
461
  }
434
462
 
463
+ /**
464
+ * E.g., "HELLO()" => "EDGE("HELLO")".
465
+ */
466
+ mapFormulaBindingToFormula(formula: string): string {
467
+ return formula.replace(/([a-zA-Z0-9]+)\((.*)\)/g, (match, binding, args) => {
468
+ const fn = this._functions.find((fn) => fn.binding === binding);
469
+ if (!fn) {
470
+ return match;
471
+ }
472
+
473
+ if (args.trim() === '') {
474
+ return `EDGE("${binding}")`;
475
+ }
476
+ return `EDGE("${binding}", ${args})`;
477
+ });
478
+ }
479
+
480
+ /**
481
+ * E.g., "EDGE("HELLO")" => "HELLO()".
482
+ */
483
+ mapFormulaBindingFromFormula(formula: string): string {
484
+ return formula.replace(/EDGE\("([a-zA-Z0-9]+)"(.*)\)/, (_match, binding, args) => {
485
+ if (args.trim() === '') {
486
+ return `${binding}()`;
487
+ }
488
+ return `${binding}(${args.slice(2)})`;
489
+ });
490
+ }
491
+
492
+ /**
493
+ * Map from binding to fully qualified ECHO ID.
494
+ */
495
+ mapFormulaBindingToId(formula: string): string {
496
+ return this._options.mapFormulaBindingToId(this._functions)(formula);
497
+ }
498
+
499
+ /**
500
+ * Map from fully qualified ECHO ID to binding.
501
+ */
502
+ mapFormulaBindingFromId(formula: string): string {
503
+ return this._options.mapFormulaBindingFromId(this._functions)(formula);
504
+ }
505
+
435
506
  /**
436
507
  * Map from A1 notation to indices.
437
508
  */
package/src/types.ts CHANGED
@@ -11,7 +11,7 @@ import type {
11
11
  } from '@dxos/app-framework';
12
12
  import { create, S, TypedObject } from '@dxos/echo-schema';
13
13
  import { type SchemaProvides } from '@dxos/plugin-client';
14
- import type { StackProvides } from '@dxos/plugin-stack';
14
+ import { type StackProvides } from '@dxos/plugin-stack';
15
15
 
16
16
  import { SHEET_PLUGIN } from './meta';
17
17