facade.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { Inject as u, ICommandService as f, Injector as _, FBase as C, IConfigService as v, FUniver as m } from "@univerjs/core";
  2. import { LexerTreeBuilder as x, SetFormulaCalculationStartMutation as l, SetFormulaCalculationStopMutation as p, SetFormulaCalculationNotificationMutation as d, ENGINE_FORMULA_CYCLE_REFERENCE_COUNT as S } from "@univerjs/engine-formula";
  3. var h = Object.defineProperty, E = Object.getOwnPropertyDescriptor, F = (e, t, r, i) => {
  4. for (var n = i > 1 ? void 0 : i ? E(t, r) : t, a = e.length - 1, c; a >= 0; a--)
  5. (c = e[a]) && (n = (i ? c(t, r, n) : c(n)) || n);
  6. return i && n && h(t, r, n), n;
  7. }, o = (e, t) => (r, i) => t(r, i, e);
  8. let s = class extends C {
  9. constructor(e, t, r, i) {
  10. super(), this._commandService = e, this._injector = t, this._lexerTreeBuilder = r, this._configService = i, this._initialize();
  11. }
  12. _initialize() {
  13. }
  14. /**
  15. * The tree builder for formula string.
  16. * @type {LexerTreeBuilder}
  17. */
  18. get lexerTreeBuilder() {
  19. return this._lexerTreeBuilder;
  20. }
  21. /**
  22. * Offsets the formula
  23. * @param {string} formulaString
  24. * @param {number} refOffsetX
  25. * @param {number} refOffsetY
  26. * @param {boolean} [ignoreAbsolute] default is false
  27. * @example
  28. * const result = moveFormulaRefOffset('sum(a1,b2)',1,1)
  29. * // result is 'sum(b2,c3)'
  30. */
  31. moveFormulaRefOffset(e, t, r, i) {
  32. return this._lexerTreeBuilder.moveFormulaRefOffset(e, t, r, i);
  33. }
  34. /**
  35. * Resolves the formula string to a 'node' node
  36. * @param {string} formulaString
  37. * @returns {*} {((string | ISequenceNode)[])}
  38. * @memberof FFormula
  39. */
  40. sequenceNodesBuilder(e) {
  41. return this._lexerTreeBuilder.sequenceNodesBuilder(e) || [];
  42. }
  43. /**
  44. * Start the calculation of the formula.
  45. */
  46. executeCalculation() {
  47. this._commandService.executeCommand(l.id, { commands: [], forceCalculation: !0 }, { onlyLocal: !0 });
  48. }
  49. /**
  50. * Stop the calculation of the formula.
  51. */
  52. stopCalculation() {
  53. this._commandService.executeCommand(p.id, {});
  54. }
  55. /**
  56. * Listening calculation starts.
  57. * @param callback
  58. */
  59. calculationStart(e) {
  60. return this._commandService.onCommandExecuted((t) => {
  61. if (t.id === l.id) {
  62. const r = t.params;
  63. e(r.forceCalculation);
  64. }
  65. });
  66. }
  67. /**
  68. * Listening calculation ends.
  69. * @param callback
  70. */
  71. calculationEnd(e) {
  72. return this._commandService.onCommandExecuted((t) => {
  73. if (t.id !== d.id)
  74. return;
  75. const r = t.params;
  76. r.functionsExecutedState !== void 0 && e(r.functionsExecutedState);
  77. });
  78. }
  79. onCalculationEnd() {
  80. return new Promise((e, t) => {
  81. const r = setTimeout(() => {
  82. t(new Error("Calculation end timeout"));
  83. }, 3e4), i = this.calculationEnd(() => {
  84. clearTimeout(r), i.dispose(), e();
  85. });
  86. });
  87. }
  88. /**
  89. * Listening calculation processing.
  90. * @param callback
  91. */
  92. calculationProcessing(e) {
  93. return this._commandService.onCommandExecuted((t) => {
  94. if (t.id !== d.id)
  95. return;
  96. const r = t.params;
  97. r.stageInfo !== void 0 && e(r.stageInfo);
  98. });
  99. }
  100. /**
  101. * When a formula contains a circular reference, set the maximum number of iterations for the formula calculation.
  102. * @param maxIteration The maximum number of iterations. The default value is 1.
  103. * @example
  104. * ```ts
  105. * // Set the maximum number of iterations for the formula calculation to 5.
  106. * // The default value is 1.
  107. * const formulaEngine = univerAPI.getFormula();
  108. * formulaEngine.setMaxIteration(5);
  109. * ```
  110. */
  111. setMaxIteration(e) {
  112. this._configService.setConfig(S, e);
  113. }
  114. };
  115. s = F([
  116. o(0, u(f)),
  117. o(1, u(_)),
  118. o(2, u(x)),
  119. o(3, v)
  120. ], s);
  121. class g extends m {
  122. getFormula() {
  123. return this._injector.createInstance(s);
  124. }
  125. }
  126. m.extend(g);
  127. export {
  128. s as FFormula
  129. };