Skip to content

Commit

Permalink
Unify Draggable opts with DnD (#282)
Browse files Browse the repository at this point in the history
* unify draggable opts

* refactor base opts

* add overflow-x method
  • Loading branch information
jalal246 authored Jul 13, 2021
1 parent c1db275 commit 94f06d8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 15 deletions.
13 changes: 9 additions & 4 deletions packages/dnd/src/DnD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import store from "./DnDStore";

import type { ElmTree } from "./DnDStore";

import type { DndOpts } from "./types";
import { DraggableOpts } from "./Draggable/types";
import type { DndOpts, FinalDndOpts } from "./types";

const defaultOpts = Object.freeze({
const defaultOpts: DndOpts = Object.freeze({
thresholds: {
vertical: 60,
horizontal: 60,
Expand All @@ -28,6 +27,12 @@ const defaultOpts = Object.freeze({
allowLeavingFromLeft: true,
allowLeavingFromRight: true,
},

scroll: {
enable: true,
speed: 10,
threshold: 50,
},
});

class DnD extends Droppable {
Expand Down Expand Up @@ -69,7 +74,7 @@ class DnD extends Droppable {
elmCoreInstanceWithTree,
siblingsBoundaries,
initCoordinates,
options as DraggableOpts
options as FinalDndOpts
);

super(draggable);
Expand Down
21 changes: 18 additions & 3 deletions packages/dnd/src/Draggable/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import type {
DraggableBaseInterface,
ThresholdPercentages,
LayoutThresholds,
DraggableOpts,
} from "./types";
import { FinalDndOpts } from "../types";

/**
* Base element.
Expand All @@ -35,7 +35,7 @@ class Base

operationID: string;

opts: DraggableOpts;
opts: FinalDndOpts;

parentsList: ELmBranch;

Expand All @@ -55,7 +55,7 @@ class Base
elmTree: ElmTree,
siblingsBoundaries: BoundariesOffset,
initCoordinates: MouseCoordinates,
opts: DraggableOpts
opts: FinalDndOpts
) {
const {
element,
Expand Down Expand Up @@ -204,6 +204,15 @@ class Base
$.maxRight = left + horizontal;
}

isParenOverflowX() {
const parentBottom =
this.activeParent!.offset.top + this.activeParent!.offset.height;

const elemOverflowX = parentBottom > window.innerHeight;

return elemOverflowX;
}

/**
* Assigns new ACTIVE_PARENT: parent who contains dragged
*
Expand All @@ -221,6 +230,12 @@ class Base
* transformed and which is not.
*/
this.isOutActiveParent = false;

if (this.opts.scroll.enable) {
this.opts.scroll.enable = this.opts.scroll.enable
? this.isParenOverflowX()
: false;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/dnd/src/Draggable/Draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import type {
TempOffset,
Threshold,
TempTranslate,
DraggableOpts,
} from "./types";
import type { FinalDndOpts } from "../types";

class Draggable extends Base implements DraggableDnDInterface {
private innerOffsetX: number;
Expand Down Expand Up @@ -46,7 +46,7 @@ class Draggable extends Base implements DraggableDnDInterface {
elmTree: ElmTree,
siblingsBoundaries: BoundariesOffset,
initCoordinates: MouseCoordinates,
opts: DraggableOpts
opts: FinalDndOpts
) {
super(elmTree, siblingsBoundaries, initCoordinates, opts);

Expand Down
8 changes: 2 additions & 6 deletions packages/dnd/src/Draggable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import type { CoreInstanceInterface } from "@dflex/core-instance";
import type { ELmBranch } from "@dflex/dom-gen";
import type { AbstractDraggableInterface } from "@dflex/draggable";
import type { FinalDndOpts } from "../types";

export interface ThresholdPercentages {
vertical: number;
Expand Down Expand Up @@ -43,17 +44,12 @@ export interface Restrictions {
allowLeavingFromRight: boolean;
}

export interface DraggableOpts {
restrictions: Restrictions;
thresholds: ThresholdPercentages;
}

export interface DraggableBaseInterface
extends AbstractDraggableInterface<CoreInstanceInterface> {
tempIndex: number;
operationID: string;

opts: DraggableOpts;
opts: FinalDndOpts;

parentsList: ELmBranch | null;
siblingsList: string[] | null;
Expand Down
16 changes: 16 additions & 0 deletions packages/dnd/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@
*/
import type { ThresholdPercentages, Restrictions } from "./Draggable";

export interface ScrollOpt {
speed: number;
threshold: number;
}

export interface Scroll extends ScrollOpt {
enable: boolean;
}

export interface FinalDndOpts {
thresholds: ThresholdPercentages;
restrictions: Restrictions;
scroll: Scroll;
}

export interface DndOpts {
thresholds?: Partial<ThresholdPercentages>;
restrictions?: Partial<Restrictions>;
scroll?: Partial<Scroll>;
}

0 comments on commit 94f06d8

Please sign in to comment.