index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. var ne = Object.defineProperty;
  2. var re = (e, t, r) => t in e ? ne(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
  3. var y = (e, t, r) => re(e, typeof t != "symbol" ? t + "" : t, r);
  4. import { numfmt as N, CommandType as I, ICommandService as M, IUniverInstanceService as D, IUndoRedoService as se, sequenceExecute as oe, Range as C, CellValueType as A, Inject as E, ThemeService as ce, LocaleService as ae, Disposable as X, LocaleType as b, ObjectMatrix as B, InterceptorEffectEnum as W, isRealNum as ie, UniverInstanceType as Y, DependentOn as ue, Injector as le, Plugin as me, registerDependencies as fe, touchDependencies as G } from "@univerjs/core";
  5. import { getSheetCommandTarget as w, transformCellsToRange as de, rangeMerge as H, SetNumfmtMutation as Z, factorySetNumfmtUndoMutation as he, RemoveNumfmtMutation as ge, factoryRemoveNumfmtUndoMutation as pe, SheetsSelectionsService as V, INumfmtService as F, SheetInterceptorService as ve, INTERCEPTOR_POINT as Se, InterceptCellContentPriority as _e, SetRangeValuesMutation as ye, UniverSheetsPlugin as be } from "@univerjs/sheets";
  6. import { BehaviorSubject as j, merge as Ce, switchMap as Me, of as $e, skip as Ee } from "rxjs";
  7. import { DEFAULT_TEXT_FORMAT as Ne } from "@univerjs/engine-numfmt";
  8. import { stripErrorMargin as Re } from "@univerjs/engine-formula";
  9. const J = [
  10. "$",
  11. "£",
  12. "¥",
  13. "¤",
  14. "֏",
  15. "؋",
  16. "৳",
  17. "฿",
  18. // '៛',
  19. "₡",
  20. "₦",
  21. "₩",
  22. "₪",
  23. "₫",
  24. "€",
  25. "₭",
  26. "₮",
  27. "₱",
  28. "₲",
  29. "₴",
  30. "₸",
  31. "₹",
  32. "₺",
  33. "₼",
  34. "₽",
  35. "₾",
  36. "₿"
  37. ], Ie = {
  38. US: "$",
  39. // United States Dollar
  40. CA: "C$",
  41. // Canadian Dollar
  42. GB: "£",
  43. // British Pound Sterling
  44. JP: "¥",
  45. // Japanese Yen
  46. IN: "₹",
  47. // Indian Rupee
  48. AU: "A$",
  49. // Australian Dollar
  50. CN: "¥",
  51. // Chinese Yuan
  52. KR: "₩",
  53. // South Korean Won
  54. RU: "₽",
  55. // Russian Ruble
  56. // Euro countries
  57. AT: "€",
  58. BE: "€",
  59. CY: "€",
  60. EE: "€",
  61. FI: "€",
  62. FR: "€",
  63. DE: "€",
  64. GR: "€",
  65. IE: "€",
  66. IT: "€",
  67. LV: "€",
  68. LT: "€",
  69. LU: "€",
  70. MT: "€",
  71. NL: "€",
  72. PT: "€",
  73. SK: "€",
  74. SI: "€",
  75. ES: "€"
  76. // Add more mappings as needed
  77. }, Te = [
  78. {
  79. label: "1930-08-05",
  80. suffix: "yyyy-MM-dd"
  81. },
  82. {
  83. label: "1930/08/05",
  84. suffix: "yyyy/MM/dd"
  85. },
  86. {
  87. label: "1930年08月05日",
  88. suffix: 'yyyy"年"MM"月"dd"日"'
  89. },
  90. {
  91. label: "08-05",
  92. suffix: "MM-dd"
  93. },
  94. {
  95. label: "8月5日",
  96. suffix: 'M"月"d"日"'
  97. },
  98. {
  99. label: "13:30:30",
  100. suffix: "h:mm:ss"
  101. },
  102. {
  103. label: "13:30",
  104. suffix: "h:mm"
  105. },
  106. {
  107. label: "下午01:30",
  108. suffix: "A/P hh:mm"
  109. },
  110. {
  111. label: "下午1:30",
  112. suffix: "A/P h:mm"
  113. },
  114. {
  115. label: "下午1:30:30",
  116. suffix: "A/P h:mm:ss"
  117. },
  118. {
  119. label: "08-05 下午 01:30",
  120. suffix: "MM-dd A/P hh:mm"
  121. }
  122. ], xe = [
  123. {
  124. label: "(1,235)",
  125. suffix: "#,##0_);(#,##0)"
  126. },
  127. {
  128. label: "(1,235) ",
  129. suffix: "#,##0_);[Red](#,##0)",
  130. color: "red"
  131. },
  132. {
  133. label: "1,234.56",
  134. suffix: "#,##0.00_);#,##0.00"
  135. },
  136. {
  137. label: "1,234.56",
  138. suffix: "#,##0.00_);[Red]#,##0.00",
  139. color: "red"
  140. },
  141. {
  142. label: "-1,234.56",
  143. suffix: "#,##0.00_);-#,##0.00"
  144. },
  145. {
  146. label: "-1,234.56",
  147. suffix: "#,##0.00_);[Red]-#,##0.00",
  148. color: "red"
  149. }
  150. ], Q = [
  151. {
  152. label: (e) => `${e}1,235`,
  153. suffix: (e) => `"${e}"#,##0.00_);"${e}"#,##0.00`
  154. },
  155. {
  156. label: (e) => `${e}1,235`,
  157. suffix: (e) => `"${e}"#,##0.00_);[Red]"${e}"#,##0.00`,
  158. color: "red"
  159. },
  160. {
  161. label: (e) => `(${e}1,235)`,
  162. suffix: (e) => `"${e}"#,##0.00_);("${e}"#,##0.00)`
  163. },
  164. {
  165. label: (e) => `(${e}1,235)`,
  166. suffix: (e) => `"${e}"#,##0.00_);[Red]("${e}"#,##0.00)`,
  167. color: "red"
  168. },
  169. {
  170. label: (e) => `-${e}1,235`,
  171. suffix: (e) => `"${e}"#,##0.00_);-"${e}"#,##0.00`
  172. },
  173. {
  174. label: (e) => `-${e}1,235`,
  175. suffix: (e) => `"${e}"#,##0.00_);[Red]-"${e}"#,##0.00`,
  176. color: "red"
  177. }
  178. ], Pe = "SHEET_NUMFMT_PLUGIN", x = (e, t = 0) => {
  179. var n;
  180. return e && (n = N.getInfo(e).maxDecimals) != null ? n : t;
  181. }, Ze = (e, t) => {
  182. if (e && !t || !e && t)
  183. return !1;
  184. const r = (c) => c.reduce(
  185. (l, h) => {
  186. if (l.isEnd)
  187. return l;
  188. const f = h.value || h.num;
  189. return h.type === "point" ? (l.isEnd = !0, l) : { ...l, result: l.result + f };
  190. },
  191. { isEnd: !1, result: "" }
  192. ).result, n = N.getInfo(e)._partitions, s = N.getInfo(t)._partitions, o = r(n[0].tokens), a = r(s[0].tokens), d = r(n[1].tokens), u = r(s[1].tokens);
  193. return o === a && d === u && n[1].color === s[1].color;
  194. }, z = (e) => new Array(Math.min(Math.max(0, Number(e)), 30)).fill(0).join(""), P = (e, t) => e.split(";").map((n) => /\.0?/.test(n) ? n.replace(
  195. /\.0*/g,
  196. `${t > 0 ? "." : ""}${z(Number(t || 0))}`
  197. ) : /0([^0]?)|0$/.test(n) ? n.replace(
  198. /0([^0]+)|0$/,
  199. `0${t > 0 ? "." : ""}${z(Number(t || 0))}$1`
  200. ) : n).join(";"), Je = (e) => /\.0?/.test(e) || /0([^0]?)|0$/.test(e), T = {
  201. id: "sheet.command.numfmt.set.numfmt",
  202. type: I.COMMAND,
  203. handler: (e, t) => {
  204. if (!t)
  205. return !1;
  206. const r = e.get(M), n = e.get(D), s = e.get(se), o = w(n, t);
  207. if (!o) return !1;
  208. const { unitId: a, subUnitId: d } = o, u = t.values.filter((i) => !!i.pattern), c = t.values.filter((i) => !i.pattern), l = de(a, d, u), h = {
  209. unitId: a,
  210. subUnitId: d,
  211. ranges: c.map((i) => ({
  212. startColumn: i.col,
  213. startRow: i.row,
  214. endColumn: i.col,
  215. endRow: i.row
  216. }))
  217. }, f = [], v = [];
  218. if (u.length) {
  219. Object.keys(l.values).forEach((g) => {
  220. const p = l.values[g];
  221. p.ranges = H(p.ranges);
  222. }), f.push({
  223. id: Z.id,
  224. params: l
  225. });
  226. const i = he(e, l);
  227. v.push(...i);
  228. }
  229. if (c.length) {
  230. h.ranges = H(h.ranges), f.push({
  231. id: ge.id,
  232. params: h
  233. });
  234. const i = pe(e, h);
  235. v.push(...i);
  236. }
  237. const m = oe(f, r).result;
  238. return m && s.pushUndoRedo({
  239. unitID: a,
  240. undoMutations: v,
  241. redoMutations: f
  242. }), m;
  243. }
  244. }, Ue = {
  245. id: "sheet.command.numfmt.add.decimal.command",
  246. type: I.COMMAND,
  247. handler: async (e) => {
  248. const t = e.get(M), r = e.get(V), n = e.get(F), s = e.get(D), o = r.getCurrentSelections();
  249. if (!o || !o.length)
  250. return !1;
  251. const a = w(s);
  252. if (!a) return !1;
  253. const { unitId: d, subUnitId: u } = a;
  254. let c = 0;
  255. o.forEach((v) => {
  256. C.foreach(v.range, (m, i) => {
  257. const g = n.getValue(d, u, m, i);
  258. if (!g) {
  259. const S = a.worksheet.getCellRaw(m, i);
  260. if (!c && S && S.t === A.NUMBER && S.v) {
  261. const _ = /\.(\d*)$/.exec(String(S.v));
  262. if (_) {
  263. const R = _[1].length;
  264. if (!R)
  265. return;
  266. c = Math.max(c, R);
  267. }
  268. }
  269. return;
  270. }
  271. const p = x(g.pattern);
  272. c = p > c ? p : c;
  273. });
  274. });
  275. const l = c + 1, h = P(`0${l > 0 ? ".0" : ""}`, l), f = [];
  276. return o.forEach((v) => {
  277. C.foreach(v.range, (m, i) => {
  278. const g = n.getValue(d, u, m, i);
  279. if (!g)
  280. f.push({
  281. row: m,
  282. col: i,
  283. pattern: h
  284. });
  285. else {
  286. const p = x(g.pattern), S = P(g.pattern, p + 1);
  287. S !== g.pattern && f.push({
  288. row: m,
  289. col: i,
  290. pattern: S
  291. });
  292. }
  293. });
  294. }), f.length ? await t.executeCommand(T.id, { values: f }) : !1;
  295. }
  296. };
  297. class k {
  298. constructor() {
  299. y(this, "_currencySymbol$", new j("US"));
  300. y(this, "currencySymbol$", this._currencySymbol$.asObservable());
  301. }
  302. /**
  303. * Set the currency symbol by setting the country code.
  304. */
  305. setCurrencySymbolByCountryCode(t) {
  306. this._currencySymbol$.next(t);
  307. }
  308. getCurrencySymbol() {
  309. return this._currencySymbol$.getValue();
  310. }
  311. }
  312. const Oe = {
  313. id: "sheet.command.numfmt.set.currency",
  314. type: I.COMMAND,
  315. handler: async (e) => {
  316. const t = e.get(M), r = e.get(V), n = e.get(k), s = Ie[n.getCurrencySymbol()] || "$", o = r.getCurrentSelections();
  317. if (!o || !o.length)
  318. return !1;
  319. const a = [], d = Q[4].suffix(s);
  320. return o.forEach((c) => {
  321. C.foreach(c.range, (l, h) => {
  322. a.push({ row: l, col: h, pattern: d, type: "currency" });
  323. });
  324. }), await t.executeCommand(T.id, { values: a });
  325. }
  326. }, De = {
  327. id: "sheet.command.numfmt.set.percent",
  328. type: I.COMMAND,
  329. handler: async (e) => {
  330. const t = e.get(M), n = e.get(V).getCurrentSelections();
  331. if (!n || !n.length)
  332. return !1;
  333. const s = [], o = "0%";
  334. return n.forEach((d) => {
  335. C.foreach(d.range, (u, c) => {
  336. s.push({ row: u, col: c, pattern: o, type: "percent" });
  337. });
  338. }), await t.executeCommand(T.id, { values: s });
  339. }
  340. }, Ve = {
  341. id: "sheet.command.numfmt.subtract.decimal.command",
  342. type: I.COMMAND,
  343. handler: async (e) => {
  344. const t = e.get(M), r = e.get(V), n = e.get(F), s = e.get(D), o = r.getCurrentSelections();
  345. if (!o || !o.length)
  346. return !1;
  347. const a = w(s);
  348. if (!a) return !1;
  349. const { unitId: d, subUnitId: u } = a;
  350. let c = 0;
  351. o.forEach((m) => {
  352. C.foreach(m.range, (i, g) => {
  353. const p = n.getValue(d, u, i, g);
  354. if (!p) {
  355. const _ = a.worksheet.getCellRaw(i, g);
  356. if (!c && _ && _.t === A.NUMBER && _.v) {
  357. const R = /\.(\d*)$/.exec(String(_.v));
  358. if (R) {
  359. const L = R[1].length;
  360. if (!L)
  361. return;
  362. c = Math.max(c, L);
  363. }
  364. }
  365. return;
  366. }
  367. const S = x(p.pattern);
  368. c = S > c ? S : c;
  369. });
  370. });
  371. const l = c - 1, h = P(`0${l > 0 ? ".0" : "."}`, l), f = [];
  372. return o.forEach((m) => {
  373. C.foreach(m.range, (i, g) => {
  374. const p = n.getValue(d, u, i, g);
  375. if (!p)
  376. f.push({
  377. row: i,
  378. col: g,
  379. pattern: h
  380. });
  381. else {
  382. const S = x(p.pattern);
  383. f.push({
  384. row: i,
  385. col: g,
  386. pattern: P(p.pattern, S - 1)
  387. });
  388. }
  389. });
  390. }), await t.executeCommand(T.id, { values: f });
  391. }
  392. }, Qe = (e) => N.getInfo(e).type || "unknown", Ae = (e, t, r = "en") => {
  393. const s = N.getInfo(e)._partitions[1], o = N.format(e, t, { locale: r, throws: !1 });
  394. return t < 0 ? {
  395. result: o,
  396. color: s.color
  397. } : {
  398. result: o
  399. };
  400. }, we = (e, t, r) => e === "General" ? {
  401. result: String(Re(t))
  402. // In Excel, the default General format also needs to handle numeric precision.
  403. } : Ae(e, t, r);
  404. var Fe = Object.defineProperty, je = Object.getOwnPropertyDescriptor, Le = (e, t, r, n) => {
  405. for (var s = n > 1 ? void 0 : n ? je(t, r) : t, o = e.length - 1, a; o >= 0; o--)
  406. (a = e[o]) && (s = (n ? a(t, r, s) : a(s)) || s);
  407. return n && s && Fe(t, r, s), s;
  408. }, $ = (e, t) => (r, n) => t(r, n, e);
  409. let U = class extends X {
  410. constructor(t, r, n, s, o, a) {
  411. super();
  412. y(this, "_local$", new j("en"));
  413. y(this, "local$", this._local$.asObservable());
  414. this._instanceService = t, this._sheetInterceptorService = r, this._themeService = n, this._commandService = s, this._numfmtService = o, this._localeService = a, this._initInterceptorCellContent();
  415. }
  416. get local() {
  417. const t = this._local$.getValue();
  418. if (t)
  419. return t;
  420. switch (this._localeService.getCurrentLocale()) {
  421. case b.FR_FR:
  422. return "fr";
  423. case b.RU_RU:
  424. return "ru";
  425. case b.VI_VN:
  426. return "vi";
  427. case b.ZH_CN:
  428. return "zh-CN";
  429. case b.ZH_TW:
  430. return "zh-TW";
  431. case b.EN_US:
  432. case b.FA_IR:
  433. default:
  434. return "en";
  435. }
  436. }
  437. // eslint-disable-next-line max-lines-per-function
  438. _initInterceptorCellContent() {
  439. const t = {
  440. tl: {
  441. size: 6,
  442. color: "#409f11"
  443. }
  444. }, r = new B();
  445. this.disposeWithMe(Ce(this._local$, this._localeService.currentLocale$).subscribe(() => {
  446. r.reset();
  447. })), this.disposeWithMe(this._sheetInterceptorService.intercept(Se.CELL_CONTENT, {
  448. effect: W.Value | W.Style,
  449. handler: (n, s, o) => {
  450. const a = s.unitId, d = s.subUnitId;
  451. let u;
  452. const c = n;
  453. if (!c)
  454. return o(n);
  455. if (n != null && n.s) {
  456. const m = s.workbook.getStyles().get(n.s);
  457. m != null && m.n && (u = m.n);
  458. }
  459. if (u || (u = this._numfmtService.getValue(a, d, s.row, s.col)), !u)
  460. return o(n);
  461. if (u.pattern === Ne && c.v && ie(c.v))
  462. return o({
  463. ...n,
  464. markers: {
  465. ...n == null ? void 0 : n.markers,
  466. ...t
  467. }
  468. });
  469. if (c.t !== A.NUMBER || c.v == null || Number.isNaN(c.v))
  470. return o(n);
  471. let l = "";
  472. const h = r.getValue(s.row, s.col);
  473. if (h && h.parameters === `${c.v}_${u.pattern}`)
  474. return o({ ...n, ...h.result });
  475. const f = we(u.pattern, Number(c.v), this.local);
  476. if (l = f.result, !l)
  477. return o(n);
  478. const v = { v: l };
  479. if (f.color) {
  480. const m = this._themeService.getCurrentTheme()[`${f.color}500`];
  481. m && (v.interceptorStyle = { cl: { rgb: m } });
  482. }
  483. return r.setValue(s.row, s.col, {
  484. result: v,
  485. parameters: `${c.v}_${u.pattern}`
  486. }), o({ ...n, ...v });
  487. },
  488. priority: _e.NUMFMT
  489. })), this.disposeWithMe(this._commandService.onCommandExecuted((n) => {
  490. if (n.id === Z.id) {
  491. const s = n.params;
  492. Object.keys(s.values).forEach((o) => {
  493. s.values[o].ranges.forEach((d) => {
  494. C.foreach(d, (u, c) => {
  495. r.realDeleteValue(u, c);
  496. });
  497. });
  498. });
  499. } else if (n.id === ye.id) {
  500. const s = n.params;
  501. new B(s.cellValue).forValue((o, a) => {
  502. r.realDeleteValue(o, a);
  503. });
  504. }
  505. })), this.disposeWithMe(
  506. this._instanceService.getCurrentTypeOfUnit$(Y.UNIVER_SHEET).pipe(
  507. Me((n) => {
  508. var s;
  509. return (s = n == null ? void 0 : n.activeSheet$) != null ? s : $e(null);
  510. }),
  511. Ee(1)
  512. ).subscribe(() => r.reset())
  513. );
  514. }
  515. setNumfmtLocal(t) {
  516. this._local$.next(t);
  517. }
  518. };
  519. U = Le([
  520. $(0, D),
  521. $(1, E(ve)),
  522. $(2, E(ce)),
  523. $(3, E(M)),
  524. $(4, E(F)),
  525. $(5, E(ae))
  526. ], U);
  527. class K extends X {
  528. constructor() {
  529. super(...arguments);
  530. y(this, "_currencySymbol$", new j("US"));
  531. y(this, "currencySymbol$", this._currencySymbol$.asObservable());
  532. }
  533. /**
  534. * Set the currency symbol by setting the country code.
  535. */
  536. setCurrencySymbolByCountryCode(r) {
  537. this._currencySymbol$.next(r);
  538. }
  539. getCurrencySymbol() {
  540. return this._currencySymbol$.getValue();
  541. }
  542. }
  543. var ee = Object.defineProperty, Be = Object.getOwnPropertyDescriptor, We = (e, t, r) => t in e ? ee(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r, Ge = (e, t, r, n) => {
  544. for (var s = n > 1 ? void 0 : n ? Be(t, r) : t, o = e.length - 1, a; o >= 0; o--)
  545. (a = e[o]) && (s = (n ? a(t, r, s) : a(s)) || s);
  546. return n && s && ee(t, r, s), s;
  547. }, q = (e, t) => (r, n) => t(r, n, e), te = (e, t, r) => We(e, typeof t != "symbol" ? t + "" : t, r);
  548. let O = class extends me {
  549. constructor(e = void 0, t, r) {
  550. super(), this._config = e, this._injector = t, this._commandService = r;
  551. }
  552. onStarting() {
  553. fe(this._injector, [
  554. [U],
  555. [k],
  556. [K]
  557. ]), G(this._injector, [
  558. [U]
  559. ]);
  560. }
  561. onRendered() {
  562. G(this._injector, [
  563. [K]
  564. ]), [
  565. Ue,
  566. Ve,
  567. Oe,
  568. De,
  569. T
  570. ].forEach((e) => {
  571. this.disposeWithMe(this._commandService.registerCommand(e));
  572. });
  573. }
  574. };
  575. te(O, "pluginName", Pe);
  576. te(O, "type", Y.UNIVER_SHEET);
  577. O = Ge([
  578. ue(be),
  579. q(1, E(le)),
  580. q(2, M)
  581. ], O);
  582. const ke = (e) => J.find((r) => e.includes(r)), et = () => J.map((e) => ({ label: e, value: e })), tt = (e) => Q.map((t) => ({
  583. label: t.label(e),
  584. value: t.suffix(e),
  585. color: t.color
  586. })), nt = () => Te.map((e) => ({ label: e.label, value: e.suffix })), rt = () => xe.map((e) => ({ label: e.label, value: e.suffix, color: e.color }));
  587. export {
  588. Ue as AddDecimalCommand,
  589. Q as CURRENCYFORMAT,
  590. Te as DATEFMTLISG,
  591. k as MenuCurrencyService,
  592. xe as NUMBERFORMAT,
  593. Oe as SetCurrencyCommand,
  594. T as SetNumfmtCommand,
  595. De as SetPercentCommand,
  596. U as SheetsNumfmtCellContentController,
  597. Ve as SubtractDecimalCommand,
  598. O as UniverSheetsNumfmtPlugin,
  599. Ie as countryCurrencyMap,
  600. J as currencySymbols,
  601. tt as getCurrencyFormatOptions,
  602. et as getCurrencyOptions,
  603. ke as getCurrencyType,
  604. nt as getDateFormatOptions,
  605. x as getDecimalFromPattern,
  606. z as getDecimalString,
  607. rt as getNumberFormatOptions,
  608. Ae as getPatternPreview,
  609. we as getPatternPreviewIgnoreGeneral,
  610. Qe as getPatternType,
  611. Ze as isPatternEqualWithoutDecimal,
  612. Je as isPatternHasDecimal,
  613. P as setPatternDecimal
  614. };