-
-
Notifications
You must be signed in to change notification settings - Fork 245
fix :huawei touch not working #101
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
45eb2b0
acb0d26
937434f
1e8a58d
3753f52
9bbe1bc
1cba262
bfd39b9
f498370
8a96b01
cc31872
e708aa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,14 +86,26 @@ async function toDataUrl(url: string) { | |
| return readBlob((r) => r.readAsDataURL(buffer)) | ||
| } | ||
|
|
||
| function proxyGithubUrl(url: string) { | ||
| if (url.includes('github.com/')) { | ||
| return url.replace(/^http(s?):\/\/github\.com/, '/github/download').replace('/raw/', '/') | ||
| } | ||
|
|
||
| if (url.includes('raw.githubusercontent.com/')) { | ||
| return url.replace('https://raw.githubusercontent.com', '/github/download') | ||
| } | ||
|
|
||
| return url | ||
| } | ||
|
|
||
| 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) | ||
|
|
||
| return ( | ||
| book ?? | ||
| fetch(url) | ||
| fetch(proxyGithubUrl(url)) | ||
|
||
| .then((res) => res.blob()) | ||
| .then((blob) => addBook(new File([blob], filename))) | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| 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 }) | ||
|
|
||
| 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, '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 } | ||
|
|
||
| // When selecting text with long tap, `touchend` is not fired, | ||
| // so instead of use `addEventlistener`, we should use `on*` | ||
| // to remove the previous listener. | ||
| if (!iframe) return | ||
| iframe.ontouchend = handleTouchEnd | ||
| }); | ||
|
|
||
| useEventListener(iframe, 'touchend', handleTouchEnd) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.