@univerjs/sheets-numfmt 0.5.4 → 0.5.5-experimental.20250123-34738ff

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/es/index.js CHANGED
@@ -1,11 +1,13 @@
1
- var te = Object.defineProperty;
2
- var ne = (e, t, n) => t in e ? te(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var N = (e, t, n) => ne(e, typeof t != "symbol" ? t + "" : t, n);
4
- import { numfmt as $, CommandType as I, ICommandService as C, IUniverInstanceService as O, IUndoRedoService as re, sequenceExecute as se, Range as _, CellValueType as V, LocaleType as oe, Inject as M, ThemeService as ce, LocaleService as ie, Disposable as q, ObjectMatrix as j, InterceptorEffectEnum as B, isRealNum as ae, UniverInstanceType as z, DependentOn as ue, Injector as le, Plugin as me, registerDependencies as fe, touchDependencies as L } from "@univerjs/core";
5
- import { getSheetCommandTarget as A, transformCellsToRange as de, rangeMerge as G, SetNumfmtMutation as X, factorySetNumfmtUndoMutation as he, RemoveNumfmtMutation as ge, factoryRemoveNumfmtUndoMutation as pe, SheetsSelectionsService as D, INumfmtService as w, SheetInterceptorService as ve, INTERCEPTOR_POINT as Se, InterceptCellContentPriority as ye, SetRangeValuesMutation as _e, UniverSheetsPlugin as Ce } from "@univerjs/sheets";
6
- import { BehaviorSubject as Y, switchMap as be, of as Me, skip as $e } from "rxjs";
7
- import { DEFAULT_TEXT_FORMAT as Ee } from "@univerjs/engine-numfmt";
8
- const J = [
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value;
3
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key != "symbol" ? key + "" : key, value);
5
+ import { numfmt, CommandType, ICommandService, IUniverInstanceService, IUndoRedoService, sequenceExecute, Range, CellValueType, Inject, ThemeService, LocaleService, Disposable, LocaleType, ObjectMatrix, InterceptorEffectEnum, isRealNum, UniverInstanceType, DependentOn, Injector, Plugin, registerDependencies, touchDependencies } from "@univerjs/core";
6
+ import { getSheetCommandTarget, transformCellsToRange, rangeMerge, SetNumfmtMutation, factorySetNumfmtUndoMutation, RemoveNumfmtMutation, factoryRemoveNumfmtUndoMutation, SheetsSelectionsService, INumfmtService, SheetInterceptorService, INTERCEPTOR_POINT, InterceptCellContentPriority, SetRangeValuesMutation, UniverSheetsPlugin } from "@univerjs/sheets";
7
+ import { BehaviorSubject, merge, switchMap, of, skip } from "rxjs";
8
+ import { DEFAULT_TEXT_FORMAT } from "@univerjs/engine-numfmt";
9
+ import { stripErrorMargin } from "@univerjs/engine-formula";
10
+ const currencySymbols = [
9
11
  "$",
10
12
  "£",
11
13
  "¥",
@@ -33,7 +35,7 @@ const J = [
33
35
  "₽",
34
36
  "₾",
35
37
  "₿"
36
- ], Ne = {
38
+ ], countryCurrencyMap = {
37
39
  US: "$",
38
40
  // United States Dollar
39
41
  CA: "C$",
@@ -73,7 +75,7 @@ const J = [
73
75
  SI: "€",
74
76
  ES: "€"
75
77
  // Add more mappings as needed
76
- }, Ie = [
78
+ }, DATEFMTLISG = [
77
79
  {
78
80
  label: "1930-08-05",
79
81
  suffix: "yyyy-MM-dd"
@@ -118,7 +120,7 @@ const J = [
118
120
  label: "08-05 下午 01:30",
119
121
  suffix: "MM-dd A/P hh:mm"
120
122
  }
121
- ], Re = [
123
+ ], NUMBERFORMAT = [
122
124
  {
123
125
  label: "(1,235)",
124
126
  suffix: "#,##0_);(#,##0)"
@@ -146,438 +148,471 @@ const J = [
146
148
  suffix: "#,##0.00_);[Red]-#,##0.00",
147
149
  color: "red"
148
150
  }
149
- ], Z = [
151
+ ], CURRENCYFORMAT = [
150
152
  {
151
- label: (e) => `${e}1,235`,
152
- suffix: (e) => `"${e}"#,##0.00_);"${e}"#,##0.00`
153
+ label: /* @__PURE__ */ __name((suffix) => `${suffix}1,235`, "label"),
154
+ suffix: /* @__PURE__ */ __name((suffix) => `"${suffix}"#,##0.00_);"${suffix}"#,##0.00`, "suffix")
153
155
  },
154
156
  {
155
- label: (e) => `${e}1,235`,
156
- suffix: (e) => `"${e}"#,##0.00_);[Red]"${e}"#,##0.00`,
157
+ label: /* @__PURE__ */ __name((suffix) => `${suffix}1,235`, "label"),
158
+ suffix: /* @__PURE__ */ __name((suffix) => `"${suffix}"#,##0.00_);[Red]"${suffix}"#,##0.00`, "suffix"),
157
159
  color: "red"
158
160
  },
159
161
  {
160
- label: (e) => `(${e}1,235)`,
161
- suffix: (e) => `"${e}"#,##0.00_);("${e}"#,##0.00)`
162
+ label: /* @__PURE__ */ __name((suffix) => `(${suffix}1,235)`, "label"),
163
+ suffix: /* @__PURE__ */ __name((suffix) => `"${suffix}"#,##0.00_);("${suffix}"#,##0.00)`, "suffix")
162
164
  },
163
165
  {
164
- label: (e) => `(${e}1,235)`,
165
- suffix: (e) => `"${e}"#,##0.00_);[Red]("${e}"#,##0.00)`,
166
+ label: /* @__PURE__ */ __name((suffix) => `(${suffix}1,235)`, "label"),
167
+ suffix: /* @__PURE__ */ __name((suffix) => `"${suffix}"#,##0.00_);[Red]("${suffix}"#,##0.00)`, "suffix"),
166
168
  color: "red"
167
169
  },
168
170
  {
169
- label: (e) => `-${e}1,235`,
170
- suffix: (e) => `"${e}"#,##0.00_);-"${e}"#,##0.00`
171
+ label: /* @__PURE__ */ __name((suffix) => `-${suffix}1,235`, "label"),
172
+ suffix: /* @__PURE__ */ __name((suffix) => `"${suffix}"#,##0.00_);-"${suffix}"#,##0.00`, "suffix")
171
173
  },
172
174
  {
173
- label: (e) => `-${e}1,235`,
174
- suffix: (e) => `"${e}"#,##0.00_);[Red]-"${e}"#,##0.00`,
175
+ label: /* @__PURE__ */ __name((suffix) => `-${suffix}1,235`, "label"),
176
+ suffix: /* @__PURE__ */ __name((suffix) => `"${suffix}"#,##0.00_);[Red]-"${suffix}"#,##0.00`, "suffix"),
175
177
  color: "red"
176
178
  }
177
- ], Te = "SHEET_NUMFMT_PLUGIN", T = (e, t = 0) => {
178
- var r;
179
- return e && (r = $.getInfo(e).maxDecimals) != null ? r : t;
180
- }, ze = (e, t) => {
181
- if (e && !t || !e && t)
179
+ ], SHEET_NUMFMT_PLUGIN = "SHEET_NUMFMT_PLUGIN", getDecimalFromPattern = /* @__PURE__ */ __name((pattern, defaultValue = 0) => {
180
+ var _a3;
181
+ return pattern && (_a3 = numfmt.getInfo(pattern).maxDecimals) != null ? _a3 : defaultValue;
182
+ }, "getDecimalFromPattern"), isPatternEqualWithoutDecimal = /* @__PURE__ */ __name((patternA, patternB) => {
183
+ if (patternA && !patternB || !patternA && patternB)
182
184
  return !1;
183
- const n = (i) => i.reduce(
184
- (m, d) => {
185
- if (m.isEnd)
186
- return m;
187
- const f = d.value || d.num;
188
- return d.type === "point" ? (m.isEnd = !0, m) : { ...m, result: m.result + f };
185
+ const getString = /* @__PURE__ */ __name((tokens) => tokens.reduce(
186
+ (pre, cur) => {
187
+ if (pre.isEnd)
188
+ return pre;
189
+ const str = cur.value || cur.num;
190
+ return cur.type === "point" ? (pre.isEnd = !0, pre) : { ...pre, result: pre.result + str };
189
191
  },
190
192
  { isEnd: !1, result: "" }
191
- ).result, r = $.getInfo(e)._partitions, s = $.getInfo(t)._partitions, o = n(r[0].tokens), c = n(s[0].tokens), u = n(r[1].tokens), l = n(s[1].tokens);
192
- return o === c && u === l && r[1].color === s[1].color;
193
- }, H = (e) => new Array(Math.min(Math.max(0, Number(e)), 30)).fill(0).join(""), x = (e, t) => e.split(";").map((r) => /\.0?/.test(r) ? r.replace(
193
+ ).result, "getString"), partitionsA = numfmt.getInfo(patternA)._partitions, partitionsB = numfmt.getInfo(patternB)._partitions, A1 = getString(partitionsA[0].tokens), B1 = getString(partitionsB[0].tokens), A2 = getString(partitionsA[1].tokens), B2 = getString(partitionsB[1].tokens);
194
+ return A1 === B1 && A2 === B2 && partitionsA[1].color === partitionsB[1].color;
195
+ }, "isPatternEqualWithoutDecimal"), getDecimalString = /* @__PURE__ */ __name((length) => new Array(Math.min(Math.max(0, Number(length)), 30)).fill(0).join(""), "getDecimalString"), setPatternDecimal = /* @__PURE__ */ __name((patterns, decimalLength) => patterns.split(";").map((pattern) => /\.0?/.test(pattern) ? pattern.replace(
194
196
  /\.0*/g,
195
- `${t > 0 ? "." : ""}${H(Number(t || 0))}`
196
- ) : /0([^0]?)|0$/.test(r) ? r.replace(
197
+ `${decimalLength > 0 ? "." : ""}${getDecimalString(Number(decimalLength || 0))}`
198
+ ) : /0([^0]?)|0$/.test(pattern) ? pattern.replace(
197
199
  /0([^0]+)|0$/,
198
- `0${t > 0 ? "." : ""}${H(Number(t || 0))}$1`
199
- ) : r).join(";"), Xe = (e) => /\.0?/.test(e) || /0([^0]?)|0$/.test(e), R = {
200
+ `0${decimalLength > 0 ? "." : ""}${getDecimalString(Number(decimalLength || 0))}$1`
201
+ ) : pattern).join(";"), "setPatternDecimal"), isPatternHasDecimal = /* @__PURE__ */ __name((pattern) => /\.0?/.test(pattern) || /0([^0]?)|0$/.test(pattern), "isPatternHasDecimal"), SetNumfmtCommand = {
200
202
  id: "sheet.command.numfmt.set.numfmt",
201
- type: I.COMMAND,
202
- handler: (e, t) => {
203
- if (!t)
203
+ type: CommandType.COMMAND,
204
+ handler: /* @__PURE__ */ __name((accessor, params) => {
205
+ if (!params)
204
206
  return !1;
205
- const n = e.get(C), r = e.get(O), s = e.get(re), o = A(r, t);
206
- if (!o) return !1;
207
- const { unitId: c, subUnitId: u } = o, l = t.values.filter((a) => !!a.pattern), i = t.values.filter((a) => !a.pattern), m = de(c, u, l), d = {
208
- unitId: c,
209
- subUnitId: u,
210
- ranges: i.map((a) => ({
211
- startColumn: a.col,
212
- startRow: a.row,
213
- endColumn: a.col,
214
- endRow: a.row
207
+ const commandService = accessor.get(ICommandService), univerInstanceService = accessor.get(IUniverInstanceService), undoRedoService = accessor.get(IUndoRedoService), target = getSheetCommandTarget(univerInstanceService, params);
208
+ if (!target) return !1;
209
+ const { unitId, subUnitId } = target, setCells = params.values.filter((value) => !!value.pattern), removeCells = params.values.filter((value) => !value.pattern), setRedos = transformCellsToRange(unitId, subUnitId, setCells), removeRedos = {
210
+ unitId,
211
+ subUnitId,
212
+ ranges: removeCells.map((cell) => ({
213
+ startColumn: cell.col,
214
+ startRow: cell.row,
215
+ endColumn: cell.col,
216
+ endRow: cell.row
215
217
  }))
216
- }, f = [], h = [];
217
- if (l.length) {
218
- Object.keys(m.values).forEach((g) => {
219
- const p = m.values[g];
220
- p.ranges = G(p.ranges);
221
- }), f.push({
222
- id: X.id,
223
- params: m
218
+ }, redos = [], undos = [];
219
+ if (setCells.length) {
220
+ Object.keys(setRedos.values).forEach((key) => {
221
+ const v = setRedos.values[key];
222
+ v.ranges = rangeMerge(v.ranges);
223
+ }), redos.push({
224
+ id: SetNumfmtMutation.id,
225
+ params: setRedos
224
226
  });
225
- const a = he(e, m);
226
- h.push(...a);
227
+ const undo = factorySetNumfmtUndoMutation(accessor, setRedos);
228
+ undos.push(...undo);
227
229
  }
228
- if (i.length) {
229
- d.ranges = G(d.ranges), f.push({
230
- id: ge.id,
231
- params: d
230
+ if (removeCells.length) {
231
+ removeRedos.ranges = rangeMerge(removeRedos.ranges), redos.push({
232
+ id: RemoveNumfmtMutation.id,
233
+ params: removeRedos
232
234
  });
233
- const a = pe(e, d);
234
- h.push(...a);
235
+ const undo = factoryRemoveNumfmtUndoMutation(accessor, removeRedos);
236
+ undos.push(...undo);
235
237
  }
236
- const v = se(f, n).result;
237
- return v && s.pushUndoRedo({
238
- unitID: c,
239
- undoMutations: h,
240
- redoMutations: f
241
- }), v;
242
- }
243
- }, xe = {
238
+ const result = sequenceExecute(redos, commandService).result;
239
+ return result && undoRedoService.pushUndoRedo({
240
+ unitID: unitId,
241
+ undoMutations: undos,
242
+ redoMutations: redos
243
+ }), result;
244
+ }, "handler")
245
+ }, AddDecimalCommand = {
244
246
  id: "sheet.command.numfmt.add.decimal.command",
245
- type: I.COMMAND,
246
- handler: async (e) => {
247
- const t = e.get(C), n = e.get(D), r = e.get(w), s = e.get(O), o = n.getCurrentSelections();
248
- if (!o || !o.length)
247
+ type: CommandType.COMMAND,
248
+ handler: /* @__PURE__ */ __name(async (accessor) => {
249
+ const commandService = accessor.get(ICommandService), selectionManagerService = accessor.get(SheetsSelectionsService), numfmtService = accessor.get(INumfmtService), univerInstanceService = accessor.get(IUniverInstanceService), selections = selectionManagerService.getCurrentSelections();
250
+ if (!selections || !selections.length)
249
251
  return !1;
250
- const c = A(s);
251
- if (!c) return !1;
252
- const { unitId: u, subUnitId: l } = c;
253
- let i = 0;
254
- o.forEach((h) => {
255
- _.foreach(h.range, (v, a) => {
256
- const g = r.getValue(u, l, v, a);
257
- if (!g) {
258
- const S = c.worksheet.getCellRaw(v, a);
259
- if (!i && S && S.t === V.NUMBER && S.v) {
260
- const y = /\.(\d*)$/.exec(String(S.v));
261
- if (y) {
262
- const E = y[1].length;
263
- if (!E)
252
+ const target = getSheetCommandTarget(univerInstanceService);
253
+ if (!target) return !1;
254
+ const { unitId, subUnitId } = target;
255
+ let maxDecimals = 0;
256
+ selections.forEach((selection) => {
257
+ Range.foreach(selection.range, (row, col) => {
258
+ const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
259
+ if (!numfmtValue) {
260
+ const cell = target.worksheet.getCellRaw(row, col);
261
+ if (!maxDecimals && cell && cell.t === CellValueType.NUMBER && cell.v) {
262
+ const regResult = /\.(\d*)$/.exec(String(cell.v));
263
+ if (regResult) {
264
+ const length = regResult[1].length;
265
+ if (!length)
264
266
  return;
265
- i = Math.max(i, E);
267
+ maxDecimals = Math.max(maxDecimals, length);
266
268
  }
267
269
  }
268
270
  return;
269
271
  }
270
- const p = T(g.pattern);
271
- i = p > i ? p : i;
272
+ const decimals2 = getDecimalFromPattern(numfmtValue.pattern);
273
+ maxDecimals = decimals2 > maxDecimals ? decimals2 : maxDecimals;
272
274
  });
273
275
  });
274
- const m = i + 1, d = x(`0${m > 0 ? ".0" : ""}`, m), f = [];
275
- return o.forEach((h) => {
276
- _.foreach(h.range, (v, a) => {
277
- const g = r.getValue(u, l, v, a);
278
- if (!g)
279
- f.push({
280
- row: v,
281
- col: a,
282
- pattern: d
276
+ const decimals = maxDecimals + 1, defaultPattern = setPatternDecimal(`0${decimals > 0 ? ".0" : ""}`, decimals), values = [];
277
+ return selections.forEach((selection) => {
278
+ Range.foreach(selection.range, (row, col) => {
279
+ const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
280
+ if (!numfmtValue)
281
+ values.push({
282
+ row,
283
+ col,
284
+ pattern: defaultPattern
283
285
  });
284
286
  else {
285
- const p = T(g.pattern), S = x(g.pattern, p + 1);
286
- S !== g.pattern && f.push({
287
- row: v,
288
- col: a,
289
- pattern: S
287
+ const decimals2 = getDecimalFromPattern(numfmtValue.pattern), pattern = setPatternDecimal(numfmtValue.pattern, decimals2 + 1);
288
+ pattern !== numfmtValue.pattern && values.push({
289
+ row,
290
+ col,
291
+ pattern
290
292
  });
291
293
  }
292
294
  });
293
- }), f.length ? await t.executeCommand(R.id, { values: f }) : !1;
294
- }
295
- };
296
- class Q {
295
+ }), values.length ? await commandService.executeCommand(SetNumfmtCommand.id, { values }) : !1;
296
+ }, "handler")
297
+ }, _MenuCurrencyService = class _MenuCurrencyService {
297
298
  constructor() {
298
- N(this, "_currencySymbol$", new Y("US"));
299
- N(this, "currencySymbol$", this._currencySymbol$.asObservable());
299
+ __publicField(this, "_currencySymbol$", new BehaviorSubject("US"));
300
+ __publicField(this, "currencySymbol$", this._currencySymbol$.asObservable());
300
301
  }
301
302
  /**
302
303
  * Set the currency symbol by setting the country code.
303
304
  */
304
- setCurrencySymbolByCountryCode(t) {
305
- this._currencySymbol$.next(t);
305
+ setCurrencySymbolByCountryCode(symbol) {
306
+ this._currencySymbol$.next(symbol);
306
307
  }
307
308
  getCurrencySymbol() {
308
309
  return this._currencySymbol$.getValue();
309
310
  }
310
- }
311
- const Pe = {
311
+ };
312
+ __name(_MenuCurrencyService, "MenuCurrencyService");
313
+ let MenuCurrencyService = _MenuCurrencyService;
314
+ const SetCurrencyCommand = {
312
315
  id: "sheet.command.numfmt.set.currency",
313
- type: I.COMMAND,
314
- handler: async (e) => {
315
- const t = e.get(C), n = e.get(D), r = e.get(Q), s = Ne[r.getCurrencySymbol()] || "$", o = n.getCurrentSelections();
316
- if (!o || !o.length)
316
+ type: CommandType.COMMAND,
317
+ handler: /* @__PURE__ */ __name(async (accessor) => {
318
+ const commandService = accessor.get(ICommandService), selectionManagerService = accessor.get(SheetsSelectionsService), menuCurrencyService = accessor.get(MenuCurrencyService), symbol = countryCurrencyMap[menuCurrencyService.getCurrencySymbol()] || "$", selections = selectionManagerService.getCurrentSelections();
319
+ if (!selections || !selections.length)
317
320
  return !1;
318
- const c = [], u = Z[4].suffix(s);
319
- return o.forEach((i) => {
320
- _.foreach(i.range, (m, d) => {
321
- c.push({ row: m, col: d, pattern: u, type: "currency" });
321
+ const values = [], suffix = CURRENCYFORMAT[4].suffix(symbol);
322
+ return selections.forEach((selection) => {
323
+ Range.foreach(selection.range, (row, col) => {
324
+ values.push({ row, col, pattern: suffix, type: "currency" });
322
325
  });
323
- }), await t.executeCommand(R.id, { values: c });
324
- }
325
- }, Ue = {
326
+ }), await commandService.executeCommand(SetNumfmtCommand.id, { values });
327
+ }, "handler")
328
+ }, SetPercentCommand = {
326
329
  id: "sheet.command.numfmt.set.percent",
327
- type: I.COMMAND,
328
- handler: async (e) => {
329
- const t = e.get(C), r = e.get(D).getCurrentSelections();
330
- if (!r || !r.length)
330
+ type: CommandType.COMMAND,
331
+ handler: /* @__PURE__ */ __name(async (accessor) => {
332
+ const commandService = accessor.get(ICommandService), selections = accessor.get(SheetsSelectionsService).getCurrentSelections();
333
+ if (!selections || !selections.length)
331
334
  return !1;
332
- const s = [], o = "0%";
333
- return r.forEach((u) => {
334
- _.foreach(u.range, (l, i) => {
335
- s.push({ row: l, col: i, pattern: o, type: "percent" });
335
+ const values = [], suffix = "0%";
336
+ return selections.forEach((selection) => {
337
+ Range.foreach(selection.range, (row, col) => {
338
+ values.push({ row, col, pattern: suffix, type: "percent" });
336
339
  });
337
- }), await t.executeCommand(R.id, { values: s });
338
- }
339
- }, Oe = {
340
+ }), await commandService.executeCommand(SetNumfmtCommand.id, { values });
341
+ }, "handler")
342
+ }, SubtractDecimalCommand = {
340
343
  id: "sheet.command.numfmt.subtract.decimal.command",
341
- type: I.COMMAND,
342
- handler: async (e) => {
343
- const t = e.get(C), n = e.get(D), r = e.get(w), s = e.get(O), o = n.getCurrentSelections();
344
- if (!o || !o.length)
344
+ type: CommandType.COMMAND,
345
+ handler: /* @__PURE__ */ __name(async (accessor) => {
346
+ const commandService = accessor.get(ICommandService), selectionManagerService = accessor.get(SheetsSelectionsService), numfmtService = accessor.get(INumfmtService), univerInstanceService = accessor.get(IUniverInstanceService), selections = selectionManagerService.getCurrentSelections();
347
+ if (!selections || !selections.length)
345
348
  return !1;
346
- const c = A(s);
347
- if (!c) return !1;
348
- const { unitId: u, subUnitId: l } = c;
349
- let i = 0;
350
- o.forEach((v) => {
351
- _.foreach(v.range, (a, g) => {
352
- const p = r.getValue(u, l, a, g);
353
- if (!p) {
354
- const y = c.worksheet.getCellRaw(a, g);
355
- if (!i && y && y.t === V.NUMBER && y.v) {
356
- const E = /\.(\d*)$/.exec(String(y.v));
357
- if (E) {
358
- const F = E[1].length;
359
- if (!F)
349
+ const target = getSheetCommandTarget(univerInstanceService);
350
+ if (!target) return !1;
351
+ const { unitId, subUnitId } = target;
352
+ let maxDecimals = 0;
353
+ selections.forEach((selection) => {
354
+ Range.foreach(selection.range, (row, col) => {
355
+ const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
356
+ if (!numfmtValue) {
357
+ const cell = target.worksheet.getCellRaw(row, col);
358
+ if (!maxDecimals && cell && cell.t === CellValueType.NUMBER && cell.v) {
359
+ const regResult = /\.(\d*)$/.exec(String(cell.v));
360
+ if (regResult) {
361
+ const length = regResult[1].length;
362
+ if (!length)
360
363
  return;
361
- i = Math.max(i, F);
364
+ maxDecimals = Math.max(maxDecimals, length);
362
365
  }
363
366
  }
364
367
  return;
365
368
  }
366
- const S = T(p.pattern);
367
- i = S > i ? S : i;
369
+ const decimals2 = getDecimalFromPattern(numfmtValue.pattern);
370
+ maxDecimals = decimals2 > maxDecimals ? decimals2 : maxDecimals;
368
371
  });
369
372
  });
370
- const m = i - 1, d = x(`0${m > 0 ? ".0" : "."}`, m), f = [];
371
- return o.forEach((v) => {
372
- _.foreach(v.range, (a, g) => {
373
- const p = r.getValue(u, l, a, g);
374
- if (!p)
375
- f.push({
376
- row: a,
377
- col: g,
378
- pattern: d
373
+ const decimals = maxDecimals - 1, defaultPattern = setPatternDecimal(`0${decimals > 0 ? ".0" : "."}`, decimals), values = [];
374
+ return selections.forEach((selection) => {
375
+ Range.foreach(selection.range, (row, col) => {
376
+ const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
377
+ if (!numfmtValue)
378
+ values.push({
379
+ row,
380
+ col,
381
+ pattern: defaultPattern
379
382
  });
380
383
  else {
381
- const S = T(p.pattern);
382
- f.push({
383
- row: a,
384
- col: g,
385
- pattern: x(p.pattern, S - 1)
384
+ const decimals2 = getDecimalFromPattern(numfmtValue.pattern);
385
+ values.push({
386
+ row,
387
+ col,
388
+ pattern: setPatternDecimal(numfmtValue.pattern, decimals2 - 1)
386
389
  });
387
390
  }
388
391
  });
389
- }), await t.executeCommand(R.id, { values: f });
390
- }
391
- }, Ye = (e) => $.getInfo(e).type || "unknown", De = (e, t, n) => {
392
- const r = $.getInfo(e), s = n === oe.ZH_CN ? "zh-CN" : "en", o = r._partitions[1], c = $.format(e, t, { locale: s, throws: !1 });
393
- return t < 0 ? {
394
- result: c,
395
- color: o.color
392
+ }), await commandService.executeCommand(SetNumfmtCommand.id, { values });
393
+ }, "handler")
394
+ }, getPatternType = /* @__PURE__ */ __name((pattern) => numfmt.getInfo(pattern).type || "unknown", "getPatternType"), getPatternPreview = /* @__PURE__ */ __name((pattern, value, locale = "en") => {
395
+ const negInfo = numfmt.getInfo(pattern)._partitions[1], result = numfmt.format(pattern, value, { locale, throws: !1 });
396
+ return value < 0 ? {
397
+ result,
398
+ color: negInfo.color
396
399
  } : {
397
- result: c
400
+ result
398
401
  };
399
- }, Ve = (e, t, n) => e === "General" ? {
400
- result: String(t)
401
- } : De(e, t, n);
402
- var Ae = Object.defineProperty, we = Object.getOwnPropertyDescriptor, Fe = (e, t, n, r) => {
403
- for (var s = r > 1 ? void 0 : r ? we(t, n) : t, o = e.length - 1, c; o >= 0; o--)
404
- (c = e[o]) && (s = (r ? c(t, n, s) : c(s)) || s);
405
- return r && s && Ae(t, n, s), s;
406
- }, b = (e, t) => (n, r) => t(n, r, e);
407
- let P = class extends q {
408
- constructor(e, t, n, r, s, o) {
409
- super(), this._instanceService = e, this._sheetInterceptorService = t, this._themeService = n, this._commandService = r, this._numfmtService = s, this._localeService = o, this._initInterceptorCellContent();
402
+ }, "getPatternPreview"), getPatternPreviewIgnoreGeneral = /* @__PURE__ */ __name((pattern, value, locale) => pattern === "General" ? {
403
+ result: String(stripErrorMargin(value))
404
+ // In Excel, the default General format also needs to handle numeric precision.
405
+ } : getPatternPreview(pattern, value, locale), "getPatternPreviewIgnoreGeneral");
406
+ var __defProp$1 = Object.defineProperty, __getOwnPropDesc$1 = Object.getOwnPropertyDescriptor, __decorateClass$1 = /* @__PURE__ */ __name((decorators, target, key, kind) => {
407
+ for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc$1(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
408
+ (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
409
+ return kind && result && __defProp$1(target, key, result), result;
410
+ }, "__decorateClass$1"), __decorateParam$1 = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam$1"), _a;
411
+ let SheetsNumfmtCellContentController = (_a = class extends Disposable {
412
+ constructor(_instanceService, _sheetInterceptorService, _themeService, _commandService, _numfmtService, _localeService) {
413
+ super();
414
+ __publicField(this, "_local$", new BehaviorSubject("en"));
415
+ __publicField(this, "local$", this._local$.asObservable());
416
+ this._instanceService = _instanceService, this._sheetInterceptorService = _sheetInterceptorService, this._themeService = _themeService, this._commandService = _commandService, this._numfmtService = _numfmtService, this._localeService = _localeService, this._initInterceptorCellContent();
417
+ }
418
+ get local() {
419
+ const _local = this._local$.getValue();
420
+ if (_local)
421
+ return _local;
422
+ switch (this._localeService.getCurrentLocale()) {
423
+ case LocaleType.FR_FR:
424
+ return "fr";
425
+ case LocaleType.RU_RU:
426
+ return "ru";
427
+ case LocaleType.VI_VN:
428
+ return "vi";
429
+ case LocaleType.ZH_CN:
430
+ return "zh-CN";
431
+ case LocaleType.ZH_TW:
432
+ return "zh-TW";
433
+ case LocaleType.EN_US:
434
+ case LocaleType.FA_IR:
435
+ default:
436
+ return "en";
437
+ }
410
438
  }
411
439
  // eslint-disable-next-line max-lines-per-function
412
440
  _initInterceptorCellContent() {
413
- const e = {
441
+ const TEXT_FORMAT_MARK = {
414
442
  tl: {
415
443
  size: 6,
416
444
  color: "#409f11"
417
445
  }
418
- }, t = new j();
419
- this.disposeWithMe(this._sheetInterceptorService.intercept(Se.CELL_CONTENT, {
420
- effect: B.Value | B.Style,
421
- handler: (n, r, s) => {
422
- const o = r.unitId, c = r.subUnitId;
423
- let u;
424
- const l = n;
425
- if (!l)
426
- return s(n);
427
- if (n != null && n.s) {
428
- const h = r.workbook.getStyles().get(n.s);
429
- h != null && h.n && (u = h.n);
446
+ }, renderCache = new ObjectMatrix();
447
+ this.disposeWithMe(merge(this._local$, this._localeService.currentLocale$).subscribe(() => {
448
+ renderCache.reset();
449
+ })), this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
450
+ effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
451
+ handler: /* @__PURE__ */ __name((cell, location, next) => {
452
+ const unitId = location.unitId, sheetId = location.subUnitId;
453
+ let numfmtValue;
454
+ const originCellValue = cell;
455
+ if (!originCellValue)
456
+ return next(cell);
457
+ if (cell != null && cell.s) {
458
+ const style = location.workbook.getStyles().get(cell.s);
459
+ style != null && style.n && (numfmtValue = style.n);
430
460
  }
431
- if (u || (u = this._numfmtService.getValue(o, c, r.row, r.col)), !u)
432
- return s(n);
433
- if (u.pattern === Ee && l.v && ae(l.v))
434
- return s({
435
- ...n,
461
+ if (numfmtValue || (numfmtValue = this._numfmtService.getValue(unitId, sheetId, location.row, location.col)), !numfmtValue)
462
+ return next(cell);
463
+ if (numfmtValue.pattern === DEFAULT_TEXT_FORMAT && originCellValue.v && isRealNum(originCellValue.v))
464
+ return next({
465
+ ...cell,
436
466
  markers: {
437
- ...n == null ? void 0 : n.markers,
438
- ...e
467
+ ...cell == null ? void 0 : cell.markers,
468
+ ...TEXT_FORMAT_MARK
439
469
  }
440
470
  });
441
- if (l.t !== V.NUMBER || l.v == null || Number.isNaN(l.v))
442
- return s(n);
443
- let i = "";
444
- const m = t.getValue(r.row, r.col);
445
- if (m && m.parameters === `${l.v}_${u.pattern}`)
446
- return s({ ...n, ...m.result });
447
- const d = Ve(u.pattern, Number(l.v), this._localeService.getCurrentLocale());
448
- if (i = d.result, !i)
449
- return s(n);
450
- const f = { v: i };
451
- if (d.color) {
452
- const h = this._themeService.getCurrentTheme()[`${d.color}500`];
453
- h && (f.interceptorStyle = { cl: { rgb: h } });
471
+ if (originCellValue.t !== CellValueType.NUMBER || originCellValue.v == null || Number.isNaN(originCellValue.v))
472
+ return next(cell);
473
+ let numfmtRes = "";
474
+ const cache = renderCache.getValue(location.row, location.col);
475
+ if (cache && cache.parameters === `${originCellValue.v}_${numfmtValue.pattern}`)
476
+ return next({ ...cell, ...cache.result });
477
+ const info = getPatternPreviewIgnoreGeneral(numfmtValue.pattern, Number(originCellValue.v), this.local);
478
+ if (numfmtRes = info.result, !numfmtRes)
479
+ return next(cell);
480
+ const res = { v: numfmtRes };
481
+ if (info.color) {
482
+ const color = this._themeService.getCurrentTheme()[`${info.color}500`];
483
+ color && (res.interceptorStyle = { cl: { rgb: color } });
454
484
  }
455
- return t.setValue(r.row, r.col, {
456
- result: f,
457
- parameters: `${l.v}_${u.pattern}`
458
- }), s({ ...n, ...f });
459
- },
460
- priority: ye.NUMFMT
461
- })), this.disposeWithMe(this._commandService.onCommandExecuted((n) => {
462
- if (n.id === X.id) {
463
- const r = n.params;
464
- Object.keys(r.values).forEach((s) => {
465
- r.values[s].ranges.forEach((c) => {
466
- _.foreach(c, (u, l) => {
467
- t.realDeleteValue(u, l);
485
+ return renderCache.setValue(location.row, location.col, {
486
+ result: res,
487
+ parameters: `${originCellValue.v}_${numfmtValue.pattern}`
488
+ }), next({ ...cell, ...res });
489
+ }, "handler"),
490
+ priority: InterceptCellContentPriority.NUMFMT
491
+ })), this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
492
+ if (commandInfo.id === SetNumfmtMutation.id) {
493
+ const params = commandInfo.params;
494
+ Object.keys(params.values).forEach((key) => {
495
+ params.values[key].ranges.forEach((range) => {
496
+ Range.foreach(range, (row, col) => {
497
+ renderCache.realDeleteValue(row, col);
468
498
  });
469
499
  });
470
500
  });
471
- } else if (n.id === _e.id) {
472
- const r = n.params;
473
- new j(r.cellValue).forValue((s, o) => {
474
- t.realDeleteValue(s, o);
501
+ } else if (commandInfo.id === SetRangeValuesMutation.id) {
502
+ const params = commandInfo.params;
503
+ new ObjectMatrix(params.cellValue).forValue((row, col) => {
504
+ renderCache.realDeleteValue(row, col);
475
505
  });
476
506
  }
477
507
  })), this.disposeWithMe(
478
- this._instanceService.getCurrentTypeOfUnit$(z.UNIVER_SHEET).pipe(
479
- be((n) => {
480
- var r;
481
- return (r = n == null ? void 0 : n.activeSheet$) != null ? r : Me(null);
508
+ this._instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SHEET).pipe(
509
+ switchMap((workbook) => {
510
+ var _a3;
511
+ return (_a3 = workbook == null ? void 0 : workbook.activeSheet$) != null ? _a3 : of(null);
482
512
  }),
483
- $e(1)
484
- ).subscribe(() => t.reset())
513
+ skip(1)
514
+ ).subscribe(() => renderCache.reset())
485
515
  );
486
516
  }
487
- };
488
- P = Fe([
489
- b(0, O),
490
- b(1, M(ve)),
491
- b(2, M(ce)),
492
- b(3, M(C)),
493
- b(4, M(w)),
494
- b(5, M(ie))
495
- ], P);
496
- class W extends q {
517
+ setNumfmtLocal(local) {
518
+ this._local$.next(local);
519
+ }
520
+ }, __name(_a, "SheetsNumfmtCellContentController"), _a);
521
+ SheetsNumfmtCellContentController = __decorateClass$1([
522
+ __decorateParam$1(0, IUniverInstanceService),
523
+ __decorateParam$1(1, Inject(SheetInterceptorService)),
524
+ __decorateParam$1(2, Inject(ThemeService)),
525
+ __decorateParam$1(3, Inject(ICommandService)),
526
+ __decorateParam$1(4, Inject(INumfmtService)),
527
+ __decorateParam$1(5, Inject(LocaleService))
528
+ ], SheetsNumfmtCellContentController);
529
+ const _NumfmtCurrencyController = class _NumfmtCurrencyController extends Disposable {
497
530
  constructor() {
498
531
  super(...arguments);
499
- N(this, "_currencySymbol$", new Y("US"));
500
- N(this, "currencySymbol$", this._currencySymbol$.asObservable());
532
+ __publicField(this, "_currencySymbol$", new BehaviorSubject("US"));
533
+ __publicField(this, "currencySymbol$", this._currencySymbol$.asObservable());
501
534
  }
502
535
  /**
503
536
  * Set the currency symbol by setting the country code.
504
537
  */
505
- setCurrencySymbolByCountryCode(n) {
506
- this._currencySymbol$.next(n);
538
+ setCurrencySymbolByCountryCode(symbol) {
539
+ this._currencySymbol$.next(symbol);
507
540
  }
508
541
  getCurrencySymbol() {
509
542
  return this._currencySymbol$.getValue();
510
543
  }
511
- }
512
- var k = Object.defineProperty, je = Object.getOwnPropertyDescriptor, Be = (e, t, n) => t in e ? k(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n, Le = (e, t, n, r) => {
513
- for (var s = r > 1 ? void 0 : r ? je(t, n) : t, o = e.length - 1, c; o >= 0; o--)
514
- (c = e[o]) && (s = (r ? c(t, n, s) : c(s)) || s);
515
- return r && s && k(t, n, s), s;
516
- }, K = (e, t) => (n, r) => t(n, r, e), ee = (e, t, n) => Be(e, typeof t != "symbol" ? t + "" : t, n);
517
- let U = class extends me {
518
- constructor(e = void 0, t, n) {
519
- super(), this._config = e, this._injector = t, this._commandService = n;
544
+ };
545
+ __name(_NumfmtCurrencyController, "NumfmtCurrencyController");
546
+ let NumfmtCurrencyController = _NumfmtCurrencyController;
547
+ var __defProp2 = Object.defineProperty, __getOwnPropDesc = Object.getOwnPropertyDescriptor, __defNormalProp2 = /* @__PURE__ */ __name((obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: !0, configurable: !0, writable: !0, value }) : obj[key] = value, "__defNormalProp"), __decorateClass = /* @__PURE__ */ __name((decorators, target, key, kind) => {
548
+ for (var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target, i = decorators.length - 1, decorator; i >= 0; i--)
549
+ (decorator = decorators[i]) && (result = (kind ? decorator(target, key, result) : decorator(result)) || result);
550
+ return kind && result && __defProp2(target, key, result), result;
551
+ }, "__decorateClass"), __decorateParam = /* @__PURE__ */ __name((index, decorator) => (target, key) => decorator(target, key, index), "__decorateParam"), __publicField2 = /* @__PURE__ */ __name((obj, key, value) => __defNormalProp2(obj, typeof key != "symbol" ? key + "" : key, value), "__publicField"), _a2;
552
+ let UniverSheetsNumfmtPlugin = (_a2 = class extends Plugin {
553
+ constructor(_config = void 0, _injector, _commandService) {
554
+ super(), this._config = _config, this._injector = _injector, this._commandService = _commandService;
520
555
  }
521
556
  onStarting() {
522
- fe(this._injector, [
523
- [P],
524
- [Q],
525
- [W]
526
- ]), L(this._injector, [
527
- [P]
557
+ registerDependencies(this._injector, [
558
+ [SheetsNumfmtCellContentController],
559
+ [MenuCurrencyService],
560
+ [NumfmtCurrencyController]
561
+ ]), touchDependencies(this._injector, [
562
+ [SheetsNumfmtCellContentController]
528
563
  ]);
529
564
  }
530
565
  onRendered() {
531
- L(this._injector, [
532
- [W]
566
+ touchDependencies(this._injector, [
567
+ [NumfmtCurrencyController]
533
568
  ]), [
534
- xe,
535
- Oe,
536
- Pe,
537
- Ue,
538
- R
539
- ].forEach((e) => {
540
- this.disposeWithMe(this._commandService.registerCommand(e));
569
+ AddDecimalCommand,
570
+ SubtractDecimalCommand,
571
+ SetCurrencyCommand,
572
+ SetPercentCommand,
573
+ SetNumfmtCommand
574
+ ].forEach((config) => {
575
+ this.disposeWithMe(this._commandService.registerCommand(config));
541
576
  });
542
577
  }
543
- };
544
- ee(U, "pluginName", Te);
545
- ee(U, "type", z.UNIVER_SHEET);
546
- U = Le([
547
- ue(Ce),
548
- K(1, M(le)),
549
- K(2, C)
550
- ], U);
551
- const Je = (e) => J.find((n) => e.includes(n)), Ze = () => J.map((e) => ({ label: e, value: e })), Qe = (e) => Z.map((t) => ({
552
- label: t.label(e),
553
- value: t.suffix(e),
554
- color: t.color
555
- })), ke = () => Ie.map((e) => ({ label: e.label, value: e.suffix })), et = () => Re.map((e) => ({ label: e.label, value: e.suffix, color: e.color }));
578
+ }, __name(_a2, "UniverSheetsNumfmtPlugin"), _a2);
579
+ __publicField2(UniverSheetsNumfmtPlugin, "pluginName", SHEET_NUMFMT_PLUGIN);
580
+ __publicField2(UniverSheetsNumfmtPlugin, "type", UniverInstanceType.UNIVER_SHEET);
581
+ UniverSheetsNumfmtPlugin = __decorateClass([
582
+ DependentOn(UniverSheetsPlugin),
583
+ __decorateParam(1, Inject(Injector)),
584
+ __decorateParam(2, ICommandService)
585
+ ], UniverSheetsNumfmtPlugin);
586
+ const getCurrencyType = /* @__PURE__ */ __name((pattern) => currencySymbols.find((code) => pattern.includes(code)), "getCurrencyType"), getCurrencyOptions = /* @__PURE__ */ __name(() => currencySymbols.map((item) => ({ label: item, value: item })), "getCurrencyOptions"), getCurrencyFormatOptions = /* @__PURE__ */ __name((suffix) => CURRENCYFORMAT.map((item) => ({
587
+ label: item.label(suffix),
588
+ value: item.suffix(suffix),
589
+ color: item.color
590
+ })), "getCurrencyFormatOptions"), getDateFormatOptions = /* @__PURE__ */ __name(() => DATEFMTLISG.map((item) => ({ label: item.label, value: item.suffix })), "getDateFormatOptions"), getNumberFormatOptions = /* @__PURE__ */ __name(() => NUMBERFORMAT.map((item) => ({ label: item.label, value: item.suffix, color: item.color })), "getNumberFormatOptions");
556
591
  export {
557
- xe as AddDecimalCommand,
558
- Z as CURRENCYFORMAT,
559
- Ie as DATEFMTLISG,
560
- Q as MenuCurrencyService,
561
- Re as NUMBERFORMAT,
562
- Pe as SetCurrencyCommand,
563
- R as SetNumfmtCommand,
564
- Ue as SetPercentCommand,
565
- P as SheetsNumfmtCellContentController,
566
- Oe as SubtractDecimalCommand,
567
- U as UniverSheetsNumfmtPlugin,
568
- Ne as countryCurrencyMap,
569
- J as currencySymbols,
570
- Qe as getCurrencyFormatOptions,
571
- Ze as getCurrencyOptions,
572
- Je as getCurrencyType,
573
- ke as getDateFormatOptions,
574
- T as getDecimalFromPattern,
575
- H as getDecimalString,
576
- et as getNumberFormatOptions,
577
- De as getPatternPreview,
578
- Ve as getPatternPreviewIgnoreGeneral,
579
- Ye as getPatternType,
580
- ze as isPatternEqualWithoutDecimal,
581
- Xe as isPatternHasDecimal,
582
- x as setPatternDecimal
592
+ AddDecimalCommand,
593
+ CURRENCYFORMAT,
594
+ DATEFMTLISG,
595
+ MenuCurrencyService,
596
+ NUMBERFORMAT,
597
+ SetCurrencyCommand,
598
+ SetNumfmtCommand,
599
+ SetPercentCommand,
600
+ SheetsNumfmtCellContentController,
601
+ SubtractDecimalCommand,
602
+ UniverSheetsNumfmtPlugin,
603
+ countryCurrencyMap,
604
+ currencySymbols,
605
+ getCurrencyFormatOptions,
606
+ getCurrencyOptions,
607
+ getCurrencyType,
608
+ getDateFormatOptions,
609
+ getDecimalFromPattern,
610
+ getDecimalString,
611
+ getNumberFormatOptions,
612
+ getPatternPreview,
613
+ getPatternPreviewIgnoreGeneral,
614
+ getPatternType,
615
+ isPatternEqualWithoutDecimal,
616
+ isPatternHasDecimal,
617
+ setPatternDecimal
583
618
  };