⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.
TypeScript bookmarkbookmark-managercollaborationnextjsreact-nativeread-it-laterself-hostedtypescript
merge mobile and web reader view styling
daniel31x13a32c908a2b2d
diff --git a/apps/mobile/components/Formats/ReadableFormat.tsx b/apps/mobile/components/Formats/ReadableFormat.tsxindex 8bec9544..aa3aefbf 100644--- a/apps/mobile/components/Formats/ReadableFormat.tsx+++ b/apps/mobile/components/Formats/ReadableFormat.tsx@@ -1,31 +1,61 @@-import React, { useEffect, useState } from "react";-import { View, Text, ScrollView, TouchableOpacity } from "react-native";+import React, { useEffect, useMemo, useState } from "react";+import { View, Linking } from "react-native";import * as FileSystem from "expo-file-system/legacy";import NetInfo from "@react-native-community/netinfo";import useAuthStore from "@/store/auth";-import { useRouter } from "expo-router";-import { useWindowDimensions } from "react-native";-import RenderHtml from "@linkwarden/react-native-render-html";-import ElementNotSupported from "@/components/ElementNotSupported";+import WebView from "react-native-webview";import { decode } from "html-entities";import { useColorScheme } from "nativewind";import { rawTheme, ThemeName } from "@/lib/colors";-import { CalendarDays, Link } from "lucide-react-native";import { ArchivedFormat } from "@linkwarden/types/global";import { Link as LinkType } from "@linkwarden/prisma/client";+import {+ readerViewCSS,+ readerViewThemeVars,+} from "@linkwarden/lib/readerViewStyles";type Props = {link: LinkType;setIsLoading: (state: boolean) => void;};+function ReadableSkeleton({ theme }: { theme: (typeof rawTheme)["light"] }) {+ const barStyle = {+ backgroundColor: theme["neutral-content"],+ borderRadius: 6,+ height: 14,+ marginBottom: 10,+ };++ return (+ <View style={{ flex: 1, padding: 16 }}>+ <View style={{ ...barStyle, height: 22, width: "75%", marginBottom: 14 }} />+ <View style={{ ...barStyle, width: "50%", height: 12 }} />+ <View style={{ ...barStyle, width: "40%", height: 12, marginBottom: 20 }} />+ <View+ style={{+ borderTopWidth: 1,+ borderTopColor: theme["neutral-content"],+ marginBottom: 20,+ }}+ />+ <View style={{ ...barStyle, width: "100%" }} />+ <View style={{ ...barStyle, width: "100%" }} />+ <View style={{ ...barStyle, width: "85%" }} />+ <View style={{ ...barStyle, width: "92%" }} />+ <View style={{ ...barStyle, width: "70%" }} />+ <View style={{ ...barStyle, width: "95%" }} />+ <View style={{ ...barStyle, width: "100%" }} />+ <View style={{ ...barStyle, width: "80%" }} />+ </View>+ );+}+export default function ReadableFormat({ link, setIsLoading }: Props) {const FORMAT = ArchivedFormat.readability;const { auth } = useAuthStore();const [content, setContent] = useState<string>("");- const { width } = useWindowDimensions();- const router = useRouter();const { colorScheme } = useColorScheme();useEffect(() => {@@ -72,71 +102,108 @@ export default function ReadableFormat({ link, setIsLoading }: Props) {loadCacheOrFetch();}, [link]);+ const theme = rawTheme[colorScheme as ThemeName];+ const isDark = colorScheme === "dark";++ const title = decode(link.name || link.description || link.url || "");+ const dateStr = new Date(link?.importDate || link.createdAt).toLocaleDateString(+ "en-US",+ { year: "numeric", month: "short", day: "numeric" }+ );++ const htmlDocument = useMemo(+ () => `+ <!DOCTYPE html>+ <html lang="en">+ <head>+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">+ <style>+ :root { ${readerViewThemeVars(theme, isDark)} }+ * { box-sizing: border-box; }+ body {+ margin: 0;+ padding: 16px;+ background-color: ${theme["base-100"]};+ color: ${theme["base-content"]};+ font-family: -apple-system, system-ui, sans-serif;+ -webkit-text-size-adjust: 100%;+ }+ a { color: ${theme.primary}; }+ img { max-width: 100%; height: auto; }+ .rv-header-title {+ font-size: 24px;+ font-weight: bold;+ margin-bottom: 10px;+ line-height: 1.3;+ overflow-wrap: anywhere;+ word-break: normal;+ hyphens: manual;+ }+ .rv-header-meta {+ font-size: 16px;+ color: ${theme.neutral};+ margin-bottom: 10px;+ display: flex;+ align-items: center;+ gap: 4px;+ min-width: 0;+ }+ .rv-header-meta a {+ color: ${theme.neutral};+ text-decoration: none;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ min-width: 0;+ }+ .rv-header-separator {+ border: none;+ border-top: 1px solid ${theme["neutral-content"]};+ margin: 10px 0 20px 0;+ }+ ${readerViewCSS}+ </style>+ </head>+ <body>+ <div class="rv-header-title">${title.replace(/</g, "<").replace(/>/g, ">")}</div>+ <div class="rv-header-meta">+ <a href="${(link.url || "").replace(/"/g, """)}">${link.url || ""}</a>+ </div>+ <div class="rv-header-meta">${dateStr}</div>+ <hr class="rv-header-separator" />+ <div class="reader-view read-only">+ ${content}+ </div>+ </body>+ </html>+ `,+ [content, colorScheme]+ );++ if (!content) {+ return <ReadableSkeleton theme={theme} />;+ }+return (- content && (- <ScrollView- className="flex-1 bg-base-100"- contentContainerClassName="p-4"- nestedScrollEnabled- contentInsetAdjustmentBehavior="automatic"- automaticallyAdjustContentInsets- automaticallyAdjustsScrollIndicatorInsets- >- <Text className="text-2xl font-bold mb-2.5 text-base-content">- {decode(link.name || link.description || link.url || "")}- </Text>-- <TouchableOpacity- className="flex-row items-center gap-1 mb-2.5 pr-5"- onPress={() => router.replace(`/links/${link.id}`)}- >- <Link- size={16}- color={rawTheme[colorScheme as ThemeName]["neutral"]}- />- <Text className="text-base text-neutral flex-1" numberOfLines={1}>- {link.url}- </Text>- </TouchableOpacity>-- <View className="flex-row items-center gap-1 mb-2.5">- <CalendarDays- size={16}- color={rawTheme[colorScheme as ThemeName]["neutral"]}- />- <Text className="text-base text-neutral">- {new Date(link?.importDate || link.createdAt).toLocaleString(- "en-US",- {- year: "numeric",- month: "short",- day: "numeric",- }- )}- </Text>- </View>-- <View className="border-t border-neutral-content mt-2.5 mb-5" />-- <RenderHtml- contentWidth={width}- source={{ html: content }}- renderers={{- table: () => (- <ElementNotSupported- onPress={() => router.replace(`/links/${link.id}`)}- />- ),- }}- onHTMLLoaded={() => setIsLoading(false)}- tagsStyles={{- p: { fontSize: 18, lineHeight: 28, marginVertical: 10 },- }}- baseStyle={{- color: rawTheme[colorScheme as ThemeName]["base-content"],- }}- />- </ScrollView>- )+ <WebView+ source={{ html: htmlDocument }}+ style={{+ flex: 1,+ backgroundColor: "transparent",+ }}+ onLoadEnd={() => setIsLoading(false)}+ onShouldStartLoadWithRequest={(request) => {+ if (+ request.url === "about:blank" ||+ request.url.startsWith("data:")+ ) {+ return true;+ }+ Linking.openURL(request.url);+ return false;+ }}+ javaScriptEnabled={false}+ originWhitelist={["*"]}+ />);}diff --git a/apps/web/components/Preservation/ReadableView.tsx b/apps/web/components/Preservation/ReadableView.tsxindex 25acb7b2..b3998ff1 100644--- a/apps/web/components/Preservation/ReadableView.tsx+++ b/apps/web/components/Preservation/ReadableView.tsx@@ -12,6 +12,10 @@ import {LinkIncludingShortenedCollectionAndTags,ArchivedFormat,} from "@linkwarden/types/global";+import {+ readerViewCSS,+ READER_VIEW_DEFAULTS,+} from "@linkwarden/lib/readerViewStyles";import ClickAwayHandler from "@/components/ClickAwayHandler";import {useGetLinkHighlights,@@ -287,7 +291,8 @@ export default function ReadableView({ link }: Props) {useEffect(() => {if (!user) return;- const readerViews = document.getElementsByClassName("reader-view");+ const container = containerRef.current;+ if (!container) return;const getFont = () => {if (user.readableFontFamily === "caveat") {@@ -297,51 +302,22 @@ export default function ReadableView({ link }: Props) {} else return user.readableFontFamily;};- for (const view of Array.from(readerViews)) {- const paragraphs = view.getElementsByTagName("p");- for (const paragraph of Array.from(paragraphs)) {- paragraph.style.fontSize = user.readableFontSize || "18px";- paragraph.style.lineHeight = user.readableLineHeight || "1.8";- }-- const paragraphToUserReadableFontSizeRatio =- parseInt(user.readableFontSize || "18") / 18;-- const headers1 = view.getElementsByTagName("h1");- for (const header of Array.from(headers1)) {- header.style.fontSize =- 35 * paragraphToUserReadableFontSizeRatio + "px";- }- const headers2 = view.getElementsByTagName("h2");- for (const header of Array.from(headers2)) {- header.style.fontSize =- 30 * paragraphToUserReadableFontSizeRatio + "px";- }- const headers3 = view.getElementsByTagName("h3");- for (const header of Array.from(headers3)) {- header.style.fontSize =- 26 * paragraphToUserReadableFontSizeRatio + "px";- }- const headers4 = view.getElementsByTagName("h4");- for (const header of Array.from(headers4)) {- header.style.fontSize =- 21 * paragraphToUserReadableFontSizeRatio + "px";- }- const headers5 = view.getElementsByTagName("h5");- for (const header of Array.from(headers5)) {- header.style.fontSize =- 18 * paragraphToUserReadableFontSizeRatio + "px";- }-- (view as HTMLElement).style.fontFamily = `${getFont()}`;- }+ const pFontSize = user.readableFontSize || `${READER_VIEW_DEFAULTS.fontSize}px`;+ const ratio = parseInt(pFontSize) / READER_VIEW_DEFAULTS.fontSize;++ container.style.setProperty("--rv-p-font-size", pFontSize);+ container.style.setProperty("--rv-p-line-height", user.readableLineHeight || String(READER_VIEW_DEFAULTS.lineHeight));+ container.style.setProperty("--rv-h1-font-size", READER_VIEW_DEFAULTS.h1Size * ratio + "px");+ container.style.setProperty("--rv-h2-font-size", READER_VIEW_DEFAULTS.h2Size * ratio + "px");+ container.style.setProperty("--rv-h3-font-size", READER_VIEW_DEFAULTS.h3Size * ratio + "px");+ container.style.setProperty("--rv-h4-font-size", READER_VIEW_DEFAULTS.h4Size * ratio + "px");+ container.style.setProperty("--rv-h5-font-size", READER_VIEW_DEFAULTS.h5Size * ratio + "px");+ container.style.fontFamily = `${getFont()}`;}, [user?.theme,user?.readableFontFamily,user?.readableFontSize,user?.readableLineHeight,- highlightedHtml,- linkContent,]);const handleHighlightSelection = async (@@ -419,9 +395,11 @@ export default function ReadableView({ link }: Props) {const containerRef = useRef<HTMLDivElement>(null);return (- <div- ref={containerRef}- className={clsx(+ <>+ <style dangerouslySetInnerHTML={{ __html: readerViewCSS }} />+ <div+ ref={containerRef}+ className={clsx("flex flex-col gap-3 items-start p-3 mx-auto bg-base-200 mt-5 relative",user?.readableLineWidth === "narrower"? "max-w-screen-sm"@@ -470,6 +448,7 @@ export default function ReadableView({ link }: Props) {<divid="readable-view"className="line-break px-1 reader-view read-only"+ style={{ contentVisibility: "auto" }}dangerouslySetInnerHTML={{ __html: highlightedHtml }}/>@@ -617,5 +596,6 @@ export default function ReadableView({ link }: Props) {</div>)}</div>+ </>);}diff --git a/apps/web/styles/globals.css b/apps/web/styles/globals.cssindex 524a3891..15020833 100644--- a/apps/web/styles/globals.css+++ b/apps/web/styles/globals.css@@ -214,114 +214,14 @@cursor: pointer;}-/* Reader view custom stylings */-.reader-view {- /* line-height: 2.8rem; */-}-.reader-view * {- text-align: inherit;-}-.reader-view mark {- background-color: oklch(var(--p) / 0.5);- color: oklch(var(--bc));- border-radius: 5px;- padding-left: 0.25rem;- padding-right: 0.25rem;-}-.reader-view p {- font-size: 18px;- line-height: 1.8;- margin-bottom: 16px;- margin-top: 16px;-}-.reader-view h1 {- font-size: 35px;- line-height: 1.4;-}-.reader-view h2 {- font-size: 30px;- line-height: 1.4;-}-.reader-view h3 {- font-size: 26px;- line-height: 1.4;-}-.reader-view h4 {- font-size: 21px;- line-height: 1.4;-}-.reader-view h5 {- font-size: 18px;- line-height: 1.4;-}-.reader-view li {- list-style: inside;- margin-left: 2rem;-}-.reader-view a {- text-decoration: underline;-}-.reader-view b {- font-weight: bolder;-}-.reader-view pre,-.reader-view code {- font-family: "Courier New", Courier, monospace;-}-.reader-view blockquote {- opacity: 50%;- padding-left: 1.5rem;- border-left: solid 3px;-}-.reader-view img {- margin: auto;- margin-top: 2rem;- margin-bottom: 2rem;- border-radius: 10px;-}-.reader-view pre {- padding: 1rem;- line-height: normal;-}-.reader-view code {- padding: 0.15rem 0.4rem 0.15rem 0.4rem;-}-[data-theme="dark"] .reader-view code,-[data-theme="dark"] .reader-view pre {- background-color: rgb(49, 49, 49);- border-radius: 8px;-}-[data-theme="light"] .reader-view code,-[data-theme="light"] .reader-view pre {- background-color: rgb(230, 230, 230);- border-radius: 8px;-}--.reader-view ul[data-type="taskList"] {- list-style: none;- margin-left: 0;- padding: 0;-}--.reader-view ul[data-type="taskList"] li {- align-items: center;- display: flex;-}--.reader-view ul[data-type="taskList"] li > label {- flex: 0 0 auto;- margin-right: 0.5rem;- user-select: none;-}--.reader-view ul[data-type="taskList"] li > div {- flex: 1 1 auto;-}--.reader-view ul[data-type="taskList"] input[type="checkbox"] {- cursor: pointer;+/* Reader view theme variables (base styles are in @linkwarden/lib/readerViewStyles) */+[data-theme="dark"] {+ --rv-code-bg: rgb(49, 49, 49);+ --rv-mark-bg: oklch(var(--p) / 0.5);+ --rv-mark-color: oklch(var(--bc));}--.read-only ul[data-type="taskList"] {- pointer-events: none;+[data-theme="light"] {+ --rv-code-bg: rgb(230, 230, 230);+ --rv-mark-bg: oklch(var(--p) / 0.5);+ --rv-mark-color: oklch(var(--bc));}diff --git a/packages/lib/readerViewStyles.ts b/packages/lib/readerViewStyles.tsnew file mode 100644index 00000000..bddbcaa7--- /dev/null+++ b/packages/lib/readerViewStyles.ts@@ -0,0 +1,131 @@+export const READER_VIEW_DEFAULTS = {+ fontSize: 18,+ lineHeight: 1.8,+ headingLineHeight: 1.4,+ paragraphMargin: 16,+ h1Size: 35,+ h2Size: 30,+ h3Size: 26,+ h4Size: 21,+ h5Size: 18,+} as const;++export const readerViewCSS = `+.reader-view,+.reader-view * {+ text-align: inherit;+ overflow-wrap: anywhere;+ word-break: normal;+ hyphens: manual;+}+.reader-view mark {+ background-color: var(--rv-mark-bg);+ color: var(--rv-mark-color);+ border-radius: 5px;+ padding-left: 0.25rem;+ padding-right: 0.25rem;+}+.reader-view p {+ font-size: var(--rv-p-font-size, ${READER_VIEW_DEFAULTS.fontSize}px);+ line-height: var(--rv-p-line-height, ${READER_VIEW_DEFAULTS.lineHeight});+ margin-bottom: ${READER_VIEW_DEFAULTS.paragraphMargin}px;+ margin-top: ${READER_VIEW_DEFAULTS.paragraphMargin}px;+}+.reader-view h1 {+ font-size: var(--rv-h1-font-size, ${READER_VIEW_DEFAULTS.h1Size}px);+ line-height: ${READER_VIEW_DEFAULTS.headingLineHeight};+}+.reader-view h2 {+ font-size: var(--rv-h2-font-size, ${READER_VIEW_DEFAULTS.h2Size}px);+ line-height: ${READER_VIEW_DEFAULTS.headingLineHeight};+}+.reader-view h3 {+ font-size: var(--rv-h3-font-size, ${READER_VIEW_DEFAULTS.h3Size}px);+ line-height: ${READER_VIEW_DEFAULTS.headingLineHeight};+}+.reader-view h4 {+ font-size: var(--rv-h4-font-size, ${READER_VIEW_DEFAULTS.h4Size}px);+ line-height: ${READER_VIEW_DEFAULTS.headingLineHeight};+}+.reader-view h5 {+ font-size: var(--rv-h5-font-size, ${READER_VIEW_DEFAULTS.h5Size}px);+ line-height: ${READER_VIEW_DEFAULTS.headingLineHeight};+}+.reader-view li {+ list-style: inside;+ margin-left: 2rem;+}+.reader-view a {+ text-decoration: underline;+}+.reader-view b {+ font-weight: bolder;+}+.reader-view pre,+.reader-view code {+ font-family: "Courier New", Courier, monospace;+}+.reader-view blockquote {+ opacity: 50%;+ padding-left: 1.5rem;+ border-left: solid 3px;+}+.reader-view img {+ max-width: 100%;+ height: auto;+ margin: auto;+ margin-top: 2rem;+ margin-bottom: 2rem;+ border-radius: 10px;+}+.reader-view pre {+ padding: 1rem;+ line-height: normal;+ white-space: pre-wrap;+}+.reader-view code {+ padding: 0.15rem 0.4rem 0.15rem 0.4rem;+}+.reader-view code,+.reader-view pre {+ background-color: var(--rv-code-bg);+ border-radius: 8px;+}+.reader-view ul[data-type="taskList"] {+ list-style: none;+ margin-left: 0;+ padding: 0;+}+.reader-view ul[data-type="taskList"] li {+ align-items: center;+ display: flex;+}+.reader-view ul[data-type="taskList"] li > label {+ flex: 0 0 auto;+ margin-right: 0.5rem;+ user-select: none;+}+.reader-view ul[data-type="taskList"] li > div {+ flex: 1 1 auto;+}+.reader-view ul[data-type="taskList"] input[type="checkbox"] {+ cursor: pointer;+}+.read-only ul[data-type="taskList"] {+ pointer-events: none;+}+`;++export function readerViewThemeVars(+ theme: {+ primary: string;+ "base-content": string;+ },+ isDark: boolean+): string {+ return `+ --rv-code-bg: ${isDark ? "rgb(49, 49, 49)" : "rgb(230, 230, 230)"};+ --rv-mark-bg: ${theme.primary}80;+ --rv-mark-color: ${theme["base-content"]};+ `;+}