@coco-vexel/coco-office-editor 1.0.2 → 1.0.4
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/dist/index.cjs +79 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +79 -21
- package/dist/index.js.map +1 -1
- package/dist/spreadsheet.cjs +79 -21
- package/dist/spreadsheet.cjs.map +1 -1
- package/dist/spreadsheet.d.cts +2 -1
- package/dist/spreadsheet.d.ts +2 -1
- package/dist/spreadsheet.js +79 -21
- package/dist/spreadsheet.js.map +1 -1
- package/package.json +3 -3
package/dist/spreadsheet.cjs
CHANGED
|
@@ -73,7 +73,7 @@ function buildSpreadsheetSheetRenderModel(sheet, styles, options = {}) {
|
|
|
73
73
|
maxColumn = Math.max(maxColumn, column.max);
|
|
74
74
|
}
|
|
75
75
|
for (const cell of sheet.cells) {
|
|
76
|
-
const renderCell = toRenderCell(cell, styles, options.formulaResults);
|
|
76
|
+
const renderCell = toRenderCell(cell, styles, options.formulaResults, options.date1904);
|
|
77
77
|
cellsByAddress.set(cell.address, renderCell);
|
|
78
78
|
cellsByRowColumn.set(rowColumnKey(cell.row, cell.column), renderCell);
|
|
79
79
|
maxRow = Math.max(maxRow, cell.row);
|
|
@@ -89,7 +89,7 @@ function buildSpreadsheetSheetRenderModel(sheet, styles, options = {}) {
|
|
|
89
89
|
kind: "formula",
|
|
90
90
|
value: null
|
|
91
91
|
};
|
|
92
|
-
const renderCell = toRenderCell(virtualCell, styles, options.formulaResults);
|
|
92
|
+
const renderCell = toRenderCell(virtualCell, styles, options.formulaResults, options.date1904);
|
|
93
93
|
cellsByAddress.set(address, renderCell);
|
|
94
94
|
cellsByRowColumn.set(rowColumnKey(parsed.row, parsed.column), renderCell);
|
|
95
95
|
maxRow = Math.max(maxRow, parsed.row);
|
|
@@ -142,7 +142,7 @@ function getRenderCell(model, row, column) {
|
|
|
142
142
|
return model.cellsByRowColumn.get(rowColumnKey(row, column));
|
|
143
143
|
}
|
|
144
144
|
__name(getRenderCell, "getRenderCell");
|
|
145
|
-
function toRenderCell(cell, styles, formulaResults) {
|
|
145
|
+
function toRenderCell(cell, styles, formulaResults, date1904 = false) {
|
|
146
146
|
var _a, _b;
|
|
147
147
|
const formulaResult = formulaResults == null ? void 0 : formulaResults.get(cell.address);
|
|
148
148
|
const resultValue = formulaResult && !formulaResult.error ? formulaResult.value : void 0;
|
|
@@ -153,16 +153,16 @@ function toRenderCell(cell, styles, formulaResults) {
|
|
|
153
153
|
address: cell.address,
|
|
154
154
|
kind: cell.kind,
|
|
155
155
|
value,
|
|
156
|
-
displayText: cellToDisplayText(cell, styles, value, formulaResult),
|
|
156
|
+
displayText: cellToDisplayText(cell, styles, value, formulaResult, date1904),
|
|
157
157
|
formula: cell.formula,
|
|
158
158
|
styleId: cell.styleId,
|
|
159
159
|
source: cell
|
|
160
160
|
};
|
|
161
161
|
}
|
|
162
162
|
__name(toRenderCell, "toRenderCell");
|
|
163
|
-
function cellToDisplayText(cell, styles, value, formulaResult) {
|
|
163
|
+
function cellToDisplayText(cell, styles, value, formulaResult, date1904 = false) {
|
|
164
164
|
if (formulaResult == null ? void 0 : formulaResult.error) return formulaResult.displayText;
|
|
165
|
-
if (value !== null) return formatSpreadsheetDisplayValue(value, numberFormatCodeForCell(cell, styles));
|
|
165
|
+
if (value !== null) return formatSpreadsheetDisplayValue(value, numberFormatCodeForCell(cell, styles), date1904);
|
|
166
166
|
return "";
|
|
167
167
|
}
|
|
168
168
|
__name(cellToDisplayText, "cellToDisplayText");
|
|
@@ -171,13 +171,13 @@ function numberFormatCodeForCell(cell, styles) {
|
|
|
171
171
|
return (_b = styles.cellFormats[(_a = cell.styleId) != null ? _a : 0]) == null ? void 0 : _b.numberFormatCode;
|
|
172
172
|
}
|
|
173
173
|
__name(numberFormatCodeForCell, "numberFormatCodeForCell");
|
|
174
|
-
function formatSpreadsheetDisplayValue(value, formatCode) {
|
|
174
|
+
function formatSpreadsheetDisplayValue(value, formatCode, date1904 = false) {
|
|
175
175
|
if (value === null) return "";
|
|
176
176
|
if (typeof value !== "number" || !formatCode) return String(value);
|
|
177
177
|
const normalized = formatCode.toLowerCase();
|
|
178
178
|
if (normalized.includes("%")) return `${formatFixedNumber(value * 100, decimalPlaces(formatCode))}%`;
|
|
179
179
|
if (normalized.includes("$")) return `$${formatFixedNumber(value, decimalPlaces(formatCode), true)}`;
|
|
180
|
-
if (
|
|
180
|
+
if (looksLikeDateTimeFormat(normalized)) return formatExcelSerialDateTime(value, normalized, date1904);
|
|
181
181
|
if (formatCode.includes(",")) return formatFixedNumber(value, decimalPlaces(formatCode), true);
|
|
182
182
|
if (formatCode.includes(".")) return formatFixedNumber(value, decimalPlaces(formatCode));
|
|
183
183
|
return String(value);
|
|
@@ -197,20 +197,71 @@ function formatFixedNumber(value, digits, grouping = false) {
|
|
|
197
197
|
}).format(value);
|
|
198
198
|
}
|
|
199
199
|
__name(formatFixedNumber, "formatFixedNumber");
|
|
200
|
-
function
|
|
200
|
+
function looksLikeDateTimeFormat(formatCode) {
|
|
201
|
+
const knownFormats = [
|
|
202
|
+
"d-mmm",
|
|
203
|
+
"mmm-yy",
|
|
204
|
+
"h:mm am/pm",
|
|
205
|
+
"h:mm:ss am/pm",
|
|
206
|
+
"h:mm",
|
|
207
|
+
"h:mm:ss",
|
|
208
|
+
"mm:ss",
|
|
209
|
+
"[h]:mm:ss",
|
|
210
|
+
"mmss.0"
|
|
211
|
+
];
|
|
212
|
+
if (knownFormats.includes(formatCode)) return true;
|
|
201
213
|
return /(^|[^a-z])y{2,4}([^a-z]|$)/.test(formatCode) && /(^|[^a-z])d{1,2}([^a-z]|$)/.test(formatCode);
|
|
202
214
|
}
|
|
203
|
-
__name(
|
|
204
|
-
function
|
|
215
|
+
__name(looksLikeDateTimeFormat, "looksLikeDateTimeFormat");
|
|
216
|
+
function formatExcelSerialDateTime(serial, formatCode, date1904) {
|
|
205
217
|
if (!Number.isFinite(serial)) return String(serial);
|
|
206
|
-
const
|
|
207
|
-
const
|
|
208
|
-
const
|
|
209
|
-
const
|
|
210
|
-
const
|
|
218
|
+
const wholeDays = Math.floor(serial);
|
|
219
|
+
const dayFraction = serial - wholeDays;
|
|
220
|
+
const totalSeconds = Math.round(dayFraction * 86400) % 86400;
|
|
221
|
+
const hour = Math.floor(totalSeconds / 3600);
|
|
222
|
+
const minute = Math.floor(totalSeconds % 3600 / 60);
|
|
223
|
+
const second = totalSeconds % 60;
|
|
224
|
+
const hour12 = hour % 12 || 12;
|
|
225
|
+
const amPm = hour < 12 ? "AM" : "PM";
|
|
226
|
+
const pad = /* @__PURE__ */ __name((value) => String(value).padStart(2, "0"), "pad");
|
|
227
|
+
if (formatCode === "h:mm am/pm") return `${hour12}:${pad(minute)} ${amPm}`;
|
|
228
|
+
if (formatCode === "h:mm:ss am/pm") return `${hour12}:${pad(minute)}:${pad(second)} ${amPm}`;
|
|
229
|
+
if (formatCode === "h:mm") return `${hour}:${pad(minute)}`;
|
|
230
|
+
if (formatCode === "h:mm:ss") return `${hour}:${pad(minute)}:${pad(second)}`;
|
|
231
|
+
if (formatCode === "mm:ss") return `${pad(minute)}:${pad(second)}`;
|
|
232
|
+
if (formatCode === "[h]:mm:ss") return `${Math.floor(serial * 24)}:${pad(minute)}:${pad(second)}`;
|
|
233
|
+
if (formatCode === "mmss.0") return `${pad(minute)}${pad(second)}.0`;
|
|
234
|
+
const phantomLeapDay = !date1904 && wholeDays === 60;
|
|
235
|
+
const epoch = date1904 ? Date.UTC(1904, 0, 1) : Date.UTC(1899, 11, 30);
|
|
236
|
+
const date = new Date(epoch + wholeDays * 864e5);
|
|
237
|
+
const year = phantomLeapDay ? 1900 : date.getUTCFullYear();
|
|
238
|
+
const monthNumber = phantomLeapDay ? 2 : date.getUTCMonth() + 1;
|
|
239
|
+
const dayNumber = phantomLeapDay ? 29 : date.getUTCDate();
|
|
240
|
+
const month = pad(monthNumber);
|
|
241
|
+
const day = pad(dayNumber);
|
|
242
|
+
const shortYear = pad(year % 100);
|
|
243
|
+
const monthName = [
|
|
244
|
+
"Jan",
|
|
245
|
+
"Feb",
|
|
246
|
+
"Mar",
|
|
247
|
+
"Apr",
|
|
248
|
+
"May",
|
|
249
|
+
"Jun",
|
|
250
|
+
"Jul",
|
|
251
|
+
"Aug",
|
|
252
|
+
"Sep",
|
|
253
|
+
"Oct",
|
|
254
|
+
"Nov",
|
|
255
|
+
"Dec"
|
|
256
|
+
][monthNumber - 1];
|
|
257
|
+
if (formatCode === "mm-dd-yy") return `${month}-${day}-${shortYear}`;
|
|
258
|
+
if (formatCode === "d-mmm-yy") return `${dayNumber}-${monthName}-${shortYear}`;
|
|
259
|
+
if (formatCode === "d-mmm") return `${dayNumber}-${monthName}`;
|
|
260
|
+
if (formatCode === "mmm-yy") return `${monthName}-${shortYear}`;
|
|
261
|
+
if (formatCode === "m/d/yy h:mm") return `${monthNumber}/${dayNumber}/${shortYear} ${hour}:${pad(minute)}`;
|
|
211
262
|
return `${year}-${month}-${day}`;
|
|
212
263
|
}
|
|
213
|
-
__name(
|
|
264
|
+
__name(formatExcelSerialDateTime, "formatExcelSerialDateTime");
|
|
214
265
|
var DEFAULT_ROW_HEIGHT = 22;
|
|
215
266
|
var DEFAULT_COLUMN_WIDTH = 96;
|
|
216
267
|
var DEFAULT_ROW_HEADER_WIDTH = 48;
|
|
@@ -1651,7 +1702,10 @@ var _SpreadsheetEditorRuntime = class _SpreadsheetEditorRuntime {
|
|
|
1651
1702
|
activeSheetName: firstSheet.name,
|
|
1652
1703
|
formulaResults,
|
|
1653
1704
|
formulaEngine,
|
|
1654
|
-
activeSheet: buildSpreadsheetSheetRenderModel(firstSheet, model.styles, {
|
|
1705
|
+
activeSheet: buildSpreadsheetSheetRenderModel(firstSheet, model.styles, {
|
|
1706
|
+
formulaResults,
|
|
1707
|
+
date1904: model.date1904
|
|
1708
|
+
})
|
|
1655
1709
|
}, options);
|
|
1656
1710
|
}
|
|
1657
1711
|
isReadOnly() {
|
|
@@ -1742,7 +1796,8 @@ var _SpreadsheetEditorRuntime = class _SpreadsheetEditorRuntime {
|
|
|
1742
1796
|
this.state.formulaResults = formulaResults;
|
|
1743
1797
|
this.state.formulaEngine = formulaEngine;
|
|
1744
1798
|
this.state.activeSheet = buildSpreadsheetSheetRenderModel(sheet, this.state.model.styles, {
|
|
1745
|
-
formulaResults
|
|
1799
|
+
formulaResults,
|
|
1800
|
+
date1904: this.state.model.date1904
|
|
1746
1801
|
});
|
|
1747
1802
|
}
|
|
1748
1803
|
async addSheet(name) {
|
|
@@ -1800,7 +1855,10 @@ var _SpreadsheetEditorRuntime = class _SpreadsheetEditorRuntime {
|
|
|
1800
1855
|
activeSheetName: sheet.name,
|
|
1801
1856
|
formulaResults,
|
|
1802
1857
|
formulaEngine,
|
|
1803
|
-
activeSheet: buildSpreadsheetSheetRenderModel(sheet, model.styles, {
|
|
1858
|
+
activeSheet: buildSpreadsheetSheetRenderModel(sheet, model.styles, {
|
|
1859
|
+
formulaResults,
|
|
1860
|
+
date1904: model.date1904
|
|
1861
|
+
})
|
|
1804
1862
|
});
|
|
1805
1863
|
}
|
|
1806
1864
|
async setCellValue(address, value, historyOptions = {}) {
|
|
@@ -2255,7 +2313,7 @@ var _SpreadsheetEditorRuntime = class _SpreadsheetEditorRuntime {
|
|
|
2255
2313
|
this.state.activeSheet = buildSpreadsheetSheetRenderModel(
|
|
2256
2314
|
this.state.activeSheet.source,
|
|
2257
2315
|
this.state.model.styles,
|
|
2258
|
-
{ formulaResults: this.state.formulaResults }
|
|
2316
|
+
{ formulaResults: this.state.formulaResults, date1904: this.state.model.date1904 }
|
|
2259
2317
|
);
|
|
2260
2318
|
}
|
|
2261
2319
|
async runUserMutation(options, mutate) {
|