-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make classes for each move types
- Loading branch information
Showing
11 changed files
with
413 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Snap from "./Snap"; | ||
import { MOVE_TYPE, EVENTS } from "../consts"; | ||
import { MoveTypeContext, DestinationInfo } from "../types"; | ||
import { circulate } from "../utils"; | ||
|
||
class FreeScroll extends Snap { | ||
protected readonly type: string = MOVE_TYPE.FREE_SCROLL; | ||
|
||
constructor() { | ||
// Set snap count to Infinity | ||
super(Infinity); | ||
} | ||
|
||
public findTargetPanel(ctx: MoveTypeContext): DestinationInfo { | ||
const { axesEvent, viewport, swipeDistance, minimumDistanceToChange } = ctx; | ||
const destPos = axesEvent.destPos.flick; | ||
|
||
const eventDelta = Math.abs(axesEvent.delta.flick); | ||
if (eventDelta > minimumDistanceToChange) { | ||
const destInfo = super.findSnappedPanel(ctx); | ||
destInfo.destPos = destPos; | ||
destInfo.eventType = destInfo.eventType === EVENTS.RESTORE | ||
? "" | ||
: EVENTS.CHANGE; | ||
|
||
return destInfo; | ||
} else { | ||
const scrollArea = viewport.getScrollArea(); | ||
const estimatedPosition = circulate(destPos, scrollArea.prev, scrollArea.next, false) | ||
+ viewport.getRelativeHangerPosition(); | ||
|
||
return { | ||
panel: viewport.findNearestPanelAt(estimatedPosition)!, | ||
destPos, | ||
duration: viewport.options.duration, | ||
eventType: swipeDistance > minimumDistanceToChange | ||
? EVENTS.CHANGE | ||
: "", | ||
}; | ||
} | ||
} | ||
|
||
public findRestorePanel(ctx: MoveTypeContext): DestinationInfo { | ||
return this.findTargetPanel(ctx); | ||
} | ||
} | ||
|
||
export default FreeScroll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { MoveTypeStringOption, MoveTypeContext, DestinationInfo } from "../types"; | ||
import Panel from "../components/Panel"; | ||
import { EVENTS } from "../consts"; | ||
|
||
abstract class MoveType { | ||
protected readonly abstract type: string; | ||
|
||
public abstract findTargetPanel(ctx: MoveTypeContext): DestinationInfo; | ||
|
||
public is(type: MoveTypeStringOption): boolean { | ||
return type === this.type; | ||
} | ||
|
||
public findRestorePanel(ctx: MoveTypeContext): DestinationInfo { | ||
const viewport = ctx.viewport; | ||
const options = viewport.options; | ||
|
||
const panel = options.circular | ||
? this.findRestorePanelInCircularMode(ctx) | ||
: viewport.getCurrentPanel()!; | ||
|
||
return { | ||
panel, | ||
destPos: viewport.findEstimatedPosition(panel), | ||
duration: options.duration, | ||
eventType: EVENTS.RESTORE, | ||
}; | ||
} | ||
|
||
private findRestorePanelInCircularMode(ctx: MoveTypeContext): Panel { | ||
const viewport = ctx.viewport; | ||
const originalPanel = viewport.getCurrentPanel()!.getOriginalPanel(); | ||
const hangerPosition = viewport.getHangerPosition(); | ||
|
||
const firstClonedPanel = originalPanel.getIdenticalPanels()[1]; | ||
const lapped = Math.abs(originalPanel.getAnchorPosition() - hangerPosition) | ||
> Math.abs(firstClonedPanel.getAnchorPosition() - hangerPosition); | ||
|
||
return (!ctx.isNextDirection && lapped) | ||
? firstClonedPanel | ||
: originalPanel; | ||
} | ||
} | ||
|
||
export default MoveType; |
Oops, something went wrong.