Skip to content

Commit 3986f9d

Browse files
committed
added dragstart and dragend events
1 parent d85515e commit 3986f9d

File tree

9 files changed

+2260
-18
lines changed

9 files changed

+2260
-18
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ For each of them you can give a style string like `"width:80px; padding-right:10
198198
|------------|---------------------|-------------------------------------------------------------------------------------|
199199
| input | (ev) => {ev.detail} | Fires when value changes. ev.detail gives the actual value |
200200
| change | (ev) => {ev.detail} | Fires when value changes, won't fire while typing. ev.detail gives the actual value |
201+
| dragstart | (ev) => {} | Fires when dragging starts |
202+
| dragend | (ev) => {} | Fires when dragging ends |
201203
| editstart | (ev) => {} | Fires when edit mode is started |
202204
| editend | (ev) => {} | Fires when edit mode is ended |
203205
| keydown | (ev) => {} | Original keydown event |

dist/index.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@
630630
wasActiveOnClick = document.activeElement === dragElement;
631631
$$invalidate(4, dragging = true);
632632
dragElement.focus();
633-
hasMoved = 0;
633+
hasMoved = false;
634634
clickX = isTouchDevice ? ev.touches[0].clientX : ev.clientX;
635635
clickY = isTouchDevice ? ev.touches[0].clientY : ev.clientY;
636636
$$invalidate(4, dragging = true);
@@ -653,11 +653,17 @@
653653
let distX = horizontal ? actX - clickX : 0;
654654
let distY = vertical ? -(actY - clickY) : 0;
655655
let stepNum = Math.abs(distX) > Math.abs(distY) ? distX : distY;
656+
657+
// fire dragstart before value changes
658+
if (stepNum != 0 && !hasMoved) {
659+
hasMoved = true;
660+
dispatch("dragstart");
661+
}
662+
656663
stepValue(stepNum * stepFactor);
657664
clickX = actX;
658665
clickY = actY;
659-
hasMoved++;
660-
}
666+
} // hasMoved++;
661667

662668
function touchendHandler(ev) {
663669
dispatch("consoleLog", ev.type);
@@ -666,10 +672,15 @@
666672

667673
function mouseupHandler(ev) {
668674
dispatch("consoleLog", ev.type);
675+
676+
if (dragging && hasMoved) {
677+
dispatch("dragend");
678+
}
679+
669680
$$invalidate(4, dragging = false);
670681

671-
// start editing only if element was already focussed on mousedown and not much dragging was done
672-
if (wasActiveOnClick && hasMoved < 2) {
682+
// start editing only if element was already focussed on mousedown and no dragging was done
683+
if (wasActiveOnClick && !hasMoved) {
673684
startEditing();
674685
}
675686
}

dist/index.mjs

+16-5
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ function instance($$self, $$props, $$invalidate) {
624624
wasActiveOnClick = document.activeElement === dragElement;
625625
$$invalidate(4, dragging = true);
626626
dragElement.focus();
627-
hasMoved = 0;
627+
hasMoved = false;
628628
clickX = isTouchDevice ? ev.touches[0].clientX : ev.clientX;
629629
clickY = isTouchDevice ? ev.touches[0].clientY : ev.clientY;
630630
$$invalidate(4, dragging = true);
@@ -647,11 +647,17 @@ function instance($$self, $$props, $$invalidate) {
647647
let distX = horizontal ? actX - clickX : 0;
648648
let distY = vertical ? -(actY - clickY) : 0;
649649
let stepNum = Math.abs(distX) > Math.abs(distY) ? distX : distY;
650+
651+
// fire dragstart before value changes
652+
if (stepNum != 0 && !hasMoved) {
653+
hasMoved = true;
654+
dispatch("dragstart");
655+
}
656+
650657
stepValue(stepNum * stepFactor);
651658
clickX = actX;
652659
clickY = actY;
653-
hasMoved++;
654-
}
660+
} // hasMoved++;
655661

656662
function touchendHandler(ev) {
657663
dispatch("consoleLog", ev.type);
@@ -660,10 +666,15 @@ function instance($$self, $$props, $$invalidate) {
660666

661667
function mouseupHandler(ev) {
662668
dispatch("consoleLog", ev.type);
669+
670+
if (dragging && hasMoved) {
671+
dispatch("dragend");
672+
}
673+
663674
$$invalidate(4, dragging = false);
664675

665-
// start editing only if element was already focussed on mousedown and not much dragging was done
666-
if (wasActiveOnClick && hasMoved < 2) {
676+
// start editing only if element was already focussed on mousedown and no dragging was done
677+
if (wasActiveOnClick && !hasMoved) {
667678
startEditing();
668679
}
669680
}

example/public/build/bundle.css

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)