@dxos/plugin-sheet 0.6.8-staging.77f93a3 → 0.6.8-staging.c55b37f
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/dist/lib/browser/SheetContainer-6K3XOBZ7.mjs +1765 -0
- package/dist/lib/browser/SheetContainer-6K3XOBZ7.mjs.map +7 -0
- package/dist/lib/browser/{chunk-6VPEAUG6.mjs → chunk-D3PUKBH6.mjs} +1 -1
- package/dist/lib/browser/{chunk-6VPEAUG6.mjs.map → chunk-D3PUKBH6.mjs.map} +1 -1
- package/dist/lib/browser/chunk-MJOFSXLS.mjs +3251 -0
- package/dist/lib/browser/chunk-MJOFSXLS.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +82 -6
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/types.mjs +1 -1
- package/dist/lib/node/SheetContainer-QWNOHX7P.cjs +1759 -0
- package/dist/lib/node/SheetContainer-QWNOHX7P.cjs.map +7 -0
- package/dist/lib/node/chunk-3AINF7RX.cjs +3275 -0
- package/dist/lib/node/chunk-3AINF7RX.cjs.map +7 -0
- package/dist/lib/node/{chunk-4CE6FK5Z.cjs → chunk-3R3J7IZR.cjs} +4 -4
- package/dist/lib/node/{chunk-4CE6FK5Z.cjs.map → chunk-3R3J7IZR.cjs.map} +1 -1
- package/dist/lib/node/index.cjs +84 -15
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/types.cjs +9 -9
- package/dist/lib/node/types.cjs.map +1 -1
- package/dist/types/src/SheetPlugin.d.ts.map +1 -1
- package/dist/types/src/components/CellEditor/CellEditor.stories.d.ts.map +1 -1
- package/dist/types/src/components/CellEditor/extension.d.ts +2 -1
- package/dist/types/src/components/CellEditor/extension.d.ts.map +1 -1
- package/dist/types/src/components/ComputeGraph/async-function.d.ts +4 -2
- package/dist/types/src/components/ComputeGraph/async-function.d.ts.map +1 -1
- package/dist/types/src/components/ComputeGraph/edge-function.d.ts.map +1 -1
- package/dist/types/src/components/ComputeGraph/graph-context.d.ts +2 -1
- package/dist/types/src/components/ComputeGraph/graph-context.d.ts.map +1 -1
- package/dist/types/src/components/ComputeGraph/graph.d.ts +9 -4
- package/dist/types/src/components/ComputeGraph/graph.d.ts.map +1 -1
- package/dist/types/src/components/ComputeGraph/index.d.ts +1 -1
- package/dist/types/src/components/ComputeGraph/index.d.ts.map +1 -1
- package/dist/types/src/components/Sheet/Sheet.d.ts.map +1 -1
- package/dist/types/src/components/Sheet/Sheet.stories.d.ts.map +1 -1
- package/dist/types/src/components/Sheet/sheet-context.d.ts +3 -2
- package/dist/types/src/components/Sheet/sheet-context.d.ts.map +1 -1
- package/dist/types/src/model/functions.d.ts +11 -0
- package/dist/types/src/model/functions.d.ts.map +1 -0
- package/dist/types/src/model/index.d.ts +1 -0
- package/dist/types/src/model/index.d.ts.map +1 -1
- package/dist/types/src/model/model.d.ts +25 -2
- package/dist/types/src/model/model.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +1 -1
- package/dist/types/src/types.d.ts.map +1 -1
- package/package.json +28 -30
- package/src/SheetPlugin.tsx +26 -3
- package/src/components/CellEditor/CellEditor.stories.tsx +5 -2
- package/src/components/CellEditor/extension.test.ts +3 -1
- package/src/components/CellEditor/extension.ts +6 -13
- package/src/components/ComputeGraph/async-function.ts +10 -1
- package/src/components/ComputeGraph/edge-function.ts +8 -9
- package/src/components/ComputeGraph/graph-context.tsx +6 -2
- package/src/components/ComputeGraph/graph.browser.test.ts +3 -2
- package/src/components/ComputeGraph/graph.ts +22 -12
- package/src/components/ComputeGraph/index.ts +1 -1
- package/src/components/Sheet/Sheet.stories.tsx +3 -7
- package/src/components/Sheet/Sheet.tsx +7 -5
- package/src/components/Sheet/sheet-context.tsx +55 -6
- package/src/model/functions.ts +2427 -0
- package/src/model/index.ts +1 -0
- package/src/model/model.browser.test.ts +1 -1
- package/src/model/model.ts +77 -6
- package/src/types.ts +1 -1
- package/dist/lib/browser/SheetContainer-H22IDJ43.mjs +0 -3740
- package/dist/lib/browser/SheetContainer-H22IDJ43.mjs.map +0 -7
- package/dist/lib/browser/chunk-AT2FJXQX.mjs +0 -861
- package/dist/lib/browser/chunk-AT2FJXQX.mjs.map +0 -7
- package/dist/lib/node/SheetContainer-S32KTNZ6.cjs +0 -3731
- package/dist/lib/node/SheetContainer-S32KTNZ6.cjs.map +0 -7
- package/dist/lib/node/chunk-FCKJ4QRM.cjs +0 -881
- package/dist/lib/node/chunk-FCKJ4QRM.cjs.map +0 -7
- package/dist/types/src/components/CellEditor/functions.d.ts +0 -66
- package/dist/types/src/components/CellEditor/functions.d.ts.map +0 -1
- package/src/components/CellEditor/functions.ts +0 -2017
|
@@ -2,27 +2,31 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { HyperFormula } from 'hyperformula';
|
|
5
|
+
import { type FunctionPluginDefinition, HyperFormula } from 'hyperformula';
|
|
6
|
+
import { type FunctionTranslationsPackage } from 'hyperformula/typings/interpreter';
|
|
6
7
|
|
|
7
8
|
import { Event } from '@dxos/async';
|
|
8
9
|
import { type Space } from '@dxos/client/echo';
|
|
9
10
|
import { PublicKey } from '@dxos/keys';
|
|
10
11
|
import { log } from '@dxos/log';
|
|
11
12
|
|
|
12
|
-
import { FunctionContext } from './async-function';
|
|
13
|
-
import { CustomPlugin, CustomPluginTranslations } from './custom';
|
|
14
|
-
import { EdgeFunctionPlugin, EdgeFunctionPluginTranslations } from './edge-function';
|
|
13
|
+
import { FunctionContext, type FunctionContextOptions } from './async-function';
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* Create root graph for space.
|
|
18
17
|
*/
|
|
19
|
-
export const createComputeGraph = (
|
|
20
|
-
// TODO(
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
export const createComputeGraph = (
|
|
19
|
+
// TODO(wittjosiah): Factor out this type to make these easier to define.
|
|
20
|
+
functionPlugins: { plugin: FunctionPluginDefinition; translations: FunctionTranslationsPackage }[] = [],
|
|
21
|
+
space?: Space,
|
|
22
|
+
options?: Partial<FunctionContextOptions>,
|
|
23
|
+
): ComputeGraph => {
|
|
24
|
+
functionPlugins.forEach(({ plugin, translations }) => {
|
|
25
|
+
HyperFormula.registerFunctionPlugin(plugin, translations);
|
|
26
|
+
});
|
|
23
27
|
|
|
24
28
|
const hf = HyperFormula.buildEmpty({ licenseKey: 'gpl-v3' });
|
|
25
|
-
return new ComputeGraph(hf, space);
|
|
29
|
+
return new ComputeGraph(hf, space, options);
|
|
26
30
|
};
|
|
27
31
|
|
|
28
32
|
/**
|
|
@@ -34,13 +38,19 @@ export class ComputeGraph {
|
|
|
34
38
|
public readonly update = new Event();
|
|
35
39
|
|
|
36
40
|
// The context is passed to all functions.
|
|
37
|
-
public readonly context = new FunctionContext(
|
|
38
|
-
this.
|
|
39
|
-
|
|
41
|
+
public readonly context = new FunctionContext(
|
|
42
|
+
this.hf,
|
|
43
|
+
this._space,
|
|
44
|
+
() => {
|
|
45
|
+
this.refresh();
|
|
46
|
+
},
|
|
47
|
+
this._options,
|
|
48
|
+
);
|
|
40
49
|
|
|
41
50
|
constructor(
|
|
42
51
|
public readonly hf: HyperFormula,
|
|
43
52
|
private readonly _space?: Space,
|
|
53
|
+
private readonly _options?: Partial<FunctionContextOptions>,
|
|
44
54
|
) {
|
|
45
55
|
this.hf.updateConfig({ context: this.context });
|
|
46
56
|
}
|
|
@@ -20,13 +20,9 @@ import { type SizeMap } from './grid';
|
|
|
20
20
|
import { useSheetContext } from './sheet-context';
|
|
21
21
|
import { SheetModel } from '../../model';
|
|
22
22
|
import { ValueTypeEnum, type CellValue, createSheet, SheetType } from '../../types';
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
ComputeGraphContextProvider,
|
|
27
|
-
createComputeGraph,
|
|
28
|
-
useComputeGraph,
|
|
29
|
-
} from '../ComputeGraph';
|
|
23
|
+
import { type ComputeGraph, createComputeGraph } from '../ComputeGraph';
|
|
24
|
+
// TODO(wittjosiah): Refactor. This is not exported from ./components due to depending on ECHO.
|
|
25
|
+
import { ComputeGraphContext, ComputeGraphContextProvider, useComputeGraph } from '../ComputeGraph/graph-context';
|
|
30
26
|
import { Toolbar, type ToolbarActionHandler } from '../Toolbar';
|
|
31
27
|
|
|
32
28
|
// TODO(burdon): Allow toolbar to access sheet context; provide state for current cursor/range.
|
|
@@ -29,6 +29,7 @@ import React, {
|
|
|
29
29
|
forwardRef,
|
|
30
30
|
useEffect,
|
|
31
31
|
useImperativeHandle,
|
|
32
|
+
useMemo,
|
|
32
33
|
useRef,
|
|
33
34
|
useState,
|
|
34
35
|
} from 'react';
|
|
@@ -1049,13 +1050,14 @@ const GridCellEditor = ({ style, value, onNav, onClose }: GridCellEditorProps) =
|
|
|
1049
1050
|
notifier.current?.(rangeToA1Notation(range));
|
|
1050
1051
|
}
|
|
1051
1052
|
}, [range]);
|
|
1052
|
-
const
|
|
1053
|
-
|
|
1053
|
+
const extension = useMemo(
|
|
1054
|
+
() => [
|
|
1054
1055
|
editorKeys({ onNav, onClose }),
|
|
1055
1056
|
sheetExtension({ functions: model.functions }),
|
|
1056
1057
|
rangeExtension((fn) => (notifier.current = fn)),
|
|
1057
|
-
]
|
|
1058
|
-
|
|
1058
|
+
],
|
|
1059
|
+
[model],
|
|
1060
|
+
);
|
|
1059
1061
|
|
|
1060
1062
|
return (
|
|
1061
1063
|
<div
|
|
@@ -1080,7 +1082,7 @@ const SheetStatusBar = () => {
|
|
|
1080
1082
|
if (cursor) {
|
|
1081
1083
|
value = model.getCellValue(cursor);
|
|
1082
1084
|
if (typeof value === 'string' && value.charAt(0) === '=') {
|
|
1083
|
-
value = model.mapFormulaIndicesToRefs(value);
|
|
1085
|
+
value = model.mapFormulaBindingFromId(model.mapFormulaIndicesToRefs(value));
|
|
1084
1086
|
isFormula = true;
|
|
1085
1087
|
} else if (value != null) {
|
|
1086
1088
|
value = String(value);
|
|
@@ -5,12 +5,18 @@
|
|
|
5
5
|
import React, { type PropsWithChildren, createContext, useContext, useState, useEffect } from 'react';
|
|
6
6
|
|
|
7
7
|
import { invariant } from '@dxos/invariant';
|
|
8
|
-
import { type
|
|
8
|
+
import { type FunctionType } from '@dxos/plugin-script';
|
|
9
|
+
import { fullyQualifiedId, type Space } from '@dxos/react-client/echo';
|
|
9
10
|
|
|
10
11
|
import { FormattingModel } from './formatting';
|
|
11
|
-
import { type CellAddress, type CellRange, SheetModel } from '../../model';
|
|
12
|
+
import { type CellAddress, type CellRange, defaultFunctions, SheetModel } from '../../model';
|
|
12
13
|
import { type SheetType } from '../../types';
|
|
13
|
-
import {
|
|
14
|
+
import { type FunctionContextOptions } from '../ComputeGraph';
|
|
15
|
+
// TODO(wittjosiah): Refactor. This is not exported from ./components due to depending on ECHO.
|
|
16
|
+
import { useComputeGraph } from '../ComputeGraph/graph-context';
|
|
17
|
+
|
|
18
|
+
// TODO(wittjosiah): Factor out.
|
|
19
|
+
const OBJECT_ID_LENGTH = 60; // 33 (space id) + 26 (object id) + 1 (separator).
|
|
14
20
|
|
|
15
21
|
export type SheetContextType = {
|
|
16
22
|
model: SheetModel;
|
|
@@ -44,7 +50,49 @@ export type SheetContextProps = {
|
|
|
44
50
|
sheet: SheetType;
|
|
45
51
|
space: Space;
|
|
46
52
|
readonly?: boolean;
|
|
47
|
-
} & Pick<SheetContextType, 'onInfo'
|
|
53
|
+
} & Pick<SheetContextType, 'onInfo'> &
|
|
54
|
+
Partial<FunctionContextOptions>;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Map from binding to fully qualified ECHO ID.
|
|
58
|
+
*/
|
|
59
|
+
const mapFormulaBindingToId =
|
|
60
|
+
(functions: FunctionType[]) =>
|
|
61
|
+
(formula: string): string => {
|
|
62
|
+
return formula.replace(/([a-zA-Z0-9]+)\((.*)\)/g, (match, binding, args) => {
|
|
63
|
+
if (defaultFunctions.find((fn) => fn.name === binding) || binding === 'EDGE') {
|
|
64
|
+
return match;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const fn = functions.find((fn) => fn.binding === binding);
|
|
68
|
+
if (fn) {
|
|
69
|
+
return `${fullyQualifiedId(fn)}(${args})`;
|
|
70
|
+
} else {
|
|
71
|
+
return match;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Map from fully qualified ECHO ID to binding.
|
|
78
|
+
*/
|
|
79
|
+
const mapFormulaBindingFromId =
|
|
80
|
+
(functions: FunctionType[]) =>
|
|
81
|
+
(formula: string): string => {
|
|
82
|
+
return formula.replace(/([a-zA-Z0-9]+):([a-zA-Z0-9]+)\((.*)\)/g, (match, spaceId, objectId, args) => {
|
|
83
|
+
const id = `${spaceId}:${objectId}`;
|
|
84
|
+
if (id.length !== OBJECT_ID_LENGTH) {
|
|
85
|
+
return match;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const fn = functions.find((fn) => fullyQualifiedId(fn) === id);
|
|
89
|
+
if (fn?.binding) {
|
|
90
|
+
return `${fn.binding}(${args})`;
|
|
91
|
+
} else {
|
|
92
|
+
return match;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
};
|
|
48
96
|
|
|
49
97
|
export const SheetContextProvider = ({
|
|
50
98
|
children,
|
|
@@ -52,8 +100,9 @@ export const SheetContextProvider = ({
|
|
|
52
100
|
space,
|
|
53
101
|
readonly,
|
|
54
102
|
onInfo,
|
|
103
|
+
...options
|
|
55
104
|
}: PropsWithChildren<SheetContextProps>) => {
|
|
56
|
-
const graph = useComputeGraph(space);
|
|
105
|
+
const graph = useComputeGraph(space, options);
|
|
57
106
|
|
|
58
107
|
const [cursor, setCursor] = useState<CellAddress>();
|
|
59
108
|
const [range, setRange] = useState<CellRange>();
|
|
@@ -64,7 +113,7 @@ export const SheetContextProvider = ({
|
|
|
64
113
|
let model: SheetModel | undefined;
|
|
65
114
|
let formatting;
|
|
66
115
|
const t = setTimeout(async () => {
|
|
67
|
-
model = new SheetModel(graph, sheet, { readonly });
|
|
116
|
+
model = new SheetModel(graph, sheet, space, { readonly, mapFormulaBindingToId, mapFormulaBindingFromId });
|
|
68
117
|
await model.initialize();
|
|
69
118
|
formatting = new FormattingModel(model);
|
|
70
119
|
setModels([model, formatting]);
|