Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
21 changes: 15 additions & 6 deletions apps/reader/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ const config = {
locales: ['en-US', 'zh-CN'],
defaultLocale: 'en-US',
},
async rewrites() {
return [
{
source: '/github/download/:path*',
destination: 'https://raw.githubusercontent.com/:path*', // The :path parameter is used here so will not be automatically passed in the query
},
]
},
...(IS_DOCKER && {
output: 'standalone',
experimental: {
Expand All @@ -55,11 +63,12 @@ const base = withPWA(withTM(withBundleAnalyzer(config)))

const dev = base
const docker = base
const prod = withSentryConfig(
base,
// Make sure adding Sentry options is the last code to run before exporting, to
// ensure that your source maps include changes from all other Webpack plugins
sentryWebpackPluginOptions,
)
// const prod = withSentryConfig(
// base,
// // Make sure adding Sentry options is the last code to run before exporting, to
// // ensure that your source maps include changes from all other Webpack plugins
// sentryWebpackPluginOptions,
// )
const prod = base

module.exports = IS_DEV ? dev : IS_DOCKER ? docker : prod
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
16 changes: 14 additions & 2 deletions apps/reader/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why add this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this, download book from Github will occur CORS error.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to move this to a new PR, but idk if we actually need this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I delete these code.

.then((res) => res.blob())
.then((blob) => addBook(new File([blob], 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'
69 changes: 69 additions & 0 deletions apps/reader/src/hooks/useTouchEvent.ts
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)
}
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