Skip to content

fix: properly scroll vertical text #656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions readium/navigator/src/main/assets/_scripts/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export function isRTL() {
return document.body.dir.toLowerCase() == "rtl";
}

export function isVerticalWritingMode() {
const writingMode = window
.getComputedStyle(document.documentElement)
.getPropertyValue("writing-mode");
return writingMode.startsWith("vertical");
}

// Scroll to the given TagId in document and snap.
export function scrollToId(id) {
var element = document.getElementById(id);
Expand Down Expand Up @@ -160,8 +167,12 @@ export function scrollToStart() {
if (!isScrollModeEnabled()) {
document.scrollingElement.scrollLeft = 0;
} else {
document.scrollingElement.scrollTop = 0;
window.scrollTo(0, 0);
if (!isVerticalWritingMode()) {
document.scrollingElement.scrollTop = 0;
window.scrollTo(0, 0);
} else {
document.scrollingElement.scrollLeft = 0;
}
}
}

Expand All @@ -173,8 +184,12 @@ export function scrollToEnd() {
document.scrollingElement.scrollWidth * factor
);
} else {
document.scrollingElement.scrollTop = document.body.scrollHeight;
window.scrollTo(0, document.body.scrollHeight);
if (!isVerticalWritingMode()) {
document.scrollingElement.scrollTop = document.body.scrollHeight;
window.scrollTo(0, document.body.scrollHeight);
} else {
document.scrollingElement.scrollLeft = -document.body.scrollWidth;
}
}
}

Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ internal class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView
private var mLastMotionX: Float = 0.toFloat()
private var mInitialMotionX: Float = 0.toFloat()
private var mInitialMotionY: Float = 0.toFloat()
private var mInitialOverscroll: OverscrollMode = OverscrollMode.NONE

/**
* Sentinel value for no current active pointer.
Expand Down Expand Up @@ -707,6 +708,7 @@ internal class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView
mInitialMotionX = ev.x
mLastMotionX = mInitialMotionX
mInitialMotionY = ev.y
mInitialOverscroll = getOverscrollMode()
mActivePointerId = ev.getPointerId(0)
}
MotionEvent.ACTION_MOVE -> {
Expand Down Expand Up @@ -745,9 +747,16 @@ internal class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView
if (scrollMode) {
val totalDelta = (y - mInitialMotionY).toInt()
if (abs(totalDelta) < 200) {
if (mInitialMotionX < x) {
if (mInitialOverscroll == OverscrollMode.BOTH) {
if (mInitialMotionX < x) {
scrollLeft(animated = true)
} else if (mInitialMotionX > x) {
scrollRight(animated = true)
}
}
else if (mInitialMotionX < x && mInitialOverscroll == OverscrollMode.LEFT) {
scrollLeft(animated = true)
} else if (mInitialMotionX > x) {
} else if (mInitialMotionX > x && mInitialOverscroll == OverscrollMode.RIGHT) {
scrollRight(animated = true)
}
}
Expand Down Expand Up @@ -1054,12 +1063,31 @@ internal class R2WebView(context: Context, attrs: AttributeSet) : R2BasicWebView
return false
}

private fun getOverscrollMode(): OverscrollMode {
val clientWidth = getClientWidth() ?: return OverscrollMode.NONE
val right = scrollX >= computeHorizontalScrollRange() - clientWidth
val left = scrollX <= 0
if (left && right) {
return OverscrollMode.BOTH
} else if (left) {
return OverscrollMode.LEFT
} else if (right) {
return OverscrollMode.RIGHT
} else {
return OverscrollMode.NONE
}
}

internal val numPages: Int get() =
getClientWidth()
?.let { clientWidth -> (computeHorizontalScrollRange() / clientWidth.toDouble()).roundToInt() }
?.coerceAtLeast(1)
?: 1

enum class OverscrollMode {
NONE, LEFT, RIGHT, BOTH
}

/**
* Layout parameters that should be supplied for views added to a
* ViewPager.
Expand Down