@univerjs/sheets-numfmt 0.20.1 → 0.21.0

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/index.js CHANGED
@@ -1 +1,913 @@
1
- import{CellValueType as e,CommandType as t,DEFAULT_NUMBER_FORMAT as n,DependentOn as r,Disposable as i,ICommandService as a,IConfigService as o,IUndoRedoService as s,IUniverInstanceService as c,Inject as l,Injector as u,InterceptorEffectEnum as d,LocaleService as f,LocaleType as p,ObjectMatrix as m,Plugin as h,Range as g,ThemeService as _,UniverInstanceType as v,isDefaultFormat as y,isTextFormat as b,merge as x,numfmt as S,registerDependencies as ee,sequenceExecute as te,touchDependencies as ne}from"@univerjs/core";import{INTERCEPTOR_POINT as re,INumfmtService as C,InterceptCellContentPriority as ie,RemoveNumfmtMutation as ae,SetNumfmtMutation as oe,SetRangeValuesMutation as w,SheetInterceptorService as se,SheetsSelectionsService as T,UniverSheetsPlugin as ce,checkCellValueType as E,factoryRemoveNumfmtUndoMutation as le,factorySetNumfmtUndoMutation as ue,getSheetCommandTarget as D,rangeMerge as O,transformCellsToRange as de}from"@univerjs/sheets";import{BehaviorSubject as fe,merge as pe,of as me,skip as he,switchMap as ge}from"rxjs";import{stripErrorMargin as _e}from"@univerjs/engine-formula";const k=`$.£.¥.¤.֏.؋.৳.฿.៛.₡.₦.₩.₪.₫.€.₭.₮.₱.₲.₴.₸.₹.₺.₼.₽.₾.₿.﷼`.split(`.`),A=new Map([[p.EN_US,`$`],[p.RU_RU,`₽`],[p.VI_VN,`₫`],[p.ZH_CN,`¥`],[p.ZH_TW,`NT$`],[p.FR_FR,`€`],[p.FA_IR,`﷼`],[p.KO_KR,`₩`],[p.ES_ES,`€`],[p.CA_ES,`€`],[p.SK_SK,`€`]]);function j(e){switch(e){case p.CA_ES:case p.ES_ES:case p.FR_FR:case p.SK_SK:return{icon:`EuroIcon`,symbol:A.get(e)||`€`,locale:e};case p.RU_RU:return{icon:`RoubleIcon`,symbol:A.get(e)||`₽`,locale:e};case p.ZH_CN:return{icon:`RmbIcon`,symbol:A.get(e)||`¥`,locale:e};case p.EN_US:default:return{icon:`DollarIcon`,symbol:`$`,locale:p.EN_US}}}function M(e){return A.get(e)||`$`}function N(e,t=2){let n=t;t>127&&(n=127);let r=``;return n>0&&(r=`.${`0`.repeat(n)}`),`"${M(e)}"#,##0${r}_);[Red]("${M(e)}"#,##0${r})`}const P=[{label:`1930-08-05`,suffix:`yyyy-MM-dd`},{label:`1930/08/05`,suffix:`yyyy/MM/dd`},{label:`1930年08月05日`,suffix:`yyyy"年"MM"月"dd"日"`},{label:`08-05`,suffix:`MM-dd`},{label:`8月5日`,suffix:`M"月"d"日"`},{label:`13:30:30`,suffix:`h:mm:ss`},{label:`13:30`,suffix:`h:mm`},{label:`下午01:30`,suffix:`A/P hh:mm`},{label:`下午1:30`,suffix:`A/P h:mm`},{label:`下午1:30:30`,suffix:`A/P h:mm:ss`},{label:`08-05 下午 01:30`,suffix:`MM-dd A/P hh:mm`}],F=[{label:`(1,235)`,suffix:`#,##0_);(#,##0)`},{label:`(1,235) `,suffix:`#,##0_);[Red](#,##0)`,color:`red`},{label:`1,234.56`,suffix:`#,##0.00_);#,##0.00`},{label:`1,234.56`,suffix:`#,##0.00_);[Red]#,##0.00`,color:`red`},{label:`-1,234.56`,suffix:`#,##0.00_);-#,##0.00`},{label:`-1,234.56`,suffix:`#,##0.00_);[Red]-#,##0.00`,color:`red`}],I=[{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);"${e}"#,##0.00`},{label:e=>`${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]"${e}"#,##0.00`,color:`red`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);("${e}"#,##0.00)`},{label:e=>`(${e}1,235)`,suffix:e=>`"${e}"#,##0.00_);[Red]("${e}"#,##0.00)`,color:`red`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);-"${e}"#,##0.00`},{label:e=>`-${e}1,235`,suffix:e=>`"${e}"#,##0.00_);[Red]-"${e}"#,##0.00`,color:`red`}],L=(e,t=0)=>{var n;return e?(n=S.getFormatInfo(e).maxDecimals)==null?t:n:t},R=e=>Array(Math.min(Math.max(0,Number(e)),30)).fill(0).join(``),z=(e,t)=>e.split(`;`).map(e=>/\.0?/.test(e)?e.replace(/\.0*/g,`${t>0?`.`:``}${R(Number(t||0))}`):/0([^0]?)|0$/.test(e)?e.replace(/0([^0]+)|0$/,`0${t>0?`.`:``}${R(Number(t||0))}$1`):e).join(`;`),ve=e=>/\.0?/.test(e)||/0([^0]?)|0$/.test(e),B={id:`sheet.command.numfmt.set.numfmt`,type:t.COMMAND,handler:(t,n)=>{if(!n)return!1;let r=t.get(a),i=t.get(c),o=t.get(s),l=D(i,n);if(!l)return!1;let{unitId:u,subUnitId:d,worksheet:f}=l,p=n.values.filter(e=>!!e.pattern),h=n.values.filter(e=>!e.pattern),g=de(u,d,p),_={unitId:u,subUnitId:d,ranges:h.map(e=>({startColumn:e.col,startRow:e.row,endColumn:e.col,endRow:e.row}))},v=[],y=[];if(p.length){let n=p.reduce((t,n)=>{b(n.pattern)&&t.setValue(n.row,n.col,{t:e.STRING});let r=f.getCellRaw(n.row,n.col);if(r){let e=E(r.v);e!==r.t&&t.setValue(n.row,n.col,{t:e})}return t},new m).getMatrix(),r=new m;new m(n).forValue((e,t)=>{let n=f.getCellRaw(e,t);n?r.setValue(e,t,{t:n.t}):r.setValue(e,t,{t:void 0})}),Object.keys(g.values).forEach(e=>{let t=g.values[e];t.ranges=O(t.ranges)}),v.push({id:oe.id,params:g});let i=ue(t,g);y.push(...i)}if(h.length){_.ranges=O(_.ranges);let e=h.reduce((e,t)=>{let n=f.getCellRaw(t.row,t.col);if(n){let r=E(n.v);r!==n.t&&e.setValue(t.row,t.col,{t:r})}return e},new m).getMatrix(),n=new m;new m(e).forValue((e,t)=>{let r=f.getCellRaw(e,t);r?n.setValue(e,t,{t:r.t}):n.setValue(e,t,{t:void 0})}),v.push({id:ae.id,params:_},{id:w.id,params:{unitId:u,subUnitId:d,cellValue:e}});let r=le(t,_);y.push({id:w.id,params:{unitId:u,subUnitId:d,cellValue:n.getMatrix()}},...r)}let x=te(v,r).result;return x&&o.pushUndoRedo({unitID:u,undoMutations:y,redoMutations:v}),x}},V={id:`sheet.command.numfmt.add.decimal.command`,type:t.COMMAND,handler:async t=>{let n=t.get(a),r=t.get(T),i=t.get(C),o=t.get(c),s=r.getCurrentSelections();if(!s||!s.length)return!1;let l=D(o);if(!l)return!1;let{unitId:u,subUnitId:d}=l,f=0;s.forEach(t=>{g.foreach(t.range,(t,n)=>{let r=i.getValue(u,d,t,n);if(!r){let r=l.worksheet.getCellRaw(t,n);if(!f&&r&&r.t===e.NUMBER&&r.v){let e=/\.(\d*)$/.exec(String(r.v));if(e){let t=e[1].length;if(!t)return;f=Math.max(f,t)}}return}let a=L(r.pattern);f=a>f?a:f})});let p=f+1,m=z(`0${p>0?`.0`:``}`,p),h=[];return s.forEach(e=>{g.foreach(e.range,(e,t)=>{let n=i.getValue(u,d,e,t);if(y(n==null?void 0:n.pattern))h.push({row:e,col:t,pattern:m});else{let r=L(n.pattern),i=z(n.pattern,r+1);i!==n.pattern&&h.push({row:e,col:t,pattern:i})}})}),h.length?await n.executeCommand(B.id,{values:h}):!1}},H={id:`sheet.command.numfmt.set.currency`,type:t.COMMAND,handler:async e=>{let t=e.get(a),n=e.get(T),r=e.get(f),i=n.getCurrentSelections();if(!i||!i.length)return!1;let o=[],s=N(j(r.getCurrentLocale()).locale);return i.forEach(e=>{g.foreach(e.range,(e,t)=>{o.push({row:e,col:t,pattern:s,type:`currency`})})}),await t.executeCommand(B.id,{values:o})}},U={id:`sheet.command.numfmt.set.percent`,type:t.COMMAND,handler:async e=>{let t=e.get(a),n=e.get(T).getCurrentSelections();if(!n||!n.length)return!1;let r=[];return n.forEach(e=>{g.foreach(e.range,(e,t)=>{r.push({row:e,col:t,pattern:`0%`,type:`percent`})})}),await t.executeCommand(B.id,{values:r})}},W={id:`sheet.command.numfmt.subtract.decimal.command`,type:t.COMMAND,handler:async t=>{let n=t.get(a),r=t.get(T),i=t.get(C),o=t.get(c),s=r.getCurrentSelections();if(!s||!s.length)return!1;let l=D(o);if(!l)return!1;let{unitId:u,subUnitId:d}=l,f=0;s.forEach(t=>{g.foreach(t.range,(t,n)=>{let r=i.getValue(u,d,t,n);if(!r){let r=l.worksheet.getCellRaw(t,n);if(!f&&r&&r.t===e.NUMBER&&r.v){let e=/\.(\d*)$/.exec(String(r.v));if(e){let t=e[1].length;if(!t)return;f=Math.max(f,t)}}return}let a=L(r.pattern);f=a>f?a:f})});let p=f-1,m=z(`0${p>0?`.0`:`.`}`,p),h=[];return s.forEach(e=>{g.foreach(e.range,(e,t)=>{let n=i.getValue(u,d,e,t);if(y(n==null?void 0:n.pattern))h.push({row:e,col:t,pattern:m});else{let r=L(n.pattern);h.push({row:e,col:t,pattern:z(n.pattern,r-1)})}})}),await n.executeCommand(B.id,{values:h})}},G=`sheets-numfmt.config`;Symbol(G);const K={},ye=e=>S.getFormatInfo(e).type||`unknown`,q=(e,t,n=`en`)=>{try{let r=S.formatColor(e,t),i=r?String(r):void 0,a=S.format(e,t,{locale:n,throws:!1});return t<0?{result:a,color:i}:{result:a}}catch(t){console.warn(`getPatternPreview error:`,e,t)}return{result:String(t)}},J=(e,t,r)=>e===n?{result:String(_e(t))}:q(e,t,r);function Y(e){"@babel/helpers - typeof";return Y=typeof Symbol==`function`&&typeof Symbol.iterator==`symbol`?function(e){return typeof e}:function(e){return e&&typeof Symbol==`function`&&e.constructor===Symbol&&e!==Symbol.prototype?`symbol`:typeof e},Y(e)}function be(e,t){if(Y(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(Y(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function xe(e){var t=be(e,`string`);return Y(t)==`symbol`?t:t+``}function X(e,t,n){return(t=xe(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function Z(e,t){return function(n,r){t(n,r,e)}}function Se(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}const Ce={tl:{size:6,color:`#409f11`}};let Q=class extends i{constructor(e,t,n,r,i,a,o){super(),this._instanceService=e,this._sheetInterceptorService=t,this._themeService=n,this._commandService=r,this._numfmtService=i,this._localeService=a,this._configService=o,X(this,`_locale$`,new fe(`en`)),X(this,`locale$`,this._locale$.asObservable()),this._initInterceptorCellContent()}get locale(){let e=this._locale$.getValue();if(e)return e;switch(this._localeService.getCurrentLocale()){case p.FR_FR:return`fr`;case p.RU_RU:return`ru`;case p.VI_VN:return`vi`;case p.ZH_CN:return`zh-CN`;case p.KO_KR:return`ko`;case p.ZH_TW:return`zh-TW`;case p.ES_ES:case p.CA_ES:return`es`;case p.SK_SK:return`sk`;case p.EN_US:case p.FA_IR:default:return`en`}}_initInterceptorCellContent(){let t=new m;this.disposeWithMe(pe(this._locale$,this._localeService.currentLocale$).subscribe(()=>{t.reset()})),this.disposeWithMe(this._sheetInterceptorService.intercept(re.CELL_CONTENT,{effect:d.Value|d.Style,handler:(n,r,i)=>{if(!n||n.v===void 0||n.v===null||n.t===e.BOOLEAN||n.t===e.FORCE_STRING)return i(n);let a=r.unitId,o=r.subUnitId,s;if(n!=null&&n.s){let e=r.workbook.getStyles().get(n.s);e!=null&&e.n&&(s=e.n)}if(s||(s=this._numfmtService.getValue(a,o,r.row,r.col)),y(s==null?void 0:s.pattern)||n.t!==e.NUMBER&&E(n.v,n.t)!==e.NUMBER)return i(n);let c=n;if((!n||n===r.rawData)&&(n={...r.rawData}),b(s==null?void 0:s.pattern)){var l;return(l=this._configService.getConfig(`sheets-numfmt.config`))!=null&&l.disableTextFormatMark?(n.t=e.STRING,i(n)):(n.t=e.STRING,n.markers={...n==null?void 0:n.markers,...Ce},i(n))}let u=``,d=t.getValue(r.row,r.col);if(d&&d.parameters===`${c.v}_${s==null?void 0:s.pattern}`)return i({...n,...d.result});let f=J(s==null?void 0:s.pattern,Number(c.v),this.locale);if(u=f.result,!u)return i(n);let p={v:u,t:e.NUMBER};if(f.color){var m;let e=(m=this._themeService.getColorFromTheme(`${f.color}.500`))==null?f.color:m;e&&(p.interceptorStyle={cl:{rgb:e}})}return t.setValue(r.row,r.col,{result:p,parameters:`${c.v}_${s==null?void 0:s.pattern}`}),Object.assign(n,p),i(n)},priority:ie.NUMFMT})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===oe.id){let n=e.params;Object.keys(n.values).forEach(e=>{n.values[e].ranges.forEach(e=>{g.foreach(e,(e,n)=>{t.realDeleteValue(e,n)})})})}else if(e.id===w.id){let n=e.params;new m(n.cellValue).forValue((e,n)=>{t.realDeleteValue(e,n)})}})),this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(v.UNIVER_SHEET).pipe(ge(e=>{var t;return(t=e==null?void 0:e.activeSheet$)==null?me(null):t}),he(1)).subscribe(()=>t.reset()))}setNumfmtLocal(e){this._locale$.next(e)}};Q=Se([Z(0,c),Z(1,l(se)),Z(2,l(_)),Z(3,l(a)),Z(4,l(C)),Z(5,l(f)),Z(6,o)],Q);var we=`@univerjs/sheets-numfmt`,Te=`0.20.1`;let $=class extends h{constructor(e=K,t,n,r){super(),this._config=e,this._injector=t,this._configService=n,this._commandService=r;let{...i}=x({},K,this._config);this._configService.setConfig(G,i)}onStarting(){ee(this._injector,[[Q]]),ne(this._injector,[[Q]]),[V,W,H,U,B].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}};X($,`pluginName`,`SHEET_NUMFMT_PLUGIN`),X($,`packageName`,we),X($,`version`,Te),X($,`type`,v.UNIVER_SHEET),$=Se([r(ce),Z(1,l(u)),Z(2,o),Z(3,a)],$);const Ee=e=>k.find(t=>e.includes(t)),De=()=>k.map(e=>({label:e,value:e})),Oe=e=>I.map(t=>({label:t.label(e),value:t.suffix(e),color:t.color})),ke=()=>P.map(e=>({label:e.label,value:e.suffix})),Ae=()=>F.map(e=>({label:e.label,value:e.suffix,color:e.color}));export{V as AddDecimalCommand,I as CURRENCYFORMAT,P as DATEFMTLISG,F as NUMBERFORMAT,G as SHEETS_NUMFMT_PLUGIN_CONFIG_KEY,H as SetCurrencyCommand,B as SetNumfmtCommand,U as SetPercentCommand,Q as SheetsNumfmtCellContentController,W as SubtractDecimalCommand,$ as UniverSheetsNumfmtPlugin,k as currencySymbols,N as getCurrencyFormat,Oe as getCurrencyFormatOptions,De as getCurrencyOptions,M as getCurrencySymbolByLocale,j as getCurrencySymbolIconByLocale,Ee as getCurrencyType,ke as getDateFormatOptions,L as getDecimalFromPattern,R as getDecimalString,Ae as getNumberFormatOptions,q as getPatternPreview,J as getPatternPreviewIgnoreGeneral,ye as getPatternType,ve as isPatternHasDecimal,A as localeCurrencySymbolMap,z as setPatternDecimal};
1
+ import { CellValueType, CommandType, DEFAULT_NUMBER_FORMAT, DependentOn, Disposable, ICommandService, IConfigService, IUndoRedoService, IUniverInstanceService, Inject, Injector, InterceptorEffectEnum, LocaleService, LocaleType, ObjectMatrix, Plugin, Range, ThemeService, UniverInstanceType, isDefaultFormat, isTextFormat, merge, numfmt, registerDependencies, sequenceExecute, touchDependencies } from "@univerjs/core";
2
+ import { INTERCEPTOR_POINT, INumfmtService, InterceptCellContentPriority, RemoveNumfmtMutation, SetNumfmtMutation, SetRangeValuesMutation, SheetInterceptorService, SheetsSelectionsService, UniverSheetsPlugin, checkCellValueType, factoryRemoveNumfmtUndoMutation, factorySetNumfmtUndoMutation, getSheetCommandTarget, rangeMerge, transformCellsToRange } from "@univerjs/sheets";
3
+ import { BehaviorSubject, merge as merge$1, of, skip, switchMap } from "rxjs";
4
+ import { stripErrorMargin } from "@univerjs/engine-formula";
5
+
6
+ //#region src/base/const/currency-symbols.ts
7
+ /**
8
+ * Copyright 2023-present DreamNum Co., Ltd.
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const currencySymbols = [
23
+ "$",
24
+ "£",
25
+ "¥",
26
+ "¤",
27
+ "֏",
28
+ "؋",
29
+ "৳",
30
+ "฿",
31
+ "៛",
32
+ "₡",
33
+ "₦",
34
+ "₩",
35
+ "₪",
36
+ "₫",
37
+ "€",
38
+ "₭",
39
+ "₮",
40
+ "₱",
41
+ "₲",
42
+ "₴",
43
+ "₸",
44
+ "₹",
45
+ "₺",
46
+ "₼",
47
+ "₽",
48
+ "₾",
49
+ "₿",
50
+ "﷼"
51
+ ];
52
+ const localeCurrencySymbolMap = new Map([
53
+ [LocaleType.EN_US, "$"],
54
+ [LocaleType.RU_RU, "₽"],
55
+ [LocaleType.VI_VN, "₫"],
56
+ [LocaleType.ZH_CN, "¥"],
57
+ [LocaleType.ZH_TW, "NT$"],
58
+ [LocaleType.FR_FR, "€"],
59
+ [LocaleType.FA_IR, "﷼"],
60
+ [LocaleType.KO_KR, "₩"],
61
+ [LocaleType.ES_ES, "€"],
62
+ [LocaleType.CA_ES, "€"],
63
+ [LocaleType.SK_SK, "€"]
64
+ ]);
65
+ /**
66
+ * Get the currency symbol icon based on the locale.
67
+ * TODO@wpxp123456: supplement more currency symbols icons. missing icons: ₩, ₫, NT$, ﷼.
68
+ */
69
+ function getCurrencySymbolIconByLocale(locale) {
70
+ switch (locale) {
71
+ case LocaleType.CA_ES:
72
+ case LocaleType.ES_ES:
73
+ case LocaleType.FR_FR:
74
+ case LocaleType.SK_SK: return {
75
+ icon: "EuroIcon",
76
+ symbol: localeCurrencySymbolMap.get(locale) || "€",
77
+ locale
78
+ };
79
+ case LocaleType.RU_RU: return {
80
+ icon: "RoubleIcon",
81
+ symbol: localeCurrencySymbolMap.get(locale) || "₽",
82
+ locale
83
+ };
84
+ case LocaleType.ZH_CN: return {
85
+ icon: "RmbIcon",
86
+ symbol: localeCurrencySymbolMap.get(locale) || "¥",
87
+ locale
88
+ };
89
+ case LocaleType.EN_US:
90
+ default: return {
91
+ icon: "DollarIcon",
92
+ symbol: "$",
93
+ locale: LocaleType.EN_US
94
+ };
95
+ }
96
+ }
97
+ /**
98
+ * Get the currency symbol by locale.
99
+ */
100
+ function getCurrencySymbolByLocale(locale) {
101
+ return localeCurrencySymbolMap.get(locale) || "$";
102
+ }
103
+ /**
104
+ * Get the currency format string based on the locale and number of digits.
105
+ */
106
+ function getCurrencyFormat(locale, numberDigits = 2) {
107
+ let _numberDigits = numberDigits;
108
+ if (numberDigits > 127) _numberDigits = 127;
109
+ let decimal = "";
110
+ if (_numberDigits > 0) decimal = `.${"0".repeat(_numberDigits)}`;
111
+ return `"${getCurrencySymbolByLocale(locale)}"#,##0${decimal}_);[Red]("${getCurrencySymbolByLocale(locale)}"#,##0${decimal})`;
112
+ }
113
+
114
+ //#endregion
115
+ //#region src/base/const/formatdetail.ts
116
+ /**
117
+ * Copyright 2023-present DreamNum Co., Ltd.
118
+ *
119
+ * Licensed under the Apache License, Version 2.0 (the "License");
120
+ * you may not use this file except in compliance with the License.
121
+ * You may obtain a copy of the License at
122
+ *
123
+ * http://www.apache.org/licenses/LICENSE-2.0
124
+ *
125
+ * Unless required by applicable law or agreed to in writing, software
126
+ * distributed under the License is distributed on an "AS IS" BASIS,
127
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128
+ * See the License for the specific language governing permissions and
129
+ * limitations under the License.
130
+ */
131
+ const DATEFMTLISG = [
132
+ {
133
+ label: "1930-08-05",
134
+ suffix: "yyyy-MM-dd"
135
+ },
136
+ {
137
+ label: "1930/08/05",
138
+ suffix: "yyyy/MM/dd"
139
+ },
140
+ {
141
+ label: "1930年08月05日",
142
+ suffix: "yyyy\"年\"MM\"月\"dd\"日\""
143
+ },
144
+ {
145
+ label: "08-05",
146
+ suffix: "MM-dd"
147
+ },
148
+ {
149
+ label: "8月5日",
150
+ suffix: "M\"月\"d\"日\""
151
+ },
152
+ {
153
+ label: "13:30:30",
154
+ suffix: "h:mm:ss"
155
+ },
156
+ {
157
+ label: "13:30",
158
+ suffix: "h:mm"
159
+ },
160
+ {
161
+ label: "下午01:30",
162
+ suffix: "A/P hh:mm"
163
+ },
164
+ {
165
+ label: "下午1:30",
166
+ suffix: "A/P h:mm"
167
+ },
168
+ {
169
+ label: "下午1:30:30",
170
+ suffix: "A/P h:mm:ss"
171
+ },
172
+ {
173
+ label: "08-05 下午 01:30",
174
+ suffix: "MM-dd A/P hh:mm"
175
+ }
176
+ ];
177
+ const NUMBERFORMAT = [
178
+ {
179
+ label: "(1,235)",
180
+ suffix: "#,##0_);(#,##0)"
181
+ },
182
+ {
183
+ label: "(1,235) ",
184
+ suffix: "#,##0_);[Red](#,##0)",
185
+ color: "red"
186
+ },
187
+ {
188
+ label: "1,234.56",
189
+ suffix: "#,##0.00_);#,##0.00"
190
+ },
191
+ {
192
+ label: "1,234.56",
193
+ suffix: "#,##0.00_);[Red]#,##0.00",
194
+ color: "red"
195
+ },
196
+ {
197
+ label: "-1,234.56",
198
+ suffix: "#,##0.00_);-#,##0.00"
199
+ },
200
+ {
201
+ label: "-1,234.56",
202
+ suffix: "#,##0.00_);[Red]-#,##0.00",
203
+ color: "red"
204
+ }
205
+ ];
206
+ const CURRENCYFORMAT = [
207
+ {
208
+ label: (suffix) => `${suffix}1,235`,
209
+ suffix: (suffix) => `"${suffix}"#,##0.00_);"${suffix}"#,##0.00`
210
+ },
211
+ {
212
+ label: (suffix) => `${suffix}1,235`,
213
+ suffix: (suffix) => `"${suffix}"#,##0.00_);[Red]"${suffix}"#,##0.00`,
214
+ color: "red"
215
+ },
216
+ {
217
+ label: (suffix) => `(${suffix}1,235)`,
218
+ suffix: (suffix) => `"${suffix}"#,##0.00_);("${suffix}"#,##0.00)`
219
+ },
220
+ {
221
+ label: (suffix) => `(${suffix}1,235)`,
222
+ suffix: (suffix) => `"${suffix}"#,##0.00_);[Red]("${suffix}"#,##0.00)`,
223
+ color: "red"
224
+ },
225
+ {
226
+ label: (suffix) => `-${suffix}1,235`,
227
+ suffix: (suffix) => `"${suffix}"#,##0.00_);-"${suffix}"#,##0.00`
228
+ },
229
+ {
230
+ label: (suffix) => `-${suffix}1,235`,
231
+ suffix: (suffix) => `"${suffix}"#,##0.00_);[Red]-"${suffix}"#,##0.00`,
232
+ color: "red"
233
+ }
234
+ ];
235
+
236
+ //#endregion
237
+ //#region src/utils/decimal.ts
238
+ /**
239
+ * Copyright 2023-present DreamNum Co., Ltd.
240
+ *
241
+ * Licensed under the Apache License, Version 2.0 (the "License");
242
+ * you may not use this file except in compliance with the License.
243
+ * You may obtain a copy of the License at
244
+ *
245
+ * http://www.apache.org/licenses/LICENSE-2.0
246
+ *
247
+ * Unless required by applicable law or agreed to in writing, software
248
+ * distributed under the License is distributed on an "AS IS" BASIS,
249
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
250
+ * See the License for the specific language governing permissions and
251
+ * limitations under the License.
252
+ */
253
+ /**
254
+ * the function decimal just use positive,negative configuration ignored
255
+ */
256
+ const getDecimalFromPattern = (pattern, defaultValue = 0) => {
257
+ var _info$maxDecimals;
258
+ if (!pattern) return defaultValue;
259
+ return (_info$maxDecimals = numfmt.getFormatInfo(pattern).maxDecimals) !== null && _info$maxDecimals !== void 0 ? _info$maxDecimals : defaultValue;
260
+ };
261
+ const getDecimalString = (length) => new Array(Math.min(Math.max(0, Number(length)), 30)).fill(0).join("");
262
+ const setPatternDecimal = (patterns, decimalLength) => {
263
+ return patterns.split(";").map((pattern) => {
264
+ if (/\.0?/.test(pattern)) return pattern.replace(/\.0*/g, `${decimalLength > 0 ? "." : ""}${getDecimalString(Number(decimalLength || 0))}`);
265
+ if (/0([^0]?)|0$/.test(pattern)) return pattern.replace(/0([^0]+)|0$/, `0${decimalLength > 0 ? "." : ""}${getDecimalString(Number(decimalLength || 0))}$1`);
266
+ return pattern;
267
+ }).join(";");
268
+ };
269
+ const isPatternHasDecimal = (pattern) => /\.0?/.test(pattern) || /0([^0]?)|0$/.test(pattern);
270
+
271
+ //#endregion
272
+ //#region src/commands/commands/set-numfmt.command.ts
273
+ const SetNumfmtCommand = {
274
+ id: "sheet.command.numfmt.set.numfmt",
275
+ type: CommandType.COMMAND,
276
+ handler: (accessor, params) => {
277
+ if (!params) return false;
278
+ const commandService = accessor.get(ICommandService);
279
+ const univerInstanceService = accessor.get(IUniverInstanceService);
280
+ const undoRedoService = accessor.get(IUndoRedoService);
281
+ const target = getSheetCommandTarget(univerInstanceService, params);
282
+ if (!target) return false;
283
+ const { unitId, subUnitId, worksheet } = target;
284
+ const setCells = params.values.filter((value) => !!value.pattern);
285
+ const removeCells = params.values.filter((value) => !value.pattern);
286
+ const setRedos = transformCellsToRange(unitId, subUnitId, setCells);
287
+ const removeRedos = {
288
+ unitId,
289
+ subUnitId,
290
+ ranges: removeCells.map((cell) => ({
291
+ startColumn: cell.col,
292
+ startRow: cell.row,
293
+ endColumn: cell.col,
294
+ endRow: cell.row
295
+ }))
296
+ };
297
+ const redos = [];
298
+ const undos = [];
299
+ if (setCells.length) {
300
+ const setCellTypeObj = setCells.reduce((pre, cur) => {
301
+ if (isTextFormat(cur.pattern)) pre.setValue(cur.row, cur.col, { t: CellValueType.STRING });
302
+ const cell = worksheet.getCellRaw(cur.row, cur.col);
303
+ if (cell) {
304
+ const type = checkCellValueType(cell.v);
305
+ if (type !== cell.t) pre.setValue(cur.row, cur.col, { t: type });
306
+ }
307
+ return pre;
308
+ }, new ObjectMatrix()).getMatrix();
309
+ const undoSetCellTypeObj = new ObjectMatrix();
310
+ new ObjectMatrix(setCellTypeObj).forValue((row, col) => {
311
+ const cell = worksheet.getCellRaw(row, col);
312
+ if (cell) undoSetCellTypeObj.setValue(row, col, { t: cell.t });
313
+ else undoSetCellTypeObj.setValue(row, col, { t: void 0 });
314
+ });
315
+ Object.keys(setRedos.values).forEach((key) => {
316
+ const v = setRedos.values[key];
317
+ v.ranges = rangeMerge(v.ranges);
318
+ });
319
+ redos.push({
320
+ id: SetNumfmtMutation.id,
321
+ params: setRedos
322
+ });
323
+ const undo = factorySetNumfmtUndoMutation(accessor, setRedos);
324
+ undos.push(...undo);
325
+ }
326
+ if (removeCells.length) {
327
+ removeRedos.ranges = rangeMerge(removeRedos.ranges);
328
+ const setCellTypeObj = removeCells.reduce((pre, cur) => {
329
+ const cell = worksheet.getCellRaw(cur.row, cur.col);
330
+ if (cell) {
331
+ const type = checkCellValueType(cell.v);
332
+ if (type !== cell.t) pre.setValue(cur.row, cur.col, { t: type });
333
+ }
334
+ return pre;
335
+ }, new ObjectMatrix()).getMatrix();
336
+ const undoSetCellTypeObj = new ObjectMatrix();
337
+ new ObjectMatrix(setCellTypeObj).forValue((row, col) => {
338
+ const cell = worksheet.getCellRaw(row, col);
339
+ if (cell) undoSetCellTypeObj.setValue(row, col, { t: cell.t });
340
+ else undoSetCellTypeObj.setValue(row, col, { t: void 0 });
341
+ });
342
+ redos.push({
343
+ id: RemoveNumfmtMutation.id,
344
+ params: removeRedos
345
+ }, {
346
+ id: SetRangeValuesMutation.id,
347
+ params: {
348
+ unitId,
349
+ subUnitId,
350
+ cellValue: setCellTypeObj
351
+ }
352
+ });
353
+ const undo = factoryRemoveNumfmtUndoMutation(accessor, removeRedos);
354
+ undos.push({
355
+ id: SetRangeValuesMutation.id,
356
+ params: {
357
+ unitId,
358
+ subUnitId,
359
+ cellValue: undoSetCellTypeObj.getMatrix()
360
+ }
361
+ }, ...undo);
362
+ }
363
+ const result = sequenceExecute(redos, commandService).result;
364
+ if (result) undoRedoService.pushUndoRedo({
365
+ unitID: unitId,
366
+ undoMutations: undos,
367
+ redoMutations: redos
368
+ });
369
+ return result;
370
+ }
371
+ };
372
+
373
+ //#endregion
374
+ //#region src/commands/commands/add-decimal.command.ts
375
+ const AddDecimalCommand = {
376
+ id: "sheet.command.numfmt.add.decimal.command",
377
+ type: CommandType.COMMAND,
378
+ handler: async (accessor) => {
379
+ const commandService = accessor.get(ICommandService);
380
+ const selectionManagerService = accessor.get(SheetsSelectionsService);
381
+ const numfmtService = accessor.get(INumfmtService);
382
+ const univerInstanceService = accessor.get(IUniverInstanceService);
383
+ const selections = selectionManagerService.getCurrentSelections();
384
+ if (!selections || !selections.length) return false;
385
+ const target = getSheetCommandTarget(univerInstanceService);
386
+ if (!target) return false;
387
+ const { unitId, subUnitId } = target;
388
+ let maxDecimals = 0;
389
+ selections.forEach((selection) => {
390
+ Range.foreach(selection.range, (row, col) => {
391
+ const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
392
+ if (!numfmtValue) {
393
+ const cell = target.worksheet.getCellRaw(row, col);
394
+ if (!maxDecimals && cell && cell.t === CellValueType.NUMBER && cell.v) {
395
+ const regResult = /\.(\d*)$/.exec(String(cell.v));
396
+ if (regResult) {
397
+ const length = regResult[1].length;
398
+ if (!length) return;
399
+ maxDecimals = Math.max(maxDecimals, length);
400
+ }
401
+ }
402
+ return;
403
+ }
404
+ const decimals = getDecimalFromPattern(numfmtValue.pattern);
405
+ maxDecimals = decimals > maxDecimals ? decimals : maxDecimals;
406
+ });
407
+ });
408
+ const decimals = maxDecimals + 1;
409
+ const defaultPattern = setPatternDecimal(`0${decimals > 0 ? ".0" : ""}`, decimals);
410
+ const values = [];
411
+ selections.forEach((selection) => {
412
+ Range.foreach(selection.range, (row, col) => {
413
+ const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
414
+ if (isDefaultFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) values.push({
415
+ row,
416
+ col,
417
+ pattern: defaultPattern
418
+ });
419
+ else {
420
+ const decimals = getDecimalFromPattern(numfmtValue.pattern);
421
+ const pattern = setPatternDecimal(numfmtValue.pattern, decimals + 1);
422
+ pattern !== numfmtValue.pattern && values.push({
423
+ row,
424
+ col,
425
+ pattern
426
+ });
427
+ }
428
+ });
429
+ });
430
+ if (values.length) return await commandService.executeCommand(SetNumfmtCommand.id, { values });
431
+ return false;
432
+ }
433
+ };
434
+
435
+ //#endregion
436
+ //#region src/commands/commands/set-currency.command.ts
437
+ /**
438
+ * This command is triggered by clicking the currency symbol icon in the menu.
439
+ * So the currency format is determined by the currency symbol icon.
440
+ */
441
+ const SetCurrencyCommand = {
442
+ id: "sheet.command.numfmt.set.currency",
443
+ type: CommandType.COMMAND,
444
+ handler: async (accessor) => {
445
+ const commandService = accessor.get(ICommandService);
446
+ const selectionManagerService = accessor.get(SheetsSelectionsService);
447
+ const localeService = accessor.get(LocaleService);
448
+ const selections = selectionManagerService.getCurrentSelections();
449
+ if (!selections || !selections.length) return false;
450
+ const values = [];
451
+ const currencyFormat = getCurrencyFormat(getCurrencySymbolIconByLocale(localeService.getCurrentLocale()).locale);
452
+ selections.forEach((selection) => {
453
+ Range.foreach(selection.range, (row, col) => {
454
+ values.push({
455
+ row,
456
+ col,
457
+ pattern: currencyFormat,
458
+ type: "currency"
459
+ });
460
+ });
461
+ });
462
+ return await commandService.executeCommand(SetNumfmtCommand.id, { values });
463
+ }
464
+ };
465
+
466
+ //#endregion
467
+ //#region src/commands/commands/set-percent.command.ts
468
+ const SetPercentCommand = {
469
+ id: "sheet.command.numfmt.set.percent",
470
+ type: CommandType.COMMAND,
471
+ handler: async (accessor) => {
472
+ const commandService = accessor.get(ICommandService);
473
+ const selections = accessor.get(SheetsSelectionsService).getCurrentSelections();
474
+ if (!selections || !selections.length) return false;
475
+ const values = [];
476
+ const suffix = "0%";
477
+ selections.forEach((selection) => {
478
+ Range.foreach(selection.range, (row, col) => {
479
+ values.push({
480
+ row,
481
+ col,
482
+ pattern: suffix,
483
+ type: "percent"
484
+ });
485
+ });
486
+ });
487
+ return await commandService.executeCommand(SetNumfmtCommand.id, { values });
488
+ }
489
+ };
490
+
491
+ //#endregion
492
+ //#region src/commands/commands/subtract-decimal.command.ts
493
+ const SubtractDecimalCommand = {
494
+ id: "sheet.command.numfmt.subtract.decimal.command",
495
+ type: CommandType.COMMAND,
496
+ handler: async (accessor) => {
497
+ const commandService = accessor.get(ICommandService);
498
+ const selectionManagerService = accessor.get(SheetsSelectionsService);
499
+ const numfmtService = accessor.get(INumfmtService);
500
+ const univerInstanceService = accessor.get(IUniverInstanceService);
501
+ const selections = selectionManagerService.getCurrentSelections();
502
+ if (!selections || !selections.length) return false;
503
+ const target = getSheetCommandTarget(univerInstanceService);
504
+ if (!target) return false;
505
+ const { unitId, subUnitId } = target;
506
+ let maxDecimals = 0;
507
+ selections.forEach((selection) => {
508
+ Range.foreach(selection.range, (row, col) => {
509
+ const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
510
+ if (!numfmtValue) {
511
+ const cell = target.worksheet.getCellRaw(row, col);
512
+ if (!maxDecimals && cell && cell.t === CellValueType.NUMBER && cell.v) {
513
+ const regResult = /\.(\d*)$/.exec(String(cell.v));
514
+ if (regResult) {
515
+ const length = regResult[1].length;
516
+ if (!length) return;
517
+ maxDecimals = Math.max(maxDecimals, length);
518
+ }
519
+ }
520
+ return;
521
+ }
522
+ const decimals = getDecimalFromPattern(numfmtValue.pattern);
523
+ maxDecimals = decimals > maxDecimals ? decimals : maxDecimals;
524
+ });
525
+ });
526
+ const decimals = maxDecimals - 1;
527
+ const defaultPattern = setPatternDecimal(`0${decimals > 0 ? ".0" : "."}`, decimals);
528
+ const values = [];
529
+ selections.forEach((selection) => {
530
+ Range.foreach(selection.range, (row, col) => {
531
+ const numfmtValue = numfmtService.getValue(unitId, subUnitId, row, col);
532
+ if (isDefaultFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) values.push({
533
+ row,
534
+ col,
535
+ pattern: defaultPattern
536
+ });
537
+ else {
538
+ const decimals = getDecimalFromPattern(numfmtValue.pattern);
539
+ values.push({
540
+ row,
541
+ col,
542
+ pattern: setPatternDecimal(numfmtValue.pattern, decimals - 1)
543
+ });
544
+ }
545
+ });
546
+ });
547
+ return await commandService.executeCommand(SetNumfmtCommand.id, { values });
548
+ }
549
+ };
550
+
551
+ //#endregion
552
+ //#region src/config/config.ts
553
+ /**
554
+ * Copyright 2023-present DreamNum Co., Ltd.
555
+ *
556
+ * Licensed under the Apache License, Version 2.0 (the "License");
557
+ * you may not use this file except in compliance with the License.
558
+ * You may obtain a copy of the License at
559
+ *
560
+ * http://www.apache.org/licenses/LICENSE-2.0
561
+ *
562
+ * Unless required by applicable law or agreed to in writing, software
563
+ * distributed under the License is distributed on an "AS IS" BASIS,
564
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
565
+ * See the License for the specific language governing permissions and
566
+ * limitations under the License.
567
+ */
568
+ const SHEETS_NUMFMT_PLUGIN_CONFIG_KEY = "sheets-numfmt.config";
569
+ const configSymbol = Symbol(SHEETS_NUMFMT_PLUGIN_CONFIG_KEY);
570
+ const defaultPluginConfig = {};
571
+
572
+ //#endregion
573
+ //#region src/utils/pattern.ts
574
+ const getPatternType = (pattern) => numfmt.getFormatInfo(pattern).type || "unknown";
575
+ const getPatternPreview = (pattern, value, locale = "en") => {
576
+ try {
577
+ const formatColor = numfmt.formatColor(pattern, value);
578
+ const color = formatColor ? String(formatColor) : void 0;
579
+ const result = numfmt.format(pattern, value, {
580
+ locale,
581
+ throws: false
582
+ });
583
+ if (value < 0) return {
584
+ result,
585
+ color
586
+ };
587
+ return { result };
588
+ } catch (e) {
589
+ console.warn("getPatternPreview error:", pattern, e);
590
+ }
591
+ return { result: String(value) };
592
+ };
593
+ const getPatternPreviewIgnoreGeneral = (pattern, value, locale) => {
594
+ if (pattern === DEFAULT_NUMBER_FORMAT) return { result: String(stripErrorMargin(value)) };
595
+ return getPatternPreview(pattern, value, locale);
596
+ };
597
+
598
+ //#endregion
599
+ //#region \0@oxc-project+runtime@0.124.0/helpers/typeof.js
600
+ function _typeof(o) {
601
+ "@babel/helpers - typeof";
602
+ return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
603
+ return typeof o;
604
+ } : function(o) {
605
+ return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
606
+ }, _typeof(o);
607
+ }
608
+
609
+ //#endregion
610
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPrimitive.js
611
+ function toPrimitive(t, r) {
612
+ if ("object" != _typeof(t) || !t) return t;
613
+ var e = t[Symbol.toPrimitive];
614
+ if (void 0 !== e) {
615
+ var i = e.call(t, r || "default");
616
+ if ("object" != _typeof(i)) return i;
617
+ throw new TypeError("@@toPrimitive must return a primitive value.");
618
+ }
619
+ return ("string" === r ? String : Number)(t);
620
+ }
621
+
622
+ //#endregion
623
+ //#region \0@oxc-project+runtime@0.124.0/helpers/toPropertyKey.js
624
+ function toPropertyKey(t) {
625
+ var i = toPrimitive(t, "string");
626
+ return "symbol" == _typeof(i) ? i : i + "";
627
+ }
628
+
629
+ //#endregion
630
+ //#region \0@oxc-project+runtime@0.124.0/helpers/defineProperty.js
631
+ function _defineProperty(e, r, t) {
632
+ return (r = toPropertyKey(r)) in e ? Object.defineProperty(e, r, {
633
+ value: t,
634
+ enumerable: !0,
635
+ configurable: !0,
636
+ writable: !0
637
+ }) : e[r] = t, e;
638
+ }
639
+
640
+ //#endregion
641
+ //#region \0@oxc-project+runtime@0.124.0/helpers/decorateParam.js
642
+ function __decorateParam(paramIndex, decorator) {
643
+ return function(target, key) {
644
+ decorator(target, key, paramIndex);
645
+ };
646
+ }
647
+
648
+ //#endregion
649
+ //#region \0@oxc-project+runtime@0.124.0/helpers/decorate.js
650
+ function __decorate(decorators, target, key, desc) {
651
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
652
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
653
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
654
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
655
+ }
656
+
657
+ //#endregion
658
+ //#region src/controllers/numfmt-cell-content.controller.ts
659
+ const TEXT_FORMAT_MARK = { tl: {
660
+ size: 6,
661
+ color: "#409f11"
662
+ } };
663
+ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController extends Disposable {
664
+ constructor(_instanceService, _sheetInterceptorService, _themeService, _commandService, _numfmtService, _localeService, _configService) {
665
+ super();
666
+ this._instanceService = _instanceService;
667
+ this._sheetInterceptorService = _sheetInterceptorService;
668
+ this._themeService = _themeService;
669
+ this._commandService = _commandService;
670
+ this._numfmtService = _numfmtService;
671
+ this._localeService = _localeService;
672
+ this._configService = _configService;
673
+ _defineProperty(this, "_locale$", new BehaviorSubject("en"));
674
+ _defineProperty(this, "locale$", this._locale$.asObservable());
675
+ this._initInterceptorCellContent();
676
+ }
677
+ get locale() {
678
+ const _locale = this._locale$.getValue();
679
+ if (_locale) return _locale;
680
+ switch (this._localeService.getCurrentLocale()) {
681
+ case LocaleType.FR_FR: return "fr";
682
+ case LocaleType.RU_RU: return "ru";
683
+ case LocaleType.VI_VN: return "vi";
684
+ case LocaleType.ZH_CN: return "zh-CN";
685
+ case LocaleType.KO_KR: return "ko";
686
+ case LocaleType.ZH_TW: return "zh-TW";
687
+ case LocaleType.ES_ES:
688
+ case LocaleType.CA_ES: return "es";
689
+ case LocaleType.SK_SK: return "sk";
690
+ case LocaleType.EN_US:
691
+ case LocaleType.FA_IR:
692
+ default: return "en";
693
+ }
694
+ }
695
+ _initInterceptorCellContent() {
696
+ const renderCache = new ObjectMatrix();
697
+ this.disposeWithMe(merge$1(this._locale$, this._localeService.currentLocale$).subscribe(() => {
698
+ renderCache.reset();
699
+ }));
700
+ this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
701
+ effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
702
+ handler: (cell, location, next) => {
703
+ if (!cell || cell.v === void 0 || cell.v === null || cell.t === CellValueType.BOOLEAN || cell.t === CellValueType.FORCE_STRING) return next(cell);
704
+ const unitId = location.unitId;
705
+ const sheetId = location.subUnitId;
706
+ let numfmtValue;
707
+ if (cell === null || cell === void 0 ? void 0 : cell.s) {
708
+ const style = location.workbook.getStyles().get(cell.s);
709
+ if (style === null || style === void 0 ? void 0 : style.n) numfmtValue = style.n;
710
+ }
711
+ if (!numfmtValue) numfmtValue = this._numfmtService.getValue(unitId, sheetId, location.row, location.col);
712
+ if (isDefaultFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) return next(cell);
713
+ if (cell.t !== CellValueType.NUMBER) {
714
+ if (checkCellValueType(cell.v, cell.t) !== CellValueType.NUMBER) return next(cell);
715
+ }
716
+ const originCellValue = cell;
717
+ if (!cell || cell === location.rawData) cell = { ...location.rawData };
718
+ if (isTextFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) {
719
+ var _this$_configService$;
720
+ if ((_this$_configService$ = this._configService.getConfig("sheets-numfmt.config")) === null || _this$_configService$ === void 0 ? void 0 : _this$_configService$.disableTextFormatMark) {
721
+ cell.t = CellValueType.STRING;
722
+ return next(cell);
723
+ }
724
+ cell.t = CellValueType.STRING;
725
+ cell.markers = {
726
+ ...cell === null || cell === void 0 ? void 0 : cell.markers,
727
+ ...TEXT_FORMAT_MARK
728
+ };
729
+ return next(cell);
730
+ }
731
+ let numfmtRes = "";
732
+ const cache = renderCache.getValue(location.row, location.col);
733
+ if (cache && cache.parameters === `${originCellValue.v}_${numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern}`) return next({
734
+ ...cell,
735
+ ...cache.result
736
+ });
737
+ const info = getPatternPreviewIgnoreGeneral(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern, Number(originCellValue.v), this.locale);
738
+ numfmtRes = info.result;
739
+ if (!numfmtRes) return next(cell);
740
+ const res = {
741
+ v: numfmtRes,
742
+ t: CellValueType.NUMBER
743
+ };
744
+ if (info.color) {
745
+ var _this$_themeService$g;
746
+ const color = (_this$_themeService$g = this._themeService.getColorFromTheme(`${info.color}.500`)) !== null && _this$_themeService$g !== void 0 ? _this$_themeService$g : info.color;
747
+ if (color) res.interceptorStyle = { cl: { rgb: color } };
748
+ }
749
+ renderCache.setValue(location.row, location.col, {
750
+ result: res,
751
+ parameters: `${originCellValue.v}_${numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern}`
752
+ });
753
+ Object.assign(cell, res);
754
+ return next(cell);
755
+ },
756
+ priority: InterceptCellContentPriority.NUMFMT
757
+ }));
758
+ this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
759
+ if (commandInfo.id === SetNumfmtMutation.id) {
760
+ const params = commandInfo.params;
761
+ Object.keys(params.values).forEach((key) => {
762
+ params.values[key].ranges.forEach((range) => {
763
+ Range.foreach(range, (row, col) => {
764
+ renderCache.realDeleteValue(row, col);
765
+ });
766
+ });
767
+ });
768
+ } else if (commandInfo.id === SetRangeValuesMutation.id) {
769
+ const params = commandInfo.params;
770
+ new ObjectMatrix(params.cellValue).forValue((row, col) => {
771
+ renderCache.realDeleteValue(row, col);
772
+ });
773
+ }
774
+ }));
775
+ this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SHEET).pipe(switchMap((workbook) => {
776
+ var _workbook$activeSheet;
777
+ return (_workbook$activeSheet = workbook === null || workbook === void 0 ? void 0 : workbook.activeSheet$) !== null && _workbook$activeSheet !== void 0 ? _workbook$activeSheet : of(null);
778
+ }), skip(1)).subscribe(() => renderCache.reset()));
779
+ }
780
+ setNumfmtLocal(locale) {
781
+ this._locale$.next(locale);
782
+ }
783
+ };
784
+ SheetsNumfmtCellContentController = __decorate([
785
+ __decorateParam(0, IUniverInstanceService),
786
+ __decorateParam(1, Inject(SheetInterceptorService)),
787
+ __decorateParam(2, Inject(ThemeService)),
788
+ __decorateParam(3, Inject(ICommandService)),
789
+ __decorateParam(4, Inject(INumfmtService)),
790
+ __decorateParam(5, Inject(LocaleService)),
791
+ __decorateParam(6, IConfigService)
792
+ ], SheetsNumfmtCellContentController);
793
+
794
+ //#endregion
795
+ //#region package.json
796
+ var name = "@univerjs/sheets-numfmt";
797
+ var version = "0.21.0";
798
+
799
+ //#endregion
800
+ //#region src/base/const/plugin-name.ts
801
+ /**
802
+ * Copyright 2023-present DreamNum Co., Ltd.
803
+ *
804
+ * Licensed under the Apache License, Version 2.0 (the "License");
805
+ * you may not use this file except in compliance with the License.
806
+ * You may obtain a copy of the License at
807
+ *
808
+ * http://www.apache.org/licenses/LICENSE-2.0
809
+ *
810
+ * Unless required by applicable law or agreed to in writing, software
811
+ * distributed under the License is distributed on an "AS IS" BASIS,
812
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
813
+ * See the License for the specific language governing permissions and
814
+ * limitations under the License.
815
+ */
816
+ const SHEET_NUMFMT_PLUGIN = "SHEET_NUMFMT_PLUGIN";
817
+
818
+ //#endregion
819
+ //#region src/plugin.ts
820
+ let UniverSheetsNumfmtPlugin = class UniverSheetsNumfmtPlugin extends Plugin {
821
+ constructor(_config = defaultPluginConfig, _injector, _configService, _commandService) {
822
+ super();
823
+ this._config = _config;
824
+ this._injector = _injector;
825
+ this._configService = _configService;
826
+ this._commandService = _commandService;
827
+ const { ...rest } = merge({}, defaultPluginConfig, this._config);
828
+ this._configService.setConfig(SHEETS_NUMFMT_PLUGIN_CONFIG_KEY, rest);
829
+ }
830
+ onStarting() {
831
+ registerDependencies(this._injector, [[SheetsNumfmtCellContentController]]);
832
+ touchDependencies(this._injector, [[SheetsNumfmtCellContentController]]);
833
+ [
834
+ AddDecimalCommand,
835
+ SubtractDecimalCommand,
836
+ SetCurrencyCommand,
837
+ SetPercentCommand,
838
+ SetNumfmtCommand
839
+ ].forEach((config) => {
840
+ this.disposeWithMe(this._commandService.registerCommand(config));
841
+ });
842
+ }
843
+ };
844
+ _defineProperty(UniverSheetsNumfmtPlugin, "pluginName", SHEET_NUMFMT_PLUGIN);
845
+ _defineProperty(UniverSheetsNumfmtPlugin, "packageName", name);
846
+ _defineProperty(UniverSheetsNumfmtPlugin, "version", version);
847
+ _defineProperty(UniverSheetsNumfmtPlugin, "type", UniverInstanceType.UNIVER_SHEET);
848
+ UniverSheetsNumfmtPlugin = __decorate([
849
+ DependentOn(UniverSheetsPlugin),
850
+ __decorateParam(1, Inject(Injector)),
851
+ __decorateParam(2, IConfigService),
852
+ __decorateParam(3, ICommandService)
853
+ ], UniverSheetsNumfmtPlugin);
854
+
855
+ //#endregion
856
+ //#region src/utils/currency.ts
857
+ /**
858
+ * Copyright 2023-present DreamNum Co., Ltd.
859
+ *
860
+ * Licensed under the Apache License, Version 2.0 (the "License");
861
+ * you may not use this file except in compliance with the License.
862
+ * You may obtain a copy of the License at
863
+ *
864
+ * http://www.apache.org/licenses/LICENSE-2.0
865
+ *
866
+ * Unless required by applicable law or agreed to in writing, software
867
+ * distributed under the License is distributed on an "AS IS" BASIS,
868
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
869
+ * See the License for the specific language governing permissions and
870
+ * limitations under the License.
871
+ */
872
+ const getCurrencyType = (pattern) => {
873
+ return currencySymbols.find((code) => pattern.includes(code));
874
+ };
875
+
876
+ //#endregion
877
+ //#region src/utils/options.ts
878
+ /**
879
+ * Copyright 2023-present DreamNum Co., Ltd.
880
+ *
881
+ * Licensed under the Apache License, Version 2.0 (the "License");
882
+ * you may not use this file except in compliance with the License.
883
+ * You may obtain a copy of the License at
884
+ *
885
+ * http://www.apache.org/licenses/LICENSE-2.0
886
+ *
887
+ * Unless required by applicable law or agreed to in writing, software
888
+ * distributed under the License is distributed on an "AS IS" BASIS,
889
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
890
+ * See the License for the specific language governing permissions and
891
+ * limitations under the License.
892
+ */
893
+ const getCurrencyOptions = () => currencySymbols.map((item) => ({
894
+ label: item,
895
+ value: item
896
+ }));
897
+ const getCurrencyFormatOptions = (suffix) => CURRENCYFORMAT.map((item) => ({
898
+ label: item.label(suffix),
899
+ value: item.suffix(suffix),
900
+ color: item.color
901
+ }));
902
+ const getDateFormatOptions = () => DATEFMTLISG.map((item) => ({
903
+ label: item.label,
904
+ value: item.suffix
905
+ }));
906
+ const getNumberFormatOptions = () => NUMBERFORMAT.map((item) => ({
907
+ label: item.label,
908
+ value: item.suffix,
909
+ color: item.color
910
+ }));
911
+
912
+ //#endregion
913
+ export { AddDecimalCommand, CURRENCYFORMAT, DATEFMTLISG, NUMBERFORMAT, SHEETS_NUMFMT_PLUGIN_CONFIG_KEY, SetCurrencyCommand, SetNumfmtCommand, SetPercentCommand, SheetsNumfmtCellContentController, SubtractDecimalCommand, UniverSheetsNumfmtPlugin, currencySymbols, getCurrencyFormat, getCurrencyFormatOptions, getCurrencyOptions, getCurrencySymbolByLocale, getCurrencySymbolIconByLocale, getCurrencyType, getDateFormatOptions, getDecimalFromPattern, getDecimalString, getNumberFormatOptions, getPatternPreview, getPatternPreviewIgnoreGeneral, getPatternType, isPatternHasDecimal, localeCurrencySymbolMap, setPatternDecimal };