Skip to content

Commit

Permalink
wip on returning state back to undefined or with non-optional props, …
Browse files Browse the repository at this point in the history
…on and emit will be exported for use and not part of state object
  • Loading branch information
sashamilenkovic committed Sep 6, 2024
1 parent c012d3e commit f2a0db1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 53 deletions.
57 changes: 20 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,15 @@ export const treeAncestors: Record<string, HTMLElement> = {};

let synthNodePointerDown = false;

const [emit, on] = createEmitter();
export const [emit, on] = createEmitter();

/**
* The state of the drag and drop. Is undefined until either dragstart or
* touchstart is called.
* The state of the drag and drop.
*/
export let state: Partial<DragState<any>> = {
emit,
on,
activeNode: undefined,
affectedNodes: [],
ascendingDirection: false,
clonedDraggedEls: [],
draggedNodeDisplay: undefined,
dynamicValues: [],
incomingDirection: undefined,
lastTargetValue: undefined,
lastValue: undefined,
longPress: false,
longPressTimeout: 0,
originalZIndex: undefined,
pointerMoved: false,
remapJustFinished: false,
scrollParentAbortController: undefined,
targetIndex: 0,
transferred: false,
};
export let state: DragState<any> | undefined;

export function resetState() {
state = {};
state = undefined;
}

/**
Expand All @@ -109,12 +88,13 @@ export function resetState() {
export function setDragState<T>(
dragStateProps: DragStateProps<T>
): DragState<T> {
// TODO:
state = {
...state,
...dragStateProps,
} as DragState<T>;

if (state.emit) state.emit("dragStarted", state);
state.emit("dragStarted", state);

return state as DragState<T>;
}
Expand Down Expand Up @@ -216,6 +196,8 @@ export function dragStateProps<T>(

const rect = data.targetData.node.el.getBoundingClientRect();

console.log("getScrollables", getScrollables());

return {
clonedDraggedNode: data.targetData.node.el.cloneNode(true) as Node,
preventEnter: false,
Expand All @@ -242,7 +224,7 @@ export function dragStateProps<T>(
el: data.targetData.parent.el,
data: data.targetData.parent.data,
},
scrollEls: getScrollables(data.targetData.parent.data),
scrollEls: getScrollables(),
startLeft: x - rect.left,
startTop: y - rect.top,
};
Expand Down Expand Up @@ -535,9 +517,10 @@ export function tearDown(parent: HTMLElement) {
}

function setup<T>(parent: HTMLElement, parentData: ParentData<T>): void {
state.on("dragStarted", () => {
console.log("dslkfkldsjfkld");
});
if (state)
on("dragStarted", () => {
console.log("dslkfkldsjfkld");
});
parentData.abortControllers.mainParent = addEvents(parent, {
dragover: parentEventData(parentData.config.handleDragoverParent),
handlePointeroverParent: parentData.config.handlePointeroverParent,
Expand Down Expand Up @@ -590,7 +573,7 @@ export function setupNodeRemap<T>(data: SetupNodeData<T>) {
}

function reapplyDragClasses<T>(node: Node, parentData: ParentData<T>) {
if (!state.draggedNode) return;
if (!state) return;

const dropZoneClass =
"clonedDraggedNode" in state
Expand Down Expand Up @@ -734,14 +717,14 @@ export function remapNodes<T>(parent: HTMLElement, force?: boolean) {
);

// TODO: maybe get rid of this — duplicate of the next if statement
if (state.draggedNode && nodeData.value === state.draggedNode.data.value) {
if (state && nodeData.value === state.draggedNode.data.value) {
state.draggedNode.data = nodeData;

state.draggedNode.el = node;
}

if (
state.draggedNode &&
state &&
state.draggedNodes.map((x) => x.data.value).includes(nodeData.value)
) {
const draggedNode = state.draggedNodes.find(
Expand Down Expand Up @@ -974,7 +957,7 @@ export function dragstart<T>(data: NodeDragEventData<T>) {
}

export function handlePointeroverNode<T>(e: PointeroverNodeEvent<T>) {
if (!state.draggedNode) return;
if (!state) return;

if (e.detail.targetData.parent.el === state.lastParent.el)
sort(e.detail, state);
Expand Down Expand Up @@ -1266,7 +1249,7 @@ function syntheticMove<T>(data: NodePointerEventData<T>) {

let dragState = state || undefined;

if (!state.draggedNode) {
if (!state) {
dragState = initSyntheticDrag(data);
} else {
dragState = state;
Expand Down Expand Up @@ -1325,7 +1308,7 @@ export function handleScroll() {
function performScroll(direction: string, x: number, y: number) {
const state = shouldScroll(direction);

if (!state.draggedNode) return;
if (!state) return;

state.scrollParent.scrollBy(x, y);

Expand All @@ -1338,7 +1321,7 @@ function performScroll(direction: string, x: number, y: number) {
}

export function handleDragoverNode<T>(data: NodeDragEventData<T>) {
if (!state.draggedNode) return;
if (!state) return;

const { x, y } = eventCoordinates(data.e);

Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,12 @@ export type EventHandlers = Record<string, (e: Event) => void>;
* @param targetIndex - The index of the node that the dragged node is moving
* into.
*/

export interface NonDragState {
emit: (event: string, data: unknown) => void;
on: (event: string, callback: () => void) => void;
}

export interface DragState<T> extends DragStateProps<T> {
/**
* The node that was most recently clicked (used optionally).
Expand Down
25 changes: 9 additions & 16 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,18 @@ function isScrollable(element) {
*
* @internal
*/
export function getScrollables<T>(
data: ParentData<T>,
withRoot = false
): [HTMLElement, AbortController] {
// Get all potentially scrollable elements
const scrollableElements = document.querySelectorAll("*");

// Filter out elements that are scrollable and not children of the excluded element

const nonChildScrollableElements: Array<Element | Document | ShadowRoot> =
Array.from(scrollableElements).filter((element) => {
return isScrollable(element);
});
export function getScrollables(): Array<[HTMLElement, AbortController]> {
const scrollables: Array<[HTMLElement, AbortController]> = [];

for (const el of Array.from(document.querySelectorAll("*"))) {
if (!isScrollable(el) || !(el instanceof HTMLElement)) continue;

//nonChildScrollableElements.push(data.config.root);
const abortController = new AbortController();

console.log(nonChildScrollableElements);
scrollables.push([el, abortController]);
}

return nonChildScrollableElements as [HTMLElement, AbortController];
return scrollables;
}
/**
* Used for setting a single event listener on x number of events for a given
Expand Down

0 comments on commit f2a0db1

Please sign in to comment.