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 2 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
66 changes: 62 additions & 4 deletions packages/core/src/events/DefaultEventHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import throttle from 'lodash/throttle';

import { CoreEventHandlers } from './CoreEventHandlers';
import { createShadow } from './createShadow';

Expand All @@ -15,6 +17,41 @@ export class DefaultEventHandlers extends CoreEventHandlers {
static draggedElementShadow: HTMLElement;
static draggedElement: DraggedElement;
static indicator: Indicator = null;
static lastDragPosition: { x: number; y: number } = null;

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

if (targetId) {
const node = this.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.store.query.getDropPlaceholder(node, targetId, {
x,
y,
});

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

@prevwong prevwong Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part seems to be the same as the dragenter handler used in the drop connector.

Can we rename this function to something like computeIndicator, so we can use for both the dragover and dragenter handler?

On that note - perhaps we don't even need the dragenter handler anymore, since this updated dragover handler is doing the same thing?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any update on this?


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

// Safely run handler if Node Id exists
defineNodeEventListener(
Expand Down Expand Up @@ -74,10 +111,30 @@ export class DefaultEventHandlers extends CoreEventHandlers {
},
drop: {
events: [
defineEventListener('dragover', (e: CraftDOMEvent<MouseEvent>) => {
e.craft.stopPropagation();
e.preventDefault();
}),
defineEventListener(
'dragover',
(e: CraftDOMEvent<MouseEvent>, targetId: NodeId) => {
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);
}
),
this.defineNodeEventListener(
'dragenter',
(e: CraftDOMEvent<MouseEvent>, targetId: NodeId) => {
Expand All @@ -102,6 +159,7 @@ export class DefaultEventHandlers extends CoreEventHandlers {
targetId,
{ x, y }
);
DefaultEventHandlers.lastDragPosition = { x, y };

if (!indicator) {
return;
Expand Down