Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(front): Add Threashold to onMouseMove to supress small movements #467

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions packages/front/src/fragments/Highlighter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ export class Highlighter extends OBC.Component implements OBC.Disposable {
/** Styles with auto toggle will be unselected when selected twice. */
autoToggle = new Set<string>();

/** Position of the mouse on mouseDown. */
private mouseDownPosition = {x: 0, y: 0};

/** Threshhold on how much the mouse have to move until its considered movement */
mouseMoveThreshold = 5

/** If defined, only the specified elements will be selected by the specified style. */
selectable: { [name: string]: FragmentIdMap } = {};

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

private onMouseDown = () => {
private onMouseDown = (e: MouseEvent) => {
if (!this.enabled) return;
this.mouseDownPosition = {x: e.clientX, y: e.clientY}
this._mouseState.down = true;

};

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

private onMouseMove = async () => {
private onMouseMove = async (e: MouseEvent) => {
if (!this.enabled) return;

// Calculate the distance the mouse has moved since mouse down
const dx = e.clientX - this.mouseDownPosition.x;
const dy = e.clientY - this.mouseDownPosition.y;
const moveDistance = Math.sqrt(dx * dx + dy * dy);

const { hoverName, hoverEnabled } = this.config;
if (this._mouseState.moved) {
this.clear(hoverName);
return;
}

this._mouseState.moved = this._mouseState.down;
const excluded: FRAGS.FragmentIdMap = {};
for (const name in this.selection) {
if (name === hoverName) continue;
const fragmentIdMap = this.selection[name];
for (const fragmentID in fragmentIdMap) {
if (!(fragmentID in excluded)) excluded[fragmentID] = new Set();
const expressIDs = fragmentIdMap[fragmentID];
for (const expressID of expressIDs) {
excluded[fragmentID].add(expressID);

// If the distance is greater than the threshold, set dragging to true
if (moveDistance > this.mouseMoveThreshold) {
this._mouseState.moved = this._mouseState.down;
const excluded: FRAGS.FragmentIdMap = {};
for (const name in this.selection) {
if (name === hoverName) continue;
const fragmentIdMap = this.selection[name];
for (const fragmentID in fragmentIdMap) {
if (!(fragmentID in excluded)) excluded[fragmentID] = new Set();
const expressIDs = fragmentIdMap[fragmentID];
for (const expressID of expressIDs) {
excluded[fragmentID].add(expressID);
}
}
}
}
if (hoverEnabled) {
await this.highlight(this.config.hoverName, true, false, excluded);
if (hoverEnabled) {
await this.highlight(this.config.hoverName, true, false, excluded);
}
}
};
}