Skip to content

Commit

Permalink
fixes typing issue from performTransfer
Browse files Browse the repository at this point in the history
  • Loading branch information
sashamilenkovic committed Dec 11, 2024
1 parent c954bf7 commit d24dbad
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 71 deletions.
46 changes: 17 additions & 29 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,7 @@ function moveBetween(data, state2) {
function moveOutside(data, state2) {
if (data.el === state2.currentParent.el) return false;
const targetConfig = data.data.config;
if (targetConfig.treeGroup && state2.draggedNode.el.contains(data.el))
return false;
if (state2.draggedNode.el.contains(data.el)) return false;
if (targetConfig.dropZone === false) return;
const initialParentConfig = state2.initialParent.data.config;
if (targetConfig.accepts) {
Expand Down Expand Up @@ -1336,7 +1335,6 @@ function dragAndDrop({
const nodesObserver = new MutationObserver(nodesMutated);
nodesObserver.observe(parent, { childList: true });
parents.set(parent, parentData);
if (config.treeGroup) treeAncestors[config.treeGroup] = parent;
config.plugins?.forEach((plugin) => {
plugin(parent)?.tearDown?.();
});
Expand Down Expand Up @@ -1514,25 +1512,32 @@ function performTransfer({
initialParent,
draggedNodes: draggedNodes2,
initialIndex,
targetNode,
targetNodes,
state: state2
}) {
console.log("perform transfer target node", targetNodes);
remapNodes(initialParent.el);
const draggedValues = draggedNodes2.map((x) => x.data.value);
console.log("currentParent el", currentParent.el);
console.log("targetParent el", targetParent.el);
console.log("initialParent el", initialParent.el);
const currentParentValues = parentValues(
currentParent.el,
currentParent.data
).filter((x) => !draggedValues.includes(x));
const targetParentValues = parentValues(targetParent.el, targetParent.data);
console.log("currentParentValues", currentParentValues);
console.log("targetParentValues", targetParentValues);
const reset = initialParent.el === targetParent.el && targetParent.data.config.sortable === false;
let targetIndex;
if (targetNode) {
if (targetNodes.length) {
console.log("targetNodes", targetNodes);
if (reset) {
targetIndex = initialIndex;
} else if (targetParent.data.config.sortable === false) {
targetIndex = targetParent.data.enabledNodes.length;
} else {
targetIndex = targetNode.data.index;
targetIndex = targetNodes[0].data.index;
}
targetParentValues.splice(targetIndex, 0, ...draggedValues);
} else {
Expand All @@ -1549,7 +1554,7 @@ function performTransfer({
draggedNodes: draggedNodes2,
targetIndex,
state: state2,
targetNodes: targetNode ? [targetNode] : []
targetNodes
});
}
if (currentParent.data.config.onTransfer) {
Expand All @@ -1560,7 +1565,7 @@ function performTransfer({
draggedNodes: draggedNodes2,
targetIndex,
state: state2,
targetNodes: targetNode ? [targetNode] : []
targetNodes: targetNodes ? targetNodes : []
});
}
}
Expand Down Expand Up @@ -1794,23 +1799,6 @@ function remapNodes(parent, force) {
);
return;
}
if (parentData.config.treeGroup) {
let nextAncestorEl = parent.parentElement;
while (nextAncestorEl) {
if (!parents.has(nextAncestorEl)) {
nextAncestorEl = nextAncestorEl.parentElement;
continue;
}
nextAncestorEl.dispatchEvent(
new CustomEvent("hasNestedParent", {
detail: {
parent: { data: parentData, el: parent }
}
})
);
nextAncestorEl = null;
}
}
const values = parentData.getValues(parent);
const enabledNodeRecords = [];
for (let x = 0; x < enabledNodes.length; x++) {
Expand Down Expand Up @@ -2519,8 +2507,7 @@ function validateTransfer({
}) {
if (targetParent.el === currentParent.el) return false;
const targetConfig = targetParent.data.config;
if (targetConfig.treeGroup && draggedNodes2[0].el.contains(targetParent.el))
return false;
if (draggedNodes2[0].el.contains(targetParent.el)) return false;
if (targetConfig.dropZone === false) return false;
const initialParentConfig = initialParent.data.config;
if (targetConfig.accepts) {
Expand Down Expand Up @@ -2561,8 +2548,7 @@ function validateSort(data, state2, x, y) {
return false;
if (data.targetData.parent.el !== state2.currentParent?.el || data.targetData.parent.data.config.sortable === false)
return false;
if (data.targetData.parent.data.config.treeGroup && data.targetData.node.el.contains(state2.draggedNodes[0].el))
return false;
if (data.targetData.node.el.contains(state2.draggedNodes[0].el)) return false;
const targetRect = data.targetData.node.el.getBoundingClientRect();
const dragRect = state2.draggedNode.el.getBoundingClientRect();
const yDiff = targetRect.y - dragRect.y;
Expand Down Expand Up @@ -2658,6 +2644,8 @@ function transfer(data, state2) {
state: state2
}))
return;
console.log("data", data);
console.trace();
data.targetData.parent.data.config.performTransfer({
currentParent: state2.currentParent,
targetParent: data.targetData.parent,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.cjs.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ interface ParentConfig<T> {
draggedNodes: Array<NodeRecord<T>>;
initialIndex: number;
state: BaseDragState<T> | DragState<T> | SynthDragState<T>;
targetNodes?: Array<NodeRecord<T>>;
targetNodes: Array<NodeRecord<T>>;
}) => void;
/**
* An array of functions to use for a given parent.
Expand Down Expand Up @@ -968,14 +968,14 @@ declare function performSort<T>({ parent, draggedNodes, targetNodes, }: {
targetNodes: Array<NodeRecord<T>>;
}): void;
declare function handleParentFocus<T>(data: ParentEventData<T>, state: BaseDragState<T> | DragState<T> | SynthDragState<T>): void;
declare function performTransfer<T>({ currentParent, targetParent, initialParent, draggedNodes, initialIndex, targetNode, state, }: {
declare function performTransfer<T>({ currentParent, targetParent, initialParent, draggedNodes, initialIndex, targetNodes, state, }: {
currentParent: ParentRecord<T>;
targetParent: ParentRecord<T>;
initialParent: ParentRecord<T>;
draggedNodes: Array<NodeRecord<T>>;
initialIndex: number;
state: BaseDragState<T> | DragState<T> | SynthDragState<T>;
targetNode?: NodeRecord<T>;
targetNodes: Array<NodeRecord<T>>;
}): void;
declare function parentValues<T>(parent: HTMLElement, parentData: ParentData<T>): Array<T>;
declare function setParentValues<T>(parent: HTMLElement, parentData: ParentData<T>, values: Array<any>): void;
Expand Down
6 changes: 3 additions & 3 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ interface ParentConfig<T> {
draggedNodes: Array<NodeRecord<T>>;
initialIndex: number;
state: BaseDragState<T> | DragState<T> | SynthDragState<T>;
targetNodes?: Array<NodeRecord<T>>;
targetNodes: Array<NodeRecord<T>>;
}) => void;
/**
* An array of functions to use for a given parent.
Expand Down Expand Up @@ -968,14 +968,14 @@ declare function performSort<T>({ parent, draggedNodes, targetNodes, }: {
targetNodes: Array<NodeRecord<T>>;
}): void;
declare function handleParentFocus<T>(data: ParentEventData<T>, state: BaseDragState<T> | DragState<T> | SynthDragState<T>): void;
declare function performTransfer<T>({ currentParent, targetParent, initialParent, draggedNodes, initialIndex, targetNode, state, }: {
declare function performTransfer<T>({ currentParent, targetParent, initialParent, draggedNodes, initialIndex, targetNodes, state, }: {
currentParent: ParentRecord<T>;
targetParent: ParentRecord<T>;
initialParent: ParentRecord<T>;
draggedNodes: Array<NodeRecord<T>>;
initialIndex: number;
state: BaseDragState<T> | DragState<T> | SynthDragState<T>;
targetNode?: NodeRecord<T>;
targetNodes: Array<NodeRecord<T>>;
}): void;
declare function parentValues<T>(parent: HTMLElement, parentData: ParentData<T>): Array<T>;
declare function setParentValues<T>(parent: HTMLElement, parentData: ParentData<T>, values: Array<any>): void;
Expand Down
46 changes: 17 additions & 29 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,7 @@ function moveBetween(data, state2) {
function moveOutside(data, state2) {
if (data.el === state2.currentParent.el) return false;
const targetConfig = data.data.config;
if (targetConfig.treeGroup && state2.draggedNode.el.contains(data.el))
return false;
if (state2.draggedNode.el.contains(data.el)) return false;
if (targetConfig.dropZone === false) return;
const initialParentConfig = state2.initialParent.data.config;
if (targetConfig.accepts) {
Expand Down Expand Up @@ -1236,7 +1235,6 @@ function dragAndDrop({
const nodesObserver = new MutationObserver(nodesMutated);
nodesObserver.observe(parent, { childList: true });
parents.set(parent, parentData);
if (config.treeGroup) treeAncestors[config.treeGroup] = parent;
config.plugins?.forEach((plugin) => {
plugin(parent)?.tearDown?.();
});
Expand Down Expand Up @@ -1414,25 +1412,32 @@ function performTransfer({
initialParent,
draggedNodes: draggedNodes2,
initialIndex,
targetNode,
targetNodes,
state: state2
}) {
console.log("perform transfer target node", targetNodes);
remapNodes(initialParent.el);
const draggedValues = draggedNodes2.map((x) => x.data.value);
console.log("currentParent el", currentParent.el);
console.log("targetParent el", targetParent.el);
console.log("initialParent el", initialParent.el);
const currentParentValues = parentValues(
currentParent.el,
currentParent.data
).filter((x) => !draggedValues.includes(x));
const targetParentValues = parentValues(targetParent.el, targetParent.data);
console.log("currentParentValues", currentParentValues);
console.log("targetParentValues", targetParentValues);
const reset = initialParent.el === targetParent.el && targetParent.data.config.sortable === false;
let targetIndex;
if (targetNode) {
if (targetNodes.length) {
console.log("targetNodes", targetNodes);
if (reset) {
targetIndex = initialIndex;
} else if (targetParent.data.config.sortable === false) {
targetIndex = targetParent.data.enabledNodes.length;
} else {
targetIndex = targetNode.data.index;
targetIndex = targetNodes[0].data.index;
}
targetParentValues.splice(targetIndex, 0, ...draggedValues);
} else {
Expand All @@ -1449,7 +1454,7 @@ function performTransfer({
draggedNodes: draggedNodes2,
targetIndex,
state: state2,
targetNodes: targetNode ? [targetNode] : []
targetNodes
});
}
if (currentParent.data.config.onTransfer) {
Expand All @@ -1460,7 +1465,7 @@ function performTransfer({
draggedNodes: draggedNodes2,
targetIndex,
state: state2,
targetNodes: targetNode ? [targetNode] : []
targetNodes: targetNodes ? targetNodes : []
});
}
}
Expand Down Expand Up @@ -1694,23 +1699,6 @@ function remapNodes(parent, force) {
);
return;
}
if (parentData.config.treeGroup) {
let nextAncestorEl = parent.parentElement;
while (nextAncestorEl) {
if (!parents.has(nextAncestorEl)) {
nextAncestorEl = nextAncestorEl.parentElement;
continue;
}
nextAncestorEl.dispatchEvent(
new CustomEvent("hasNestedParent", {
detail: {
parent: { data: parentData, el: parent }
}
})
);
nextAncestorEl = null;
}
}
const values = parentData.getValues(parent);
const enabledNodeRecords = [];
for (let x = 0; x < enabledNodes.length; x++) {
Expand Down Expand Up @@ -2419,8 +2407,7 @@ function validateTransfer({
}) {
if (targetParent.el === currentParent.el) return false;
const targetConfig = targetParent.data.config;
if (targetConfig.treeGroup && draggedNodes2[0].el.contains(targetParent.el))
return false;
if (draggedNodes2[0].el.contains(targetParent.el)) return false;
if (targetConfig.dropZone === false) return false;
const initialParentConfig = initialParent.data.config;
if (targetConfig.accepts) {
Expand Down Expand Up @@ -2461,8 +2448,7 @@ function validateSort(data, state2, x, y) {
return false;
if (data.targetData.parent.el !== state2.currentParent?.el || data.targetData.parent.data.config.sortable === false)
return false;
if (data.targetData.parent.data.config.treeGroup && data.targetData.node.el.contains(state2.draggedNodes[0].el))
return false;
if (data.targetData.node.el.contains(state2.draggedNodes[0].el)) return false;
const targetRect = data.targetData.node.el.getBoundingClientRect();
const dragRect = state2.draggedNode.el.getBoundingClientRect();
const yDiff = targetRect.y - dragRect.y;
Expand Down Expand Up @@ -2558,6 +2544,8 @@ function transfer(data, state2) {
state: state2
}))
return;
console.log("data", data);
console.trace();
data.targetData.parent.data.config.performTransfer({
currentParent: state2.currentParent,
targetParent: data.targetData.parent,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.mjs.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/react/index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ interface ParentConfig<T> {
draggedNodes: Array<NodeRecord<T>>;
initialIndex: number;
state: BaseDragState<T> | DragState<T> | SynthDragState<T>;
targetNodes?: Array<NodeRecord<T>>;
targetNodes: Array<NodeRecord<T>>;
}) => void;
/**
* An array of functions to use for a given parent.
Expand Down
2 changes: 1 addition & 1 deletion dist/react/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ interface ParentConfig<T> {
draggedNodes: Array<NodeRecord<T>>;
initialIndex: number;
state: BaseDragState<T> | DragState<T> | SynthDragState<T>;
targetNodes?: Array<NodeRecord<T>>;
targetNodes: Array<NodeRecord<T>>;
}) => void;
/**
* An array of functions to use for a given parent.
Expand Down
2 changes: 1 addition & 1 deletion dist/vue/index.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ interface ParentConfig<T> {
draggedNodes: Array<NodeRecord<T>>;
initialIndex: number;
state: BaseDragState<T> | DragState<T> | SynthDragState<T>;
targetNodes?: Array<NodeRecord<T>>;
targetNodes: Array<NodeRecord<T>>;
}) => void;
/**
* An array of functions to use for a given parent.
Expand Down
2 changes: 1 addition & 1 deletion dist/vue/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ interface ParentConfig<T> {
draggedNodes: Array<NodeRecord<T>>;
initialIndex: number;
state: BaseDragState<T> | DragState<T> | SynthDragState<T>;
targetNodes?: Array<NodeRecord<T>>;
targetNodes: Array<NodeRecord<T>>;
}) => void;
/**
* An array of functions to use for a given parent.
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export interface ParentConfig<T> {
draggedNodes: Array<NodeRecord<T>>;
initialIndex: number;
state: BaseDragState<T> | DragState<T> | SynthDragState<T>;
targetNodes?: Array<NodeRecord<T>>;
targetNodes: Array<NodeRecord<T>>;
}) => void;
/**
* An array of functions to use for a given parent.
Expand Down

0 comments on commit d24dbad

Please sign in to comment.