@mk-drag-and-drop/dom 0.4.6 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,6 +33,7 @@ export type DragControllerOptions = {
33
33
  targetingConstraint?: TargetingConstraint;
34
34
  } & DragLifecycleCallbacks;
35
35
  export type DragController = {
36
+ cancelDrag: () => void;
36
37
  remeasureDropTargets: (input?: RemeasureDropTargetsInput) => void;
37
38
  remeasureOverlay: () => void;
38
39
  recomputeActiveDrag: () => void;
@@ -19,6 +19,9 @@ export function createDragController(options = {}) {
19
19
  configureRuntime(options);
20
20
  syncLiveRegion();
21
21
  const controller = {
22
+ cancelDrag: () => {
23
+ runtime.cancelDrag();
24
+ },
22
25
  remeasureDropTargets: (input) => {
23
26
  runtime.remeasureDropTargets(input);
24
27
  },
package/dist/index.d.ts CHANGED
@@ -5,7 +5,8 @@ export { createDropContainer, type CreateDropContainerInput, } from "./droppable
5
5
  export { createSortable, type CreateSortableInput, } from "./sortable/create-sortable-binding.js";
6
6
  export type { SortableAxis, SortablePlacementBoundary, } from "./sortable/sortable-options.js";
7
7
  export { createDragHandle, type CreateDragHandleInput, } from "./input/create-drag-handle.js";
8
- export type { DragEndResult, DragEndEvent, DragLifecycleCallbacks, DragLifecycleHelpers, DragSource, DragStartEvent, DragUpdateEvent, DropEvent, } from "./runtime/lifecycle.js";
8
+ export { domPointerDragActiveAttribute } from "./input/pointer-drag-state.js";
9
+ export type { DragEndResult, DragEndEvent, DragLifecycleCallbacks, DragLifecycleHelpers, DragSource, DragStartEvent, DragUpdateEvent, DropEvent, ActiveDragResetEvent, SortablePreview, } from "./runtime/lifecycle.js";
9
10
  export type { RemeasureDropTargetsInput, SortableDropPlacement, } from "./runtime/drop-target-registry.js";
10
11
  export type { OverlayReleaseMode } from "./runtime/types.js";
11
12
  export { centerToCenter, pointerToCenter, pointerToRectDistance, } from "./targeting/algorithms.js";
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ export { createDroppable, } from "./droppable/create-droppable-binding.js";
4
4
  export { createDropContainer, } from "./droppable/create-drop-container-binding.js";
5
5
  export { createSortable, } from "./sortable/create-sortable-binding.js";
6
6
  export { createDragHandle, } from "./input/create-drag-handle.js";
7
+ export { domPointerDragActiveAttribute } from "./input/pointer-drag-state.js";
7
8
  export { centerToCenter, pointerToCenter, pointerToRectDistance, } from "./targeting/algorithms.js";
8
9
  export { getDistanceToRect, maxOverlayCenterDistanceToRect, maxPointerDistanceToRect, } from "./targeting/constraints.js";
9
10
  export { lockToXAxis, lockToYAxis, restrictToContainer, } from "./modifiers/built-ins.js";
@@ -24,5 +24,5 @@ export declare class KeyboardDragController {
24
24
  isEnabled(): boolean;
25
25
  handleSourceKeyDown(input: SourceKeyboardDragKeyDownInput): boolean;
26
26
  handleActiveKeyDown(key: string): boolean;
27
- bindWindowListeners(): () => void;
27
+ bindWindowListeners(targetWindow?: Window & typeof globalThis): () => void;
28
28
  }
@@ -56,7 +56,7 @@ export class KeyboardDragController {
56
56
  }
57
57
  return false;
58
58
  }
59
- bindWindowListeners() {
59
+ bindWindowListeners(targetWindow = window) {
60
60
  let isCleanedUp = false;
61
61
  const handleKeyDown = (event) => {
62
62
  if (!this.handleActiveKeyDown(event.key)) {
@@ -65,13 +65,13 @@ export class KeyboardDragController {
65
65
  event.preventDefault();
66
66
  event.stopPropagation();
67
67
  };
68
- window.addEventListener("keydown", handleKeyDown);
68
+ targetWindow.addEventListener("keydown", handleKeyDown);
69
69
  return () => {
70
70
  if (isCleanedUp) {
71
71
  return;
72
72
  }
73
73
  isCleanedUp = true;
74
- window.removeEventListener("keydown", handleKeyDown);
74
+ targetWindow.removeEventListener("keydown", handleKeyDown);
75
75
  };
76
76
  }
77
77
  }
@@ -11,6 +11,7 @@ export class PointerActivationController {
11
11
  return;
12
12
  }
13
13
  const { activationDelay, activationDistance } = this.options.getConfiguration();
14
+ const targetWindow = request.element.ownerDocument.defaultView ?? window;
14
15
  if (activationDelay === null && activationDistance === null) {
15
16
  this.options.startImmediately(request);
16
17
  return;
@@ -26,6 +27,7 @@ export class PointerActivationController {
26
27
  pointerId: request.pointerId,
27
28
  initialPointerPosition,
28
29
  latestPointerPosition: initialPointerPosition,
30
+ targetWindow,
29
31
  activationTimerId: null,
30
32
  releasePendingPointerListeners: null,
31
33
  };
@@ -55,17 +57,17 @@ export class PointerActivationController {
55
57
  }
56
58
  this.cancelPendingActivation();
57
59
  };
58
- window.addEventListener("pointermove", handlePointerMove, globalPointerListenerCapture);
59
- window.addEventListener("pointerup", handlePointerEnd, globalPointerListenerCapture);
60
- window.addEventListener("pointercancel", handlePointerEnd, globalPointerListenerCapture);
60
+ targetWindow.addEventListener("pointermove", handlePointerMove, globalPointerListenerCapture);
61
+ targetWindow.addEventListener("pointerup", handlePointerEnd, globalPointerListenerCapture);
62
+ targetWindow.addEventListener("pointercancel", handlePointerEnd, globalPointerListenerCapture);
61
63
  pendingActivation.releasePendingPointerListeners = () => {
62
- window.removeEventListener("pointermove", handlePointerMove, globalPointerListenerCapture);
63
- window.removeEventListener("pointerup", handlePointerEnd, globalPointerListenerCapture);
64
- window.removeEventListener("pointercancel", handlePointerEnd, globalPointerListenerCapture);
64
+ targetWindow.removeEventListener("pointermove", handlePointerMove, globalPointerListenerCapture);
65
+ targetWindow.removeEventListener("pointerup", handlePointerEnd, globalPointerListenerCapture);
66
+ targetWindow.removeEventListener("pointercancel", handlePointerEnd, globalPointerListenerCapture);
65
67
  };
66
68
  this.pendingActivation = pendingActivation;
67
69
  if (activationDelay !== null) {
68
- pendingActivation.activationTimerId = window.setTimeout(() => {
70
+ pendingActivation.activationTimerId = targetWindow.setTimeout(() => {
69
71
  this.activatePendingPointerDrag(pendingActivation);
70
72
  }, activationDelay);
71
73
  }
@@ -90,7 +92,7 @@ export class PointerActivationController {
90
92
  if (pendingActivation.activationTimerId === null) {
91
93
  return;
92
94
  }
93
- window.clearTimeout(pendingActivation.activationTimerId);
95
+ pendingActivation.targetWindow.clearTimeout(pendingActivation.activationTimerId);
94
96
  pendingActivation.activationTimerId = null;
95
97
  }
96
98
  activatePendingPointerDrag(pendingActivation) {
@@ -0,0 +1,2 @@
1
+ export declare const domPointerDragActiveAttribute = "data-dnd-pointer-drag-active";
2
+ export declare function acquireDocumentPointerDragState(sourceDocument: Document): () => void;
@@ -0,0 +1,32 @@
1
+ export const domPointerDragActiveAttribute = "data-dnd-pointer-drag-active";
2
+ const documentPointerDragStates = new WeakMap();
3
+ export function acquireDocumentPointerDragState(sourceDocument) {
4
+ const owner = {};
5
+ let state = documentPointerDragStates.get(sourceDocument);
6
+ if (!state) {
7
+ state = {
8
+ root: sourceDocument.documentElement,
9
+ previousValue: sourceDocument.documentElement.getAttribute(domPointerDragActiveAttribute),
10
+ owners: new Set(),
11
+ };
12
+ documentPointerDragStates.set(sourceDocument, state);
13
+ }
14
+ state.owners.add(owner);
15
+ state.root.setAttribute(domPointerDragActiveAttribute, "true");
16
+ let released = false;
17
+ return () => {
18
+ if (released) {
19
+ return;
20
+ }
21
+ released = true;
22
+ if (!state?.owners.delete(owner) || state.owners.size > 0) {
23
+ return;
24
+ }
25
+ documentPointerDragStates.delete(sourceDocument);
26
+ if (state.previousValue === null) {
27
+ state.root.removeAttribute(domPointerDragActiveAttribute);
28
+ return;
29
+ }
30
+ state.root.setAttribute(domPointerDragActiveAttribute, state.previousValue);
31
+ };
32
+ }
@@ -1,6 +1,6 @@
1
1
  export { createDragRuntimeScope, type DragRuntimeScope, type DragRuntimeScopeConfigureInput, type DragRuntimeScopeOptions, } from "../runtime/drag-runtime-scope.js";
2
2
  export type { DragOverlayHostUpdate, DragOverlayPhase, DragOverlayRenderState, DragState, OverlayReleaseMode, } from "../runtime/types.js";
3
- export type { DragRuntimeSubscription } from "../runtime/lifecycle.js";
3
+ export type { ActiveDragResetEvent, DragRuntimeSubscription, SortablePreview, } from "../runtime/lifecycle.js";
4
4
  export { createDomDraggable, type CreateDomDraggableInput, type DomDraggableBehavior, type DomDraggableKeyDownEvent, type DomDraggablePointerDownEvent, type DomDraggableRuntime, } from "../draggable/create-draggable.js";
5
5
  export { createDomDroppable, type CreateDomDroppableInput, type DomDroppableBehavior, type DomDroppableRuntime, } from "../droppable/create-droppable.js";
6
6
  export { createDomDropContainer, type CreateDomDropContainerInput, type DomDropContainerBehavior, type DomDropContainerRuntime, } from "../droppable/create-drop-container.js";
@@ -8,3 +8,4 @@ export { createDomSortable, type CreateDomSortableInput, type DomSortableBehavio
8
8
  export type { DomSortableRuntime } from "../sortable/sortable-registry.js";
9
9
  export type { SortableAxis, SortablePlacementBoundary, } from "../sortable/sortable-options.js";
10
10
  export { domDragHandleAttribute } from "../input/drag-handle.js";
11
+ export { domPointerDragActiveAttribute } from "../input/pointer-drag-state.js";
@@ -4,3 +4,4 @@ export { createDomDroppable, } from "../droppable/create-droppable.js";
4
4
  export { createDomDropContainer, } from "../droppable/create-drop-container.js";
5
5
  export { createDomSortable, } from "../sortable/create-sortable.js";
6
6
  export { domDragHandleAttribute } from "../input/drag-handle.js";
7
+ export { domPointerDragActiveAttribute } from "../input/pointer-drag-state.js";
@@ -4,6 +4,7 @@ import type { DomDropContainerRuntime } from "../droppable/create-drop-container
4
4
  import type { DomDroppableRuntime } from "../droppable/create-droppable.js";
5
5
  import type { DomSortableRuntime } from "../sortable/sortable-registry.js";
6
6
  import { type StaleDomBindingRecord } from "./drag-runtime.js";
7
+ import type { DragRuntimeSubscription } from "./lifecycle.js";
7
8
  import type { DragRuntimeConfigureInput, DragRuntimeOptions } from "./types.js";
8
9
  export type DragRuntimeScopeOptions = DragRuntimeOptions;
9
10
  export type DragRuntimeScopeConfigureInput = DragRuntimeConfigureInput;
@@ -13,6 +14,7 @@ export type DragRuntimeScope = DomDraggableRuntime & DomDroppableRuntime & DomDr
13
14
  releaseActiveDragResources: () => void;
14
15
  recomputeActiveDrag: () => void;
15
16
  setOverlayRect: (overlayRect: DragRect | null) => void;
17
+ subscribe: (subscription: DragRuntimeSubscription) => () => void;
16
18
  };
17
19
  export type InternalStaleDomBindingRuntime = {
18
20
  registerStaleDomBinding: (record: StaleDomBindingRecord) => () => void;
@@ -1,4 +1,5 @@
1
1
  import { DragRuntime } from "./drag-runtime.js";
2
+ import { linkSortableCoordinatorHost } from "./sortable-coordinator.js";
2
3
  export function createDragRuntimeScope(options) {
3
4
  const runtime = new DragRuntime(options);
4
5
  const scope = {
@@ -29,5 +30,6 @@ export function createDragRuntimeScope(options) {
29
30
  pruneDisconnectedDomBindings: () => runtime.pruneDisconnectedDomBindings(),
30
31
  getStaleDomBindingRecordCount: () => runtime.getStaleDomBindingRecordCount(),
31
32
  };
33
+ linkSortableCoordinatorHost(scope, runtime);
32
34
  return scope;
33
35
  }
@@ -18,8 +18,12 @@ export declare class DragRuntime {
18
18
  private activeDragModifiers;
19
19
  private releaseActiveInputListeners;
20
20
  private restoreTextSelectionSuppression;
21
+ private releasePointerDragState;
22
+ private releasePostDragClickSuppression;
21
23
  private queuedPointerPosition;
22
24
  private pointerFrameId;
25
+ private pointerFrameWindow;
26
+ private activeDragWindow;
23
27
  private subscriptions;
24
28
  private staleDomBindingRecords;
25
29
  private lifecycleCallbacks;
@@ -70,6 +74,8 @@ export declare class DragRuntime {
70
74
  private releaseOverlayAfterDrag;
71
75
  private bindPointerWindowListeners;
72
76
  private bindKeyboardWindowListeners;
77
+ private suppressNextRelatedClick;
78
+ private clearPostDragClickSuppression;
73
79
  private suppressTextSelection;
74
80
  private resetActiveDragState;
75
81
  private releaseActiveInputResources;
@@ -81,6 +87,7 @@ export declare class DragRuntime {
81
87
  private getActiveInput;
82
88
  private getDraggingSession;
83
89
  private getDropEventSortablePlacement;
90
+ private getSortablePreview;
84
91
  private createDragState;
85
92
  private getLifecycleDropTargetRect;
86
93
  private createLifecycleDropTargetRectSnapshot;
@@ -90,7 +97,6 @@ export declare class DragRuntime {
90
97
  private getCurrentDragRectAt;
91
98
  private removeDisconnectedDropTargets;
92
99
  private pruneDisconnectedDomBindingRecords;
93
- private clearActiveDropTargetIdIfRemoved;
94
100
  private notifyDragStartSubscriptions;
95
101
  private notifyDragStartLifecycle;
96
102
  private notifyDragUpdate;