Skip to content
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

fix :huawei touch not working #101

Open
wants to merge 11 commits into
base: main
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
46 changes: 2 additions & 44 deletions apps/reader/src/components/Reader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import { navbarState } from '@flow/reader/state'
import { db } from '../db'
import { handleFiles } from '../file'
import {
hasSelection,
useBackground,
useColorScheme,
useDisablePinchZooming,
useMobile,
useSync,
useTranslation,
useTypography,
useTouchEvent,
} from '../hooks'
import { BookTab, reader, useReaderSnapshot } from '../models'
import { isTouchScreen } from '../platform'
Expand Down Expand Up @@ -336,49 +336,7 @@ function BookPane({ tab, onMouseDown }: BookPaneProps) {

useEventListener(iframe, 'keydown', handleKeyDown(tab))

useEventListener(iframe, 'touchstart', (e) => {
const x0 = e.targetTouches[0]?.clientX ?? 0
const y0 = e.targetTouches[0]?.clientY ?? 0
const t0 = Date.now()

if (!iframe) return

// When selecting text with long tap, `touchend` is not fired,
// so instead of use `addEventlistener`, we should use `on*`
// to remove the previous listener.
iframe.ontouchend = function handleTouchEnd(e: TouchEvent) {
iframe.ontouchend = undefined
const selection = iframe.getSelection()
if (hasSelection(selection)) return

const x1 = e.changedTouches[0]?.clientX ?? 0
const y1 = e.changedTouches[0]?.clientY ?? 0
const t1 = Date.now()

const deltaX = x1 - x0
const deltaY = y1 - y0
const deltaT = t1 - t0

const absX = Math.abs(deltaX)
const absY = Math.abs(deltaY)

if (absX < 10) return

if (absY / absX > 2) {
if (deltaT > 100 || absX < 30) {
return
}
}

if (deltaX > 0) {
tab.prev()
}

if (deltaX < 0) {
tab.next()
}
}
})
useTouchEvent({iframe: iframe, tab: tab});

useDisablePinchZooming(iframe)

Expand Down
2 changes: 1 addition & 1 deletion apps/reader/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function toDataUrl(url: string) {
}

export async function fetchBook(url: string) {
const filename = /\/([^/]*\.epub)$/i.exec(url)?.[1] ?? ''
const filename = decodeURIComponent(/\/([^/]*\.epub)$/i.exec(url)?.[1] ?? '')
const books = await db?.books.toArray()
const book = books?.find((b) => b.name === filename)

Expand Down
1 change: 1 addition & 0 deletions apps/reader/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './useMobile'
export * from './useTextSelection'
export * from './useTranslation'
export * from './useTypography'
export * from './useTouchEvent'
75 changes: 75 additions & 0 deletions apps/reader/src/hooks/useTouchEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { useEventListener } from '@literal-ui/hooks'
import { useCallback, useRef } from 'react'

import { AsRef, BookTab } from '../models/reader'

import { hasSelection } from './useTextSelection'

export function useTouchEvent(props: {
iframe?: Window & AsRef
tab: BookTab
}) {
const { iframe, tab } = props
const params = useRef({ x: -1, y: -1, t: 0, expired: false })

useEventListener(iframe, 'touchstart', (e) => {
const x0 = e.targetTouches[0]?.clientX ?? 0
const y0 = e.targetTouches[0]?.clientY ?? 0
const t0 = Date.now()

params.current = { x: x0, y: y0, t: t0, expired: false }

if (!iframe) return

// When selecting text with long tap, `touchend` is not fired,
// so instead of use `addEventlistener`, we should use `on*`
// to remove the previous listener.
iframe.ontouchend = handleTouchEnd
})

const handleTouchEnd = useCallback(
function (e: TouchEvent) {
if (!iframe) return
iframe.ontouchend = undefined
console.log('params:', params.current)
const selection = iframe.getSelection()

if (hasSelection(selection)) return
if (params.current.expired) return

params.current.expired = true

const x1 = e.changedTouches[0]?.clientX ?? 0
const y1 = e.changedTouches[0]?.clientY ?? 0
const t1 = Date.now()

const { x, y, t } = params.current

const deltaX = x1 - x
const deltaY = y1 - y
const deltaT = t1 - t

const absX = Math.abs(deltaX)
const absY = Math.abs(deltaY)

if (absX < 10) return

if (absY / absX > 2) {
if (deltaT > 100 || absX < 30) {
return
}
}

if (deltaX > 0) {
tab.prev()
}

if (deltaX < 0) {
tab.next()
}
},
[tab, iframe],
)

useEventListener(iframe, 'touchend', handleTouchEnd)
}
2 changes: 1 addition & 1 deletion apps/reader/src/models/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BaseTab {
}

// https://github.com/pmndrs/valtio/blob/92f3311f7f1a9fe2a22096cd30f9174b860488ed/src/vanilla.ts#L6
type AsRef = { $$valtioRef: true }
export type AsRef = { $$valtioRef: true }

export class BookTab extends BaseTab {
epub?: Book
Expand Down