@chalabi/svelte-sheets 2.0.4 → 2.0.6

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/Sheet.svelte CHANGED
@@ -131,11 +131,23 @@ $: {
131
131
  currentValue = "";
132
132
  }
133
133
  }
134
+ function getDefaultColWidth() {
135
+ const w = config.defaultColWidth;
136
+ if (typeof w === "string") {
137
+ return Number(w.replace("px", "")) || 50;
138
+ }
139
+ return Number(w) || 50;
140
+ }
134
141
  function getColumnsWidth(i) {
135
- var _a, _b, _c;
136
- return Number(typeof ((_a = columns[i]) === null || _a === void 0 ? void 0 : _a.width) == "string"
137
- ? (_b = columns[i]) === null || _b === void 0 ? void 0 : _b.width.replace("px", "")
138
- : ((_c = columns[i]) === null || _c === void 0 ? void 0 : _c.width) || config.defaultColWidth);
142
+ var _a;
143
+ const colWidth = (_a = columns[i]) === null || _a === void 0 ? void 0 : _a.width;
144
+ if (colWidth !== undefined && colWidth !== null) {
145
+ if (typeof colWidth === "string") {
146
+ return Number(colWidth.replace("px", "")) || getDefaultColWidth();
147
+ }
148
+ return Number(colWidth) || getDefaultColWidth();
149
+ }
150
+ return getDefaultColWidth();
139
151
  }
140
152
  function getRowHeight(i) {
141
153
  var _a, _b, _c, _d;
@@ -178,6 +190,51 @@ export function onInputChange(value, row, column) {
178
190
  data[row.i][column.i] = value;
179
191
  }
180
192
  }
193
+ function isBooleanCell(colIndex, rowIndex) {
194
+ var _a, _b;
195
+ // Don't treat first row as boolean (headers)
196
+ if (rowIndex === 0)
197
+ return false;
198
+ return (((_a = config.booleanColumns) === null || _a === void 0 ? void 0 : _a.includes(colIndex)) ||
199
+ ((_b = config.booleanRows) === null || _b === void 0 ? void 0 : _b.includes(rowIndex)));
200
+ }
201
+ function toggleBooleanCell(colIndex, rowIndex) {
202
+ var _a, _b;
203
+ if (isReadOnly)
204
+ return;
205
+ if ((_a = columns[colIndex]) === null || _a === void 0 ? void 0 : _a.readOnly)
206
+ return;
207
+ if (!isBooleanCell(colIndex, rowIndex))
208
+ return;
209
+ cmdz = true;
210
+ const currentValue = (_b = data[rowIndex]) === null || _b === void 0 ? void 0 : _b[colIndex];
211
+ const newValue = !currentValue;
212
+ if (rowIndex > data.length - 1) {
213
+ data = [
214
+ ...data,
215
+ ...Array.from({ length: rowIndex - data.length + 1 }, (v, i) => {
216
+ if (i == rowIndex) {
217
+ return Array.from({ length: columns.length }, (_, i) => {
218
+ if (i == colIndex) {
219
+ return newValue;
220
+ }
221
+ else {
222
+ return "";
223
+ }
224
+ });
225
+ }
226
+ else {
227
+ return Array.from({ length: columns.length }, (_) => "");
228
+ }
229
+ }),
230
+ ];
231
+ }
232
+ else {
233
+ data[rowIndex][colIndex] = newValue;
234
+ }
235
+ historyPush(data, rows, columns, style);
236
+ cmdz = false;
237
+ }
181
238
  function refresh(data, viewport_height, viewport_width) {
182
239
  return __awaiter(this, void 0, void 0, function* () {
183
240
  if (!viewport)
@@ -185,7 +242,7 @@ function refresh(data, viewport_height, viewport_width) {
185
242
  const { scrollTop, scrollLeft } = viewport;
186
243
  yield tick(); // wait until the DOM is up to date
187
244
  const defaultHeight = 24;
188
- const defaultWidth = config.defaultColWidth || 50;
245
+ const defaultWidth = getDefaultColWidth();
189
246
  // Safety limits to prevent infinite loops
190
247
  const maxRowsToRender = Math.max(data.length, Math.ceil((viewport_height + bottom_buffer * 2) / defaultHeight) + 10, 100);
191
248
  const maxColsToRender = Math.max(columns.length, Math.ceil((viewport_width + right_buffer * 2) / defaultWidth) + 10, 50);
@@ -244,7 +301,7 @@ $: (function scrollX() {
244
301
  return;
245
302
  // Ensure we have valid dimensions to work with
246
303
  const totalCols = Math.max(columns.length, endX, 1);
247
- const defaultWidth = config.defaultColWidth || 50;
304
+ const defaultWidth = getDefaultColWidth();
248
305
  // if (!scrollLeft) ;
249
306
  // horizontal scrolling
250
307
  for (let v = 0; v < colElements.length; v += 1) {
@@ -978,6 +1035,7 @@ function historyPush(data, rows, columns, style) {
978
1035
  on:dblclick={(_) =>
979
1036
  !isReadOnly &&
980
1037
  !(columns[x.i] && columns[x.i].readOnly) &&
1038
+ !isBooleanCell(x.i, r.i) &&
981
1039
  (edition = [x.i, r.i])}
982
1040
  class:readonly={columns[x.i] && columns[x.i].readOnly}
983
1041
  style={computeStyles(
@@ -990,7 +1048,7 @@ function historyPush(data, rows, columns, style) {
990
1048
  r.data && r.data[x.i + 1]
991
1049
  )}
992
1050
  >
993
- {#if String(edition) == String([x.i, r.i])}
1051
+ {#if String(edition) == String([x.i, r.i]) && !isBooleanCell(x.i, r.i)}
994
1052
  <input
995
1053
  autofocus
996
1054
  on:blur={(e) => {
@@ -1003,6 +1061,20 @@ function historyPush(data, rows, columns, style) {
1003
1061
  x.i
1004
1062
  )}px; height: ${getRowHeight(r.i)}px; min-height: 22px;`}
1005
1063
  />
1064
+ {:else if isBooleanCell(x.i, r.i)}
1065
+ <span
1066
+ class="boolean-cell"
1067
+ class:readonly={columns[x.i] && columns[x.i].readOnly}
1068
+ on:click|stopPropagation={() => toggleBooleanCell(x.i, r.i)}
1069
+ role="button"
1070
+ tabindex="-1"
1071
+ >
1072
+ {#if r.data && r.data[x.i]}
1073
+ <span class="checkmark">✓</span>
1074
+ {:else}
1075
+ <span class="crossmark">⦻</span>
1076
+ {/if}
1077
+ </span>
1006
1078
  {:else}{(r.data && r.data[x.i]) || ""}{/if}
1007
1079
  </td>
1008
1080
  {/each}
@@ -1231,4 +1303,28 @@ function historyPush(data, rows, columns, style) {
1231
1303
  .hidden {
1232
1304
  display: none;
1233
1305
  }
1306
+ .boolean-cell {
1307
+ display: flex;
1308
+ align-items: center;
1309
+ justify-content: center;
1310
+ width: 100%;
1311
+ height: 100%;
1312
+ cursor: pointer;
1313
+ user-select: none;
1314
+ }
1315
+ .boolean-cell.readonly {
1316
+ cursor: default;
1317
+ }
1318
+ .checkmark {
1319
+ font-size: 18px;
1320
+ font-weight: bold;
1321
+ color: var(--sheet-accent);
1322
+ line-height: 1;
1323
+ }
1324
+ .crossmark {
1325
+ font-size: 18px;
1326
+ font-weight: bold;
1327
+ color: #ef4444;
1328
+ line-height: 1;
1329
+ }
1234
1330
  </style>
@@ -8,6 +8,8 @@ export declare const defaultconfig: {
8
8
  nestedHeaders: any;
9
9
  defaultColWidth: number;
10
10
  defaultColAlign: string;
11
+ booleanColumns: number[];
12
+ booleanRows: number[];
11
13
  minSpareRows: number;
12
14
  minSpareCols: number;
13
15
  minDimensions: number[];
@@ -17,6 +17,10 @@ export const defaultconfig = {
17
17
  // Column width that is used by default
18
18
  defaultColWidth: 50,
19
19
  defaultColAlign: "center",
20
+ // Boolean columns for checkbox mode
21
+ booleanColumns: [],
22
+ // Boolean rows for checkbox mode
23
+ booleanRows: [],
20
24
  // Spare rows and columns
21
25
  minSpareRows: 0,
22
26
  minSpareCols: 0,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chalabi/svelte-sheets",
3
- "version": "2.0.4",
3
+ "version": "2.0.6",
4
4
  "description": "Run your excel sheet in the browser!",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",