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

Fix drag events not moving indicator when staying in the same node #222

Open
wants to merge 3 commits into
base: v0.1.x
Choose a base branch
from
Open
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
62 changes: 61 additions & 1 deletion packages/core/src/events/DefaultEventHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isFunction } from 'lodash';
import { isFunction, throttle } from 'lodash';

import { CoreEventHandlers, CreateHandlerOptions } from './CoreEventHandlers';
import { createShadow } from './createShadow';
Expand All @@ -14,6 +14,48 @@ export class DefaultEventHandlers extends CoreEventHandlers {
static draggedElementShadow: HTMLElement;
static draggedElement: DraggedElement;
static indicator: Indicator = null;
static lastDragPosition: { x: number; y: number } = null;

computeDragIndicator(x: number, y: number, targetId: NodeId) {
const draggedElement = DefaultEventHandlers.draggedElement;
if (!draggedElement) {
return;
}

if (targetId) {
const node = this.options.store.query.node(targetId).get();
if (!node) {
return;
}
}

let node = (draggedElement as unknown) as Node;

if ((draggedElement as NodeTree).rootNodeId) {
const nodeTree = draggedElement as NodeTree;
node = nodeTree.nodes[nodeTree.rootNodeId];
}

const indicator = this.options.store.query.getDropPlaceholder(
node,
targetId,
{
x,
y,
}
);

if (!indicator) {
return;
}
this.options.store.actions.setIndicator(indicator);
DefaultEventHandlers.indicator = indicator;
}

throttledDragOver = throttle(this.computeDragIndicator, 200, {
leading: false,
});

currentSelectedElementIds = [];

handlers() {
Expand Down Expand Up @@ -67,6 +109,23 @@ export class DefaultEventHandlers extends CoreEventHandlers {
(e) => {
e.craft.stopPropagation();
e.preventDefault();

const { clientX: x, clientY: y } = e;
if (
DefaultEventHandlers.lastDragPosition &&
DefaultEventHandlers.lastDragPosition.x === x &&
DefaultEventHandlers.lastDragPosition.y === y
) {
return;
}
DefaultEventHandlers.lastDragPosition = { x, y };

const draggedElement = DefaultEventHandlers.draggedElement;
if (!draggedElement) {
return;
}

this.throttledDragOver(x, y, targetId);
}
);

Expand Down Expand Up @@ -94,6 +153,7 @@ export class DefaultEventHandlers extends CoreEventHandlers {
x,
y,
});
DefaultEventHandlers.lastDragPosition = { x, y };

if (!indicator) {
return;
Expand Down