Skip to content

Commit

Permalink
fix: some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Jul 19, 2024
1 parent ccd20f0 commit 61e77cd
Show file tree
Hide file tree
Showing 16 changed files with 584 additions and 392 deletions.
46 changes: 34 additions & 12 deletions packages/core/src/enhancers/navigation/manualNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Reader } from "../../reader"
import { Report } from "../../report"
import { ViewportPosition } from "../../navigation/ViewportNavigator"
import { getNavigationForSpineItemPage } from "./resolvers/getNavigationForSpineItemPage"
import { getNavigationForRightPage } from "./resolvers/getNavigationForRightPage"
import { getNavigationForLeftPage } from "./resolvers/getNavigationForLeftPage"

export class ManualNavigator {
movingLastDelta = { x: 0, y: 0 }
Expand All @@ -11,26 +13,46 @@ export class ManualNavigator {
constructor(protected reader: Reader) {}

turnRight() {
const navigation =
this.reader.navigation.navigationResolver.getNavigationForRightPage(
this.reader.navigation.getNavigation().position,
)
const navigation = this.reader.navigation.getNavigation()
const spineItem = this.reader.spineItemManager.get(navigation.spineItem)

if (!spineItem) return

const position = getNavigationForRightPage({
context: this.reader.context,
navigationResolver: this.reader.navigation.navigationResolver,
position: navigation.position,
computedPageTurnDirection:
this.reader.settings.settings.computedPageTurnDirection,
spineItem,
spineItemManager: this.reader.spineItemManager,
spineLocator: this.reader.spine.locator,
})

return this.reader.navigation.navigate({
animation: "turn",
position: navigation,
position,
})
}

turnLeft() {
const navigation =
this.reader.navigation.navigationResolver.getNavigationForLeftPage(
this.reader.navigation.getNavigation().position,
)
const navigation = this.reader.navigation.getNavigation()
const spineItem = this.reader.spineItemManager.get(navigation.spineItem)

if (!spineItem) return

const position = getNavigationForLeftPage({
context: this.reader.context,
navigationResolver: this.reader.navigation.navigationResolver,
position: navigation.position,
computedPageTurnDirection:
this.reader.settings.settings.computedPageTurnDirection,
spineItem,
spineItemManager: this.reader.spineItemManager,
spineLocator: this.reader.spine.locator,
})

return this.reader.navigation.navigate({
animation: "turn",
position: navigation,
position,
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Context } from "../../../context/Context"
import { NavigationResolver } from "../../../navigation/resolvers/NavigationResolver"
import { ViewportPosition } from "../../../navigation/ViewportNavigator"
import { SpineLocator } from "../../../spine/locationResolver"
import { SpineItem } from "../../../spineItem/createSpineItem"
import { SpineItemManager } from "../../../spineItemManager"
import { getNavigationForLeftSinglePage } from "./getNavigationForLeftSinglePage"

/**
* Very naive approach for spread. It could be optimized but by using this approach
* we do not add complexity to the code and use the current logic to handle it correctly.
*
* @important
* Special case for vertical content, read content
*/
export const getNavigationForLeftPage = ({
position,
spineItem,
context,
navigationResolver,
spineItemManager,
spineLocator,
computedPageTurnDirection
}: {
position: ViewportPosition
spineItem: SpineItem
context: Context
spineItemManager: SpineItemManager
navigationResolver: NavigationResolver
spineLocator: SpineLocator
computedPageTurnDirection: "horizontal" | "vertical"
}): ViewportPosition => {
const navigation = getNavigationForLeftSinglePage({
position,
context,
navigationResolver,
computedPageTurnDirection,
spineItemManager,
spineLocator,
})

// when we move withing vertical content, because only y moves, we don't need two navigation
if (spineItem?.isUsingVerticalWriting() && position.x === navigation.x) {
return navigationResolver.getAdjustedPositionForSpread(navigation)
}

if (context.state.isUsingSpreadMode) {
// in case of spread the entire screen is taken as one real page for vertical content
// in order to move out from it we add an extra page width.
// using `getNavigationForLeftSinglePage` again would keep x as it is and wrongly move y
// for the next item in case it's also a vertical content
if (spineItem?.isUsingVerticalWriting() && position.x !== navigation.x) {
return navigationResolver.getAdjustedPositionForSpread(
navigationResolver.wrapPositionWithSafeEdge(
context.isRTL()
? { ...navigation, x: navigation.x + context.getPageSize().width }
: {
...navigation,
x: navigation.x - context.getPageSize().width,
},
),
)
}

/**
* In vase we move vertically and the y is already different, we don't need a second navigation
* since we already jumped to a new screen
*/
if (
computedPageTurnDirection === `vertical` &&
position.y !== navigation.y
) {
return navigationResolver.getAdjustedPositionForSpread(navigation)
}

const doubleNavigation = getNavigationForLeftSinglePage({
position: navigation,
context,
navigationResolver,
computedPageTurnDirection,
spineItemManager,
spineLocator,
})

return navigationResolver.getAdjustedPositionForSpread(doubleNavigation)
}

return navigationResolver.getAdjustedPositionForSpread(navigation)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Context } from "../../../context/Context"
import { NavigationResolver } from "../../../navigation/resolvers/NavigationResolver"
import { ViewportPosition } from "../../../navigation/ViewportNavigator"
import { SpineLocator } from "../../../spine/locationResolver"
import { SpineItemManager } from "../../../spineItemManager"
import { getSpineItemPositionForLeftPage } from "./getSpineItemPositionForLeftPage"

export const getNavigationForLeftSinglePage = ({
position,
navigationResolver,
computedPageTurnDirection,
spineItemManager,
spineLocator,
context,
}: {
position: ViewportPosition
navigationResolver: NavigationResolver
computedPageTurnDirection: "horizontal" | "vertical"
spineItemManager: SpineItemManager
spineLocator: SpineLocator
context: Context
}): ViewportPosition => {
const pageTurnDirection = computedPageTurnDirection
const spineItem =
spineLocator.getSpineItemFromPosition(position) || spineItemManager.get(0)
const defaultNavigation = position

if (!spineItem) {
return defaultNavigation
}

const spineItemPosition = spineLocator.getSpineItemPositionFromSpinePosition(
position,
spineItem,
)

const spineItemNavigation = getSpineItemPositionForLeftPage({
position: spineItemPosition,
spineItem,
pageHeight: context.getPageSize().height,
pageWidth: context.getPageSize().width,
spineItemLocator: spineLocator.spineItemLocator,
})

const isNewNavigationInCurrentItem = navigationResolver.arePositionsDifferent(
spineItemNavigation,
spineItemPosition,
)

if (!isNewNavigationInCurrentItem) {
return navigationResolver.wrapPositionWithSafeEdge(
pageTurnDirection === `horizontal`
? { x: position.x - context.getPageSize().width, y: 0 }
: { y: position.y - context.getPageSize().height, x: 0 },
)
} else {
const readingOrderPosition =
spineLocator.getSpinePositionFromSpineItemPosition(
spineItemNavigation,
spineItem,
)

return readingOrderPosition
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Context } from "../../../context/Context"
import { NavigationResolver } from "../../../navigation/resolvers/NavigationResolver"
import { ViewportPosition } from "../../../navigation/ViewportNavigator"
import { SpineLocator } from "../../../spine/locationResolver"
import { SpineItem } from "../../../spineItem/createSpineItem"
import { SpineItemManager } from "../../../spineItemManager"
import { getNavigationForRightSinglePage } from "./getNavigationForRightSinglePage"

/**
* Very naive approach for spread. It could be optimized but by using this approach
* we do not add complexity to the code and use the current logic to handle it correctly.
*
* @important
* Special case for vertical content, read content
*/
export const getNavigationForRightPage = ({
position,
spineItem,
context,
navigationResolver,
spineItemManager,
spineLocator,
computedPageTurnDirection,
}: {
position: ViewportPosition
spineItem: SpineItem
context: Context
spineItemManager: SpineItemManager
navigationResolver: NavigationResolver
spineLocator: SpineLocator
computedPageTurnDirection: "horizontal" | "vertical"
}): ViewportPosition => {
const navigation = getNavigationForRightSinglePage({
position,
context,
navigationResolver,
computedPageTurnDirection,
spineItemManager,
spineLocator,
})

// when we move withing vertical content, because only y moves, we don't need two navigation
if (spineItem?.isUsingVerticalWriting() && position.x === navigation.x) {
return navigationResolver.getAdjustedPositionForSpread(navigation)
}

if (context.state.isUsingSpreadMode) {
// in case of spread the entire screen is taken as one real page for vertical content
// in order to move out from it we add an extra page width.
// using `getNavigationForLeftSinglePage` again would keep x as it is and wrongly move y
// for the next item in case it's also a vertical content
if (spineItem?.isUsingVerticalWriting() && position.x !== navigation.x) {
return navigationResolver.getAdjustedPositionForSpread(
navigationResolver.wrapPositionWithSafeEdge(
context.isRTL()
? {
...navigation,
x: navigation.x - context.getPageSize().width,
}
: {
...navigation,
x: navigation.x + context.getPageSize().width,
},
),
)
}

/**
* In vase we move vertically and the y is already different, we don't need a second navigation
* since we already jumped to a new screen
*/
if (
computedPageTurnDirection === `vertical` &&
position.y !== navigation.y
) {
return navigationResolver.getAdjustedPositionForSpread(navigation)
}

const doubleNavigation = getNavigationForRightSinglePage({
position: navigation,
context,
navigationResolver,
computedPageTurnDirection,
spineItemManager,
spineLocator,
})

return navigationResolver.getAdjustedPositionForSpread(doubleNavigation)
}

return navigationResolver.getAdjustedPositionForSpread(navigation)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Context } from "../../../context/Context"
import { NavigationResolver } from "../../../navigation/resolvers/NavigationResolver"
import { ViewportPosition } from "../../../navigation/ViewportNavigator"
import { SpineLocator } from "../../../spine/locationResolver"
import { SpineItemManager } from "../../../spineItemManager"
import { getSpineItemPositionForRightPage } from "./getSpineItemPositionForRightPage"

export const getNavigationForRightSinglePage = ({
position,
navigationResolver,
computedPageTurnDirection,
spineItemManager,
spineLocator,
context,
}: {
position: ViewportPosition
navigationResolver: NavigationResolver
computedPageTurnDirection: "horizontal" | "vertical"
spineItemManager: SpineItemManager
spineLocator: SpineLocator
context: Context
}): ViewportPosition => {
const pageTurnDirection = computedPageTurnDirection
const spineItem =
spineLocator.getSpineItemFromPosition(position) || spineItemManager.get(0)
const defaultNavigation = position

if (!spineItem) {
return defaultNavigation
}

// translate viewport position into reading item local position
const spineItemPosition = spineLocator.getSpineItemPositionFromSpinePosition(
position,
spineItem,
)
// get reading item local position for right page
const spineItemNavigationForRightPage = getSpineItemPositionForRightPage({
position: spineItemPosition,
spineItem,
pageHeight: context.getPageSize().height,
pageWidth: context.getPageSize().width,
spineItemLocator: spineLocator.spineItemLocator,
})

// check both position to see if we moved out of it
const isNewNavigationInCurrentItem = navigationResolver.arePositionsDifferent(
spineItemNavigationForRightPage,
spineItemPosition,
)

if (!isNewNavigationInCurrentItem) {
return navigationResolver.wrapPositionWithSafeEdge(
pageTurnDirection === `horizontal`
? { x: position.x + context.getPageSize().width, y: 0 }
: { y: position.y + context.getPageSize().height, x: 0 },
)
} else {
const readingOrderPosition =
spineLocator.getSpinePositionFromSpineItemPosition(
spineItemNavigationForRightPage,
spineItem,
)

return readingOrderPosition
}
}
Loading

0 comments on commit 61e77cd

Please sign in to comment.