AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
feat(web): add reading protection mode
tianma-if9af8bfed0fde
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsxindex 73c1580f..bd16b820 100644--- a/apps/web/src/components/EditorPane.tsx+++ b/apps/web/src/components/EditorPane.tsx@@ -40,6 +40,8 @@ import {Link2,Share2,Copy,+ Lock,+ LockOpen,} from "lucide-react";import { Button } from "@/components/ui/button";import { GitHubRepositoryLink } from "@/components/GitHubRepositoryLink";@@ -138,6 +140,8 @@ import {formatShortcutBinding,getEditableMemoTitle,getNotebookMoveOptions,+ readDesktopReadingProtectionPreference,+ writeDesktopReadingProtectionPreference,type EditorContentAlignment,type MemoDocumentActionRequest,type ShortcutSettings,@@ -814,6 +818,7 @@ const RichEditorPane = ({typeof window === "undefined" ? false : window.matchMedia(MOBILE_EDITOR_QUERY).matches);const [isMobileEditing, setIsMobileEditing] = useState(false);+ const [desktopReadingProtection, setDesktopReadingProtection] = useState(readDesktopReadingProtectionPreference);const [mobilePlainText, setMobilePlainText] = useState("");const [markdownSource, setMarkdownSource] = useState("");const [isMarkdownMode, setIsMarkdownMode] = useState(false);@@ -879,10 +884,37 @@ const RichEditorPane = ({const mobileDefaultEditRequested = Boolean(memo?.id && memo.id === mobileDefaultEditMemoId && !readOnly);const mobileEditingActive = isMobileEditing || mobileDefaultEditRequested;- const effectiveReadOnly = readOnly || (isMobileViewport && !mobileEditingActive);+ const effectiveReadOnly = readOnly+ || (isMobileViewport && !mobileEditingActive)+ || (!isMobileViewport && desktopReadingProtection);const useMobilePlainTextEditor = isMobileViewport && mobileEditingActive && !readOnly;const useMarkdownSourceEditor = !useMobilePlainTextEditor && isMarkdownMode;+ const toggleDesktopReadingProtection = useCallback(() => {+ setDesktopReadingProtection((protectedMode) => {+ const nextProtectedMode = !protectedMode;+ writeDesktopReadingProtectionPreference(nextProtectedMode);+ return nextProtectedMode;+ });+ }, []);++ useEffect(() => {+ if (!isMobileViewport && mobileDefaultEditRequested && desktopReadingProtection) {+ setDesktopReadingProtection(false);+ writeDesktopReadingProtectionPreference(false);+ }+ }, [desktopReadingProtection, isMobileViewport, mobileDefaultEditRequested]);++ useEffect(() => {+ if (!desktopReadingProtection) return;+ setAiAssistantOpen(false);+ setAiSelection(null);+ setAiInsertionTarget(null);+ setNoteSearchReplaceOpen(false);+ setExternalLinkDialogOpen(false);+ setNoteLinkPickerOpen(false);+ }, [desktopReadingProtection]);+const memoRef = useRef<MemoDetail | null>(memo);const editSessionRef = useRef<MemoEditSession | null>(null);const editorRef = useRef<Editor | null>(null);@@ -1869,10 +1901,11 @@ const RichEditorPane = ({}, [focusNoteSearchInput]);const openNoteReplace = useCallback(() => {+ if (effectiveReadOnly) return;setNoteSearchOpen(true);setNoteSearchReplaceOpen(true);focusNoteSearchInput();- }, [focusNoteSearchInput]);+ }, [effectiveReadOnly, focusNoteSearchInput]);const closeNoteSearch = useCallback(() => {setNoteSearchOpen(false);@@ -2102,6 +2135,7 @@ const RichEditorPane = ({}, [editor, getMobilePlainTextValue, markdownSource, useMarkdownSourceEditor, useMobilePlainTextEditor]);const openAiAssistant = useCallback(() => {+ if (effectiveReadOnly) return;let selection: AiSelectionContext | null = null;let insertionTarget: AiInsertionTarget | null = null;@@ -2156,7 +2190,7 @@ const RichEditorPane = ({setAiSelection(selection);setAiInsertionTarget(insertionTarget);setAiAssistantOpen(true);- }, [editor, getMobilePlainTextValue, markdownSource, useMarkdownSourceEditor, useMobilePlainTextEditor]);+ }, [editor, effectiveReadOnly, getMobilePlainTextValue, markdownSource, useMarkdownSourceEditor, useMobilePlainTextEditor]);useEffect(() => {openAiAssistantRef.current = openAiAssistant;@@ -2171,6 +2205,7 @@ const RichEditorPane = ({}, []);const applyAiDraft = useCallback((draft: string, mode: "append" | "replace") => {+ if (effectiveReadOnly) return false;if (mode === "replace" && aiSelection) {const replacementDraft = normalizeAiSelectionReplacement(draft);if (!replacementDraft) return false;@@ -2285,7 +2320,7 @@ const RichEditorPane = ({setAiInsertionTarget(null);setAiAssistantOpen(false);return true;- }, [aiInsertionTarget, aiSelection, editor, getCurrentMarkdownForAi, getMobilePlainTextValue, markDirty, markdownSource, persistCurrentDraft, tagsText, title, useMarkdownSourceEditor, useMobilePlainTextEditor]);+ }, [aiInsertionTarget, aiSelection, editor, effectiveReadOnly, getCurrentMarkdownForAi, getMobilePlainTextValue, markDirty, markdownSource, persistCurrentDraft, tagsText, title, useMarkdownSourceEditor, useMobilePlainTextEditor]);const getCurrentContentJson = useCallback((): TiptapDoc | null => {if (useMobilePlainTextEditor) {@@ -2906,7 +2941,7 @@ const RichEditorPane = ({}, [buildImageExportOptions]);const handleSaveAsTemplate = useCallback(() => {- if (!memo) {+ if (!memo || effectiveReadOnly) {return;}@@ -2928,7 +2963,7 @@ const RichEditorPane = ({contentMarkdown: currentMarkdown,};void onSaveAsTemplate(currentTemplateMemo, name.trim());- }, [editor, getMobilePlainTextValue, memo, onSaveAsTemplate, t, tagsText, title, useMobilePlainTextEditor]);+ }, [editor, effectiveReadOnly, getMobilePlainTextValue, memo, onSaveAsTemplate, t, tagsText, title, useMobilePlainTextEditor]);useEffect(() => {if (@@ -2944,7 +2979,7 @@ const RichEditorPane = ({switch (documentActionRequest.action) {case "share":- setShareOpen(true);+ if (!effectiveReadOnly) setShareOpen(true);break;case "export-markdown":handleExportMarkdown();@@ -2965,6 +3000,7 @@ const RichEditorPane = ({}, [documentActionRequest,editor,+ effectiveReadOnly,handleExportHtml,handleExportMarkdown,handleExportPdf,@@ -4012,6 +4048,7 @@ const RichEditorPane = ({type="button"title={t("sharing.manage")}aria-label={t("sharing.manage")}+ disabled={effectiveReadOnly}onClick={() => setShareOpen(true)}><Link2 className="h-3.5 w-3.5" aria-hidden="true" />@@ -4124,7 +4161,7 @@ const RichEditorPane = ({<Search className="h-5 w-5" strokeWidth={2.25} /></Button></IconTooltip>- {!readOnly && (+ {!effectiveReadOnly && (<IconTooltip label={`${t("aiAssistant.open")} (${formatShortcutBinding(shortcutSettings.openAiAssistant)})`}><Button className="hidden h-8 w-8 text-emerald-600 transition-colors hover:bg-emerald-50 hover:text-emerald-800 focus-visible:ring-2 focus-visible:ring-emerald-300 sm:inline-flex" size="icon" variant="ghost" aria-label={t("aiAssistant.open")} onClick={openAiAssistant}><Sparkles className="h-5 w-5" strokeWidth={2.25} />@@ -4145,7 +4182,7 @@ const RichEditorPane = ({variant="ghost"aria-label={t("editor.copyToWeChat")}onClick={() => void handleCopyToWeChat()}- disabled={!editor || effectiveReadOnly || useMobilePlainTextEditor || wechatCopyState === "copying"}+ disabled={!editor || useMobilePlainTextEditor || wechatCopyState === "copying"}>{wechatCopyState === "copying" ? (<LoaderCircle className="h-4 w-4 animate-spin" />@@ -4177,7 +4214,7 @@ const RichEditorPane = ({</IconTooltip><PluginToolbarMenu host={pluginHost} onManage={onOpenPluginManager} /><ThemeToggle />- {!readOnly && (+ {!effectiveReadOnly && (<IconTooltip label={t("editor.save")}><ButtonclassName="hidden sm:inline-flex"@@ -4204,7 +4241,7 @@ const RichEditorPane = ({</Button></DropdownMenuTrigger><DropdownMenuContent align="end" className="w-44 bg-white border border-slate-200 rounded-md py-1 shadow-md">- {!readOnly && (+ {!effectiveReadOnly && (<DropdownMenuItemclassName="flex h-9 w-full items-center gap-2 px-3 text-left text-sm text-emerald-700 hover:bg-emerald-50 cursor-pointer outline-none"onClick={openAiAssistant}@@ -4231,6 +4268,7 @@ const RichEditorPane = ({<DropdownMenuItemclassName="flex h-9 w-full items-center gap-2 px-3 text-left text-sm text-slate-700 hover:bg-slate-50 cursor-pointer outline-none"onClick={openNoteReplace}+ disabled={effectiveReadOnly}><ReplaceAll className="h-4 w-4 text-slate-500" />{t("editor.replaceCurrentMemo")}@@ -4244,7 +4282,7 @@ const RichEditorPane = ({<History className="h-4 w-4 text-slate-500" />{t("editor.versionHistory")}</DropdownMenuItem>- {!readOnly && (+ {!effectiveReadOnly && (<DropdownMenuItemclassName={cn("flex h-9 w-full items-center gap-2 px-3 text-left text-sm hover:bg-slate-50 cursor-pointer outline-none",@@ -4308,6 +4346,7 @@ const RichEditorPane = ({<DropdownMenuItemclassName="flex h-9 w-full items-center gap-2 px-3 text-left text-sm text-slate-700 hover:bg-slate-50 cursor-pointer outline-none"onClick={handleSaveAsTemplate}+ disabled={effectiveReadOnly}><Pencil className="h-4 w-4 text-slate-500" />{t("templates.saveAsTemplate")}@@ -4316,6 +4355,7 @@ const RichEditorPane = ({<DropdownMenuItemclassName="flex h-9 w-full items-center gap-2 px-3 text-left text-sm text-rose-700 hover:bg-rose-50 cursor-pointer outline-none"onClick={() => void onDeleted(memo.id)}+ disabled={effectiveReadOnly}><Trash2 className="h-4 w-4" />{t("editor.deleteMemo")}@@ -4381,6 +4421,23 @@ const RichEditorPane = ({markDirty();}}/>+ {!readOnly && (+ <IconTooltip label={t(desktopReadingProtection ? "editor.disableReadingProtection" : "editor.enableReadingProtection")}>+ <Button+ className={cn(+ "hidden shrink-0 sm:inline-flex",+ desktopReadingProtection && "bg-emerald-100 text-emerald-800 ring-1 ring-inset ring-emerald-300 hover:bg-emerald-100 hover:text-emerald-900"+ )}+ size="icon"+ variant={desktopReadingProtection ? "soft" : "ghost"}+ aria-label={t(desktopReadingProtection ? "editor.disableReadingProtection" : "editor.enableReadingProtection")}+ aria-pressed={desktopReadingProtection}+ onClick={toggleDesktopReadingProtection}+ >+ {desktopReadingProtection ? <Lock className="h-4 w-4" /> : <LockOpen className="h-4 w-4" />}+ </Button>+ </IconTooltip>+ )}</div></div>{noteSearchOpen && (diff --git a/apps/web/src/components/editor/NoteLinkSuggestion.tsx b/apps/web/src/components/editor/NoteLinkSuggestion.tsxindex 9595bc86..a6d25b07 100644--- a/apps/web/src/components/editor/NoteLinkSuggestion.tsx+++ b/apps/web/src/components/editor/NoteLinkSuggestion.tsx@@ -11,6 +11,7 @@ import {CommandGroup,CommandItem,CommandList,+ COMMAND_ITEM_STRONG_SELECTED_CLASS_NAME,} from "@/components/ui/command";export type NoteLinkSuggestionLabels = {@@ -77,6 +78,7 @@ export const NoteLinkSuggestion = forwardRef<NoteLinkSuggestionHandle, NoteLinkS<CommandItemkey={item.id}value={item.id}+ className={COMMAND_ITEM_STRONG_SELECTED_CLASS_NAME}onMouseDown={(event) => event.preventDefault()}onSelect={() => command(item)}>diff --git a/apps/web/src/components/editor/SlashCommandMenu.tsx b/apps/web/src/components/editor/SlashCommandMenu.tsxindex 5e4f2eb3..8e701bd3 100644--- a/apps/web/src/components/editor/SlashCommandMenu.tsx+++ b/apps/web/src/components/editor/SlashCommandMenu.tsx@@ -29,6 +29,7 @@ import {CommandItem,CommandList,CommandShortcut,+ COMMAND_ITEM_STRONG_SELECTED_CLASS_NAME,} from "@/components/ui/command";export type SlashCommandId =@@ -179,6 +180,7 @@ export const SlashCommandMenu = forwardRef<SlashCommandMenuHandle, SlashCommandMkey={item.id}value={item.id}keywords={item.keywords}+ className={COMMAND_ITEM_STRONG_SELECTED_CLASS_NAME}onMouseDown={(event) => event.preventDefault()}onSelect={() => command(item)}>diff --git a/apps/web/src/components/ui/command.tsx b/apps/web/src/components/ui/command.tsxindex c56a865c..a28602f5 100644--- a/apps/web/src/components/ui/command.tsx+++ b/apps/web/src/components/ui/command.tsx@@ -3,6 +3,9 @@ import { Command as CommandPrimitive } from "cmdk";import { Search } from "lucide-react";import { cn } from "@/lib/utils";+const COMMAND_ITEM_STRONG_SELECTED_CLASS_NAME =+ "border-l-[3px] border-l-transparent data-[selected=true]:border-l-slate-500 data-[selected=true]:bg-slate-100 data-[selected=true]:text-slate-950 data-[selected=true]:shadow-[inset_0_0_0_1px_rgb(203_213_225)] data-[selected=true]:[&_svg]:text-slate-700";+const Command = React.forwardRef<React.ElementRef<typeof CommandPrimitive>,React.ComponentPropsWithoutRef<typeof CommandPrimitive>@@ -108,4 +111,5 @@ export {CommandItem,CommandSeparator,CommandShortcut,+ COMMAND_ITEM_STRONG_SELECTED_CLASS_NAME,};diff --git a/apps/web/src/lib/app-helpers.test.mjs b/apps/web/src/lib/app-helpers.test.mjsindex 689f4a51..dc426515 100644--- a/apps/web/src/lib/app-helpers.test.mjs+++ b/apps/web/src/lib/app-helpers.test.mjs@@ -3,6 +3,7 @@ import {DEFAULT_SHORTCUT_SETTINGS,DEFAULT_SYNC_INTERVAL_MS,DESKTOP_FOCUS_MODE_STORAGE_KEY,+ DESKTOP_READING_PROTECTION_STORAGE_KEY,EDITOR_CONTENT_ALIGNMENT_STORAGE_KEY,NOTEBOOK_SORT_STORAGE_KEY,SHORTCUT_SETTINGS_STORAGE_KEY,@@ -13,11 +14,13 @@ import {readNotebookSortPreference,readSyncIntervalPreference,readDesktopFocusModePreference,+ readDesktopReadingProtectionPreference,readShortcutSettingsPreference,writeEditorContentAlignmentPreference,writeNotebookSortPreference,writeSyncIntervalPreference,writeDesktopFocusModePreference,+ writeDesktopReadingProtectionPreference,} from "./app-helpers.ts";const originalWindow = globalThis.window;@@ -81,6 +84,45 @@ describe("desktop focus mode preference", () => {});});+describe("desktop reading protection preference", () => {+ test("defaults to editing and only accepts an explicit true value", () => {+ const values = installLocalStorage();+ expect(readDesktopReadingProtectionPreference()).toBe(false);++ values.set(DESKTOP_READING_PROTECTION_STORAGE_KEY, "false");+ expect(readDesktopReadingProtectionPreference()).toBe(false);++ values.set(DESKTOP_READING_PROTECTION_STORAGE_KEY, "true");+ expect(readDesktopReadingProtectionPreference()).toBe(true);+ });++ test("persists protected and editable modes", () => {+ const values = installLocalStorage();++ writeDesktopReadingProtectionPreference(true);+ expect(values.get(DESKTOP_READING_PROTECTION_STORAGE_KEY)).toBe("true");++ writeDesktopReadingProtectionPreference(false);+ expect(values.get(DESKTOP_READING_PROTECTION_STORAGE_KEY)).toBe("false");+ });++ test("fails open when local storage is unavailable", () => {+ globalThis.window = {+ localStorage: {+ getItem: () => {+ throw new Error("blocked");+ },+ setItem: () => {+ throw new Error("blocked");+ },+ },+ };++ expect(readDesktopReadingProtectionPreference()).toBe(false);+ expect(() => writeDesktopReadingProtectionPreference(true)).not.toThrow();+ });+});+describe("editor content alignment preference", () => {test("defaults to left aligned and persists both supported alignments", () => {const values = installLocalStorage();diff --git a/apps/web/src/lib/app-helpers.ts b/apps/web/src/lib/app-helpers.tsindex 65e69ce2..43da6d63 100644--- a/apps/web/src/lib/app-helpers.ts+++ b/apps/web/src/lib/app-helpers.ts@@ -123,6 +123,7 @@ export const IMAGE_COMPRESSION_STORAGE_KEY = "edgeever.imageCompressionEnabled";export const SYNC_INTERVAL_STORAGE_KEY = "edgeever.syncInterval";const LEGACY_AUTO_SAVE_INTERVAL_STORAGE_KEY = "edgeever.autoSaveInterval";export const DESKTOP_FOCUS_MODE_STORAGE_KEY = "edgeever.desktopFocusMode";+export const DESKTOP_READING_PROTECTION_STORAGE_KEY = "edgeever.desktopReadingProtection";export const EDITOR_CONTENT_ALIGNMENT_STORAGE_KEY = "edgeever.editorContentAlignment";export const MEMO_LIST_DENSITY_STORAGE_KEY = "edgeever.memoListDensity";export const MEMO_LIST_WIDTH_STORAGE_KEY = "edgeever.memoListWidth";@@ -317,6 +318,22 @@ export const writeDesktopFocusModePreference = (enabled: boolean) => {}};+export const readDesktopReadingProtectionPreference = () => {+ try {+ return window.localStorage.getItem(DESKTOP_READING_PROTECTION_STORAGE_KEY) === "true";+ } catch {+ return false;+ }+};++export const writeDesktopReadingProtectionPreference = (enabled: boolean) => {+ try {+ window.localStorage.setItem(DESKTOP_READING_PROTECTION_STORAGE_KEY, enabled ? "true" : "false");+ } catch {+ // Local storage can be unavailable in private or restricted browser contexts.+ }+};+export const readEditorContentAlignmentPreference = (): EditorContentAlignment => {try {return window.localStorage.getItem(EDITOR_CONTENT_ALIGNMENT_STORAGE_KEY) === "center" ? "center" : "start";diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.tsindex 397d33b1..ff53af93 100644--- a/packages/shared/src/i18n/en-US.ts+++ b/packages/shared/src/i18n/en-US.ts@@ -991,6 +991,8 @@ export const enUS = {focusMode: "Focus mode",enterFocusMode: "Enter focus mode",exitFocusMode: "Exit focus mode",+ enableReadingProtection: "Enable reading protection",+ disableReadingProtection: "Disable reading protection and edit",updatedAt: "Updated {{time}}",done: "Done",uploadAttachment: "Upload attachment",diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.tsindex e2a4368a..8c9880ee 100644--- a/packages/shared/src/i18n/zh-CN.ts+++ b/packages/shared/src/i18n/zh-CN.ts@@ -991,6 +991,8 @@ export const zhCN = {focusMode: "专注模式",enterFocusMode: "进入专注模式",exitFocusMode: "退出专注模式",+ enableReadingProtection: "开启阅读保护",+ disableReadingProtection: "关闭阅读保护并进入编辑",updatedAt: "更新于 {{time}}",done: "完成",uploadAttachment: "上传附件",