@univerjs/sheets-numfmt 0.17.0 → 0.18.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/cjs/facade.js CHANGED
@@ -1 +1 @@
1
- "use strict";const c=require("@univerjs/sheets-numfmt"),u=require("@univerjs/sheets/facade");class l extends u.FRange{setNumberFormat(o){const t=[],{startColumn:e,startRow:n,endColumn:r,endRow:i}=this._range;for(let m=n;m<=i;m++)for(let s=e;s<=r;s++)t.push({row:m,col:s,pattern:o});return this._commandService.syncExecuteCommand(c.SetNumfmtCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId(),values:t}),this}setNumberFormats(o){var m;const t=[],{startColumn:e,startRow:n,endColumn:r,endRow:i}=this._range;for(let s=n;s<=i;s++)for(let a=e;a<=r;a++){const h=(m=o[s-n])==null?void 0:m[a-e];t.push({row:s,col:a,pattern:h})}return this._commandService.syncExecuteCommand(c.SetNumfmtCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId(),values:t}),this}getNumberFormat(){var t,e;const o=this.getCellStyle();return(e=(t=o==null?void 0:o.numberFormat)==null?void 0:t.pattern)!=null?e:""}getNumberFormats(){return this.getCellStyles().map(t=>t.map(e=>{var n,r;return(r=(n=e==null?void 0:e.numberFormat)==null?void 0:n.pattern)!=null?r:""}))}}u.FRange.extend(l);class C extends u.FWorkbook{setNumfmtLocal(o){return this._injector.get(c.SheetsNumfmtCellContentController).setNumfmtLocal(o),this}}u.FWorkbook.extend(C);
1
+ let e=require(`@univerjs/sheets-numfmt`),t=require(`@univerjs/sheets/facade`);var n=class extends t.FRange{setNumberFormat(t){let n=[],{startColumn:r,startRow:i,endColumn:a,endRow:o}=this._range;for(let e=i;e<=o;e++)for(let i=r;i<=a;i++)n.push({row:e,col:i,pattern:t});return this._commandService.syncExecuteCommand(e.SetNumfmtCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId(),values:n}),this}setNumberFormats(t){let n=[],{startColumn:r,startRow:i,endColumn:a,endRow:o}=this._range;for(let e=i;e<=o;e++)for(let o=r;o<=a;o++){var s;let a=(s=t[e-i])==null?void 0:s[o-r];n.push({row:e,col:o,pattern:a})}return this._commandService.syncExecuteCommand(e.SetNumfmtCommand.id,{unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId(),values:n}),this}getNumberFormat(){var e,t;let n=this.getCellStyle();return(e=n==null||(t=n.numberFormat)==null?void 0:t.pattern)==null?``:e}getNumberFormats(){return this.getCellStyles().map(e=>e.map(e=>{var t,n;return(t=e==null||(n=e.numberFormat)==null?void 0:n.pattern)==null?``:t}))}};t.FRange.extend(n);var r=class extends t.FWorkbook{setNumfmtLocal(t){return this._injector.get(e.SheetsNumfmtCellContentController).setNumfmtLocal(t),this}};t.FWorkbook.extend(r);
package/lib/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var X=Object.defineProperty;var J=(e,r,s)=>r in e?X(e,r,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[r]=s;var P=(e,r,s)=>J(e,typeof r!="symbol"?r+"":r,s);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("@univerjs/core"),f=require("@univerjs/sheets"),I=require("rxjs"),Q=require("@univerjs/engine-formula"),D=["$","£","¥","¤","֏","؋","৳","฿","៛","₡","₦","₩","₪","₫","€","₭","₮","₱","₲","₴","₸","₹","₺","₼","₽","₾","₿","﷼"],E=new Map([[t.LocaleType.EN_US,"$"],[t.LocaleType.RU_RU,"₽"],[t.LocaleType.VI_VN,"₫"],[t.LocaleType.ZH_CN,"¥"],[t.LocaleType.ZH_TW,"NT$"],[t.LocaleType.FR_FR,"€"],[t.LocaleType.FA_IR,"﷼"],[t.LocaleType.KO_KR,"₩"],[t.LocaleType.ES_ES,"€"],[t.LocaleType.CA_ES,"€"],[t.LocaleType.SK_SK,"€"]]);function j(e){switch(e){case t.LocaleType.CA_ES:case t.LocaleType.ES_ES:case t.LocaleType.FR_FR:case t.LocaleType.SK_SK:return{icon:"EuroIcon",symbol:E.get(e)||"€",locale:e};case t.LocaleType.RU_RU:return{icon:"RoubleIcon",symbol:E.get(e)||"₽",locale:e};case t.LocaleType.ZH_CN:return{icon:"RmbIcon",symbol:E.get(e)||"¥",locale:e};case t.LocaleType.EN_US:default:return{icon:"DollarIcon",symbol:"$",locale:t.LocaleType.EN_US}}}function F(e){return E.get(e)||"$"}function A(e,r=2){let s=r;r>127&&(s=127);let n="";return s>0&&(n=`.${"0".repeat(s)}`),`"${F(e)}"#,##0${n}_);[Red]("${F(e)}"#,##0${n})`}const B=[{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"}],G=[{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"}],H=[{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"}],$=(e,r=0)=>{var n;return e&&(n=t.numfmt.getFormatInfo(e).maxDecimals)!=null?n:r},x=e=>new Array(Math.min(Math.max(0,Number(e)),30)).fill(0).join(""),U=(e,r)=>e.split(";").map(n=>/\.0?/.test(n)?n.replace(/\.0*/g,`${r>0?".":""}${x(Number(r||0))}`):/0([^0]?)|0$/.test(n)?n.replace(/0([^0]+)|0$/,`0${r>0?".":""}${x(Number(r||0))}$1`):n).join(";"),ee=e=>/\.0?/.test(e)||/0([^0]?)|0$/.test(e),N={id:"sheet.command.numfmt.set.numfmt",type:t.CommandType.COMMAND,handler:(e,r)=>{if(!r)return!1;const s=e.get(t.ICommandService),n=e.get(t.IUniverInstanceService),a=e.get(t.IUndoRedoService),o=f.getSheetCommandTarget(n,r);if(!o)return!1;const{unitId:l,subUnitId:d,worksheet:i}=o,u=r.values.filter(c=>!!c.pattern),_=r.values.filter(c=>!c.pattern),T=f.transformCellsToRange(l,d,u),h={unitId:l,subUnitId:d,ranges:_.map(c=>({startColumn:c.col,startRow:c.row,endColumn:c.col,endRow:c.row}))},M=[],v=[];if(u.length){const c=u.reduce((g,m)=>{t.isTextFormat(m.pattern)&&g.setValue(m.row,m.col,{t:t.CellValueType.STRING});const C=i.getCellRaw(m.row,m.col);if(C){const R=f.checkCellValueType(C.v);R!==C.t&&g.setValue(m.row,m.col,{t:R})}return g},new t.ObjectMatrix).getMatrix(),p=new t.ObjectMatrix;new t.ObjectMatrix(c).forValue((g,m)=>{const C=i.getCellRaw(g,m);C?p.setValue(g,m,{t:C.t}):p.setValue(g,m,{t:void 0})}),Object.keys(T.values).forEach(g=>{const m=T.values[g];m.ranges=f.rangeMerge(m.ranges)}),M.push({id:f.SetNumfmtMutation.id,params:T});const y=f.factorySetNumfmtUndoMutation(e,T);v.push(...y)}if(_.length){h.ranges=f.rangeMerge(h.ranges);const c=_.reduce((g,m)=>{const C=i.getCellRaw(m.row,m.col);if(C){const R=f.checkCellValueType(C.v);R!==C.t&&g.setValue(m.row,m.col,{t:R})}return g},new t.ObjectMatrix).getMatrix(),p=new t.ObjectMatrix;new t.ObjectMatrix(c).forValue((g,m)=>{const C=i.getCellRaw(g,m);C?p.setValue(g,m,{t:C.t}):p.setValue(g,m,{t:void 0})}),M.push({id:f.RemoveNumfmtMutation.id,params:h},{id:f.SetRangeValuesMutation.id,params:{unitId:l,subUnitId:d,cellValue:c}});const y=f.factoryRemoveNumfmtUndoMutation(e,h);v.push({id:f.SetRangeValuesMutation.id,params:{unitId:l,subUnitId:d,cellValue:p.getMatrix()}},...y)}const S=t.sequenceExecute(M,s).result;return S&&a.pushUndoRedo({unitID:l,undoMutations:v,redoMutations:M}),S}},K={id:"sheet.command.numfmt.add.decimal.command",type:t.CommandType.COMMAND,handler:async e=>{const r=e.get(t.ICommandService),s=e.get(f.SheetsSelectionsService),n=e.get(f.INumfmtService),a=e.get(t.IUniverInstanceService),o=s.getCurrentSelections();if(!o||!o.length)return!1;const l=f.getSheetCommandTarget(a);if(!l)return!1;const{unitId:d,subUnitId:i}=l;let u=0;o.forEach(M=>{t.Range.foreach(M.range,(v,S)=>{const c=n.getValue(d,i,v,S);if(!c){const y=l.worksheet.getCellRaw(v,S);if(!u&&y&&y.t===t.CellValueType.NUMBER&&y.v){const g=/\.(\d*)$/.exec(String(y.v));if(g){const m=g[1].length;if(!m)return;u=Math.max(u,m)}}return}const p=$(c.pattern);u=p>u?p:u})});const _=u+1,T=U(`0${_>0?".0":""}`,_),h=[];return o.forEach(M=>{t.Range.foreach(M.range,(v,S)=>{const c=n.getValue(d,i,v,S);if(t.isDefaultFormat(c==null?void 0:c.pattern))h.push({row:v,col:S,pattern:T});else{const p=$(c.pattern),y=U(c.pattern,p+1);y!==c.pattern&&h.push({row:v,col:S,pattern:y})}})}),h.length?await r.executeCommand(N.id,{values:h}):!1}},W={id:"sheet.command.numfmt.set.currency",type:t.CommandType.COMMAND,handler:async e=>{const r=e.get(t.ICommandService),s=e.get(f.SheetsSelectionsService),n=e.get(t.LocaleService),a=s.getCurrentSelections();if(!a||!a.length)return!1;const o=[],l=j(n.getCurrentLocale()),d=A(l.locale);return a.forEach(u=>{t.Range.foreach(u.range,(_,T)=>{o.push({row:_,col:T,pattern:d,type:"currency"})})}),await r.executeCommand(N.id,{values:o})}},k={id:"sheet.command.numfmt.set.percent",type:t.CommandType.COMMAND,handler:async e=>{const r=e.get(t.ICommandService),n=e.get(f.SheetsSelectionsService).getCurrentSelections();if(!n||!n.length)return!1;const a=[],o="0%";return n.forEach(d=>{t.Range.foreach(d.range,(i,u)=>{a.push({row:i,col:u,pattern:o,type:"percent"})})}),await r.executeCommand(N.id,{values:a})}},q={id:"sheet.command.numfmt.subtract.decimal.command",type:t.CommandType.COMMAND,handler:async e=>{const r=e.get(t.ICommandService),s=e.get(f.SheetsSelectionsService),n=e.get(f.INumfmtService),a=e.get(t.IUniverInstanceService),o=s.getCurrentSelections();if(!o||!o.length)return!1;const l=f.getSheetCommandTarget(a);if(!l)return!1;const{unitId:d,subUnitId:i}=l;let u=0;o.forEach(v=>{t.Range.foreach(v.range,(S,c)=>{const p=n.getValue(d,i,S,c);if(!p){const g=l.worksheet.getCellRaw(S,c);if(!u&&g&&g.t===t.CellValueType.NUMBER&&g.v){const m=/\.(\d*)$/.exec(String(g.v));if(m){const C=m[1].length;if(!C)return;u=Math.max(u,C)}}return}const y=$(p.pattern);u=y>u?y:u})});const _=u-1,T=U(`0${_>0?".0":"."}`,_),h=[];return o.forEach(v=>{t.Range.foreach(v.range,(S,c)=>{const p=n.getValue(d,i,S,c);if(t.isDefaultFormat(p==null?void 0:p.pattern))h.push({row:S,col:c,pattern:T});else{const y=$(p.pattern);h.push({row:S,col:c,pattern:U(p.pattern,y-1)})}})}),await r.executeCommand(N.id,{values:h})}},w="sheets-numfmt.config",V={},te=e=>t.numfmt.getFormatInfo(e).type||"unknown",Z=(e,r,s="en")=>{try{const n=t.numfmt.formatColor(e,r),a=n?String(n):void 0,o=t.numfmt.format(e,r,{locale:s,throws:!1});return r<0?{result:o,color:a}:{result:o}}catch(n){console.warn("getPatternPreview error:",e,n)}return{result:String(r)}},Y=(e,r,s)=>e===t.DEFAULT_NUMBER_FORMAT?{result:String(Q.stripErrorMargin(r))}:Z(e,r,s);var ne=Object.getOwnPropertyDescriptor,re=(e,r,s,n)=>{for(var a=n>1?void 0:n?ne(r,s):r,o=e.length-1,l;o>=0;o--)(l=e[o])&&(a=l(a)||a);return a},b=(e,r)=>(s,n)=>r(s,n,e);const se={tl:{size:6,color:"#409f11"}};exports.SheetsNumfmtCellContentController=class extends t.Disposable{constructor(s,n,a,o,l,d,i){super();P(this,"_locale$",new I.BehaviorSubject("en"));P(this,"locale$",this._locale$.asObservable());this._instanceService=s,this._sheetInterceptorService=n,this._themeService=a,this._commandService=o,this._numfmtService=l,this._localeService=d,this._configService=i,this._initInterceptorCellContent()}get locale(){const s=this._locale$.getValue();if(s)return s;switch(this._localeService.getCurrentLocale()){case t.LocaleType.FR_FR:return"fr";case t.LocaleType.RU_RU:return"ru";case t.LocaleType.VI_VN:return"vi";case t.LocaleType.ZH_CN:return"zh-CN";case t.LocaleType.KO_KR:return"ko";case t.LocaleType.ZH_TW:return"zh-TW";case t.LocaleType.ES_ES:case t.LocaleType.CA_ES:return"es";case t.LocaleType.SK_SK:return"sk";case t.LocaleType.EN_US:case t.LocaleType.FA_IR:default:return"en"}}_initInterceptorCellContent(){const s=new t.ObjectMatrix;this.disposeWithMe(I.merge(this._locale$,this._localeService.currentLocale$).subscribe(()=>{s.reset()})),this.disposeWithMe(this._sheetInterceptorService.intercept(f.INTERCEPTOR_POINT.CELL_CONTENT,{effect:t.InterceptorEffectEnum.Value|t.InterceptorEffectEnum.Style,handler:(n,a,o)=>{var v,S;if(!n||n.v===void 0||n.v===null||n.t===t.CellValueType.BOOLEAN||n.t===t.CellValueType.FORCE_STRING)return o(n);const l=a.unitId,d=a.subUnitId;let i;if(n!=null&&n.s){const c=a.workbook.getStyles().get(n.s);c!=null&&c.n&&(i=c.n)}if(i||(i=this._numfmtService.getValue(l,d,a.row,a.col)),t.isDefaultFormat(i==null?void 0:i.pattern)||n.t!==t.CellValueType.NUMBER&&f.checkCellValueType(n.v,n.t)!==t.CellValueType.NUMBER)return o(n);const u=n;if((!n||n===a.rawData)&&(n={...a.rawData}),t.isTextFormat(i==null?void 0:i.pattern))return(v=this._configService.getConfig(w))!=null&&v.disableTextFormatMark?(n.t=t.CellValueType.STRING,o(n)):(n.t=t.CellValueType.STRING,n.markers={...n==null?void 0:n.markers,...se},o(n));let _="";const T=s.getValue(a.row,a.col);if(T&&T.parameters===`${u.v}_${i==null?void 0:i.pattern}`)return o({...n,...T.result});const h=Y(i==null?void 0:i.pattern,Number(u.v),this.locale);if(_=h.result,!_)return o(n);const M={v:_,t:t.CellValueType.NUMBER};if(h.color){const c=(S=this._themeService.getColorFromTheme(`${h.color}.500`))!=null?S:h.color;c&&(M.interceptorStyle={cl:{rgb:c}})}return s.setValue(a.row,a.col,{result:M,parameters:`${u.v}_${i==null?void 0:i.pattern}`}),Object.assign(n,M),o(n)},priority:f.InterceptCellContentPriority.NUMFMT})),this.disposeWithMe(this._commandService.onCommandExecuted(n=>{if(n.id===f.SetNumfmtMutation.id){const a=n.params;Object.keys(a.values).forEach(o=>{a.values[o].ranges.forEach(d=>{t.Range.foreach(d,(i,u)=>{s.realDeleteValue(i,u)})})})}else if(n.id===f.SetRangeValuesMutation.id){const a=n.params;new t.ObjectMatrix(a.cellValue).forValue((o,l)=>{s.realDeleteValue(o,l)})}})),this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(t.UniverInstanceType.UNIVER_SHEET).pipe(I.switchMap(n=>{var a;return(a=n==null?void 0:n.activeSheet$)!=null?a:I.of(null)}),I.skip(1)).subscribe(()=>s.reset()))}setNumfmtLocal(s){this._locale$.next(s)}};exports.SheetsNumfmtCellContentController=re([b(0,t.IUniverInstanceService),b(1,t.Inject(f.SheetInterceptorService)),b(2,t.Inject(t.ThemeService)),b(3,t.Inject(t.ICommandService)),b(4,t.Inject(f.INumfmtService)),b(5,t.Inject(t.LocaleService)),b(6,t.IConfigService)],exports.SheetsNumfmtCellContentController);const ae="@univerjs/sheets-numfmt",oe="0.17.0",z={name:ae,version:oe},ce="SHEET_NUMFMT_PLUGIN";var ie=Object.defineProperty,le=Object.getOwnPropertyDescriptor,ue=(e,r,s)=>r in e?ie(e,r,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[r]=s,me=(e,r,s,n)=>{for(var a=n>1?void 0:n?le(r,s):r,o=e.length-1,l;o>=0;o--)(l=e[o])&&(a=l(a)||a);return a},L=(e,r)=>(s,n)=>r(s,n,e),O=(e,r,s)=>ue(e,typeof r!="symbol"?r+"":r,s);exports.UniverSheetsNumfmtPlugin=class extends t.Plugin{constructor(r=V,s,n,a){super(),this._config=r,this._injector=s,this._configService=n,this._commandService=a;const{...o}=t.merge({},V,this._config);this._configService.setConfig(w,o)}onStarting(){t.registerDependencies(this._injector,[[exports.SheetsNumfmtCellContentController]]),t.touchDependencies(this._injector,[[exports.SheetsNumfmtCellContentController]]),[K,q,W,k,N].forEach(r=>{this.disposeWithMe(this._commandService.registerCommand(r))})}};O(exports.UniverSheetsNumfmtPlugin,"pluginName",ce);O(exports.UniverSheetsNumfmtPlugin,"packageName",z.name);O(exports.UniverSheetsNumfmtPlugin,"version",z.version);O(exports.UniverSheetsNumfmtPlugin,"type",t.UniverInstanceType.UNIVER_SHEET);exports.UniverSheetsNumfmtPlugin=me([t.DependentOn(f.UniverSheetsPlugin),L(1,t.Inject(t.Injector)),L(2,t.IConfigService),L(3,t.ICommandService)],exports.UniverSheetsNumfmtPlugin);const fe=e=>D.find(s=>e.includes(s)),ge=()=>D.map(e=>({label:e,value:e})),de=e=>H.map(r=>({label:r.label(e),value:r.suffix(e),color:r.color})),pe=()=>B.map(e=>({label:e.label,value:e.suffix})),Se=()=>G.map(e=>({label:e.label,value:e.suffix,color:e.color}));exports.AddDecimalCommand=K;exports.CURRENCYFORMAT=H;exports.DATEFMTLISG=B;exports.NUMBERFORMAT=G;exports.SHEETS_NUMFMT_PLUGIN_CONFIG_KEY=w;exports.SetCurrencyCommand=W;exports.SetNumfmtCommand=N;exports.SetPercentCommand=k;exports.SubtractDecimalCommand=q;exports.currencySymbols=D;exports.getCurrencyFormat=A;exports.getCurrencyFormatOptions=de;exports.getCurrencyOptions=ge;exports.getCurrencySymbolByLocale=F;exports.getCurrencySymbolIconByLocale=j;exports.getCurrencyType=fe;exports.getDateFormatOptions=pe;exports.getDecimalFromPattern=$;exports.getDecimalString=x;exports.getNumberFormatOptions=Se;exports.getPatternPreview=Z;exports.getPatternPreviewIgnoreGeneral=Y;exports.getPatternType=te;exports.isPatternHasDecimal=ee;exports.localeCurrencySymbolMap=E;exports.setPatternDecimal=U;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require(`@univerjs/core`),t=require(`@univerjs/sheets`),n=require(`rxjs`),r=require(`@univerjs/engine-formula`);const i=`$.£.¥.¤.֏.؋.৳.฿.៛.₡.₦.₩.₪.₫.€.₭.₮.₱.₲.₴.₸.₹.₺.₼.₽.₾.₿.﷼`.split(`.`),a=new Map([[e.LocaleType.EN_US,`$`],[e.LocaleType.RU_RU,`₽`],[e.LocaleType.VI_VN,`₫`],[e.LocaleType.ZH_CN,`¥`],[e.LocaleType.ZH_TW,`NT$`],[e.LocaleType.FR_FR,`€`],[e.LocaleType.FA_IR,`﷼`],[e.LocaleType.KO_KR,`₩`],[e.LocaleType.ES_ES,`€`],[e.LocaleType.CA_ES,`€`],[e.LocaleType.SK_SK,`€`]]);function o(t){switch(t){case e.LocaleType.CA_ES:case e.LocaleType.ES_ES:case e.LocaleType.FR_FR:case e.LocaleType.SK_SK:return{icon:`EuroIcon`,symbol:a.get(t)||`€`,locale:t};case e.LocaleType.RU_RU:return{icon:`RoubleIcon`,symbol:a.get(t)||`₽`,locale:t};case e.LocaleType.ZH_CN:return{icon:`RmbIcon`,symbol:a.get(t)||`¥`,locale:t};case e.LocaleType.EN_US:default:return{icon:`DollarIcon`,symbol:`$`,locale:e.LocaleType.EN_US}}}function s(e){return a.get(e)||`$`}function c(e,t=2){let n=t;t>127&&(n=127);let r=``;return n>0&&(r=`.${`0`.repeat(n)}`),`"${s(e)}"#,##0${r}_);[Red]("${s(e)}"#,##0${r})`}const l=[{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`}],u=[{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`}],d=[{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`}],f=(t,n=0)=>{var r;return t?(r=e.numfmt.getFormatInfo(t).maxDecimals)==null?n:r:n},p=e=>Array(Math.min(Math.max(0,Number(e)),30)).fill(0).join(``),m=(e,t)=>e.split(`;`).map(e=>/\.0?/.test(e)?e.replace(/\.0*/g,`${t>0?`.`:``}${p(Number(t||0))}`):/0([^0]?)|0$/.test(e)?e.replace(/0([^0]+)|0$/,`0${t>0?`.`:``}${p(Number(t||0))}$1`):e).join(`;`),h=e=>/\.0?/.test(e)||/0([^0]?)|0$/.test(e),g={id:`sheet.command.numfmt.set.numfmt`,type:e.CommandType.COMMAND,handler:(n,r)=>{if(!r)return!1;let i=n.get(e.ICommandService),a=n.get(e.IUniverInstanceService),o=n.get(e.IUndoRedoService),s=(0,t.getSheetCommandTarget)(a,r);if(!s)return!1;let{unitId:c,subUnitId:l,worksheet:u}=s,d=r.values.filter(e=>!!e.pattern),f=r.values.filter(e=>!e.pattern),p=(0,t.transformCellsToRange)(c,l,d),m={unitId:c,subUnitId:l,ranges:f.map(e=>({startColumn:e.col,startRow:e.row,endColumn:e.col,endRow:e.row}))},h=[],g=[];if(d.length){let r=d.reduce((n,r)=>{(0,e.isTextFormat)(r.pattern)&&n.setValue(r.row,r.col,{t:e.CellValueType.STRING});let i=u.getCellRaw(r.row,r.col);if(i){let e=(0,t.checkCellValueType)(i.v);e!==i.t&&n.setValue(r.row,r.col,{t:e})}return n},new e.ObjectMatrix).getMatrix(),i=new e.ObjectMatrix;new e.ObjectMatrix(r).forValue((e,t)=>{let n=u.getCellRaw(e,t);n?i.setValue(e,t,{t:n.t}):i.setValue(e,t,{t:void 0})}),Object.keys(p.values).forEach(e=>{let n=p.values[e];n.ranges=(0,t.rangeMerge)(n.ranges)}),h.push({id:t.SetNumfmtMutation.id,params:p});let a=(0,t.factorySetNumfmtUndoMutation)(n,p);g.push(...a)}if(f.length){m.ranges=(0,t.rangeMerge)(m.ranges);let r=f.reduce((e,n)=>{let r=u.getCellRaw(n.row,n.col);if(r){let i=(0,t.checkCellValueType)(r.v);i!==r.t&&e.setValue(n.row,n.col,{t:i})}return e},new e.ObjectMatrix).getMatrix(),i=new e.ObjectMatrix;new e.ObjectMatrix(r).forValue((e,t)=>{let n=u.getCellRaw(e,t);n?i.setValue(e,t,{t:n.t}):i.setValue(e,t,{t:void 0})}),h.push({id:t.RemoveNumfmtMutation.id,params:m},{id:t.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:r}});let a=(0,t.factoryRemoveNumfmtUndoMutation)(n,m);g.push({id:t.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:i.getMatrix()}},...a)}let _=(0,e.sequenceExecute)(h,i).result;return _&&o.pushUndoRedo({unitID:c,undoMutations:g,redoMutations:h}),_}},_={id:`sheet.command.numfmt.add.decimal.command`,type:e.CommandType.COMMAND,handler:async n=>{let r=n.get(e.ICommandService),i=n.get(t.SheetsSelectionsService),a=n.get(t.INumfmtService),o=n.get(e.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,t.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(t=>{e.Range.foreach(t.range,(t,n)=>{let r=a.getValue(l,u,t,n);if(!r){let r=c.worksheet.getCellRaw(t,n);if(!d&&r&&r.t===e.CellValueType.NUMBER&&r.v){let e=/\.(\d*)$/.exec(String(r.v));if(e){let t=e[1].length;if(!t)return;d=Math.max(d,t)}}return}let i=f(r.pattern);d=i>d?i:d})});let p=d+1,h=m(`0${p>0?`.0`:``}`,p),_=[];return s.forEach(t=>{e.Range.foreach(t.range,(t,n)=>{let r=a.getValue(l,u,t,n);if((0,e.isDefaultFormat)(r==null?void 0:r.pattern))_.push({row:t,col:n,pattern:h});else{let e=f(r.pattern),i=m(r.pattern,e+1);i!==r.pattern&&_.push({row:t,col:n,pattern:i})}})}),_.length?await r.executeCommand(g.id,{values:_}):!1}},v={id:`sheet.command.numfmt.set.currency`,type:e.CommandType.COMMAND,handler:async n=>{let r=n.get(e.ICommandService),i=n.get(t.SheetsSelectionsService),a=n.get(e.LocaleService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let l=[],u=c(o(a.getCurrentLocale()).locale);return s.forEach(t=>{e.Range.foreach(t.range,(e,t)=>{l.push({row:e,col:t,pattern:u,type:`currency`})})}),await r.executeCommand(g.id,{values:l})}},y={id:`sheet.command.numfmt.set.percent`,type:e.CommandType.COMMAND,handler:async n=>{let r=n.get(e.ICommandService),i=n.get(t.SheetsSelectionsService).getCurrentSelections();if(!i||!i.length)return!1;let a=[];return i.forEach(t=>{e.Range.foreach(t.range,(e,t)=>{a.push({row:e,col:t,pattern:`0%`,type:`percent`})})}),await r.executeCommand(g.id,{values:a})}},b={id:`sheet.command.numfmt.subtract.decimal.command`,type:e.CommandType.COMMAND,handler:async n=>{let r=n.get(e.ICommandService),i=n.get(t.SheetsSelectionsService),a=n.get(t.INumfmtService),o=n.get(e.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,t.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(t=>{e.Range.foreach(t.range,(t,n)=>{let r=a.getValue(l,u,t,n);if(!r){let r=c.worksheet.getCellRaw(t,n);if(!d&&r&&r.t===e.CellValueType.NUMBER&&r.v){let e=/\.(\d*)$/.exec(String(r.v));if(e){let t=e[1].length;if(!t)return;d=Math.max(d,t)}}return}let i=f(r.pattern);d=i>d?i:d})});let p=d-1,h=m(`0${p>0?`.0`:`.`}`,p),_=[];return s.forEach(t=>{e.Range.foreach(t.range,(t,n)=>{let r=a.getValue(l,u,t,n);if((0,e.isDefaultFormat)(r==null?void 0:r.pattern))_.push({row:t,col:n,pattern:h});else{let e=f(r.pattern);_.push({row:t,col:n,pattern:m(r.pattern,e-1)})}})}),await r.executeCommand(g.id,{values:_})}},x=`sheets-numfmt.config`;Symbol(x);const S={},C=t=>e.numfmt.getFormatInfo(t).type||`unknown`,w=(t,n,r=`en`)=>{try{let i=e.numfmt.formatColor(t,n),a=i?String(i):void 0,o=e.numfmt.format(t,n,{locale:r,throws:!1});return n<0?{result:o,color:a}:{result:o}}catch(e){console.warn(`getPatternPreview error:`,t,e)}return{result:String(n)}},T=(t,n,i)=>t===e.DEFAULT_NUMBER_FORMAT?{result:String((0,r.stripErrorMargin)(n))}:w(t,n,i);function E(e){"@babel/helpers - typeof";return E=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},E(e)}function D(e,t){if(E(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(E(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function O(e){var t=D(e,`string`);return E(t)==`symbol`?t:t+``}function k(e,t,n){return(t=O(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function A(e,t){return function(n,r){t(n,r,e)}}function j(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 M={tl:{size:6,color:`#409f11`}};let N=class extends e.Disposable{constructor(e,t,r,i,a,o,s){super(),this._instanceService=e,this._sheetInterceptorService=t,this._themeService=r,this._commandService=i,this._numfmtService=a,this._localeService=o,this._configService=s,k(this,`_locale$`,new n.BehaviorSubject(`en`)),k(this,`locale$`,this._locale$.asObservable()),this._initInterceptorCellContent()}get locale(){let t=this._locale$.getValue();if(t)return t;switch(this._localeService.getCurrentLocale()){case e.LocaleType.FR_FR:return`fr`;case e.LocaleType.RU_RU:return`ru`;case e.LocaleType.VI_VN:return`vi`;case e.LocaleType.ZH_CN:return`zh-CN`;case e.LocaleType.KO_KR:return`ko`;case e.LocaleType.ZH_TW:return`zh-TW`;case e.LocaleType.ES_ES:case e.LocaleType.CA_ES:return`es`;case e.LocaleType.SK_SK:return`sk`;case e.LocaleType.EN_US:case e.LocaleType.FA_IR:default:return`en`}}_initInterceptorCellContent(){let r=new e.ObjectMatrix;this.disposeWithMe((0,n.merge)(this._locale$,this._localeService.currentLocale$).subscribe(()=>{r.reset()})),this.disposeWithMe(this._sheetInterceptorService.intercept(t.INTERCEPTOR_POINT.CELL_CONTENT,{effect:e.InterceptorEffectEnum.Value|e.InterceptorEffectEnum.Style,handler:(n,i,a)=>{if(!n||n.v===void 0||n.v===null||n.t===e.CellValueType.BOOLEAN||n.t===e.CellValueType.FORCE_STRING)return a(n);let o=i.unitId,s=i.subUnitId,c;if(n!=null&&n.s){let e=i.workbook.getStyles().get(n.s);e!=null&&e.n&&(c=e.n)}if(c||(c=this._numfmtService.getValue(o,s,i.row,i.col)),(0,e.isDefaultFormat)(c==null?void 0:c.pattern)||n.t!==e.CellValueType.NUMBER&&(0,t.checkCellValueType)(n.v,n.t)!==e.CellValueType.NUMBER)return a(n);let l=n;if((!n||n===i.rawData)&&(n={...i.rawData}),(0,e.isTextFormat)(c==null?void 0:c.pattern)){var u;return(u=this._configService.getConfig(`sheets-numfmt.config`))!=null&&u.disableTextFormatMark?(n.t=e.CellValueType.STRING,a(n)):(n.t=e.CellValueType.STRING,n.markers={...n==null?void 0:n.markers,...M},a(n))}let d=``,f=r.getValue(i.row,i.col);if(f&&f.parameters===`${l.v}_${c==null?void 0:c.pattern}`)return a({...n,...f.result});let p=T(c==null?void 0:c.pattern,Number(l.v),this.locale);if(d=p.result,!d)return a(n);let m={v:d,t:e.CellValueType.NUMBER};if(p.color){var h;let e=(h=this._themeService.getColorFromTheme(`${p.color}.500`))==null?p.color:h;e&&(m.interceptorStyle={cl:{rgb:e}})}return r.setValue(i.row,i.col,{result:m,parameters:`${l.v}_${c==null?void 0:c.pattern}`}),Object.assign(n,m),a(n)},priority:t.InterceptCellContentPriority.NUMFMT})),this.disposeWithMe(this._commandService.onCommandExecuted(n=>{if(n.id===t.SetNumfmtMutation.id){let t=n.params;Object.keys(t.values).forEach(n=>{t.values[n].ranges.forEach(t=>{e.Range.foreach(t,(e,t)=>{r.realDeleteValue(e,t)})})})}else if(n.id===t.SetRangeValuesMutation.id){let t=n.params;new e.ObjectMatrix(t.cellValue).forValue((e,t)=>{r.realDeleteValue(e,t)})}})),this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(e.UniverInstanceType.UNIVER_SHEET).pipe((0,n.switchMap)(e=>{var t;return(t=e==null?void 0:e.activeSheet$)==null?(0,n.of)(null):t}),(0,n.skip)(1)).subscribe(()=>r.reset()))}setNumfmtLocal(e){this._locale$.next(e)}};N=j([A(0,e.IUniverInstanceService),A(1,(0,e.Inject)(t.SheetInterceptorService)),A(2,(0,e.Inject)(e.ThemeService)),A(3,(0,e.Inject)(e.ICommandService)),A(4,(0,e.Inject)(t.INumfmtService)),A(5,(0,e.Inject)(e.LocaleService)),A(6,e.IConfigService)],N);var P=`@univerjs/sheets-numfmt`,F=`0.18.0`;let I=class extends e.Plugin{constructor(t=S,n,r,i){super(),this._config=t,this._injector=n,this._configService=r,this._commandService=i;let{...a}=(0,e.merge)({},S,this._config);this._configService.setConfig(x,a)}onStarting(){(0,e.registerDependencies)(this._injector,[[N]]),(0,e.touchDependencies)(this._injector,[[N]]),[_,b,v,y,g].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}};k(I,`pluginName`,`SHEET_NUMFMT_PLUGIN`),k(I,`packageName`,P),k(I,`version`,F),k(I,`type`,e.UniverInstanceType.UNIVER_SHEET),I=j([(0,e.DependentOn)(t.UniverSheetsPlugin),A(1,(0,e.Inject)(e.Injector)),A(2,e.IConfigService),A(3,e.ICommandService)],I);const L=e=>i.find(t=>e.includes(t)),R=()=>i.map(e=>({label:e,value:e})),z=e=>d.map(t=>({label:t.label(e),value:t.suffix(e),color:t.color})),B=()=>l.map(e=>({label:e.label,value:e.suffix})),V=()=>u.map(e=>({label:e.label,value:e.suffix,color:e.color}));exports.AddDecimalCommand=_,exports.CURRENCYFORMAT=d,exports.DATEFMTLISG=l,exports.NUMBERFORMAT=u,exports.SHEETS_NUMFMT_PLUGIN_CONFIG_KEY=x,exports.SetCurrencyCommand=v,exports.SetNumfmtCommand=g,exports.SetPercentCommand=y,Object.defineProperty(exports,`SheetsNumfmtCellContentController`,{enumerable:!0,get:function(){return N}}),exports.SubtractDecimalCommand=b,Object.defineProperty(exports,`UniverSheetsNumfmtPlugin`,{enumerable:!0,get:function(){return I}}),exports.currencySymbols=i,exports.getCurrencyFormat=c,exports.getCurrencyFormatOptions=z,exports.getCurrencyOptions=R,exports.getCurrencySymbolByLocale=s,exports.getCurrencySymbolIconByLocale=o,exports.getCurrencyType=L,exports.getDateFormatOptions=B,exports.getDecimalFromPattern=f,exports.getDecimalString=p,exports.getNumberFormatOptions=V,exports.getPatternPreview=w,exports.getPatternPreviewIgnoreGeneral=T,exports.getPatternType=C,exports.isPatternHasDecimal=h,exports.localeCurrencySymbolMap=a,exports.setPatternDecimal=m;
package/lib/es/facade.js CHANGED
@@ -1,47 +1 @@
1
- import { SetNumfmtCommand as i, SheetsNumfmtCellContentController as C } from "@univerjs/sheets-numfmt";
2
- import { FRange as d, FWorkbook as h } from "@univerjs/sheets/facade";
3
- class g extends d {
4
- setNumberFormat(o) {
5
- const t = [], { startColumn: e, startRow: s, endColumn: r, endRow: u } = this._range;
6
- for (let m = s; m <= u; m++)
7
- for (let n = e; n <= r; n++)
8
- t.push({ row: m, col: n, pattern: o });
9
- return this._commandService.syncExecuteCommand(i.id, {
10
- unitId: this._workbook.getUnitId(),
11
- subUnitId: this._worksheet.getSheetId(),
12
- values: t
13
- }), this;
14
- }
15
- setNumberFormats(o) {
16
- var m;
17
- const t = [], { startColumn: e, startRow: s, endColumn: r, endRow: u } = this._range;
18
- for (let n = s; n <= u; n++)
19
- for (let a = e; a <= r; a++) {
20
- const c = (m = o[n - s]) == null ? void 0 : m[a - e];
21
- t.push({ row: n, col: a, pattern: c });
22
- }
23
- return this._commandService.syncExecuteCommand(i.id, {
24
- unitId: this._workbook.getUnitId(),
25
- subUnitId: this._worksheet.getSheetId(),
26
- values: t
27
- }), this;
28
- }
29
- getNumberFormat() {
30
- var t, e;
31
- const o = this.getCellStyle();
32
- return (e = (t = o == null ? void 0 : o.numberFormat) == null ? void 0 : t.pattern) != null ? e : "";
33
- }
34
- getNumberFormats() {
35
- return this.getCellStyles().map((t) => t.map((e) => {
36
- var s, r;
37
- return (r = (s = e == null ? void 0 : e.numberFormat) == null ? void 0 : s.pattern) != null ? r : "";
38
- }));
39
- }
40
- }
41
- d.extend(g);
42
- class b extends h {
43
- setNumfmtLocal(o) {
44
- return this._injector.get(C).setNumfmtLocal(o), this;
45
- }
46
- }
47
- h.extend(b);
1
+ import{SetNumfmtCommand as e,SheetsNumfmtCellContentController as t}from"@univerjs/sheets-numfmt";import{FRange as n,FWorkbook as r}from"@univerjs/sheets/facade";var i=class extends n{setNumberFormat(t){let n=[],{startColumn:r,startRow:i,endColumn:a,endRow:o}=this._range;for(let e=i;e<=o;e++)for(let i=r;i<=a;i++)n.push({row:e,col:i,pattern:t});return this._commandService.syncExecuteCommand(e.id,{unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId(),values:n}),this}setNumberFormats(t){let n=[],{startColumn:r,startRow:i,endColumn:a,endRow:o}=this._range;for(let e=i;e<=o;e++)for(let o=r;o<=a;o++){var s;let a=(s=t[e-i])==null?void 0:s[o-r];n.push({row:e,col:o,pattern:a})}return this._commandService.syncExecuteCommand(e.id,{unitId:this._workbook.getUnitId(),subUnitId:this._worksheet.getSheetId(),values:n}),this}getNumberFormat(){var e,t;let n=this.getCellStyle();return(e=n==null||(t=n.numberFormat)==null?void 0:t.pattern)==null?``:e}getNumberFormats(){return this.getCellStyles().map(e=>e.map(e=>{var t,n;return(t=e==null||(n=e.numberFormat)==null?void 0:n.pattern)==null?``:t}))}};n.extend(i);var a=class extends r{setNumfmtLocal(e){return this._injector.get(t).setNumfmtLocal(e),this}};r.extend(a);export{};