@univerjs/engine-formula 0.6.7 → 0.6.9

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/lib/facade.js ADDED
@@ -0,0 +1,212 @@
1
+ import { FBase as f, FUniver as d } from "@univerjs/core/facade";
2
+ import { Inject as c, ICommandService as p, Injector as _, IConfigService as C } from "@univerjs/core";
3
+ import { LexerTreeBuilder as v, SetFormulaCalculationStartMutation as l, SetFormulaCalculationStopMutation as S, SetFormulaCalculationNotificationMutation as m, GlobalComputingStatusService as x, ENGINE_FORMULA_CYCLE_REFERENCE_COUNT as h } from "@univerjs/engine-formula";
4
+ import { firstValueFrom as g, race as E, filter as F, timer as B, map as I } from "rxjs";
5
+ var T = Object.getOwnPropertyDescriptor, N = (e, t, r, i) => {
6
+ for (var o = i > 1 ? void 0 : i ? T(t, r) : t, a = e.length - 1, u; a >= 0; a--)
7
+ (u = e[a]) && (o = u(o) || o);
8
+ return o;
9
+ }, n = (e, t) => (r, i) => t(r, i, e);
10
+ let s = class extends f {
11
+ constructor(e, t, r, i) {
12
+ super(), this._commandService = e, this._injector = t, this._lexerTreeBuilder = r, this._configService = i, this._initialize();
13
+ }
14
+ /**
15
+ * @ignore
16
+ */
17
+ _initialize() {
18
+ }
19
+ /**
20
+ * The tree builder for formula string.
21
+ * @type {LexerTreeBuilder}
22
+ */
23
+ get lexerTreeBuilder() {
24
+ return this._lexerTreeBuilder;
25
+ }
26
+ /**
27
+ * Offsets the formula
28
+ * @param {string} formulaString - The formula string to offset
29
+ * @param {number} refOffsetX - The offset column
30
+ * @param {number} refOffsetY - The offset row
31
+ * @param {boolean} [ignoreAbsolute] - Whether to ignore the absolute reference
32
+ * @returns {string} The offset formula string
33
+ *
34
+ * @example
35
+ * ```ts
36
+ * const formulaEngine = univerAPI.getFormula();
37
+ * const result = formulaEngine.moveFormulaRefOffset('=SUM(A1,B2)', 1, 1);
38
+ * console.log(result);
39
+ * ```
40
+ */
41
+ moveFormulaRefOffset(e, t, r, i) {
42
+ return this._lexerTreeBuilder.moveFormulaRefOffset(e, t, r, i);
43
+ }
44
+ /**
45
+ * Resolves the formula string to a 'node' node
46
+ * @param {string} formulaString - The formula string to resolve
47
+ * @returns {Array<ISequenceNode | string>} The nodes of the formula string
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const formulaEngine = univerAPI.getFormula();
52
+ * const nodes = formulaEngine.sequenceNodesBuilder('=SUM(A1,B2)');
53
+ * console.log(nodes);
54
+ * ```
55
+ */
56
+ sequenceNodesBuilder(e) {
57
+ return this._lexerTreeBuilder.sequenceNodesBuilder(e) || [];
58
+ }
59
+ /**
60
+ * Start the calculation of the formula.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const formulaEngine = univerAPI.getFormula();
65
+ * formulaEngine.executeCalculation();
66
+ * ```
67
+ */
68
+ executeCalculation() {
69
+ this._commandService.executeCommand(l.id, { commands: [], forceCalculation: !0 }, { onlyLocal: !0 });
70
+ }
71
+ /**
72
+ * Stop the calculation of the formula.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const formulaEngine = univerAPI.getFormula();
77
+ * formulaEngine.stopCalculation();
78
+ * ```
79
+ */
80
+ stopCalculation() {
81
+ this._commandService.executeCommand(S.id, {});
82
+ }
83
+ /**
84
+ * Listening calculation starts.
85
+ * @param {Function} callback - The callback function to be called when the formula calculation starts.
86
+ * @returns {IDisposable} The disposable instance.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * const formulaEngine = univerAPI.getFormula();
91
+ * formulaEngine.calculationStart((forceCalculation) => {
92
+ * console.log('Calculation start', forceCalculation);
93
+ * });
94
+ * ```
95
+ */
96
+ calculationStart(e) {
97
+ return this._commandService.onCommandExecuted((t) => {
98
+ if (t.id === l.id) {
99
+ const r = t.params;
100
+ e(r.forceCalculation);
101
+ }
102
+ });
103
+ }
104
+ /**
105
+ * Listening calculation ends.
106
+ * @param {Function} callback - The callback function to be called when the formula calculation ends.
107
+ * @returns {IDisposable} The disposable instance.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const formulaEngine = univerAPI.getFormula();
112
+ * formulaEngine.calculationEnd((functionsExecutedState) => {
113
+ * console.log('Calculation end', functionsExecutedState);
114
+ * });
115
+ * ```
116
+ */
117
+ calculationEnd(e) {
118
+ return this._commandService.onCommandExecuted((t) => {
119
+ if (t.id !== m.id)
120
+ return;
121
+ const r = t.params;
122
+ r.functionsExecutedState !== void 0 && e(r.functionsExecutedState);
123
+ });
124
+ }
125
+ /**
126
+ * Wait for computing in the Univer instance to complete. Please note that this does not only include formula calculation,
127
+ * but also other computing tasks, e.g. pivot table calculation.
128
+ * @param {number} [timeout] The maximum time to wait for the computing to complete, in milliseconds. The default
129
+ * value is 30,000 milliseconds.
130
+ * @returns {Promise<boolean>} This method returns `true` if the computing is complete. If the timeout is reached, this
131
+ * method returns `false`.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * const formulaEngine = univerAPI.getFormula();
136
+ * formulaEngine.whenComputingCompleteAsync(3000).then((isComplete) => {
137
+ * console.log('Computing complete:', isComplete);
138
+ * });
139
+ * ```
140
+ */
141
+ whenComputingCompleteAsync(e) {
142
+ const t = this._injector.get(x);
143
+ return t.computingStatus ? Promise.resolve(!0) : g(E(
144
+ t.computingStatus$.pipe(F((r) => r)),
145
+ B(e != null ? e : 3e4).pipe(I(() => !1))
146
+ ));
147
+ }
148
+ /**
149
+ * @deprecated Use `whenComputingCompleteAsync` instead.
150
+ * @returns {Promise<void>} This method returns a promise that resolves when the calculation is complete.
151
+ */
152
+ onCalculationEnd() {
153
+ return new Promise((e, t) => {
154
+ const r = setTimeout(() => {
155
+ t(new Error("Calculation end timeout"));
156
+ }, 3e4), i = this.calculationEnd(() => {
157
+ clearTimeout(r), i.dispose(), e();
158
+ });
159
+ });
160
+ }
161
+ /**
162
+ * Listening calculation processing.
163
+ * @param {Function} callback - The callback function to be called when the formula calculation is in progress.
164
+ * @returns {IDisposable} The disposable instance.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * const formulaEngine = univerAPI.getFormula();
169
+ * formulaEngine.calculationProcessing((stageInfo) => {
170
+ * console.log('Calculation processing', stageInfo);
171
+ * });
172
+ * ```
173
+ */
174
+ calculationProcessing(e) {
175
+ return this._commandService.onCommandExecuted((t) => {
176
+ if (t.id !== m.id)
177
+ return;
178
+ const r = t.params;
179
+ r.stageInfo !== void 0 && e(r.stageInfo);
180
+ });
181
+ }
182
+ /**
183
+ * When a formula contains a circular reference, set the maximum number of iterations for the formula calculation.
184
+ * @param {number} maxIteration The maximum number of iterations. The default value is 1.
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * // Set the maximum number of iterations for the formula calculation to 5.
189
+ * // The default value is 1.
190
+ * const formulaEngine = univerAPI.getFormula();
191
+ * formulaEngine.setMaxIteration(5);
192
+ * ```
193
+ */
194
+ setMaxIteration(e) {
195
+ this._configService.setConfig(h, e);
196
+ }
197
+ };
198
+ s = N([
199
+ n(0, c(p)),
200
+ n(1, c(_)),
201
+ n(2, c(v)),
202
+ n(3, C)
203
+ ], s);
204
+ class O extends d {
205
+ getFormula() {
206
+ return this._injector.createInstance(s);
207
+ }
208
+ }
209
+ d.extend(O);
210
+ export {
211
+ s as FFormula
212
+ };