@chalabi/svelte-sheets 2.0.1 → 2.0.2

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 +113 -81
  2. package/package.json +1 -1
package/dist/Sheet.svelte CHANGED
@@ -74,6 +74,8 @@ let hoverRaf = null;
74
74
  let pendingHover = null;
75
75
  let lastHover = null;
76
76
  let lastDrag = null;
77
+ let selectionRaf = null;
78
+ let extendRaf = null;
77
79
  // $: ((_) => {
78
80
  // history = history.slice(0, historyIndex);
79
81
  // history.push(data);
@@ -429,6 +431,92 @@ function onMouseOver(e) {
429
431
  });
430
432
  return;
431
433
  }
434
+ function getSelectionRect(start, end) {
435
+ if (!contents || !viewport)
436
+ return null;
437
+ const endC = Math.max(end.c - 1, start.c);
438
+ const endR = Math.max(end.r - 1, start.r);
439
+ const tlCell = contents.querySelector(`td[data-x="${start.c}"][data-y="${start.r}"]`);
440
+ const brCell = contents.querySelector(`td[data-x="${endC}"][data-y="${endR}"]`);
441
+ if (!tlCell || !brCell)
442
+ return null;
443
+ const containerRect = viewport.getBoundingClientRect();
444
+ const tlRect = tlCell.getBoundingClientRect();
445
+ const brRect = brCell.getBoundingClientRect();
446
+ const left = tlRect.left - containerRect.left + viewport.scrollLeft;
447
+ const top = tlRect.top - containerRect.top + viewport.scrollTop;
448
+ const right = brRect.right - containerRect.left + viewport.scrollLeft;
449
+ const bottom = brRect.bottom - containerRect.top + viewport.scrollTop;
450
+ if (!Number.isFinite(left) ||
451
+ !Number.isFinite(top) ||
452
+ !Number.isFinite(right) ||
453
+ !Number.isFinite(bottom)) {
454
+ return null;
455
+ }
456
+ return { left, top, right, bottom };
457
+ }
458
+ function updateSelectionOverlays() {
459
+ if (!mounted)
460
+ return;
461
+ const tl = (selected && decode(selected[0])) || { c: 0, r: 0 };
462
+ const br = (selected && decode(selected[1])) || { c: 0, r: 0 };
463
+ topLeft = {
464
+ c: br.c < tl.c ? br.c : tl.c,
465
+ r: br.r < tl.r ? br.r : tl.r,
466
+ };
467
+ bottomRight = {
468
+ c: br.c > tl.c ? br.c + 1 : tl.c + 1,
469
+ r: br.r > tl.r ? br.r + 1 : tl.r + 1,
470
+ };
471
+ const rect = getSelectionRect(topLeft, bottomRight);
472
+ if (!rect) {
473
+ tops.style = "display: none";
474
+ rights.style = "display: none";
475
+ bottoms.style = "display: none";
476
+ lefts.style = "display: none";
477
+ colLine.style = "display: none";
478
+ rowLine.style = "display: none";
479
+ square.style = "display: none";
480
+ return;
481
+ }
482
+ const { left, top, right, bottom } = rect;
483
+ tops.style = `display: block; width: ${right - left}px; left: ${left}px; top: ${top}px`;
484
+ rights.style = `display: block; height: ${bottom - top}px; left: ${right}px; top: ${top}px`;
485
+ bottoms.style = `display: block; width: ${right - left}px; left: ${left}px; top: ${bottom}px`;
486
+ lefts.style = `display: block; height: ${bottom - top}px; left: ${left}px; top: ${top}px`;
487
+ colLine.style = `display: block; width: ${right - left}px; left: ${left}px; top: ${top}px;`;
488
+ rowLine.style = `display: block; height: ${bottom - top}px; left: ${left}px; top: ${top}px`;
489
+ square.style = `display: block; left:${right}px; top:${bottom}px`;
490
+ selectWidth = right - left;
491
+ selectHeight = bottom - top;
492
+ }
493
+ function updateExtendOverlays() {
494
+ if (!extension || !extended)
495
+ return;
496
+ const tl = (selected && decode(extended[0])) || { c: 0, r: 0 };
497
+ const br = (selected && decode(extended[1])) || { c: 0, r: 0 };
498
+ topLeft = {
499
+ c: br.c < tl.c ? br.c : tl.c,
500
+ r: br.r < tl.r ? br.r : tl.r,
501
+ };
502
+ bottomRight = {
503
+ c: br.c > tl.c ? br.c + 1 : tl.c + 1,
504
+ r: br.r > tl.r ? br.r + 1 : tl.r + 1,
505
+ };
506
+ const rect = getSelectionRect(topLeft, bottomRight);
507
+ if (!rect) {
508
+ topextend.style = "display: none";
509
+ rightextend.style = "display: none";
510
+ bottomextend.style = "display: none";
511
+ leftextend.style = "display: none";
512
+ return;
513
+ }
514
+ const { left, top, right, bottom } = rect;
515
+ topextend.style = `display: block; width: ${right - left}px; left: ${left}px; top: ${top}px`;
516
+ rightextend.style = `display: block; height: ${bottom - top}px; left: ${right}px; top: ${top}px`;
517
+ bottomextend.style = `display: block; width: ${right - left}px; left: ${left}px; top: ${bottom}px`;
518
+ leftextend.style = `display: block; height: ${bottom - top}px; left: ${left}px; top: ${top}px`;
519
+ }
432
520
  function onKeyUp(e) {
433
521
  // on keyup just reinitialize everything
434
522
  keypressed[e.keyCode] = false;
@@ -583,80 +671,23 @@ let squareX;
583
671
  let squareY;
584
672
  let topLeft;
585
673
  let bottomRight;
586
- $: {
587
- if (extension && extended) {
588
- let tl = (selected && decode(extended[0])) || { c: 0, r: 0 };
589
- let br = (selected && decode(extended[1])) || { c: 0, r: 0 };
590
- topLeft = {
591
- c: br.c < tl.c ? br.c : tl.c,
592
- r: br.r < tl.r ? br.r : tl.r,
593
- };
594
- bottomRight = {
595
- c: br.c > tl.c ? br.c + 1 : tl.c + 1,
596
- r: br.r > tl.r ? br.r + 1 : tl.r + 1,
597
- };
598
- let top = 28;
599
- let right = 51;
600
- let bottom = 28;
601
- let left = 51;
602
- for (let i = 0; i < topLeft.r; i++) {
603
- top = top + getRowHeight(i);
604
- }
605
- for (let i = 0; i < topLeft.c; i++) {
606
- left = left + getColumnsWidth(i);
607
- }
608
- for (let i = 0; i < bottomRight.r; i++) {
609
- bottom = bottom + getRowHeight(i);
610
- }
611
- for (let i = 0; i < bottomRight.c; i++) {
612
- right = right + getColumnsWidth(i);
613
- }
614
- topextend.style = `width: ${right - left}px; left: ${left}px; top: ${top}px`;
615
- rightextend.style = `height: ${bottom - top}px; left: ${right}px; top: ${top}px`;
616
- bottomextend.style = `width: ${right - left}px; left: ${left}px; top: ${bottom}px`;
617
- leftextend.style = `height: ${bottom - top}px; left: ${left}px; top: ${top}px`;
618
- }
674
+ $: if (mounted) {
675
+ if (extendRaf)
676
+ cancelAnimationFrame(extendRaf);
677
+ extendRaf = requestAnimationFrame(() => {
678
+ extendRaf = null;
679
+ updateExtendOverlays();
680
+ });
619
681
  }
620
682
  let selectWidth;
621
683
  let selectHeight;
622
- $: {
623
- if (mounted) {
624
- let tl = (selected && decode(selected[0])) || { c: 0, r: 0 };
625
- let br = (selected && decode(selected[1])) || { c: 0, r: 0 };
626
- topLeft = {
627
- c: br.c < tl.c ? br.c : tl.c,
628
- r: br.r < tl.r ? br.r : tl.r,
629
- };
630
- bottomRight = {
631
- c: br.c > tl.c ? br.c + 1 : tl.c + 1,
632
- r: br.r > tl.r ? br.r + 1 : tl.r + 1,
633
- };
634
- let top = 28;
635
- let right = 51;
636
- let bottom = 28;
637
- let left = 51;
638
- for (let i = 0; i < topLeft.r; i++) {
639
- top = top + getRowHeight(i);
640
- }
641
- for (let i = 0; i < topLeft.c; i++) {
642
- left = left + getColumnsWidth(i);
643
- }
644
- for (let i = 0; i < bottomRight.r; i++) {
645
- bottom = bottom + getRowHeight(i);
646
- }
647
- for (let i = 0; i < bottomRight.c; i++) {
648
- right = right + getColumnsWidth(i);
649
- }
650
- tops.style = `width: ${right - left}px; left: ${left}px; top: ${top}px`;
651
- rights.style = `height: ${bottom - top}px; left: ${right}px; top: ${top}px`;
652
- bottoms.style = `width: ${right - left}px; left: ${left}px; top: ${bottom}px`;
653
- lefts.style = `height: ${bottom - top}px; left: ${left}px; top: ${top}px`;
654
- colLine.style = `width: ${right - left}px; left: ${left}px; top: 28px;`;
655
- rowLine.style = `height: ${bottom - top}px; left: 51px; top: ${top}px`;
656
- square.style = `left:${right}px; top:${bottom}px`;
657
- selectWidth = right - left;
658
- selectHeight = bottom - top;
659
- }
684
+ $: if (mounted) {
685
+ if (selectionRaf)
686
+ cancelAnimationFrame(selectionRaf);
687
+ selectionRaf = requestAnimationFrame(() => {
688
+ selectionRaf = null;
689
+ updateSelectionOverlays();
690
+ });
660
691
  }
661
692
  // history logic
662
693
  function historyPush(data, rows, columns, style) {
@@ -723,17 +754,18 @@ function historyPush(data, rows, columns, style) {
723
754
  <div class="row-line absolute" bind:this={rowLine} />
724
755
  <div
725
756
  tabindex={-1}
726
- use:draggable
727
- on:dragging={(e) => {
728
- if (isReadOnly) return;
729
- if (lastDrag && lastDrag.x === e.detail.x && lastDrag.y === e.detail.y) {
730
- return;
731
- }
732
- lastDrag = { x: e.detail.x, y: e.detail.y };
733
- squareX = e.detail.x;
734
- squareY = e.detail.y;
735
- }}
757
+ use:draggable
758
+ on:dragging={(e) => {
759
+ if (isReadOnly) return;
760
+ if (lastDrag && lastDrag.x === e.detail.x && lastDrag.y === e.detail.y) {
761
+ return;
762
+ }
763
+ lastDrag = { x: e.detail.x, y: e.detail.y };
764
+ squareX = e.detail.x;
765
+ squareY = e.detail.y;
766
+ }}
736
767
  class="square absolute interactive"
768
+ class:hidden={isReadOnly || config.disableHover || !config.selectionCopy}
737
769
  id="square"
738
770
  bind:this={square}
739
771
  />
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chalabi/svelte-sheets",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "Run your excel sheet in the browser!",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",