-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
584 additions
and
392 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
89 changes: 89 additions & 0 deletions
89
packages/core/src/enhancers/navigation/resolvers/getNavigationForLeftPage.ts
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,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) | ||
} |
65 changes: 65 additions & 0 deletions
65
packages/core/src/enhancers/navigation/resolvers/getNavigationForLeftSinglePage.ts
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,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 | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
packages/core/src/enhancers/navigation/resolvers/getNavigationForRightPage.ts
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,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) | ||
} |
67 changes: 67 additions & 0 deletions
67
packages/core/src/enhancers/navigation/resolvers/getNavigationForRightSinglePage.ts
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,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 | ||
} | ||
} |
Oops, something went wrong.