@univerjs/sheets-numfmt 1.2.0 → 1.3.1
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/index.js +63 -73
- package/lib/es/index.js +63 -73
- package/lib/index.js +63 -73
- package/lib/types/controllers/numfmt-cell-content.controller.d.ts +3 -0
- package/lib/umd/index.js +1 -1
- package/package.json +5 -5
package/lib/cjs/index.js
CHANGED
|
@@ -288,16 +288,14 @@ const getPatternPreview = (pattern, value, locale = "en", dateSystem) => {
|
|
|
288
288
|
try {
|
|
289
289
|
const formatColor = _univerjs_core.numfmt.formatColor(pattern, value);
|
|
290
290
|
const color = formatColor ? String(formatColor) : void 0;
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
result,
|
|
291
|
+
return {
|
|
292
|
+
result: _univerjs_core.numfmt.format(pattern, value, {
|
|
293
|
+
locale,
|
|
294
|
+
dateSystem,
|
|
295
|
+
throws: false
|
|
296
|
+
}),
|
|
298
297
|
color
|
|
299
298
|
};
|
|
300
|
-
return { result };
|
|
301
299
|
} catch (e) {
|
|
302
300
|
console.warn("getPatternPreview error:", pattern, e);
|
|
303
301
|
}
|
|
@@ -718,6 +716,7 @@ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController
|
|
|
718
716
|
this._numfmtService = _numfmtService;
|
|
719
717
|
this._localeService = _localeService;
|
|
720
718
|
this._configService = _configService;
|
|
719
|
+
_defineProperty(this, "_renderCache", new _univerjs_core.ObjectMatrix());
|
|
721
720
|
_defineProperty(this, "_locale$", new rxjs.BehaviorSubject(null));
|
|
722
721
|
_defineProperty(this, "locale$", this._locale$.asObservable());
|
|
723
722
|
this._initInterceptorCellContent();
|
|
@@ -729,71 +728,12 @@ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController
|
|
|
729
728
|
return this._locale$.getValue() ?? (0, _univerjs_core.getNumfmtLocaleTag)((workbook === null || workbook === void 0 ? void 0 : workbook.getSnapshot().locale) ?? this._localeService.getCurrentLocale());
|
|
730
729
|
}
|
|
731
730
|
_initInterceptorCellContent() {
|
|
732
|
-
const renderCache = new _univerjs_core.ObjectMatrix();
|
|
733
731
|
this.disposeWithMe((0, rxjs.merge)(this._locale$, this._localeService.currentLocale$).subscribe(() => {
|
|
734
|
-
|
|
732
|
+
this._renderCache.reset();
|
|
735
733
|
}));
|
|
736
734
|
this.disposeWithMe(this._sheetInterceptorService.intercept(_univerjs_sheets.INTERCEPTOR_POINT.CELL_CONTENT, {
|
|
737
735
|
effect: _univerjs_core.InterceptorEffectEnum.Value | _univerjs_core.InterceptorEffectEnum.Style,
|
|
738
|
-
handler: (cell, location, next) =>
|
|
739
|
-
if (!cell || cell.v === void 0 || cell.v === null || cell.t === _univerjs_core.CellValueType.BOOLEAN || cell.t === _univerjs_core.CellValueType.FORCE_STRING) return next(cell);
|
|
740
|
-
const unitId = location.unitId;
|
|
741
|
-
const sheetId = location.subUnitId;
|
|
742
|
-
const locale = this.getLocale(location.workbook);
|
|
743
|
-
const dateSystem = location.workbook.getDateSystem();
|
|
744
|
-
let numfmtValue;
|
|
745
|
-
if (cell === null || cell === void 0 ? void 0 : cell.s) {
|
|
746
|
-
const style = location.workbook.getStyles().get(cell.s);
|
|
747
|
-
if (style === null || style === void 0 ? void 0 : style.n) numfmtValue = style.n;
|
|
748
|
-
}
|
|
749
|
-
if (!numfmtValue) numfmtValue = this._numfmtService.getValue(unitId, sheetId, location.row, location.col);
|
|
750
|
-
if ((0, _univerjs_core.isDefaultFormat)(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) return next(cell);
|
|
751
|
-
if (cell.t !== _univerjs_core.CellValueType.NUMBER) {
|
|
752
|
-
var _getNumfmtParseValueF;
|
|
753
|
-
if ((0, _univerjs_sheets.checkCellValueType)(cell.v, cell.t) !== _univerjs_core.CellValueType.NUMBER && !((0, _univerjs_core.isTextFormat)(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern) && typeof cell.v === "string" && typeof ((_getNumfmtParseValueF = (0, _univerjs_core.getNumfmtParseValueFilter)(cell.v, {
|
|
754
|
-
locale,
|
|
755
|
-
dateSystem
|
|
756
|
-
})) === null || _getNumfmtParseValueF === void 0 ? void 0 : _getNumfmtParseValueF.v) === "number")) return next(cell);
|
|
757
|
-
}
|
|
758
|
-
const originCellValue = cell;
|
|
759
|
-
if (!cell || cell === location.rawData) cell = { ...location.rawData };
|
|
760
|
-
if ((0, _univerjs_core.isTextFormat)(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) {
|
|
761
|
-
var _this$_configService$;
|
|
762
|
-
if ((_this$_configService$ = this._configService.getConfig("sheets-numfmt.config")) === null || _this$_configService$ === void 0 ? void 0 : _this$_configService$.disableTextFormatMark) {
|
|
763
|
-
cell.t = _univerjs_core.CellValueType.STRING;
|
|
764
|
-
return next(cell);
|
|
765
|
-
}
|
|
766
|
-
cell.t = _univerjs_core.CellValueType.STRING;
|
|
767
|
-
cell.markers = {
|
|
768
|
-
...cell === null || cell === void 0 ? void 0 : cell.markers,
|
|
769
|
-
...TEXT_FORMAT_MARK
|
|
770
|
-
};
|
|
771
|
-
return next(cell);
|
|
772
|
-
}
|
|
773
|
-
const cacheParameters = `${unitId}_${sheetId}_${originCellValue.v}_${numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern}_${locale}_${dateSystem}`;
|
|
774
|
-
const cache = renderCache.getValue(location.row, location.col);
|
|
775
|
-
if (cache && cache.parameters === cacheParameters) return next({
|
|
776
|
-
...cell,
|
|
777
|
-
...cache.result
|
|
778
|
-
});
|
|
779
|
-
const info = getPatternPreviewIgnoreGeneral(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern, Number(originCellValue.v), locale, dateSystem);
|
|
780
|
-
const numfmtRes = info.result;
|
|
781
|
-
const res = {
|
|
782
|
-
v: numfmtRes,
|
|
783
|
-
t: _univerjs_core.CellValueType.NUMBER
|
|
784
|
-
};
|
|
785
|
-
if (numfmtRes === "") res.coverable = false;
|
|
786
|
-
if (info.color) {
|
|
787
|
-
const color = this._themeService.getColorFromTheme(`${info.color}.500`) ?? info.color;
|
|
788
|
-
if (color) res.interceptorStyle = { cl: { rgb: color } };
|
|
789
|
-
}
|
|
790
|
-
renderCache.setValue(location.row, location.col, {
|
|
791
|
-
result: res,
|
|
792
|
-
parameters: cacheParameters
|
|
793
|
-
});
|
|
794
|
-
Object.assign(cell, res);
|
|
795
|
-
return next(cell);
|
|
796
|
-
},
|
|
736
|
+
handler: (cell, location, next) => next(this._formatCell(cell, location)),
|
|
797
737
|
priority: _univerjs_sheets.InterceptCellContentPriority.NUMFMT
|
|
798
738
|
}));
|
|
799
739
|
this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
|
|
@@ -802,18 +742,68 @@ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController
|
|
|
802
742
|
Object.keys(params.values).forEach((key) => {
|
|
803
743
|
params.values[key].ranges.forEach((range) => {
|
|
804
744
|
_univerjs_core.Range.foreach(range, (row, col) => {
|
|
805
|
-
|
|
745
|
+
this._renderCache.realDeleteValue(row, col);
|
|
806
746
|
});
|
|
807
747
|
});
|
|
808
748
|
});
|
|
809
749
|
} else if (commandInfo.id === _univerjs_sheets.SetRangeValuesMutation.id) {
|
|
810
750
|
const params = commandInfo.params;
|
|
811
751
|
new _univerjs_core.ObjectMatrix(params.cellValue).forValue((row, col) => {
|
|
812
|
-
|
|
752
|
+
this._renderCache.realDeleteValue(row, col);
|
|
813
753
|
});
|
|
814
754
|
}
|
|
815
755
|
}));
|
|
816
|
-
this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(_univerjs_core.UniverInstanceType.UNIVER_SHEET).pipe((0, rxjs.switchMap)((workbook) => (workbook === null || workbook === void 0 ? void 0 : workbook.activeSheet$) ?? (0, rxjs.of)(null)), (0, rxjs.skip)(1)).subscribe(() =>
|
|
756
|
+
this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(_univerjs_core.UniverInstanceType.UNIVER_SHEET).pipe((0, rxjs.switchMap)((workbook) => (workbook === null || workbook === void 0 ? void 0 : workbook.activeSheet$) ?? (0, rxjs.of)(null)), (0, rxjs.skip)(1)).subscribe(() => this._renderCache.reset()));
|
|
757
|
+
}
|
|
758
|
+
_formatCell(cell, location) {
|
|
759
|
+
var _workbook$getStyles$g;
|
|
760
|
+
if (!cell || cell.v == null || cell.t === _univerjs_core.CellValueType.BOOLEAN || cell.t === _univerjs_core.CellValueType.FORCE_STRING) return cell;
|
|
761
|
+
const { unitId, subUnitId: sheetId, row, col, workbook } = location;
|
|
762
|
+
const locale = this.getLocale(workbook);
|
|
763
|
+
const dateSystem = workbook.getDateSystem();
|
|
764
|
+
const numfmtValue = ((_workbook$getStyles$g = workbook.getStyles().get(cell.s)) === null || _workbook$getStyles$g === void 0 ? void 0 : _workbook$getStyles$g.n) ?? this._numfmtService.getValue(unitId, sheetId, row, col);
|
|
765
|
+
const pattern = numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern;
|
|
766
|
+
if ((0, _univerjs_core.isDefaultFormat)(pattern)) return cell;
|
|
767
|
+
if (!this._isNumericCell(cell, pattern, locale, dateSystem)) return cell;
|
|
768
|
+
const formattedCell = cell === location.rawData ? { ...cell } : cell;
|
|
769
|
+
if ((0, _univerjs_core.isTextFormat)(pattern)) {
|
|
770
|
+
var _this$_configService$;
|
|
771
|
+
formattedCell.t = _univerjs_core.CellValueType.STRING;
|
|
772
|
+
if (!((_this$_configService$ = this._configService.getConfig("sheets-numfmt.config")) === null || _this$_configService$ === void 0 ? void 0 : _this$_configService$.disableTextFormatMark)) formattedCell.markers = {
|
|
773
|
+
...formattedCell.markers,
|
|
774
|
+
...TEXT_FORMAT_MARK
|
|
775
|
+
};
|
|
776
|
+
return formattedCell;
|
|
777
|
+
}
|
|
778
|
+
const cacheParameters = `${unitId}_${sheetId}_${cell.v}_${pattern}_${locale}_${dateSystem}`;
|
|
779
|
+
const cache = this._renderCache.getValue(row, col);
|
|
780
|
+
if (cache && cache.parameters === cacheParameters) return {
|
|
781
|
+
...formattedCell,
|
|
782
|
+
...cache.result
|
|
783
|
+
};
|
|
784
|
+
const info = getPatternPreviewIgnoreGeneral(pattern, Number(cell.v), locale, dateSystem);
|
|
785
|
+
const res = {
|
|
786
|
+
v: info.result,
|
|
787
|
+
t: _univerjs_core.CellValueType.NUMBER
|
|
788
|
+
};
|
|
789
|
+
if (info.result === "") res.coverable = false;
|
|
790
|
+
if (info.color) {
|
|
791
|
+
const color = this._themeService.getColorFromTheme(`${info.color}.500`) ?? info.color;
|
|
792
|
+
if (color) res.interceptorStyle = { cl: { rgb: color } };
|
|
793
|
+
}
|
|
794
|
+
this._renderCache.setValue(row, col, {
|
|
795
|
+
result: res,
|
|
796
|
+
parameters: cacheParameters
|
|
797
|
+
});
|
|
798
|
+
Object.assign(formattedCell, res);
|
|
799
|
+
return formattedCell;
|
|
800
|
+
}
|
|
801
|
+
_isNumericCell(cell, pattern, locale, dateSystem) {
|
|
802
|
+
var _getNumfmtParseValueF;
|
|
803
|
+
return cell.t === _univerjs_core.CellValueType.NUMBER || (0, _univerjs_sheets.checkCellValueType)(cell.v, cell.t) === _univerjs_core.CellValueType.NUMBER || (0, _univerjs_core.isTextFormat)(pattern) && typeof cell.v === "string" && typeof ((_getNumfmtParseValueF = (0, _univerjs_core.getNumfmtParseValueFilter)(cell.v, {
|
|
804
|
+
locale,
|
|
805
|
+
dateSystem
|
|
806
|
+
})) === null || _getNumfmtParseValueF === void 0 ? void 0 : _getNumfmtParseValueF.v) === "number";
|
|
817
807
|
}
|
|
818
808
|
setNumfmtLocal(locale) {
|
|
819
809
|
this._locale$.next(locale);
|
|
@@ -832,7 +822,7 @@ SheetsNumfmtCellContentController = __decorate([
|
|
|
832
822
|
//#endregion
|
|
833
823
|
//#region package.json
|
|
834
824
|
var name = "@univerjs/sheets-numfmt";
|
|
835
|
-
var version = "1.
|
|
825
|
+
var version = "1.3.1";
|
|
836
826
|
|
|
837
827
|
//#endregion
|
|
838
828
|
//#region src/base/const/plugin-name.ts
|
package/lib/es/index.js
CHANGED
|
@@ -287,16 +287,14 @@ const getPatternPreview = (pattern, value, locale = "en", dateSystem) => {
|
|
|
287
287
|
try {
|
|
288
288
|
const formatColor = numfmt.formatColor(pattern, value);
|
|
289
289
|
const color = formatColor ? String(formatColor) : void 0;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
result,
|
|
290
|
+
return {
|
|
291
|
+
result: numfmt.format(pattern, value, {
|
|
292
|
+
locale,
|
|
293
|
+
dateSystem,
|
|
294
|
+
throws: false
|
|
295
|
+
}),
|
|
297
296
|
color
|
|
298
297
|
};
|
|
299
|
-
return { result };
|
|
300
298
|
} catch (e) {
|
|
301
299
|
console.warn("getPatternPreview error:", pattern, e);
|
|
302
300
|
}
|
|
@@ -717,6 +715,7 @@ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController
|
|
|
717
715
|
this._numfmtService = _numfmtService;
|
|
718
716
|
this._localeService = _localeService;
|
|
719
717
|
this._configService = _configService;
|
|
718
|
+
_defineProperty(this, "_renderCache", new ObjectMatrix());
|
|
720
719
|
_defineProperty(this, "_locale$", new BehaviorSubject(null));
|
|
721
720
|
_defineProperty(this, "locale$", this._locale$.asObservable());
|
|
722
721
|
this._initInterceptorCellContent();
|
|
@@ -728,71 +727,12 @@ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController
|
|
|
728
727
|
return this._locale$.getValue() ?? getNumfmtLocaleTag((workbook === null || workbook === void 0 ? void 0 : workbook.getSnapshot().locale) ?? this._localeService.getCurrentLocale());
|
|
729
728
|
}
|
|
730
729
|
_initInterceptorCellContent() {
|
|
731
|
-
const renderCache = new ObjectMatrix();
|
|
732
730
|
this.disposeWithMe(merge$1(this._locale$, this._localeService.currentLocale$).subscribe(() => {
|
|
733
|
-
|
|
731
|
+
this._renderCache.reset();
|
|
734
732
|
}));
|
|
735
733
|
this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
|
|
736
734
|
effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
|
|
737
|
-
handler: (cell, location, next) =>
|
|
738
|
-
if (!cell || cell.v === void 0 || cell.v === null || cell.t === CellValueType.BOOLEAN || cell.t === CellValueType.FORCE_STRING) return next(cell);
|
|
739
|
-
const unitId = location.unitId;
|
|
740
|
-
const sheetId = location.subUnitId;
|
|
741
|
-
const locale = this.getLocale(location.workbook);
|
|
742
|
-
const dateSystem = location.workbook.getDateSystem();
|
|
743
|
-
let numfmtValue;
|
|
744
|
-
if (cell === null || cell === void 0 ? void 0 : cell.s) {
|
|
745
|
-
const style = location.workbook.getStyles().get(cell.s);
|
|
746
|
-
if (style === null || style === void 0 ? void 0 : style.n) numfmtValue = style.n;
|
|
747
|
-
}
|
|
748
|
-
if (!numfmtValue) numfmtValue = this._numfmtService.getValue(unitId, sheetId, location.row, location.col);
|
|
749
|
-
if (isDefaultFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) return next(cell);
|
|
750
|
-
if (cell.t !== CellValueType.NUMBER) {
|
|
751
|
-
var _getNumfmtParseValueF;
|
|
752
|
-
if (checkCellValueType(cell.v, cell.t) !== CellValueType.NUMBER && !(isTextFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern) && typeof cell.v === "string" && typeof ((_getNumfmtParseValueF = getNumfmtParseValueFilter(cell.v, {
|
|
753
|
-
locale,
|
|
754
|
-
dateSystem
|
|
755
|
-
})) === null || _getNumfmtParseValueF === void 0 ? void 0 : _getNumfmtParseValueF.v) === "number")) return next(cell);
|
|
756
|
-
}
|
|
757
|
-
const originCellValue = cell;
|
|
758
|
-
if (!cell || cell === location.rawData) cell = { ...location.rawData };
|
|
759
|
-
if (isTextFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) {
|
|
760
|
-
var _this$_configService$;
|
|
761
|
-
if ((_this$_configService$ = this._configService.getConfig("sheets-numfmt.config")) === null || _this$_configService$ === void 0 ? void 0 : _this$_configService$.disableTextFormatMark) {
|
|
762
|
-
cell.t = CellValueType.STRING;
|
|
763
|
-
return next(cell);
|
|
764
|
-
}
|
|
765
|
-
cell.t = CellValueType.STRING;
|
|
766
|
-
cell.markers = {
|
|
767
|
-
...cell === null || cell === void 0 ? void 0 : cell.markers,
|
|
768
|
-
...TEXT_FORMAT_MARK
|
|
769
|
-
};
|
|
770
|
-
return next(cell);
|
|
771
|
-
}
|
|
772
|
-
const cacheParameters = `${unitId}_${sheetId}_${originCellValue.v}_${numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern}_${locale}_${dateSystem}`;
|
|
773
|
-
const cache = renderCache.getValue(location.row, location.col);
|
|
774
|
-
if (cache && cache.parameters === cacheParameters) return next({
|
|
775
|
-
...cell,
|
|
776
|
-
...cache.result
|
|
777
|
-
});
|
|
778
|
-
const info = getPatternPreviewIgnoreGeneral(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern, Number(originCellValue.v), locale, dateSystem);
|
|
779
|
-
const numfmtRes = info.result;
|
|
780
|
-
const res = {
|
|
781
|
-
v: numfmtRes,
|
|
782
|
-
t: CellValueType.NUMBER
|
|
783
|
-
};
|
|
784
|
-
if (numfmtRes === "") res.coverable = false;
|
|
785
|
-
if (info.color) {
|
|
786
|
-
const color = this._themeService.getColorFromTheme(`${info.color}.500`) ?? info.color;
|
|
787
|
-
if (color) res.interceptorStyle = { cl: { rgb: color } };
|
|
788
|
-
}
|
|
789
|
-
renderCache.setValue(location.row, location.col, {
|
|
790
|
-
result: res,
|
|
791
|
-
parameters: cacheParameters
|
|
792
|
-
});
|
|
793
|
-
Object.assign(cell, res);
|
|
794
|
-
return next(cell);
|
|
795
|
-
},
|
|
735
|
+
handler: (cell, location, next) => next(this._formatCell(cell, location)),
|
|
796
736
|
priority: InterceptCellContentPriority.NUMFMT
|
|
797
737
|
}));
|
|
798
738
|
this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
|
|
@@ -801,18 +741,68 @@ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController
|
|
|
801
741
|
Object.keys(params.values).forEach((key) => {
|
|
802
742
|
params.values[key].ranges.forEach((range) => {
|
|
803
743
|
Range.foreach(range, (row, col) => {
|
|
804
|
-
|
|
744
|
+
this._renderCache.realDeleteValue(row, col);
|
|
805
745
|
});
|
|
806
746
|
});
|
|
807
747
|
});
|
|
808
748
|
} else if (commandInfo.id === SetRangeValuesMutation.id) {
|
|
809
749
|
const params = commandInfo.params;
|
|
810
750
|
new ObjectMatrix(params.cellValue).forValue((row, col) => {
|
|
811
|
-
|
|
751
|
+
this._renderCache.realDeleteValue(row, col);
|
|
812
752
|
});
|
|
813
753
|
}
|
|
814
754
|
}));
|
|
815
|
-
this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SHEET).pipe(switchMap((workbook) => (workbook === null || workbook === void 0 ? void 0 : workbook.activeSheet$) ?? of(null)), skip(1)).subscribe(() =>
|
|
755
|
+
this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SHEET).pipe(switchMap((workbook) => (workbook === null || workbook === void 0 ? void 0 : workbook.activeSheet$) ?? of(null)), skip(1)).subscribe(() => this._renderCache.reset()));
|
|
756
|
+
}
|
|
757
|
+
_formatCell(cell, location) {
|
|
758
|
+
var _workbook$getStyles$g;
|
|
759
|
+
if (!cell || cell.v == null || cell.t === CellValueType.BOOLEAN || cell.t === CellValueType.FORCE_STRING) return cell;
|
|
760
|
+
const { unitId, subUnitId: sheetId, row, col, workbook } = location;
|
|
761
|
+
const locale = this.getLocale(workbook);
|
|
762
|
+
const dateSystem = workbook.getDateSystem();
|
|
763
|
+
const numfmtValue = ((_workbook$getStyles$g = workbook.getStyles().get(cell.s)) === null || _workbook$getStyles$g === void 0 ? void 0 : _workbook$getStyles$g.n) ?? this._numfmtService.getValue(unitId, sheetId, row, col);
|
|
764
|
+
const pattern = numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern;
|
|
765
|
+
if (isDefaultFormat(pattern)) return cell;
|
|
766
|
+
if (!this._isNumericCell(cell, pattern, locale, dateSystem)) return cell;
|
|
767
|
+
const formattedCell = cell === location.rawData ? { ...cell } : cell;
|
|
768
|
+
if (isTextFormat(pattern)) {
|
|
769
|
+
var _this$_configService$;
|
|
770
|
+
formattedCell.t = CellValueType.STRING;
|
|
771
|
+
if (!((_this$_configService$ = this._configService.getConfig("sheets-numfmt.config")) === null || _this$_configService$ === void 0 ? void 0 : _this$_configService$.disableTextFormatMark)) formattedCell.markers = {
|
|
772
|
+
...formattedCell.markers,
|
|
773
|
+
...TEXT_FORMAT_MARK
|
|
774
|
+
};
|
|
775
|
+
return formattedCell;
|
|
776
|
+
}
|
|
777
|
+
const cacheParameters = `${unitId}_${sheetId}_${cell.v}_${pattern}_${locale}_${dateSystem}`;
|
|
778
|
+
const cache = this._renderCache.getValue(row, col);
|
|
779
|
+
if (cache && cache.parameters === cacheParameters) return {
|
|
780
|
+
...formattedCell,
|
|
781
|
+
...cache.result
|
|
782
|
+
};
|
|
783
|
+
const info = getPatternPreviewIgnoreGeneral(pattern, Number(cell.v), locale, dateSystem);
|
|
784
|
+
const res = {
|
|
785
|
+
v: info.result,
|
|
786
|
+
t: CellValueType.NUMBER
|
|
787
|
+
};
|
|
788
|
+
if (info.result === "") res.coverable = false;
|
|
789
|
+
if (info.color) {
|
|
790
|
+
const color = this._themeService.getColorFromTheme(`${info.color}.500`) ?? info.color;
|
|
791
|
+
if (color) res.interceptorStyle = { cl: { rgb: color } };
|
|
792
|
+
}
|
|
793
|
+
this._renderCache.setValue(row, col, {
|
|
794
|
+
result: res,
|
|
795
|
+
parameters: cacheParameters
|
|
796
|
+
});
|
|
797
|
+
Object.assign(formattedCell, res);
|
|
798
|
+
return formattedCell;
|
|
799
|
+
}
|
|
800
|
+
_isNumericCell(cell, pattern, locale, dateSystem) {
|
|
801
|
+
var _getNumfmtParseValueF;
|
|
802
|
+
return cell.t === CellValueType.NUMBER || checkCellValueType(cell.v, cell.t) === CellValueType.NUMBER || isTextFormat(pattern) && typeof cell.v === "string" && typeof ((_getNumfmtParseValueF = getNumfmtParseValueFilter(cell.v, {
|
|
803
|
+
locale,
|
|
804
|
+
dateSystem
|
|
805
|
+
})) === null || _getNumfmtParseValueF === void 0 ? void 0 : _getNumfmtParseValueF.v) === "number";
|
|
816
806
|
}
|
|
817
807
|
setNumfmtLocal(locale) {
|
|
818
808
|
this._locale$.next(locale);
|
|
@@ -831,7 +821,7 @@ SheetsNumfmtCellContentController = __decorate([
|
|
|
831
821
|
//#endregion
|
|
832
822
|
//#region package.json
|
|
833
823
|
var name = "@univerjs/sheets-numfmt";
|
|
834
|
-
var version = "1.
|
|
824
|
+
var version = "1.3.1";
|
|
835
825
|
|
|
836
826
|
//#endregion
|
|
837
827
|
//#region src/base/const/plugin-name.ts
|
package/lib/index.js
CHANGED
|
@@ -287,16 +287,14 @@ const getPatternPreview = (pattern, value, locale = "en", dateSystem) => {
|
|
|
287
287
|
try {
|
|
288
288
|
const formatColor = numfmt.formatColor(pattern, value);
|
|
289
289
|
const color = formatColor ? String(formatColor) : void 0;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
result,
|
|
290
|
+
return {
|
|
291
|
+
result: numfmt.format(pattern, value, {
|
|
292
|
+
locale,
|
|
293
|
+
dateSystem,
|
|
294
|
+
throws: false
|
|
295
|
+
}),
|
|
297
296
|
color
|
|
298
297
|
};
|
|
299
|
-
return { result };
|
|
300
298
|
} catch (e) {
|
|
301
299
|
console.warn("getPatternPreview error:", pattern, e);
|
|
302
300
|
}
|
|
@@ -717,6 +715,7 @@ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController
|
|
|
717
715
|
this._numfmtService = _numfmtService;
|
|
718
716
|
this._localeService = _localeService;
|
|
719
717
|
this._configService = _configService;
|
|
718
|
+
_defineProperty(this, "_renderCache", new ObjectMatrix());
|
|
720
719
|
_defineProperty(this, "_locale$", new BehaviorSubject(null));
|
|
721
720
|
_defineProperty(this, "locale$", this._locale$.asObservable());
|
|
722
721
|
this._initInterceptorCellContent();
|
|
@@ -728,71 +727,12 @@ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController
|
|
|
728
727
|
return this._locale$.getValue() ?? getNumfmtLocaleTag((workbook === null || workbook === void 0 ? void 0 : workbook.getSnapshot().locale) ?? this._localeService.getCurrentLocale());
|
|
729
728
|
}
|
|
730
729
|
_initInterceptorCellContent() {
|
|
731
|
-
const renderCache = new ObjectMatrix();
|
|
732
730
|
this.disposeWithMe(merge$1(this._locale$, this._localeService.currentLocale$).subscribe(() => {
|
|
733
|
-
|
|
731
|
+
this._renderCache.reset();
|
|
734
732
|
}));
|
|
735
733
|
this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
|
|
736
734
|
effect: InterceptorEffectEnum.Value | InterceptorEffectEnum.Style,
|
|
737
|
-
handler: (cell, location, next) =>
|
|
738
|
-
if (!cell || cell.v === void 0 || cell.v === null || cell.t === CellValueType.BOOLEAN || cell.t === CellValueType.FORCE_STRING) return next(cell);
|
|
739
|
-
const unitId = location.unitId;
|
|
740
|
-
const sheetId = location.subUnitId;
|
|
741
|
-
const locale = this.getLocale(location.workbook);
|
|
742
|
-
const dateSystem = location.workbook.getDateSystem();
|
|
743
|
-
let numfmtValue;
|
|
744
|
-
if (cell === null || cell === void 0 ? void 0 : cell.s) {
|
|
745
|
-
const style = location.workbook.getStyles().get(cell.s);
|
|
746
|
-
if (style === null || style === void 0 ? void 0 : style.n) numfmtValue = style.n;
|
|
747
|
-
}
|
|
748
|
-
if (!numfmtValue) numfmtValue = this._numfmtService.getValue(unitId, sheetId, location.row, location.col);
|
|
749
|
-
if (isDefaultFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) return next(cell);
|
|
750
|
-
if (cell.t !== CellValueType.NUMBER) {
|
|
751
|
-
var _getNumfmtParseValueF;
|
|
752
|
-
if (checkCellValueType(cell.v, cell.t) !== CellValueType.NUMBER && !(isTextFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern) && typeof cell.v === "string" && typeof ((_getNumfmtParseValueF = getNumfmtParseValueFilter(cell.v, {
|
|
753
|
-
locale,
|
|
754
|
-
dateSystem
|
|
755
|
-
})) === null || _getNumfmtParseValueF === void 0 ? void 0 : _getNumfmtParseValueF.v) === "number")) return next(cell);
|
|
756
|
-
}
|
|
757
|
-
const originCellValue = cell;
|
|
758
|
-
if (!cell || cell === location.rawData) cell = { ...location.rawData };
|
|
759
|
-
if (isTextFormat(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern)) {
|
|
760
|
-
var _this$_configService$;
|
|
761
|
-
if ((_this$_configService$ = this._configService.getConfig("sheets-numfmt.config")) === null || _this$_configService$ === void 0 ? void 0 : _this$_configService$.disableTextFormatMark) {
|
|
762
|
-
cell.t = CellValueType.STRING;
|
|
763
|
-
return next(cell);
|
|
764
|
-
}
|
|
765
|
-
cell.t = CellValueType.STRING;
|
|
766
|
-
cell.markers = {
|
|
767
|
-
...cell === null || cell === void 0 ? void 0 : cell.markers,
|
|
768
|
-
...TEXT_FORMAT_MARK
|
|
769
|
-
};
|
|
770
|
-
return next(cell);
|
|
771
|
-
}
|
|
772
|
-
const cacheParameters = `${unitId}_${sheetId}_${originCellValue.v}_${numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern}_${locale}_${dateSystem}`;
|
|
773
|
-
const cache = renderCache.getValue(location.row, location.col);
|
|
774
|
-
if (cache && cache.parameters === cacheParameters) return next({
|
|
775
|
-
...cell,
|
|
776
|
-
...cache.result
|
|
777
|
-
});
|
|
778
|
-
const info = getPatternPreviewIgnoreGeneral(numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern, Number(originCellValue.v), locale, dateSystem);
|
|
779
|
-
const numfmtRes = info.result;
|
|
780
|
-
const res = {
|
|
781
|
-
v: numfmtRes,
|
|
782
|
-
t: CellValueType.NUMBER
|
|
783
|
-
};
|
|
784
|
-
if (numfmtRes === "") res.coverable = false;
|
|
785
|
-
if (info.color) {
|
|
786
|
-
const color = this._themeService.getColorFromTheme(`${info.color}.500`) ?? info.color;
|
|
787
|
-
if (color) res.interceptorStyle = { cl: { rgb: color } };
|
|
788
|
-
}
|
|
789
|
-
renderCache.setValue(location.row, location.col, {
|
|
790
|
-
result: res,
|
|
791
|
-
parameters: cacheParameters
|
|
792
|
-
});
|
|
793
|
-
Object.assign(cell, res);
|
|
794
|
-
return next(cell);
|
|
795
|
-
},
|
|
735
|
+
handler: (cell, location, next) => next(this._formatCell(cell, location)),
|
|
796
736
|
priority: InterceptCellContentPriority.NUMFMT
|
|
797
737
|
}));
|
|
798
738
|
this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
|
|
@@ -801,18 +741,68 @@ let SheetsNumfmtCellContentController = class SheetsNumfmtCellContentController
|
|
|
801
741
|
Object.keys(params.values).forEach((key) => {
|
|
802
742
|
params.values[key].ranges.forEach((range) => {
|
|
803
743
|
Range.foreach(range, (row, col) => {
|
|
804
|
-
|
|
744
|
+
this._renderCache.realDeleteValue(row, col);
|
|
805
745
|
});
|
|
806
746
|
});
|
|
807
747
|
});
|
|
808
748
|
} else if (commandInfo.id === SetRangeValuesMutation.id) {
|
|
809
749
|
const params = commandInfo.params;
|
|
810
750
|
new ObjectMatrix(params.cellValue).forValue((row, col) => {
|
|
811
|
-
|
|
751
|
+
this._renderCache.realDeleteValue(row, col);
|
|
812
752
|
});
|
|
813
753
|
}
|
|
814
754
|
}));
|
|
815
|
-
this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SHEET).pipe(switchMap((workbook) => (workbook === null || workbook === void 0 ? void 0 : workbook.activeSheet$) ?? of(null)), skip(1)).subscribe(() =>
|
|
755
|
+
this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(UniverInstanceType.UNIVER_SHEET).pipe(switchMap((workbook) => (workbook === null || workbook === void 0 ? void 0 : workbook.activeSheet$) ?? of(null)), skip(1)).subscribe(() => this._renderCache.reset()));
|
|
756
|
+
}
|
|
757
|
+
_formatCell(cell, location) {
|
|
758
|
+
var _workbook$getStyles$g;
|
|
759
|
+
if (!cell || cell.v == null || cell.t === CellValueType.BOOLEAN || cell.t === CellValueType.FORCE_STRING) return cell;
|
|
760
|
+
const { unitId, subUnitId: sheetId, row, col, workbook } = location;
|
|
761
|
+
const locale = this.getLocale(workbook);
|
|
762
|
+
const dateSystem = workbook.getDateSystem();
|
|
763
|
+
const numfmtValue = ((_workbook$getStyles$g = workbook.getStyles().get(cell.s)) === null || _workbook$getStyles$g === void 0 ? void 0 : _workbook$getStyles$g.n) ?? this._numfmtService.getValue(unitId, sheetId, row, col);
|
|
764
|
+
const pattern = numfmtValue === null || numfmtValue === void 0 ? void 0 : numfmtValue.pattern;
|
|
765
|
+
if (isDefaultFormat(pattern)) return cell;
|
|
766
|
+
if (!this._isNumericCell(cell, pattern, locale, dateSystem)) return cell;
|
|
767
|
+
const formattedCell = cell === location.rawData ? { ...cell } : cell;
|
|
768
|
+
if (isTextFormat(pattern)) {
|
|
769
|
+
var _this$_configService$;
|
|
770
|
+
formattedCell.t = CellValueType.STRING;
|
|
771
|
+
if (!((_this$_configService$ = this._configService.getConfig("sheets-numfmt.config")) === null || _this$_configService$ === void 0 ? void 0 : _this$_configService$.disableTextFormatMark)) formattedCell.markers = {
|
|
772
|
+
...formattedCell.markers,
|
|
773
|
+
...TEXT_FORMAT_MARK
|
|
774
|
+
};
|
|
775
|
+
return formattedCell;
|
|
776
|
+
}
|
|
777
|
+
const cacheParameters = `${unitId}_${sheetId}_${cell.v}_${pattern}_${locale}_${dateSystem}`;
|
|
778
|
+
const cache = this._renderCache.getValue(row, col);
|
|
779
|
+
if (cache && cache.parameters === cacheParameters) return {
|
|
780
|
+
...formattedCell,
|
|
781
|
+
...cache.result
|
|
782
|
+
};
|
|
783
|
+
const info = getPatternPreviewIgnoreGeneral(pattern, Number(cell.v), locale, dateSystem);
|
|
784
|
+
const res = {
|
|
785
|
+
v: info.result,
|
|
786
|
+
t: CellValueType.NUMBER
|
|
787
|
+
};
|
|
788
|
+
if (info.result === "") res.coverable = false;
|
|
789
|
+
if (info.color) {
|
|
790
|
+
const color = this._themeService.getColorFromTheme(`${info.color}.500`) ?? info.color;
|
|
791
|
+
if (color) res.interceptorStyle = { cl: { rgb: color } };
|
|
792
|
+
}
|
|
793
|
+
this._renderCache.setValue(row, col, {
|
|
794
|
+
result: res,
|
|
795
|
+
parameters: cacheParameters
|
|
796
|
+
});
|
|
797
|
+
Object.assign(formattedCell, res);
|
|
798
|
+
return formattedCell;
|
|
799
|
+
}
|
|
800
|
+
_isNumericCell(cell, pattern, locale, dateSystem) {
|
|
801
|
+
var _getNumfmtParseValueF;
|
|
802
|
+
return cell.t === CellValueType.NUMBER || checkCellValueType(cell.v, cell.t) === CellValueType.NUMBER || isTextFormat(pattern) && typeof cell.v === "string" && typeof ((_getNumfmtParseValueF = getNumfmtParseValueFilter(cell.v, {
|
|
803
|
+
locale,
|
|
804
|
+
dateSystem
|
|
805
|
+
})) === null || _getNumfmtParseValueF === void 0 ? void 0 : _getNumfmtParseValueF.v) === "number";
|
|
816
806
|
}
|
|
817
807
|
setNumfmtLocal(locale) {
|
|
818
808
|
this._locale$.next(locale);
|
|
@@ -831,7 +821,7 @@ SheetsNumfmtCellContentController = __decorate([
|
|
|
831
821
|
//#endregion
|
|
832
822
|
//#region package.json
|
|
833
823
|
var name = "@univerjs/sheets-numfmt";
|
|
834
|
-
var version = "1.
|
|
824
|
+
var version = "1.3.1";
|
|
835
825
|
|
|
836
826
|
//#endregion
|
|
837
827
|
//#region src/base/const/plugin-name.ts
|
|
@@ -24,11 +24,14 @@ export declare class SheetsNumfmtCellContentController extends Disposable {
|
|
|
24
24
|
private _numfmtService;
|
|
25
25
|
private _localeService;
|
|
26
26
|
private readonly _configService;
|
|
27
|
+
private readonly _renderCache;
|
|
27
28
|
private _locale$;
|
|
28
29
|
locale$: import("rxjs").Observable<INumfmtLocaleTag | null>;
|
|
29
30
|
constructor(_instanceService: IUniverInstanceService, _sheetInterceptorService: SheetInterceptorService, _themeService: ThemeService, _commandService: ICommandService, _numfmtService: INumfmtService, _localeService: LocaleService, _configService: IConfigService);
|
|
30
31
|
get locale(): INumfmtLocaleTag;
|
|
31
32
|
getLocale(workbook?: Workbook): INumfmtLocaleTag;
|
|
32
33
|
private _initInterceptorCellContent;
|
|
34
|
+
private _formatCell;
|
|
35
|
+
private _isNumericCell;
|
|
33
36
|
setNumfmtLocal(locale: INumfmtLocaleTag): void;
|
|
34
37
|
}
|
package/lib/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-formula"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/sheets`,`@univerjs/engine-formula`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverSheetsNumfmt={},e.UniverCore,e.UniverSheets,e.UniverEngineFormula,e.rxjs))})(this,function(e,t,n,r,i){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let a=/* @__PURE__ */ 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.ZH_HK,`HK$`],[t.LocaleType.FR_FR,`€`],[t.LocaleType.FA_IR,`﷼`],[t.LocaleType.KO_KR,`₩`],[t.LocaleType.ES_ES,`€`],[t.LocaleType.CA_ES,`€`],[t.LocaleType.SK_SK,`€`],[t.LocaleType.JA_JP,`¥`],[t.LocaleType.PT_BR,`R$`],[t.LocaleType.DE_DE,`€`],[t.LocaleType.IT_IT,`€`],[t.LocaleType.ID_ID,`Rp`],[t.LocaleType.PL_PL,`zł`],[t.LocaleType.AR_SA,`﷼`]]);function o(e){switch(e){case t.LocaleType.CA_ES:case t.LocaleType.DE_DE:case t.LocaleType.ES_ES:case t.LocaleType.FR_FR:case t.LocaleType.IT_IT:case t.LocaleType.SK_SK:return{icon:`EuroIcon`,symbol:a.get(e)||`€`,locale:e};case t.LocaleType.RU_RU:return{icon:`RoubleIcon`,symbol:a.get(e)||`₽`,locale:e};case t.LocaleType.JA_JP:case t.LocaleType.ZH_CN:return{icon:`RmbIcon`,symbol:a.get(e)||`¥`,locale:e};case t.LocaleType.VI_VN:return{icon:`DongIcon`,symbol:a.get(e)||`₫`,locale:e};case t.LocaleType.AR_SA:case t.LocaleType.FA_IR:return{icon:`RialIcon`,symbol:a.get(e)||`﷼`,locale:e};case t.LocaleType.KO_KR:return{icon:`WonIcon`,symbol:a.get(e)||`₩`,locale:e};case t.LocaleType.ID_ID:return{icon:`RupiahIcon`,symbol:a.get(e)||`Rp`,locale:e};case t.LocaleType.PL_PL:return{icon:`ZlotyIcon`,symbol:a.get(e)||`zł`,locale:e};case t.LocaleType.EN_US:case t.LocaleType.PT_BR:case t.LocaleType.ZH_HK:case t.LocaleType.ZH_TW:default:return{icon:`DollarIcon`,symbol:a.get(e)||`$`,locale:e}}}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})`}let 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=(e,n=0)=>e?t.numfmt.getFormatInfo(e).maxDecimals??n: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=e=>{let n=t.numfmt.getFormatInfo(e).type||`unknown`;return n===`general`&&t.numfmt.tokenize(e).some(e=>e.type===t.numfmt.tokenTypes.CONDITION)?`number`:n},_=(e,n,r=`en`,i)=>{try{let a=t.numfmt.formatColor(e,n),o=a?String(a):void 0,s=t.numfmt.format(e,n,{locale:r,dateSystem:i,throws:!1});return n<0?{result:s,color:o}:{result:s}}catch(t){console.warn(`getPatternPreview error:`,e,t)}return{result:String(n)}},v=(e,n,i,a)=>e===t.DEFAULT_NUMBER_FORMAT?{result:String((0,r.stripErrorMargin)(n))}:_(e,n,i,a);function y(e){return(e==null?void 0:e.t)===t.CellValueType.NUMBER&&e.v!==void 0&&e.v!==null&&/e/i.test(String(e.v))}function b(e){let t=String(e==null?void 0:e.v),n=t.search(/e/i),r=t.indexOf(`.`),i=n>-1&&r>-1?n-r-1:0;return`0.${`0`.repeat(Math.max(2,i))}E+00`}function x(e){return(0,t.isDefaultFormat)(e)||(0,t.isTextFormat)(e)||g(e)===`scientific`}let S={id:`sheet.command.numfmt.set.numfmt`,type:t.CommandType.COMMAND,handler:(e,r)=>{if(!r)return!1;let i=e.get(t.ICommandService),a=e.get(t.IUniverInstanceService),o=e.get(t.IUndoRedoService),s=(0,n.getSheetCommandTarget)(a,r);if(!s)return!1;let{unitId:c,subUnitId:l,worksheet:u}=s,d=r.values.filter(e=>!y(u.getCellRaw(e.row,e.col))||x(e.pattern)),f=d.filter(e=>!!e.pattern),p=d.filter(e=>!e.pattern);if(!f.length&&!p.length)return!1;let m=(0,n.transformCellsToRange)(c,l,f),h={unitId:c,subUnitId:l,ranges:p.map(e=>({startColumn:e.col,startRow:e.row,endColumn:e.col,endRow:e.row}))},g=[],_=[];if(f.length){let r=f.reduce((e,r)=>{(0,t.isTextFormat)(r.pattern)&&e.setValue(r.row,r.col,{t:t.CellValueType.STRING});let i=u.getCellRaw(r.row,r.col);if(i){let t=(0,n.checkCellValueType)(i.v);t!==i.t&&e.setValue(r.row,r.col,{t})}return e},new t.ObjectMatrix).getMatrix(),i=new t.ObjectMatrix;new t.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(m.values).forEach(e=>{let t=m.values[e];t.ranges=(0,n.rangeMerge)(t.ranges)}),g.push({id:n.SetNumfmtMutation.id,params:m});let a=(0,n.factorySetNumfmtUndoMutation)(e,m);_.push(...a)}if(p.length){h.ranges=(0,n.rangeMerge)(h.ranges);let r=p.reduce((e,t)=>{let r=u.getCellRaw(t.row,t.col);if(r){let i=(0,n.checkCellValueType)(r.v);i!==r.t&&e.setValue(t.row,t.col,{t:i})}return e},new t.ObjectMatrix).getMatrix(),i=new t.ObjectMatrix;new t.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})}),g.push({id:n.RemoveNumfmtMutation.id,params:h},{id:n.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:r}});let a=(0,n.factoryRemoveNumfmtUndoMutation)(e,h);_.push({id:n.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:i.getMatrix()}},...a)}let v=(0,t.sequenceExecute)(g,i).result;return v&&o.pushUndoRedo({unitID:c,undoMutations:_,redoMutations:g}),v}},C={id:`sheet.command.numfmt.add.decimal.command`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(n.INumfmtService),o=e.get(t.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,n.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if(!r){let r=c.worksheet.getCellRaw(e,n);if(y(r)){d=Math.max(d,f(b(r)));return}if(!d&&r&&r.t===t.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),g=[];return s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if((0,t.isDefaultFormat)(r==null?void 0:r.pattern)){let t=c.worksheet.getCellRaw(e,n);g.push({row:e,col:n,pattern:y(t)?m(b(t),p):h})}else{let t=f(r.pattern),i=m(r.pattern,t+1);i!==r.pattern&&g.push({row:e,col:n,pattern:i})}})}),g.length?await r.executeCommand(S.id,{values:g}):!1}},w={id:`sheet.command.numfmt.set.currency`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(t.RegionService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let l=[],u=c(o(a.getCurrentRegion()).locale);return s.forEach(e=>{t.Range.foreach(e.range,(e,t)=>{l.push({row:e,col:t,pattern:u,type:`currency`})})}),await r.executeCommand(S.id,{values:l})}},T={id:`sheet.command.numfmt.set.percent`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService).getCurrentSelections();if(!i||!i.length)return!1;let a=[];return i.forEach(e=>{t.Range.foreach(e.range,(e,t)=>{a.push({row:e,col:t,pattern:`0%`,type:`percent`})})}),await r.executeCommand(S.id,{values:a})}},E={id:`sheet.command.numfmt.subtract.decimal.command`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(n.INumfmtService),o=e.get(t.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,n.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if(!r){let r=c.worksheet.getCellRaw(e,n);if(y(r)){d=Math.max(d,f(b(r)));return}if(!d&&r&&r.t===t.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),g=[];return s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if((0,t.isDefaultFormat)(r==null?void 0:r.pattern)){let t=c.worksheet.getCellRaw(e,n);g.push({row:e,col:n,pattern:y(t)?m(b(t),p):h})}else{let t=f(r.pattern);g.push({row:e,col:n,pattern:m(r.pattern,t-1)})}})}),await r.executeCommand(S.id,{values:g})}},D=`sheets-numfmt.config`,O={};function k(e){"@babel/helpers - typeof";return k=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},k(e)}function A(e,t){if(k(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(k(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function j(e){var t=A(e,`string`);return k(t)==`symbol`?t:t+``}function M(e,t,n){return(t=j(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function N(e,t){return function(n,r){t(n,r,e)}}function P(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}let F={tl:{size:6,color:`#409f11`}},I=class extends t.Disposable{constructor(e,t,n,r,a,o,s){super(),this._instanceService=e,this._sheetInterceptorService=t,this._themeService=n,this._commandService=r,this._numfmtService=a,this._localeService=o,this._configService=s,M(this,`_locale$`,new i.BehaviorSubject(null)),M(this,`locale$`,this._locale$.asObservable()),this._initInterceptorCellContent()}get locale(){return this.getLocale()}getLocale(e){return this._locale$.getValue()??(0,t.getNumfmtLocaleTag)((e==null?void 0:e.getSnapshot().locale)??this._localeService.getCurrentLocale())}_initInterceptorCellContent(){let e=new t.ObjectMatrix;this.disposeWithMe((0,i.merge)(this._locale$,this._localeService.currentLocale$).subscribe(()=>{e.reset()})),this.disposeWithMe(this._sheetInterceptorService.intercept(n.INTERCEPTOR_POINT.CELL_CONTENT,{effect:t.InterceptorEffectEnum.Value|t.InterceptorEffectEnum.Style,handler:(r,i,a)=>{if(!r||r.v===void 0||r.v===null||r.t===t.CellValueType.BOOLEAN||r.t===t.CellValueType.FORCE_STRING)return a(r);let o=i.unitId,s=i.subUnitId,c=this.getLocale(i.workbook),l=i.workbook.getDateSystem(),u;if(r!=null&&r.s){let e=i.workbook.getStyles().get(r.s);e!=null&&e.n&&(u=e.n)}if(u||=this._numfmtService.getValue(o,s,i.row,i.col),(0,t.isDefaultFormat)(u==null?void 0:u.pattern))return a(r);if(r.t!==t.CellValueType.NUMBER){var d;if((0,n.checkCellValueType)(r.v,r.t)!==t.CellValueType.NUMBER&&!((0,t.isTextFormat)(u==null?void 0:u.pattern)&&typeof r.v==`string`&&typeof((d=(0,t.getNumfmtParseValueFilter)(r.v,{locale:c,dateSystem:l}))==null?void 0:d.v)==`number`))return a(r)}let f=r;if((!r||r===i.rawData)&&(r={...i.rawData}),(0,t.isTextFormat)(u==null?void 0:u.pattern)){var p;return(p=this._configService.getConfig(`sheets-numfmt.config`))!=null&&p.disableTextFormatMark?(r.t=t.CellValueType.STRING,a(r)):(r.t=t.CellValueType.STRING,r.markers={...r==null?void 0:r.markers,...F},a(r))}let m=`${o}_${s}_${f.v}_${u==null?void 0:u.pattern}_${c}_${l}`,h=e.getValue(i.row,i.col);if(h&&h.parameters===m)return a({...r,...h.result});let g=v(u==null?void 0:u.pattern,Number(f.v),c,l),_=g.result,y={v:_,t:t.CellValueType.NUMBER};if(_===``&&(y.coverable=!1),g.color){let e=this._themeService.getColorFromTheme(`${g.color}.500`)??g.color;e&&(y.interceptorStyle={cl:{rgb:e}})}return e.setValue(i.row,i.col,{result:y,parameters:m}),Object.assign(r,y),a(r)},priority:n.InterceptCellContentPriority.NUMFMT})),this.disposeWithMe(this._commandService.onCommandExecuted(r=>{if(r.id===n.SetNumfmtMutation.id){let n=r.params;Object.keys(n.values).forEach(r=>{n.values[r].ranges.forEach(n=>{t.Range.foreach(n,(t,n)=>{e.realDeleteValue(t,n)})})})}else if(r.id===n.SetRangeValuesMutation.id){let n=r.params;new t.ObjectMatrix(n.cellValue).forValue((t,n)=>{e.realDeleteValue(t,n)})}})),this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,i.switchMap)(e=>(e==null?void 0:e.activeSheet$)??(0,i.of)(null)),(0,i.skip)(1)).subscribe(()=>e.reset()))}setNumfmtLocal(e){this._locale$.next(e)}};I=P([N(0,t.IUniverInstanceService),N(1,(0,t.Inject)(n.SheetInterceptorService)),N(2,(0,t.Inject)(t.ThemeService)),N(3,(0,t.Inject)(t.ICommandService)),N(4,(0,t.Inject)(n.INumfmtService)),N(5,(0,t.Inject)(t.LocaleService)),N(6,t.IConfigService)],I);var L=`@univerjs/sheets-numfmt`,R=`1.2.0`;let z=class extends t.Plugin{constructor(e=O,n,r,i){super(),this._config=e,this._injector=n,this._configService=r,this._commandService=i;let{...a}=(0,t.merge)({},O,this._config);this._configService.setConfig(D,a)}onStarting(){(0,t.registerDependencies)(this._injector,[[I]]),(0,t.touchDependencies)(this._injector,[[I]]),[C,E,w,T,S].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}};M(z,`pluginName`,`SHEET_NUMFMT_PLUGIN`),M(z,`packageName`,L),M(z,`version`,R),M(z,`type`,t.UniverInstanceType.UNIVER_SHEET),z=P([(0,t.DependentOn)(n.UniverSheetsPlugin),N(1,(0,t.Inject)(t.Injector)),N(2,t.IConfigService),N(3,t.ICommandService)],z);let B=e=>t.currencySymbols.find(t=>e.includes(t)),V={dates:[`yyyy/MM/dd`,`MM-dd`],shortDate:`MM-dd`},H={dates:[`m/d/yyyy`,`mm/dd/yyyy`,`mmm d, yyyy`,`m/d`,`mmmm d`],shortDate:`m/d`},U={dates:[`dd/mm/yyyy`,`d/m/yyyy`,`d mmm yyyy`,`dd/mm`,`d mmmm`],shortDate:`dd/mm`},W={dates:[`dd.mm.yyyy`,`d.m.yyyy`,`d mmm yyyy`,`dd.mm`,`d mmmm`],shortDate:`dd.mm`},G={dates:[`yyyy/MM/dd`,`yyyy"年"MM"月"dd"日"`,`MM-dd`,`M"月"d"日"`],shortDate:`MM-dd`,periodBeforeTime:!0},K={[t.LocaleType.EN_US]:H,[t.LocaleType.FR_FR]:U,[t.LocaleType.ZH_CN]:G,[t.LocaleType.RU_RU]:W,[t.LocaleType.ZH_TW]:G,[t.LocaleType.ZH_HK]:{...G,dates:[`dd/MM/yyyy`,...G.dates],shortDate:`dd/MM`},[t.LocaleType.VI_VN]:U,[t.LocaleType.FA_IR]:V,[t.LocaleType.CA_ES]:{dates:[`dd/mm/yyyy`,`d/m/yyyy`,`dd/mm`],shortDate:`dd/mm`},[t.LocaleType.JA_JP]:G,[t.LocaleType.KO_KR]:{dates:[`yyyy. mm. dd.`,`yyyy"년" m"월" d"일"`,`mm-dd`,`m"월" d"일"`],shortDate:`mm-dd`,periodBeforeTime:!0},[t.LocaleType.ES_ES]:U,[t.LocaleType.SK_SK]:W,[t.LocaleType.PT_BR]:U,[t.LocaleType.DE_DE]:W,[t.LocaleType.IT_IT]:U,[t.LocaleType.ID_ID]:U,[t.LocaleType.PL_PL]:W,[t.LocaleType.AR_SA]:{dates:[`dd/mm/yyyy`,`d/m/yyyy`,`dd/mm`],shortDate:`dd/mm`}};function q(e){let t=K[e]??V,n=[`hh:mm`,`h:mm`,`h:mm:ss`].map(e=>t.periodBeforeTime?`AM/PM ${e}`:`${e} AM/PM`);return[.../* @__PURE__ */ new Set([`yyyy-MM-dd`,...t.dates,`h:mm:ss`,`h:mm`,...n,`${t.shortDate} ${n[0]}`])]}let J=()=>t.currencySymbols.map(e=>({label:e,value:e})),Y=e=>d.map(t=>({label:t.label(e),value:t.suffix(e),color:t.color})),X=(0,t.excelDateTimeSerial)(new Date(Date.UTC(1930,7,5,13,30,30)));e.AddDecimalCommand=C,e.CURRENCYFORMAT=d,e.DATEFMTLISG=l,e.NUMBERFORMAT=u,e.SHEETS_NUMFMT_PLUGIN_CONFIG_KEY=D,e.SetCurrencyCommand=w,e.SetNumfmtCommand=S,e.SetPercentCommand=T,Object.defineProperty(e,"SheetsNumfmtCellContentController",{enumerable:!0,get:function(){return I}}),e.SubtractDecimalCommand=E,Object.defineProperty(e,"UniverSheetsNumfmtPlugin",{enumerable:!0,get:function(){return z}}),e.getCurrencyFormat=c,e.getCurrencyFormatOptions=Y,e.getCurrencyOptions=J,e.getCurrencySymbolByLocale=s,e.getCurrencySymbolIconByLocale=o,e.getCurrencyType=B,e.getDateFormatOptions=(e,n)=>{if(e===void 0)return l.map(e=>({label:e.label,value:e.suffix}));let r=(0,t.getNumfmtLocaleTag)(e),i=q(e);return n&&!i.includes(n)&&i.unshift(n),i.map(e=>({label:t.numfmt.format(e,X,{locale:r}),value:e}))},e.getDecimalFromPattern=f,e.getDecimalString=p,e.getNumberFormatOptions=()=>u.map(e=>({label:e.label,value:e.suffix,color:e.color})),e.getPatternPreview=_,e.getPatternPreviewIgnoreGeneral=v,e.getPatternType=g,e.isPatternHasDecimal=h,e.localeCurrencySymbolMap=a,e.setPatternDecimal=m});
|
|
1
|
+
(function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports,require("@univerjs/core"),require("@univerjs/sheets"),require("@univerjs/engine-formula"),require("rxjs")):typeof define==`function`&&define.amd?define([`exports`,`@univerjs/core`,`@univerjs/sheets`,`@univerjs/engine-formula`,`rxjs`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.UniverSheetsNumfmt={},e.UniverCore,e.UniverSheets,e.UniverEngineFormula,e.rxjs))})(this,function(e,t,n,r,i){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});let a=/* @__PURE__ */ 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.ZH_HK,`HK$`],[t.LocaleType.FR_FR,`€`],[t.LocaleType.FA_IR,`﷼`],[t.LocaleType.KO_KR,`₩`],[t.LocaleType.ES_ES,`€`],[t.LocaleType.CA_ES,`€`],[t.LocaleType.SK_SK,`€`],[t.LocaleType.JA_JP,`¥`],[t.LocaleType.PT_BR,`R$`],[t.LocaleType.DE_DE,`€`],[t.LocaleType.IT_IT,`€`],[t.LocaleType.ID_ID,`Rp`],[t.LocaleType.PL_PL,`zł`],[t.LocaleType.AR_SA,`﷼`]]);function o(e){switch(e){case t.LocaleType.CA_ES:case t.LocaleType.DE_DE:case t.LocaleType.ES_ES:case t.LocaleType.FR_FR:case t.LocaleType.IT_IT:case t.LocaleType.SK_SK:return{icon:`EuroIcon`,symbol:a.get(e)||`€`,locale:e};case t.LocaleType.RU_RU:return{icon:`RoubleIcon`,symbol:a.get(e)||`₽`,locale:e};case t.LocaleType.JA_JP:case t.LocaleType.ZH_CN:return{icon:`RmbIcon`,symbol:a.get(e)||`¥`,locale:e};case t.LocaleType.VI_VN:return{icon:`DongIcon`,symbol:a.get(e)||`₫`,locale:e};case t.LocaleType.AR_SA:case t.LocaleType.FA_IR:return{icon:`RialIcon`,symbol:a.get(e)||`﷼`,locale:e};case t.LocaleType.KO_KR:return{icon:`WonIcon`,symbol:a.get(e)||`₩`,locale:e};case t.LocaleType.ID_ID:return{icon:`RupiahIcon`,symbol:a.get(e)||`Rp`,locale:e};case t.LocaleType.PL_PL:return{icon:`ZlotyIcon`,symbol:a.get(e)||`zł`,locale:e};case t.LocaleType.EN_US:case t.LocaleType.PT_BR:case t.LocaleType.ZH_HK:case t.LocaleType.ZH_TW:default:return{icon:`DollarIcon`,symbol:a.get(e)||`$`,locale:e}}}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})`}let 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=(e,n=0)=>e?t.numfmt.getFormatInfo(e).maxDecimals??n: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=e=>{let n=t.numfmt.getFormatInfo(e).type||`unknown`;return n===`general`&&t.numfmt.tokenize(e).some(e=>e.type===t.numfmt.tokenTypes.CONDITION)?`number`:n},_=(e,n,r=`en`,i)=>{try{let a=t.numfmt.formatColor(e,n),o=a?String(a):void 0;return{result:t.numfmt.format(e,n,{locale:r,dateSystem:i,throws:!1}),color:o}}catch(t){console.warn(`getPatternPreview error:`,e,t)}return{result:String(n)}},v=(e,n,i,a)=>e===t.DEFAULT_NUMBER_FORMAT?{result:String((0,r.stripErrorMargin)(n))}:_(e,n,i,a);function y(e){return(e==null?void 0:e.t)===t.CellValueType.NUMBER&&e.v!==void 0&&e.v!==null&&/e/i.test(String(e.v))}function b(e){let t=String(e==null?void 0:e.v),n=t.search(/e/i),r=t.indexOf(`.`),i=n>-1&&r>-1?n-r-1:0;return`0.${`0`.repeat(Math.max(2,i))}E+00`}function x(e){return(0,t.isDefaultFormat)(e)||(0,t.isTextFormat)(e)||g(e)===`scientific`}let S={id:`sheet.command.numfmt.set.numfmt`,type:t.CommandType.COMMAND,handler:(e,r)=>{if(!r)return!1;let i=e.get(t.ICommandService),a=e.get(t.IUniverInstanceService),o=e.get(t.IUndoRedoService),s=(0,n.getSheetCommandTarget)(a,r);if(!s)return!1;let{unitId:c,subUnitId:l,worksheet:u}=s,d=r.values.filter(e=>!y(u.getCellRaw(e.row,e.col))||x(e.pattern)),f=d.filter(e=>!!e.pattern),p=d.filter(e=>!e.pattern);if(!f.length&&!p.length)return!1;let m=(0,n.transformCellsToRange)(c,l,f),h={unitId:c,subUnitId:l,ranges:p.map(e=>({startColumn:e.col,startRow:e.row,endColumn:e.col,endRow:e.row}))},g=[],_=[];if(f.length){let r=f.reduce((e,r)=>{(0,t.isTextFormat)(r.pattern)&&e.setValue(r.row,r.col,{t:t.CellValueType.STRING});let i=u.getCellRaw(r.row,r.col);if(i){let t=(0,n.checkCellValueType)(i.v);t!==i.t&&e.setValue(r.row,r.col,{t})}return e},new t.ObjectMatrix).getMatrix(),i=new t.ObjectMatrix;new t.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(m.values).forEach(e=>{let t=m.values[e];t.ranges=(0,n.rangeMerge)(t.ranges)}),g.push({id:n.SetNumfmtMutation.id,params:m});let a=(0,n.factorySetNumfmtUndoMutation)(e,m);_.push(...a)}if(p.length){h.ranges=(0,n.rangeMerge)(h.ranges);let r=p.reduce((e,t)=>{let r=u.getCellRaw(t.row,t.col);if(r){let i=(0,n.checkCellValueType)(r.v);i!==r.t&&e.setValue(t.row,t.col,{t:i})}return e},new t.ObjectMatrix).getMatrix(),i=new t.ObjectMatrix;new t.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})}),g.push({id:n.RemoveNumfmtMutation.id,params:h},{id:n.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:r}});let a=(0,n.factoryRemoveNumfmtUndoMutation)(e,h);_.push({id:n.SetRangeValuesMutation.id,params:{unitId:c,subUnitId:l,cellValue:i.getMatrix()}},...a)}let v=(0,t.sequenceExecute)(g,i).result;return v&&o.pushUndoRedo({unitID:c,undoMutations:_,redoMutations:g}),v}},C={id:`sheet.command.numfmt.add.decimal.command`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(n.INumfmtService),o=e.get(t.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,n.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if(!r){let r=c.worksheet.getCellRaw(e,n);if(y(r)){d=Math.max(d,f(b(r)));return}if(!d&&r&&r.t===t.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),g=[];return s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if((0,t.isDefaultFormat)(r==null?void 0:r.pattern)){let t=c.worksheet.getCellRaw(e,n);g.push({row:e,col:n,pattern:y(t)?m(b(t),p):h})}else{let t=f(r.pattern),i=m(r.pattern,t+1);i!==r.pattern&&g.push({row:e,col:n,pattern:i})}})}),g.length?await r.executeCommand(S.id,{values:g}):!1}},w={id:`sheet.command.numfmt.set.currency`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(t.RegionService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let l=[],u=c(o(a.getCurrentRegion()).locale);return s.forEach(e=>{t.Range.foreach(e.range,(e,t)=>{l.push({row:e,col:t,pattern:u,type:`currency`})})}),await r.executeCommand(S.id,{values:l})}},T={id:`sheet.command.numfmt.set.percent`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService).getCurrentSelections();if(!i||!i.length)return!1;let a=[];return i.forEach(e=>{t.Range.foreach(e.range,(e,t)=>{a.push({row:e,col:t,pattern:`0%`,type:`percent`})})}),await r.executeCommand(S.id,{values:a})}},E={id:`sheet.command.numfmt.subtract.decimal.command`,type:t.CommandType.COMMAND,handler:async e=>{let r=e.get(t.ICommandService),i=e.get(n.SheetsSelectionsService),a=e.get(n.INumfmtService),o=e.get(t.IUniverInstanceService),s=i.getCurrentSelections();if(!s||!s.length)return!1;let c=(0,n.getSheetCommandTarget)(o);if(!c)return!1;let{unitId:l,subUnitId:u}=c,d=0;s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if(!r){let r=c.worksheet.getCellRaw(e,n);if(y(r)){d=Math.max(d,f(b(r)));return}if(!d&&r&&r.t===t.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),g=[];return s.forEach(e=>{t.Range.foreach(e.range,(e,n)=>{let r=a.getValue(l,u,e,n);if((0,t.isDefaultFormat)(r==null?void 0:r.pattern)){let t=c.worksheet.getCellRaw(e,n);g.push({row:e,col:n,pattern:y(t)?m(b(t),p):h})}else{let t=f(r.pattern);g.push({row:e,col:n,pattern:m(r.pattern,t-1)})}})}),await r.executeCommand(S.id,{values:g})}},D=`sheets-numfmt.config`,O={};function k(e){"@babel/helpers - typeof";return k=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},k(e)}function A(e,t){if(k(e)!=`object`||!e)return e;var n=e[Symbol.toPrimitive];if(n!==void 0){var r=n.call(e,t||`default`);if(k(r)!=`object`)return r;throw TypeError(`@@toPrimitive must return a primitive value.`)}return(t===`string`?String:Number)(e)}function j(e){var t=A(e,`string`);return k(t)==`symbol`?t:t+``}function M(e,t,n){return(t=j(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function N(e,t){return function(n,r){t(n,r,e)}}function P(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}let F={tl:{size:6,color:`#409f11`}},I=class extends t.Disposable{constructor(e,n,r,a,o,s,c){super(),this._instanceService=e,this._sheetInterceptorService=n,this._themeService=r,this._commandService=a,this._numfmtService=o,this._localeService=s,this._configService=c,M(this,`_renderCache`,new t.ObjectMatrix),M(this,`_locale$`,new i.BehaviorSubject(null)),M(this,`locale$`,this._locale$.asObservable()),this._initInterceptorCellContent()}get locale(){return this.getLocale()}getLocale(e){return this._locale$.getValue()??(0,t.getNumfmtLocaleTag)((e==null?void 0:e.getSnapshot().locale)??this._localeService.getCurrentLocale())}_initInterceptorCellContent(){this.disposeWithMe((0,i.merge)(this._locale$,this._localeService.currentLocale$).subscribe(()=>{this._renderCache.reset()})),this.disposeWithMe(this._sheetInterceptorService.intercept(n.INTERCEPTOR_POINT.CELL_CONTENT,{effect:t.InterceptorEffectEnum.Value|t.InterceptorEffectEnum.Style,handler:(e,t,n)=>n(this._formatCell(e,t)),priority:n.InterceptCellContentPriority.NUMFMT})),this.disposeWithMe(this._commandService.onCommandExecuted(e=>{if(e.id===n.SetNumfmtMutation.id){let n=e.params;Object.keys(n.values).forEach(e=>{n.values[e].ranges.forEach(e=>{t.Range.foreach(e,(e,t)=>{this._renderCache.realDeleteValue(e,t)})})})}else if(e.id===n.SetRangeValuesMutation.id){let n=e.params;new t.ObjectMatrix(n.cellValue).forValue((e,t)=>{this._renderCache.realDeleteValue(e,t)})}})),this.disposeWithMe(this._instanceService.getCurrentTypeOfUnit$(t.UniverInstanceType.UNIVER_SHEET).pipe((0,i.switchMap)(e=>(e==null?void 0:e.activeSheet$)??(0,i.of)(null)),(0,i.skip)(1)).subscribe(()=>this._renderCache.reset()))}_formatCell(e,n){var r;if(!e||e.v==null||e.t===t.CellValueType.BOOLEAN||e.t===t.CellValueType.FORCE_STRING)return e;let{unitId:i,subUnitId:a,row:o,col:s,workbook:c}=n,l=this.getLocale(c),u=c.getDateSystem(),d=((r=c.getStyles().get(e.s))==null?void 0:r.n)??this._numfmtService.getValue(i,a,o,s),f=d==null?void 0:d.pattern;if((0,t.isDefaultFormat)(f)||!this._isNumericCell(e,f,l,u))return e;let p=e===n.rawData?{...e}:e;if((0,t.isTextFormat)(f)){var m;return p.t=t.CellValueType.STRING,(m=this._configService.getConfig(`sheets-numfmt.config`))!=null&&m.disableTextFormatMark||(p.markers={...p.markers,...F}),p}let h=`${i}_${a}_${e.v}_${f}_${l}_${u}`,g=this._renderCache.getValue(o,s);if(g&&g.parameters===h)return{...p,...g.result};let _=v(f,Number(e.v),l,u),y={v:_.result,t:t.CellValueType.NUMBER};if(_.result===``&&(y.coverable=!1),_.color){let e=this._themeService.getColorFromTheme(`${_.color}.500`)??_.color;e&&(y.interceptorStyle={cl:{rgb:e}})}return this._renderCache.setValue(o,s,{result:y,parameters:h}),Object.assign(p,y),p}_isNumericCell(e,r,i,a){var o;return e.t===t.CellValueType.NUMBER||(0,n.checkCellValueType)(e.v,e.t)===t.CellValueType.NUMBER||(0,t.isTextFormat)(r)&&typeof e.v==`string`&&typeof((o=(0,t.getNumfmtParseValueFilter)(e.v,{locale:i,dateSystem:a}))==null?void 0:o.v)==`number`}setNumfmtLocal(e){this._locale$.next(e)}};I=P([N(0,t.IUniverInstanceService),N(1,(0,t.Inject)(n.SheetInterceptorService)),N(2,(0,t.Inject)(t.ThemeService)),N(3,(0,t.Inject)(t.ICommandService)),N(4,(0,t.Inject)(n.INumfmtService)),N(5,(0,t.Inject)(t.LocaleService)),N(6,t.IConfigService)],I);var L=`@univerjs/sheets-numfmt`,R=`1.3.1`;let z=class extends t.Plugin{constructor(e=O,n,r,i){super(),this._config=e,this._injector=n,this._configService=r,this._commandService=i;let{...a}=(0,t.merge)({},O,this._config);this._configService.setConfig(D,a)}onStarting(){(0,t.registerDependencies)(this._injector,[[I]]),(0,t.touchDependencies)(this._injector,[[I]]),[C,E,w,T,S].forEach(e=>{this.disposeWithMe(this._commandService.registerCommand(e))})}};M(z,`pluginName`,`SHEET_NUMFMT_PLUGIN`),M(z,`packageName`,L),M(z,`version`,R),M(z,`type`,t.UniverInstanceType.UNIVER_SHEET),z=P([(0,t.DependentOn)(n.UniverSheetsPlugin),N(1,(0,t.Inject)(t.Injector)),N(2,t.IConfigService),N(3,t.ICommandService)],z);let B=e=>t.currencySymbols.find(t=>e.includes(t)),V={dates:[`yyyy/MM/dd`,`MM-dd`],shortDate:`MM-dd`},H={dates:[`m/d/yyyy`,`mm/dd/yyyy`,`mmm d, yyyy`,`m/d`,`mmmm d`],shortDate:`m/d`},U={dates:[`dd/mm/yyyy`,`d/m/yyyy`,`d mmm yyyy`,`dd/mm`,`d mmmm`],shortDate:`dd/mm`},W={dates:[`dd.mm.yyyy`,`d.m.yyyy`,`d mmm yyyy`,`dd.mm`,`d mmmm`],shortDate:`dd.mm`},G={dates:[`yyyy/MM/dd`,`yyyy"年"MM"月"dd"日"`,`MM-dd`,`M"月"d"日"`],shortDate:`MM-dd`,periodBeforeTime:!0},K={[t.LocaleType.EN_US]:H,[t.LocaleType.FR_FR]:U,[t.LocaleType.ZH_CN]:G,[t.LocaleType.RU_RU]:W,[t.LocaleType.ZH_TW]:G,[t.LocaleType.ZH_HK]:{...G,dates:[`dd/MM/yyyy`,...G.dates],shortDate:`dd/MM`},[t.LocaleType.VI_VN]:U,[t.LocaleType.FA_IR]:V,[t.LocaleType.CA_ES]:{dates:[`dd/mm/yyyy`,`d/m/yyyy`,`dd/mm`],shortDate:`dd/mm`},[t.LocaleType.JA_JP]:G,[t.LocaleType.KO_KR]:{dates:[`yyyy. mm. dd.`,`yyyy"년" m"월" d"일"`,`mm-dd`,`m"월" d"일"`],shortDate:`mm-dd`,periodBeforeTime:!0},[t.LocaleType.ES_ES]:U,[t.LocaleType.SK_SK]:W,[t.LocaleType.PT_BR]:U,[t.LocaleType.DE_DE]:W,[t.LocaleType.IT_IT]:U,[t.LocaleType.ID_ID]:U,[t.LocaleType.PL_PL]:W,[t.LocaleType.AR_SA]:{dates:[`dd/mm/yyyy`,`d/m/yyyy`,`dd/mm`],shortDate:`dd/mm`}};function q(e){let t=K[e]??V,n=[`hh:mm`,`h:mm`,`h:mm:ss`].map(e=>t.periodBeforeTime?`AM/PM ${e}`:`${e} AM/PM`);return[.../* @__PURE__ */ new Set([`yyyy-MM-dd`,...t.dates,`h:mm:ss`,`h:mm`,...n,`${t.shortDate} ${n[0]}`])]}let J=()=>t.currencySymbols.map(e=>({label:e,value:e})),Y=e=>d.map(t=>({label:t.label(e),value:t.suffix(e),color:t.color})),X=(0,t.excelDateTimeSerial)(new Date(Date.UTC(1930,7,5,13,30,30)));e.AddDecimalCommand=C,e.CURRENCYFORMAT=d,e.DATEFMTLISG=l,e.NUMBERFORMAT=u,e.SHEETS_NUMFMT_PLUGIN_CONFIG_KEY=D,e.SetCurrencyCommand=w,e.SetNumfmtCommand=S,e.SetPercentCommand=T,Object.defineProperty(e,"SheetsNumfmtCellContentController",{enumerable:!0,get:function(){return I}}),e.SubtractDecimalCommand=E,Object.defineProperty(e,"UniverSheetsNumfmtPlugin",{enumerable:!0,get:function(){return z}}),e.getCurrencyFormat=c,e.getCurrencyFormatOptions=Y,e.getCurrencyOptions=J,e.getCurrencySymbolByLocale=s,e.getCurrencySymbolIconByLocale=o,e.getCurrencyType=B,e.getDateFormatOptions=(e,n)=>{if(e===void 0)return l.map(e=>({label:e.label,value:e.suffix}));let r=(0,t.getNumfmtLocaleTag)(e),i=q(e);return n&&!i.includes(n)&&i.unshift(n),i.map(e=>({label:t.numfmt.format(e,X,{locale:r}),value:e}))},e.getDecimalFromPattern=f,e.getDecimalString=p,e.getNumberFormatOptions=()=>u.map(e=>({label:e.label,value:e.suffix,color:e.color})),e.getPatternPreview=_,e.getPatternPreviewIgnoreGeneral=v,e.getPatternType=g,e.isPatternHasDecimal=h,e.localeCurrencySymbolMap=a,e.setPatternDecimal=m});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@univerjs/sheets-numfmt",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Number format services and commands for Univer Sheets.",
|
|
6
6
|
"author": "DreamNum Co., Ltd. <developer@univer.ai>",
|
|
@@ -62,12 +62,12 @@
|
|
|
62
62
|
"rxjs": ">=7.0.0"
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
|
-
"@univerjs/core": "1.
|
|
66
|
-
"@univerjs/engine-formula": "1.
|
|
67
|
-
"@univerjs/sheets": "1.
|
|
65
|
+
"@univerjs/core": "1.3.1",
|
|
66
|
+
"@univerjs/engine-formula": "1.3.1",
|
|
67
|
+
"@univerjs/sheets": "1.3.1"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@univerjs-infra/shared": "1.
|
|
70
|
+
"@univerjs-infra/shared": "1.3.1",
|
|
71
71
|
"rxjs": "7.8.2",
|
|
72
72
|
"typescript": "^6.0.3",
|
|
73
73
|
"vitest": "^5.0.4"
|