@chalabi/svelte-sheets 2.0.3 → 2.0.5

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.
Files changed (2) hide show
  1. package/dist/Sheet.svelte +71 -28
  2. package/package.json +1 -1
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;
@@ -180,45 +192,58 @@ export function onInputChange(value, row, column) {
180
192
  }
181
193
  function refresh(data, viewport_height, viewport_width) {
182
194
  return __awaiter(this, void 0, void 0, function* () {
195
+ if (!viewport)
196
+ return;
183
197
  const { scrollTop, scrollLeft } = viewport;
184
198
  yield tick(); // wait until the DOM is up to date
199
+ const defaultHeight = 24;
200
+ const defaultWidth = getDefaultColWidth();
201
+ // Safety limits to prevent infinite loops
202
+ const maxRowsToRender = Math.max(data.length, Math.ceil((viewport_height + bottom_buffer * 2) / defaultHeight) + 10, 100);
203
+ const maxColsToRender = Math.max(columns.length, Math.ceil((viewport_width + right_buffer * 2) / defaultWidth) + 10, 50);
185
204
  let content_height = top - scrollTop - bottom_buffer;
186
205
  let content_width = left - scrollLeft - left_buffer;
187
206
  // vertical
188
207
  let y = startY;
189
- while (content_height < viewport_height) {
208
+ while (content_height < viewport_height && y < maxRowsToRender) {
190
209
  let row = rowElements[y - startY];
191
210
  if (!row) {
192
211
  endY = y + 1;
193
212
  yield tick(); // render the newly visible row
194
213
  row = rowElements[y - startY];
214
+ // If still no row after tick, break to prevent infinite loop
215
+ if (!row)
216
+ break;
195
217
  }
196
218
  const row_height = (height_map[y] = getRowHeight(y));
197
219
  content_height += row_height;
198
220
  y += 1;
199
221
  }
200
- endY = y;
222
+ endY = Math.max(y, 1);
201
223
  let remaining = data.length - endY;
202
- average_height = (top + content_height) / endY;
203
- bottom = remaining * average_height;
224
+ average_height = endY > 0 ? (top + content_height) / endY : defaultHeight;
225
+ bottom = remaining * (average_height || defaultHeight);
204
226
  height_map.length = data.length;
205
227
  // horizontal
206
228
  let x = startX;
207
- while (content_width < viewport_width) {
229
+ while (content_width < viewport_width && x < maxColsToRender) {
208
230
  let col = colElements[x - startX];
209
231
  if (!col) {
210
232
  endX = x + 1;
211
233
  yield tick(); // render the newly visible col
212
234
  col = colElements[x - startX];
235
+ // If still no col after tick, break to prevent infinite loop
236
+ if (!col)
237
+ break;
213
238
  }
214
239
  const col_width = (width_map[x] = getColumnsWidth(x));
215
240
  content_width += col_width;
216
241
  x += 1;
217
242
  }
218
- endX = x;
243
+ endX = Math.max(x, 1);
219
244
  let remains = columns.length - endX;
220
- average_width = (left + content_width) / endX;
221
- right = remains * average_width;
245
+ average_width = endX > 0 ? (left + content_width) / endX : defaultWidth;
246
+ right = remains * (average_width || defaultWidth);
222
247
  width_map.length = columns.length;
223
248
  });
224
249
  }
@@ -229,6 +254,9 @@ let scrollTop;
229
254
  $: (function scrollX() {
230
255
  if (scrollLeft === undefined || !colElements)
231
256
  return;
257
+ // Ensure we have valid dimensions to work with
258
+ const totalCols = Math.max(columns.length, endX, 1);
259
+ const defaultWidth = getDefaultColWidth();
232
260
  // if (!scrollLeft) ;
233
261
  // horizontal scrolling
234
262
  for (let v = 0; v < colElements.length; v += 1) {
@@ -236,8 +264,8 @@ $: (function scrollX() {
236
264
  }
237
265
  let c = 0;
238
266
  let x = 0;
239
- while (true) {
240
- const col_width = width_map[c] || average_width;
267
+ while (c < totalCols) {
268
+ const col_width = width_map[c] || average_width || defaultWidth;
241
269
  if (x + col_width > scrollLeft - left_buffer) {
242
270
  startX = c;
243
271
  left = x;
@@ -246,31 +274,40 @@ $: (function scrollX() {
246
274
  x += col_width;
247
275
  c += 1;
248
276
  }
249
- while (true) {
250
- x += width_map[c] || average_width;
277
+ // Safety: ensure we found a valid startX
278
+ if (c >= totalCols) {
279
+ startX = Math.max(0, totalCols - 1);
280
+ left = x;
281
+ }
282
+ while (c < totalCols + Math.ceil((viewport_width + right_buffer) / defaultWidth)) {
283
+ const w = width_map[c] || average_width || defaultWidth;
284
+ x += w;
251
285
  c += 1;
252
286
  if (x > scrollLeft + viewport_width + right_buffer)
253
287
  break;
254
288
  }
255
- endX = c;
289
+ endX = Math.max(c, 1);
256
290
  const remaining = endX > columns.length
257
- ? (viewport_width + right_buffer) / 24
291
+ ? (viewport_width + right_buffer) / defaultWidth
258
292
  : columns.length - endX;
259
- average_width = x / endX;
293
+ average_width = endX > 0 ? x / endX : defaultWidth;
260
294
  // while (c < columns.length) width_map[c++] = average_width;
261
- right = remaining * average_width;
295
+ right = remaining * (average_width || defaultWidth);
262
296
  })();
263
297
  $: (function scrollY() {
264
298
  if (scrollTop === undefined || !rowElements)
265
299
  return;
300
+ // Ensure we have valid dimensions to work with
301
+ const totalRows = Math.max(data.length, endY, 1);
302
+ const defaultHeight = 24;
266
303
  // vertical scrolling
267
304
  for (let v = 0; v < rowElements.length; v += 1) {
268
305
  height_map[startY + v] = getRowHeight(startY + v);
269
306
  }
270
307
  let r = 0;
271
308
  let y = 0;
272
- while (true) {
273
- const row_height = height_map[r] || average_height;
309
+ while (r < totalRows) {
310
+ const row_height = height_map[r] || average_height || defaultHeight;
274
311
  if (y + row_height > scrollTop - top_buffer) {
275
312
  startY = r;
276
313
  top = y;
@@ -279,19 +316,25 @@ $: (function scrollY() {
279
316
  y += row_height;
280
317
  r += 1;
281
318
  }
282
- while (true) {
283
- y += height_map[r] || average_height;
319
+ // Safety: ensure we found a valid startY
320
+ if (r >= totalRows) {
321
+ startY = Math.max(0, totalRows - 1);
322
+ top = y;
323
+ }
324
+ while (r < totalRows + Math.ceil((viewport_height + bottom_buffer) / defaultHeight)) {
325
+ const h = height_map[r] || average_height || defaultHeight;
326
+ y += h;
284
327
  r += 1;
285
328
  if (y > scrollTop + viewport_height + bottom_buffer)
286
329
  break;
287
330
  }
288
- endY = r;
331
+ endY = Math.max(r, 1);
289
332
  const remaining = endY > data.length
290
- ? (viewport_height + bottom_buffer) / 24
333
+ ? (viewport_height + bottom_buffer) / defaultHeight
291
334
  : data.length - endY;
292
- average_height = y / endY;
335
+ average_height = endY > 0 ? y / endY : defaultHeight;
293
336
  // while (r < data.length) height_map[r++] = average_height;
294
- bottom = remaining * average_height;
337
+ bottom = remaining * (average_height || defaultHeight);
295
338
  })();
296
339
  function handle_scroll(e) {
297
340
  scrollTop = viewport.scrollTop;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chalabi/svelte-sheets",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "Run your excel sheet in the browser!",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",