Skip to content

Commit

Permalink
restores sort functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sashamilenkovic committed Jan 22, 2024
1 parent d89e3f5 commit 56a3570
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
23 changes: 12 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ function handleDragover(eventData: NodeEventData) {
function dragover(eventData: NodeEventData) {
eventData.e.preventDefault();

if (eventData.targetData.parent.el === State.lastParent?.el) sort(eventData);
if (eventData.targetData.parent.el === dragState.lastParent?.el)
sort(eventData);
}

function handleTouchmove(eventData: NodeEventData) {
Expand All @@ -481,7 +482,7 @@ function touchmove(eventData: NodeEventData) {

const config = eventData.targetData.parent.data.config;

const state = State;
const state = dragState;

if (!state.touchedNode) return;

Expand Down Expand Up @@ -625,13 +626,13 @@ export function sort(data: NodeEventData) {
? [data.e.touches[0].clientX, data.e.touches[0].clientY]
: [data.e.clientX, data.e.clientY];

if (State.lastValue === data.targetData.node.data.value) {
if (State.direction !== 1) return;
if (dragState.lastValue === data.targetData.node.data.value) {
if (dragState.direction !== 1) return;

if (
clientY >= State.lastCoordinates.y - 20 &&
clientY >= dragState.lastCoordinates.y - 20 &&
clientX >=
State.lastCoordinates.x -
dragState.lastCoordinates.x -
data.targetData.node.el.getBoundingClientRect().width / 20
)
return;
Expand All @@ -641,7 +642,7 @@ export function sort(data: NodeEventData) {
data.targetData.parent.el
);

const draggedNodeValues = State.draggedNodes.map(
const draggedNodeValues = dragState.draggedNodes.map(
(x: NodeRecord) => x.data.value
);

Expand All @@ -660,12 +661,12 @@ export function sort(data: NodeEventData) {
newParentValues
);

State.lastValue = data.targetData.node.data.value;
dragState.lastValue = data.targetData.node.data.value;

State.lastCoordinates = { x: clientX, y: clientY };
dragState.lastCoordinates = { x: clientX, y: clientY };

State.direction =
data.targetData.node.data.index > State.draggedNodes[0]?.data.index
dragState.direction =
data.targetData.node.data.index > dragState.draggedNodes[0]?.data.index
? 1
: -1;
}
4 changes: 2 additions & 2 deletions src/plugins/dropZones/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import type {
TearDownNodeData,
} from "../../types";

import type { DropZoneConfig, DropZoneEventData } from "./types";
import type { DropZoneConfig, DropZoneEventData, DropZoneData } from "./types";

import { parents, nodes, dragState } from "../../index";

Check failure on line 9 in src/plugins/dropZones/index.ts

View workflow job for this annotation

GitHub Actions / lint

'nodes' is defined but never used

import { addDropZoneEvents } from "./utils";

export const dropZones = new WeakMap<HTMLElement, DropZoneConfig>();
export const dropZones = new WeakMap<HTMLElement, DropZoneData>();

export function dropZone(dzConfig: Partial<DropZoneConfig>) {
return (parent: HTMLElement) => {
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/dropZones/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ export interface DropZoneConfig {
accepts?: (any: any) => boolean;
disabled?: boolean;
}

export interface DropZoneData {
config: DropZoneConfig;
parent: HTMLElement;
abortControllers: Record<string, AbortController>;
}
2 changes: 0 additions & 2 deletions src/plugins/dropZones/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export function addDropZoneEvents(
): void {
const dropZoneData = dropZones.get(dropZone);

console.log("dropZoneData", dropZoneData);

if (!dropZoneData) return;

const abortController = new AbortController();
Expand Down
15 changes: 5 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ export interface NodeEventData {
// targetData: ParentTargetData;
//}

export interface NodeTargetData extends DropZoneTargetData {
node: NodeRecord;
}

export interface DropZone {
dropZone: HTMLElement;
config: DropZoneConfig;
Expand Down Expand Up @@ -521,12 +517,6 @@ export interface TouchOverNodeEvent extends Event {

export type NodeEventToHandler = Array<[keyof HTMLElementEventMap, NodeEvent]>;

export interface DropZoneData {
config: DropZoneConfig;
parent: HTMLElement;
abortControllers: Record<string, AbortController>;
}

export interface DNDData {
nodes: WeakMap<Node, NodeData>;
parents: WeakMap<HTMLElement, ParentData>;
Expand All @@ -539,3 +529,8 @@ export type NodesData = WeakMap<Node, NodeData>;
export type ParentsData = WeakMap<HTMLElement, ParentData>;

export type ParentObservers = WeakMap<HTMLElement, MutationObserver>;

export interface NodeTargetData {
node: NodeRecord;
parent: ParentRecord;
}
12 changes: 6 additions & 6 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@ export function addClass(
if (classNames.includes("longTouch")) return;

for (const node of nodes) {
if (!isNode(node, state) || !state.nodeData.has(node)) {
if (!isNode(node, state) || !nodes.has(node)) {
node.classList.add(...classNames);

continue;
}

const privateClasses = [];

const nodeData = state.nodeData.get(node);
const nodeData = nodes.get(node);

if (!nodeData) continue;

Expand All @@ -274,7 +274,7 @@ export function addClass(

nodeData.privateClasses = privateClasses;

state.nodeData.set(node, nodeData);
nodes.set(node, nodeData);
}
}

Expand Down Expand Up @@ -445,13 +445,13 @@ export type EventListener = {
};

export function nodeTargetData(node: Node): NodeTargetData | undefined {
const nodeData = Data.nodes.get(node);
const nodeData = nodes.get(node);

const parent = node.parentNode || State.lastParent?.el;
const parent = node.parentNode || dragState.lastParent?.el;

if (!nodeData) return;

const parentData = Data.parents.get(parent);
const parentData = parents.get(parent);

if (!parentData) return;

Expand Down

0 comments on commit 56a3570

Please sign in to comment.