Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

feat(editor): add collapsible toolbar (#349)

tianma-ife0609ca12088
diff --git a/apps/web/src/components/EditorToolbar.tsx b/apps/web/src/components/EditorToolbar.tsx
index 613af869..a7c5894a 100644
--- a/apps/web/src/components/EditorToolbar.tsx
+++ b/apps/web/src/components/EditorToolbar.tsx
@@ -1,4 +1,4 @@
-import type { ReactNode } from "react";
+import { useEffect, useRef, useState, type ReactNode } from "react";
import type { Editor } from "@tiptap/react";
import { useTranslation } from "react-i18next";
import {
@@ -18,11 +18,19 @@ import {
Paperclip,
Link,
Link2,
+ ChevronDown,
+ ChevronUp,
} from "lucide-react";
import { cn } from "@/lib/utils";
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "@/components/ui/select";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
-import { formatShortcutBinding, getActiveBlockValue, type ShortcutBinding } from "@/lib/app-helpers";
+import {
+ formatShortcutBinding,
+ getActiveBlockValue,
+ readEditorToolbarExpandedPreference,
+ writeEditorToolbarExpandedPreference,
+ type ShortcutBinding,
+} from "@/lib/app-helpers";
import { CODE_BLOCK_LANGUAGES, getCodeBlockLanguageValue } from "@/lib/code-block";
import { EditorTableMenu } from "@/components/EditorTableMenu";
import { wrapIndentedParagraphInList } from "@/lib/editor-shortcuts";
@@ -152,6 +160,9 @@ export const EditorToolbar = ({
externalLinkActive?: boolean;
}) => {
const { t } = useTranslation();
+ const controlsRef = useRef<HTMLDivElement>(null);
+ const [expanded, setExpanded] = useState(readEditorToolbarExpandedPreference);
+ const [hasOverflow, setHasOverflow] = useState(false);
const markdownModeShortcutLabel = markdownModeShortcut ? formatShortcutBinding(markdownModeShortcut) : null;
const editorReady = isToolbarEditorReady(editor);
const disabled = readOnly || !editorReady;
@@ -173,6 +184,50 @@ export const EditorToolbar = ({
? getCodeBlockLanguageValue(editor.getAttributes("codeBlock").language)
: "plaintext";
+ useEffect(() => {
+ const controls = controlsRef.current;
+ if (!controls) return;
+
+ const updateOverflow = () => {
+ const style = window.getComputedStyle(controls);
+ const gap = Number.parseFloat(style.columnGap) || 0;
+ const horizontalPadding = (Number.parseFloat(style.paddingLeft) || 0) * 2;
+ const visibleItems = Array.from(controls.children).filter(
+ (child): child is HTMLElement => child instanceof HTMLElement && child.offsetWidth > 0
+ );
+ const requiredWidth = visibleItems.reduce((width, item) => width + item.offsetWidth, 0)
+ + Math.max(0, visibleItems.length - 1) * gap;
+ const availableWidth = Math.max(0, controls.clientWidth - horizontalPadding);
+ const next = requiredWidth > availableWidth + 1;
+ const firstRowTop = Math.min(...visibleItems.map((item) => item.offsetTop));
+
+ visibleItems.forEach((item) => {
+ item.inert = !expanded && next && item.offsetTop > firstRowTop + 1;
+ });
+
+ setHasOverflow((current) => {
+ return current === next ? current : next;
+ });
+ };
+
+ updateOverflow();
+ const observer = new ResizeObserver(updateOverflow);
+ observer.observe(controls);
+ Array.from(controls.children).forEach((child) => observer.observe(child));
+ return () => {
+ observer.disconnect();
+ Array.from(controls.children).forEach((child) => {
+ if (child instanceof HTMLElement) child.inert = false;
+ });
+ };
+ });
+
+ const toggleExpanded = () => {
+ const next = !expanded;
+ setExpanded(next);
+ writeEditorToolbarExpandedPreference(next);
+ };
+
const canRun = (command: (editor: Editor) => boolean) => {
if (!isToolbarEditorReady(editor) || readOnly) {
return false;
@@ -224,13 +279,19 @@ export const EditorToolbar = ({
return (
<TooltipProvider delayDuration={0} skipDelayDuration={0}>
- <div className="relative min-w-0 max-w-full border-t border-slate-100 bg-white">
+ <div
+ className="relative min-w-0 max-w-full border-t border-slate-100 bg-white"
+ role="toolbar"
+ aria-label={t("editorToolbar.toolbar")}
+ >
<div className="pointer-events-none absolute inset-y-0 left-0 z-10 w-4 bg-gradient-to-r from-white to-transparent sm:hidden" />
- <div className="pointer-events-none absolute inset-y-0 right-0 z-10 w-4 bg-gradient-to-l from-white to-transparent sm:hidden" />
<div
- className="flex min-w-0 max-w-full flex-wrap items-center gap-1 overflow-visible px-3 py-2 sm:gap-2 sm:px-5"
- role="toolbar"
- aria-label={t("editorToolbar.toolbar")}
+ ref={controlsRef}
+ className={cn(
+ "flex min-w-0 max-w-full flex-wrap items-center gap-1 px-3 py-2 sm:px-5",
+ hasOverflow && "pr-14 sm:pr-16",
+ !expanded && "max-h-12 overflow-hidden"
+ )}
>
{onMarkdownModeChange && (
<>
@@ -459,6 +520,27 @@ export const EditorToolbar = ({
</>
)}
</div>
+ {hasOverflow && (
+ <div className="absolute right-3 top-2 z-20 flex h-8 items-center bg-gradient-to-l from-white via-white to-transparent pl-5 sm:right-5">
+ <Tooltip>
+ <TooltipTrigger asChild>
+ <button
+ className="flex h-8 w-8 items-center justify-center rounded-md border border-slate-200 bg-white text-slate-700 shadow-sm transition hover:border-slate-300 hover:bg-slate-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-emerald-500/30"
+ type="button"
+ aria-expanded={expanded}
+ aria-label={t(expanded ? "editorToolbar.showLess" : "editorToolbar.showMore")}
+ onMouseDown={(event) => event.preventDefault()}
+ onClick={toggleExpanded}
+ >
+ {expanded ? <ChevronUp className="h-4 w-4" /> : <ChevronDown className="h-4 w-4" />}
+ </button>
+ </TooltipTrigger>
+ <TooltipContent side="bottom">
+ {t(expanded ? "editorToolbar.showLess" : "editorToolbar.showMore")}
+ </TooltipContent>
+ </Tooltip>
+ </div>
+ )}
</div>
</TooltipProvider>
);
diff --git a/apps/web/src/lib/app-helpers.test.mjs b/apps/web/src/lib/app-helpers.test.mjs
index ff244c09..e1a1fff2 100644
--- a/apps/web/src/lib/app-helpers.test.mjs
+++ b/apps/web/src/lib/app-helpers.test.mjs
@@ -5,6 +5,7 @@ import {
DESKTOP_READING_PROTECTION_STORAGE_KEY,
EDITOR_OUTLINE_COLLAPSED_STORAGE_KEY,
EDITOR_CONTENT_ALIGNMENT_STORAGE_KEY,
+ EDITOR_TOOLBAR_EXPANDED_STORAGE_KEY,
NOTEBOOK_SORT_STORAGE_KEY,
SHORTCUT_SETTINGS_STORAGE_KEY,
getSearchShortcutScope,
@@ -15,12 +16,14 @@ import {
readDesktopFocusModePreference,
readDesktopReadingProtectionPreference,
readEditorOutlineCollapsedPreference,
+ readEditorToolbarExpandedPreference,
readShortcutSettingsPreference,
writeEditorContentAlignmentPreference,
writeNotebookSortPreference,
writeDesktopFocusModePreference,
writeDesktopReadingProtectionPreference,
writeEditorOutlineCollapsedPreference,
+ writeEditorToolbarExpandedPreference,
} from "./app-helpers.ts";
const originalWindow = globalThis.window;
@@ -94,6 +97,29 @@ describe("desktop focus mode preference", () => {
});
});
+describe("editor toolbar expanded preference", () => {
+ test("defaults to collapsed and only accepts an explicit true value", () => {
+ const values = installLocalStorage();
+ expect(readEditorToolbarExpandedPreference()).toBe(false);
+
+ values.set(EDITOR_TOOLBAR_EXPANDED_STORAGE_KEY, "false");
+ expect(readEditorToolbarExpandedPreference()).toBe(false);
+
+ values.set(EDITOR_TOOLBAR_EXPANDED_STORAGE_KEY, "true");
+ expect(readEditorToolbarExpandedPreference()).toBe(true);
+ });
+
+ test("persists expanded and collapsed values", () => {
+ const values = installLocalStorage();
+
+ writeEditorToolbarExpandedPreference(true);
+ expect(values.get(EDITOR_TOOLBAR_EXPANDED_STORAGE_KEY)).toBe("true");
+
+ writeEditorToolbarExpandedPreference(false);
+ expect(values.get(EDITOR_TOOLBAR_EXPANDED_STORAGE_KEY)).toBe("false");
+ });
+});
+
describe("desktop reading protection preference", () => {
test("defaults to editing and only accepts an explicit true value", () => {
const values = installLocalStorage();
diff --git a/apps/web/src/lib/app-helpers.ts b/apps/web/src/lib/app-helpers.ts
index 03421589..29c4d55a 100644
--- a/apps/web/src/lib/app-helpers.ts
+++ b/apps/web/src/lib/app-helpers.ts
@@ -133,6 +133,7 @@ export const DESKTOP_FOCUS_MODE_STORAGE_KEY = "edgeever.desktopFocusMode";
export const DESKTOP_READING_PROTECTION_STORAGE_KEY = "edgeever.desktopReadingProtection";
export const EDITOR_OUTLINE_COLLAPSED_STORAGE_KEY = "edgeever.editorOutlineCollapsed";
export const EDITOR_CONTENT_ALIGNMENT_STORAGE_KEY = "edgeever.editorContentAlignment";
+export const EDITOR_TOOLBAR_EXPANDED_STORAGE_KEY = "edgeever.editorToolbarExpanded";
export const MEMO_LIST_DENSITY_STORAGE_KEY = "edgeever.memoListDensity";
export const MEMO_LIST_WIDTH_STORAGE_KEY = "edgeever.memoListWidth";
export const NOTEBOOK_SORT_STORAGE_KEY = "edgeever.notebookSort";
@@ -388,6 +389,22 @@ export const writeEditorContentAlignmentPreference = (alignment: EditorContentAl
}
};
+export const readEditorToolbarExpandedPreference = () => {
+ try {
+ return window.localStorage.getItem(EDITOR_TOOLBAR_EXPANDED_STORAGE_KEY) === "true";
+ } catch {
+ return false;
+ }
+};
+
+export const writeEditorToolbarExpandedPreference = (expanded: boolean) => {
+ try {
+ window.localStorage.setItem(EDITOR_TOOLBAR_EXPANDED_STORAGE_KEY, expanded ? "true" : "false");
+ } catch {
+ // Local storage can be unavailable in private or restricted browser contexts.
+ }
+};
+
export const readMemoListDensityPreference = (): MemoListDensity => {
try {
const density = window.localStorage.getItem(MEMO_LIST_DENSITY_STORAGE_KEY);
diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.ts
index 36dbfe5d..b3873b74 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -1617,6 +1617,8 @@ export const enUS = {
},
editorToolbar: {
toolbar: "Editor toolbar",
+ showMore: "Show more editing tools",
+ showLess: "Collapse editor toolbar",
paragraph: "Body",
heading1: "Heading 1",
heading2: "Heading 2",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index 7b664af3..d6b240c5 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -1615,6 +1615,8 @@ export const zhCN = {
},
editorToolbar: {
toolbar: "编辑器工具栏",
+ showMore: "显示更多编辑工具",
+ showLess: "收起编辑器工具栏",
paragraph: "正文",
heading1: "标题 1",
heading2: "标题 2",