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

feat: read mode #35

Merged
merged 3 commits into from
Nov 9, 2024
Merged
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
2 changes: 2 additions & 0 deletions .github/workflows/preview-service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ on:
branches:
- main
- hotfix
- preview
- read_mode

jobs:
deploy:
Expand Down
18 changes: 0 additions & 18 deletions .github/workflows/sync-forks.yml

This file was deleted.

19 changes: 0 additions & 19 deletions packages/server/README.md

This file was deleted.

2 changes: 2 additions & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
},
"dependencies": {
"@generouted/react-router": "^1.19.6",
"@mozilla/readability": "^0.5.0",
"@tailwindcss/line-clamp": "^0.4.4",
"react-router-dom": "^6.24.0"
},
"devDependencies": {
"@tailwindcss/typography": "^0.5.15",
"@web-archive/shared": "workspace:*"
}
}
16 changes: 16 additions & 0 deletions packages/web/src/components/iframe-page-content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
interface IframePageContentProps {
pageHtml: string
}

function IframePageContent({ pageHtml }: IframePageContentProps) {
const pageContentUrl = URL.createObjectURL(new Blob([pageHtml], { type: 'text/html' }))
return (
<iframe
src={pageContentUrl}
className="w-full h-full bg-current"
sandbox="allow-scripts allow-same-origin allow-popups allow-forms"
/>
)
}

export default IframePageContent
30 changes: 30 additions & 0 deletions packages/web/src/components/readability-page-content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Readability } from '@mozilla/readability'
import { useEffect, useRef } from 'react'

interface ReadabilityPageContentProps {
pageHtml: string
}

function ReadabilityPageContent({ pageHtml }: ReadabilityPageContentProps) {
const article = new Readability(
new DOMParser().parseFromString(pageHtml, 'text/html'),
).parse()

const articleContainerRef = useRef<HTMLDivElement>(null)

useEffect(() => {
if (articleContainerRef.current) {
articleContainerRef.current.innerHTML = article?.content || ''
}
}, [article?.content])

return (
<main className="flex flex-col gap-4 items-center">
<h1 className="text-2xl font-bold">{article?.title}</h1>
<h3 className="text-sm text-muted-foreground max-w-prose">{article?.excerpt}</h3>
<div className="prose dark:prose-invert" ref={articleContainerRef}></div>
</main>
)
}

export default ReadabilityPageContent
15 changes: 14 additions & 1 deletion packages/web/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ function Routes() {
const [view, setView] = useLocalStorageState('view', {
defaultValue: 'card',
})
const [readMode, setReadMode] = useLocalStorageState('readMode', {
defaultValue: false,
})
const {
data: tagCache,
runAsync: refreshTagCache,
} = useRequest(getAllTag)
return (
<AppContext.Provider value={useMemo(() => ({ view: view as 'card' | 'list', setView, tagCache, refreshTagCache }), [view, setView, tagCache, refreshTagCache])}>
<AppContext.Provider value={
useMemo(() => ({
view: view as 'card' | 'list',
setView,
tagCache,
refreshTagCache,
readMode: readMode as boolean,
setReadMode,
}), [view, setView, tagCache, refreshTagCache, readMode, setReadMode])
}
>
<ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
<RouterProvider router={router} />
</ThemeProvider>
Expand Down
39 changes: 20 additions & 19 deletions packages/web/src/pages/(layout)/page.[slug].tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Button } from '@web-archive/shared/components/button'
import { useRequest } from 'ahooks'
import { ArrowLeft, Trash } from 'lucide-react'
import { useEffect } from 'react'
import { useContext, useEffect } from 'react'
import IframePageContent from '~/components/iframe-page-content'
import LoadingWrapper from '~/components/loading-wrapper'
import ReadabilityPageContent from '~/components/readability-page-content'
import { deletePage, getPageDetail } from '~/data/page'
import { useNavigate, useParams } from '~/router'
import AppContext from '~/store/app'

async function getPageContent(pageId: string | undefined) {
if (!pageId)
Expand Down Expand Up @@ -50,8 +54,7 @@ function ArchivePage() {

const { data: pageContentUrl, loading: pageLoading } = useRequest(async () => {
const pageHtml = await getPageContent(slug)
const objectUrl = URL.createObjectURL(new Blob([pageHtml], { type: 'text/html' }))
return objectUrl
return pageHtml
})
useEffect(() => {
return () => {
Expand All @@ -74,13 +77,22 @@ function ArchivePage() {
goBack()
}

const { readMode, setReadMode } = useContext(AppContext)

return (
<main className="h-screen flex flex-col">
<nav className="p-2 flex justify-between items-center">
<Button variant="ghost" size="sm" onClick={goBack}>
<ArrowLeft className="w-5 h-5" />
</Button>
<div className="flex space-x-2">
<Button
variant="secondary"
size="sm"
onClick={() => setReadMode(!readMode)}
>
{readMode ? 'open Iframe mode' : 'open Read mode'}
</Button>
<Button
variant="destructive"
size="sm"
Expand All @@ -91,22 +103,11 @@ function ArchivePage() {
</div>
</nav>
<div className="flex-1 p-4">
{
pageLoading
? (
<div className="w-full h-full flex flex-col items-center justify-center">
<div className="m-b-xl h-8 w-8 animate-spin border-4 border-t-transparent rounded-full border-primary"></div>
<div>Loading...</div>
</div>
)
: (
<iframe
src={pageContentUrl}
className="w-full h-full bg-current"
sandbox="allow-scripts allow-same-origin allow-popups allow-forms"
/>
)
}
<LoadingWrapper loading={pageLoading}>
{readMode
? <ReadabilityPageContent pageHtml={pageContentUrl || ''} />
: <IframePageContent pageHtml={pageContentUrl || ''} />}
</LoadingWrapper>
</div>
</main>
)
Expand Down
4 changes: 4 additions & 0 deletions packages/web/src/store/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ const AppContext = createContext<{
setView: (view: 'card' | 'list') => void
tagCache?: Array<Tag>
refreshTagCache: () => Promise<Array<Tag>>
readMode: boolean
setReadMode: (readMode: boolean) => void
}>({
view: 'card',
setView: () => { },
tagCache: [],
refreshTagCache: async () => { return [] },
readMode: false,
setReadMode: (value: boolean) => { },
})

export default AppContext
3 changes: 2 additions & 1 deletion packages/web/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { fontFamily } = require('tailwindcss/defaultTheme')
const typography = require('@tailwindcss/typography')

/** @type {import('tailwindcss').Config} */
module.exports = {
Expand Down Expand Up @@ -82,5 +83,5 @@ module.exports = {
},
},
},
plugins: [require('tailwindcss-animate'), require('@tailwindcss/line-clamp')],
plugins: [require('tailwindcss-animate'), require('@tailwindcss/line-clamp'), typography],
}
44 changes: 44 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.