Skip to content

Commit b5c8365

Browse files
feat(front): Add Threashold to onMouseMove to supress small movements (#467)
In order to enable drag the movement has to be bigger than a customizable threshold so small movements will still trigger the select highlight! Co-authored-by: Antonio González Viegas <antoniogviegas@hotmail.com>
1 parent 3bfdcbf commit b5c8365

File tree

1 file changed

+33
-16
lines changed
  • packages/front/src/fragments/Highlighter

1 file changed

+33
-16
lines changed

packages/front/src/fragments/Highlighter/index.ts

+33-16
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ export class Highlighter extends OBC.Component implements OBC.Disposable {
107107
/** Styles with auto toggle will be unselected when selected twice. */
108108
autoToggle = new Set<string>();
109109

110+
/** Position of the mouse on mouseDown. */
111+
private mouseDownPosition = {x: 0, y: 0};
112+
113+
/** Threshhold on how much the mouse have to move until its considered movement */
114+
mouseMoveThreshold = 5
115+
110116
/** If defined, only the specified elements will be selected by the specified style. */
111117
selectable: { [name: string]: FragmentIdMap } = {};
112118

@@ -629,9 +635,11 @@ export class Highlighter extends OBC.Component implements OBC.Disposable {
629635
this.selection[this.config.hoverName] = {};
630636
};
631637

632-
private onMouseDown = () => {
638+
private onMouseDown = (e: MouseEvent) => {
633639
if (!this.enabled) return;
640+
this.mouseDownPosition = {x: e.clientX, y: e.clientY}
634641
this._mouseState.down = true;
642+
635643
};
636644

637645
private onMouseUp = async (event: MouseEvent) => {
@@ -656,29 +664,38 @@ export class Highlighter extends OBC.Component implements OBC.Disposable {
656664
}
657665
};
658666

659-
private onMouseMove = async () => {
667+
private onMouseMove = async (e: MouseEvent) => {
660668
if (!this.enabled) return;
669+
670+
// Calculate the distance the mouse has moved since mouse down
671+
const dx = e.clientX - this.mouseDownPosition.x;
672+
const dy = e.clientY - this.mouseDownPosition.y;
673+
const moveDistance = Math.sqrt(dx * dx + dy * dy);
674+
661675
const { hoverName, hoverEnabled } = this.config;
662676
if (this._mouseState.moved) {
663677
this.clear(hoverName);
664678
return;
665679
}
666-
667-
this._mouseState.moved = this._mouseState.down;
668-
const excluded: FRAGS.FragmentIdMap = {};
669-
for (const name in this.selection) {
670-
if (name === hoverName) continue;
671-
const fragmentIdMap = this.selection[name];
672-
for (const fragmentID in fragmentIdMap) {
673-
if (!(fragmentID in excluded)) excluded[fragmentID] = new Set();
674-
const expressIDs = fragmentIdMap[fragmentID];
675-
for (const expressID of expressIDs) {
676-
excluded[fragmentID].add(expressID);
680+
681+
// If the distance is greater than the threshold, set dragging to true
682+
if (moveDistance > this.mouseMoveThreshold) {
683+
this._mouseState.moved = this._mouseState.down;
684+
const excluded: FRAGS.FragmentIdMap = {};
685+
for (const name in this.selection) {
686+
if (name === hoverName) continue;
687+
const fragmentIdMap = this.selection[name];
688+
for (const fragmentID in fragmentIdMap) {
689+
if (!(fragmentID in excluded)) excluded[fragmentID] = new Set();
690+
const expressIDs = fragmentIdMap[fragmentID];
691+
for (const expressID of expressIDs) {
692+
excluded[fragmentID].add(expressID);
693+
}
677694
}
678695
}
679-
}
680-
if (hoverEnabled) {
681-
await this.highlight(this.config.hoverName, true, false, excluded);
696+
if (hoverEnabled) {
697+
await this.highlight(this.config.hoverName, true, false, excluded);
698+
}
682699
}
683700
};
684701
}

0 commit comments

Comments
 (0)