Skip to content

Commit

Permalink
refactor: more code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sashamilenkovic committed Dec 15, 2023
1 parent 57663f2 commit aa585e9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 68 deletions.
30 changes: 15 additions & 15 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ export function nodeDragEvent(e: DragEvent): void {
? draggedConfig.dragstart(eventData, state, dragstart)
: dragstart(eventData, state);
break;

case "dragover":
eventData.event.preventDefault();

Expand All @@ -346,6 +347,7 @@ export function nodeDragEvent(e: DragEvent): void {
: transferReturn(eventData, state);
}
break;

case "dragend":
draggedConfig?.end
? draggedConfig.end(eventData, state, end)
Expand Down Expand Up @@ -412,6 +414,7 @@ function nodeTouchEvent(e: TouchEvent): void {
? config.touchstart(touchEvent, state, touchstart)
: touchstart(touchEvent, state);
break;

case "touchmove":
config?.touchmove
? config.touchmove(touchEvent, state, touchmove)
Expand Down Expand Up @@ -485,6 +488,7 @@ function dzDragEvent(e: DragEvent): void {

state.leftParent = false;
}

break;
case "dragleave":
event.event.preventDefault();
Expand Down Expand Up @@ -515,6 +519,7 @@ function dzDragEvent(e: DragEvent): void {
// state.leftParent = false;
//}
}

break;
case "drop":
if (event.targetParent == event.draggedParent) {
Expand All @@ -538,23 +543,18 @@ function handleSort(
"touchedNode" in event
? event.event.touches[0].clientY
: event.event.clientY;
if (state.lastValue === event.targetNodeData.value) {
if (state.direction !== 1) {
return;
}

if (
clientY >= state.lastCoordinates.y - 20 &&
clientX >=
state.lastCoordinates.x -
event.targetNode.getBoundingClientRect().width / 20
) {
return;
}
}
if (!validateSort(event)) {
if (
state.lastValue === event.targetNodeData.value &&
(state.direction !== 1 ||
(clientY >= state.lastCoordinates.y - 20 &&
clientX >=
state.lastCoordinates.x -
event.targetNode.getBoundingClientRect().width / 20))
)
return;
}

if (!validateSort(event)) return;

state.lastValue = event.targetNodeData.value;

Expand Down
2 changes: 1 addition & 1 deletion src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { DNDState, Node, ParentData, NodeData } from "./types";

export const state: DNDState = {
activeNode: undefined,
enterCount: 0,
clonedDraggedNodes: Array<HTMLElement>(),
direction: undefined,
draggedNode: undefined,
draggedNodes: Array<Node>(),
dropZones: new WeakMap<HTMLElement, HTMLElement>(),
dropped: false,
enterCount: 0,
hiddenNodes: new WeakMap<HTMLElement, Array<Node>>(),
initialParent: undefined,
initialParentValues: [],
Expand Down
52 changes: 27 additions & 25 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,25 @@ export function cleanUp(
);

state.touchedNode?.remove();
state.touchedNode = undefined;
state.lastParent = undefined;
state.draggedNode = undefined;
state.draggedNodes = [];
state.selectedNodes = [];
state.selectedValues = [];
state.touchStartTop = state.touchedNode = undefined;
state.touchStartLeft = 0;
state.lastValue = undefined;

state.touchedNode =
state.lastParent =
state.draggedNode =
state.touchStartTop =
state.touchStartLeft =
state.lastValue =
state.direction =
undefined;

state.draggedNodes = state.selectedNodes = state.selectedValues = [];

state.touchMoving = false;

state.lastCoordinates = {
x: 0,
y: 0,
};
state.direction = undefined;

if (state.scrollParent) {
state.scrollParent.style.overflow = state.scrollParentOverflow || "";
state.scrollParent = undefined;
Expand All @@ -102,15 +106,15 @@ export function dragTransfer(): DragTransfer | undefined {
const lastParentData = state.parentData.get(state.lastParent);

if (!draggedNodeData || !draggedParentData || !lastParentData) return;
else
return {
draggedNode: state.draggedNode,
draggedNodeData,
draggedParent: state.initialParent,
draggedParentData,
lastParent: state.lastParent,
lastParentData,
};

return {
draggedNode: state.draggedNode,
draggedNodeData,
draggedParent: state.initialParent,
draggedParentData,
lastParent: state.lastParent,
lastParentData,
};
}

export function touchTransfer(): TouchTransfer | undefined {
Expand Down Expand Up @@ -422,15 +426,13 @@ export function splitClass(className: string): Array<string> {
export function getScrollParent(
node: HTMLElement | null
): HTMLElement | undefined {
if (node == null) {
return undefined;
}
if (node == null) return undefined;

if (node.scrollHeight > node.clientHeight) {
return node;
} else {
if (node.parentNode instanceof HTMLElement)
return getScrollParent(node.parentNode);
} else if (node.parentNode instanceof HTMLElement) {
return getScrollParent(node.parentNode);
}

return undefined;
}
19 changes: 8 additions & 11 deletions src/vue/dropZone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isBrowser } from "../utils";
import type { Ref } from "vue";
import { watch } from "vue";
import type { TransferEvent, DropEvent } from "../types";
import { getEl } from "./utils";

interface DropZoneConfigVue {
group?: string;
Expand Down Expand Up @@ -34,10 +35,13 @@ export function dropZone(dzConfig: DropZoneConfigVue = {}) {
return {
setUp(parent: HTMLElement, config: any) {
const enabledDZs = [];

if (dzConfig.dropZones && isBrowser) {
const dropZones = dzConfig.dropZones;

for (const dz of dropZones) {
const el = getEl(dz.element);

if (el instanceof HTMLElement) enabledDZs.push(el);
else {
const stop = watch(dz.element, (newEl) => {
Expand All @@ -49,24 +53,17 @@ export function dropZone(dzConfig: DropZoneConfigVue = {}) {
});
}
}

delete dzConfig.dropZones;
}

dzConfig.validDropZones = enabledDZs;

return setUpDropZone(dzConfig, parent, config);
},

tearDown(parent: HTMLElement) {
return tearDownDropZone(parent);
},
};
}

function getEl(el: HTMLElement | Ref<HTMLElement | null>) {
if (el instanceof HTMLElement) {
return el;
} else if (el.value instanceof HTMLElement) {
return el.value;
} else if ("$el" in el && el.$el instanceof HTMLElement) {
return el.$el;
}
return false;
}
19 changes: 3 additions & 16 deletions src/vue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ref, watch } from "vue";
import initParent from "../index";
import { isBrowser } from "../utils";
import type { VueDND, VueDNDCollection } from "./types";
import { getEl } from "./utils";

/**
* Global store for parent values.
Expand Down Expand Up @@ -57,14 +58,14 @@ export function dragAndDrop(data: VueDND | VueDNDCollection): void {
if (!Array.isArray(data)) data = [data];

data.forEach((dnd) => {
const validParent = isEl(dnd.parent);
const validParent = getEl(dnd.parent);

if (validParent) initialize(validParent, dnd);
else {
const stop = watch(dnd.parent, (newParent) => {
if (!newParent) return;

const validParent = isEl(newParent);
const validParent = getEl(newParent);

if (!validParent) console.warn("Invalid parent element", newParent);
else initialize(validParent, dnd);
Expand Down Expand Up @@ -93,17 +94,3 @@ function initialize(parent: HTMLElement, dnd: VueDND): void {
config: dnd.config,
});
}

/**
* Checks if the given element is an HTMLElement.
*
* @param dnd - The drag and drop configuration.
*/
export function isEl(
parent: HTMLElement | Ref<HTMLElement | null>
): HTMLElement | undefined {
if (parent instanceof HTMLElement) return parent;
else if (parent.value instanceof HTMLElement) return parent.value;
else if ("$el" in parent && parent.$el instanceof HTMLElement)
return parent.$el;
}
15 changes: 15 additions & 0 deletions src/vue/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Ref } from "vue";

/**
* Checks if the given element is an HTMLElement.
*
* @param dnd - The drag and drop configuration.
*/
export function getEl(
parent: HTMLElement | Ref<HTMLElement | null>
): HTMLElement | undefined {
if (parent instanceof HTMLElement) return parent;
else if (parent.value instanceof HTMLElement) return parent.value;
else if ("$el" in parent && parent.$el instanceof HTMLElement)
return parent.$el;
}

0 comments on commit aa585e9

Please sign in to comment.