Skip to content

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

fix(web): keep attachment actions from covering in-card controls

tianma-if676a2bb92d90
diff --git a/apps/web/src/components/EditorPane.tsx b/apps/web/src/components/EditorPane.tsx
index 9c0bb37d..1340ed56 100644
--- a/apps/web/src/components/EditorPane.tsx
+++ b/apps/web/src/components/EditorPane.tsx
@@ -205,6 +205,11 @@ import {
normalizeAiSelectionReplacement,
} from "@/lib/ai-selection-replacement";
import { getAttachmentFilenameFromLabel, getAttachmentResourceId } from "@/lib/attachment-links";
+import {
+ getAttachmentHoverTarget,
+ getAttachmentLinkFromEventTarget,
+ isInsideAttachmentHoverRegion,
+} from "./editor/attachment-resource-menu";
import {
IMAGE_MENU_HIDE_EVENT,
IMAGE_MENU_SHOW_EVENT,
@@ -280,13 +285,6 @@ type AiInsertionTarget = {
position: number;
};
-const getAttachmentLinkFromEventTarget = (target: EventTarget | null) =>
- target instanceof Element
- ? target.closest<HTMLAnchorElement>(
- 'a.edgeever-attachment-link, a[href*="/api/v1/resources/"], a[href^="edgeever-resource://"]'
- )
- : null;
-
const getNoteLinkFromEventTarget = (target: EventTarget | null) =>
target instanceof Element
? target.closest<HTMLAnchorElement>('a.edgeever-note-link, a[href^="#memo="]')
@@ -1608,18 +1606,19 @@ const RichEditorPane = ({
const showAttachmentMenu = useCallback((target: EventTarget | null) => {
if (isMobileViewport) return false;
- const link = getAttachmentLinkFromEventTarget(target);
- if (!link) return false;
+ const hover = getAttachmentHoverTarget(target);
+ if (!hover) return false;
- const href = link.getAttribute("href") || "";
+ const href = hover.link.getAttribute("href") || "";
cancelResourceMenuHide();
setNoteLinkHintPosition(null);
showResourceMenu({
kind: "attachment",
+ element: hover.toolbar ?? undefined,
url: href,
- filename: getAttachmentFilenameFromLabel(link.textContent || "") || getAttachmentResourceId(href) || "attachment",
+ filename: getAttachmentFilenameFromLabel(hover.link.textContent || "") || getAttachmentResourceId(href) || "attachment",
resourceId: getAttachmentResourceId(href),
- position: getNoteLinkHintPosition(link),
+ position: hover.toolbar ? { left: 0, top: 0, placement: "above" } : getNoteLinkHintPosition(hover.link),
});
return true;
}, [cancelResourceMenuHide, isMobileViewport, showResourceMenu]);
@@ -1648,16 +1647,9 @@ const RichEditorPane = ({
}, [showAttachmentMenu, showEditorLinkOpenHint]);
const handleEditorMouseOut = useCallback((event: ReactMouseEvent<HTMLDivElement>) => {
- const attachmentLink = getAttachmentLinkFromEventTarget(event.target);
- if (attachmentLink) {
- const relatedTarget = event.relatedTarget;
- if (
- relatedTarget instanceof Node &&
- (attachmentLink.contains(relatedTarget) ||
- (relatedTarget instanceof Element && relatedTarget.closest("[data-edgeever-resource-menu]")))
- ) {
- return;
- }
+ const attachmentHover = getAttachmentHoverTarget(event.target);
+ if (attachmentHover) {
+ if (isInsideAttachmentHoverRegion(attachmentHover, event.relatedTarget)) return;
scheduleResourceMenuHide();
return;
}
diff --git a/apps/web/src/components/editor/EditorPaneChrome.tsx b/apps/web/src/components/editor/EditorPaneChrome.tsx
index 0ffc27e4..b7417bff 100644
--- a/apps/web/src/components/editor/EditorPaneChrome.tsx
+++ b/apps/web/src/components/editor/EditorPaneChrome.tsx
@@ -18,6 +18,7 @@ import { ThemeToggle } from "@/components/ThemeToggle";
import { cn } from "@/lib/utils";
import type { getNotebookMoveOptions } from "@/lib/app-helpers";
import type { ResourceMenuTarget } from "./useEditorResourceActions";
+import { attachmentResourceMenuPosition } from "./attachment-resource-menu";
import { imageResourceMenuPosition } from "./image-resource-menu-position";
export const IconTooltip = ({ label, children }: { label: string; children: ReactNode }) => (
@@ -93,10 +94,10 @@ export const ResourceActionMenu = ({
onMouseEnter: () => void;
onMouseLeave: () => void;
}) => {
- const imageElement = target.kind === "image" ? target.element : undefined;
+ const referenceElement = target.element ?? null;
const { refs, floatingStyles, isPositioned } = useFloating({
- ...imageResourceMenuPosition,
- elements: { reference: imageElement ?? null },
+ ...(target.kind === "image" ? imageResourceMenuPosition : attachmentResourceMenuPosition),
+ elements: { reference: referenceElement },
whileElementsMounted: autoUpdate,
});
@@ -107,8 +108,8 @@ export const ResourceActionMenu = ({
data-edgeever-resource-menu
role="toolbar"
aria-label={labels.download}
- className="fixed z-[110] flex items-center gap-1 rounded-lg border border-slate-200 bg-card p-1 shadow-lg"
- style={imageElement ? {
+ className="fixed z-[110] flex items-center gap-1 overflow-hidden rounded-lg border border-slate-200 bg-card p-1 shadow-lg"
+ style={referenceElement ? {
...floatingStyles,
visibility: isPositioned ? "visible" : "hidden",
} : {
diff --git a/apps/web/src/components/editor/FileAttachment.tsx b/apps/web/src/components/editor/FileAttachment.tsx
index 9efd45f0..be453ef6 100644
--- a/apps/web/src/components/editor/FileAttachment.tsx
+++ b/apps/web/src/components/editor/FileAttachment.tsx
@@ -31,7 +31,7 @@ const FileAttachmentNodeView = ({ node }: NodeViewProps) => {
return (
<NodeViewWrapper as="span" className="edgeever-file-attachment-node" contentEditable={false}>
<span className={cn("edgeever-file-viewer flex min-h-12 flex-col overflow-hidden rounded-xl border border-slate-200 bg-card shadow-sm", COMPACT_ATTACHMENT_WIDTH_CLASS)}>
- <span className="flex min-h-12 items-center gap-2 px-3">
+ <span data-edgeever-resource-toolbar className="flex min-h-12 items-center gap-2 px-3">
<a
href={resolvedUrl}
target="_blank"
diff --git a/apps/web/src/components/editor/attachment-resource-menu.test.mjs b/apps/web/src/components/editor/attachment-resource-menu.test.mjs
new file mode 100644
index 00000000..e52a6763
--- /dev/null
+++ b/apps/web/src/components/editor/attachment-resource-menu.test.mjs
@@ -0,0 +1,55 @@
+import { describe, expect, test } from "bun:test";
+import { computePosition } from "@floating-ui/react-dom";
+import { attachmentResourceMenuPosition } from "./attachment-resource-menu.ts";
+
+const menuSize = { width: 300, height: 42 };
+const viewport = { x: 0, y: 0, width: 1024, height: 768 };
+
+const positionMenu = (reference) => computePosition({}, {}, {
+ ...attachmentResourceMenuPosition,
+ platform: {
+ getElementRects: () => ({ reference, floating: { x: 0, y: 0, ...menuSize } }),
+ getClippingRect: () => viewport,
+ getDimensions: () => menuSize,
+ getOffsetParent: () => null,
+ isElement: () => false,
+ getDocumentElement: () => ({}),
+ isRTL: () => false,
+ },
+});
+
+const expectOutsideToolbar = (position, toolbar) => {
+ expect(
+ position.y + menuSize.height <= toolbar.y - 8 ||
+ position.y >= toolbar.y + toolbar.height + 8,
+ ).toBe(true);
+ expect(position.x).toBeGreaterThanOrEqual(12);
+ expect(position.x + menuSize.width).toBeLessThanOrEqual(viewport.width - 12);
+};
+
+describe("attachment resource menu positioning", () => {
+ test("keeps actions above the toolbar instead of covering open-external", async () => {
+ const toolbar = { x: 200, y: 240, width: 680, height: 48 };
+ const position = await positionMenu(toolbar);
+ expect(position.placement).toBe("top-end");
+ expect(position.y + menuSize.height).toBeLessThanOrEqual(toolbar.y - 8);
+ expectOutsideToolbar(position, toolbar);
+ });
+
+ test("flips below a toolbar near the top of the viewport", async () => {
+ const toolbar = { x: 200, y: 8, width: 680, height: 48 };
+ const position = await positionMenu(toolbar);
+ expect(position.placement.startsWith("bottom")).toBe(true);
+ expect(position.y).toBeGreaterThanOrEqual(toolbar.y + toolbar.height + 8);
+ expectOutsideToolbar(position, toolbar);
+ });
+
+ test("stays within horizontal bounds on a compact card", async () => {
+ for (const toolbar of [
+ { x: 0, y: 240, width: 320, height: 48 },
+ { x: 700, y: 240, width: 320, height: 48 },
+ ]) {
+ expectOutsideToolbar(await positionMenu(toolbar), toolbar);
+ }
+ });
+});
diff --git a/apps/web/src/components/editor/attachment-resource-menu.ts b/apps/web/src/components/editor/attachment-resource-menu.ts
new file mode 100644
index 00000000..ef064653
--- /dev/null
+++ b/apps/web/src/components/editor/attachment-resource-menu.ts
@@ -0,0 +1,67 @@
+import { flip, offset, shift, type ComputePositionConfig } from "@floating-ui/react-dom";
+
+/** File/PDF cards own in-card download and open-external controls. */
+export const ATTACHMENT_CARD_SELECTOR = ".edgeever-file-viewer, .edgeever-pdf-viewer";
+export const ATTACHMENT_TOOLBAR_SELECTOR = "[data-edgeever-resource-toolbar]";
+export const ATTACHMENT_ACTION_SELECTOR = ".pdf-viewer-action";
+const ATTACHMENT_LINK_SELECTOR =
+ 'a.edgeever-attachment-link, a[href*="/api/v1/resources/"], a[href^="edgeever-resource://"]';
+
+/** Keep rename/delete outside the toolbar so they do not cover in-card actions or their tooltips. */
+export const attachmentResourceMenuPosition: Partial<ComputePositionConfig> = {
+ placement: "top-end",
+ strategy: "fixed",
+ middleware: [
+ offset(8),
+ flip({ padding: 12 }),
+ shift({ padding: 12 }),
+ ],
+};
+
+export type AttachmentHoverTarget = {
+ link: HTMLAnchorElement;
+ card: HTMLElement | null;
+ toolbar: HTMLElement | null;
+};
+
+const isResourceAnchor = (anchor: HTMLAnchorElement) => anchor.matches(ATTACHMENT_LINK_SELECTOR);
+
+const findCardResourceLink = (card: HTMLElement): HTMLAnchorElement | null => {
+ const anchors = [...card.querySelectorAll<HTMLAnchorElement>("a[href]")];
+ const resourceAnchors = anchors.filter(isResourceAnchor);
+ return resourceAnchors.find((anchor) => !anchor.closest(ATTACHMENT_ACTION_SELECTOR))
+ ?? resourceAnchors[0]
+ ?? null;
+};
+
+export const getAttachmentHoverTarget = (target: EventTarget | null): AttachmentHoverTarget | null => {
+ if (!(target instanceof Element)) return null;
+
+ const card = target.closest<HTMLElement>(ATTACHMENT_CARD_SELECTOR);
+ if (card) {
+ const link = findCardResourceLink(card);
+ if (!link) return null;
+ return {
+ link,
+ card,
+ toolbar: card.querySelector<HTMLElement>(ATTACHMENT_TOOLBAR_SELECTOR) ?? card,
+ };
+ }
+
+ const link = target.closest<HTMLAnchorElement>(ATTACHMENT_LINK_SELECTOR);
+ if (!link || link.closest(ATTACHMENT_ACTION_SELECTOR)) return null;
+ return { link, card: null, toolbar: null };
+};
+
+export const getAttachmentLinkFromEventTarget = (target: EventTarget | null) =>
+ getAttachmentHoverTarget(target)?.link ?? null;
+
+export const isInsideAttachmentHoverRegion = (
+ origin: AttachmentHoverTarget,
+ related: EventTarget | null,
+) => {
+ if (!(related instanceof Node)) return false;
+ if (origin.card?.contains(related)) return true;
+ if (!origin.card && origin.link.contains(related)) return true;
+ return related instanceof Element && Boolean(related.closest("[data-edgeever-resource-menu]"));
+};
diff --git a/apps/web/src/components/editor/useEditorResourceActions.ts b/apps/web/src/components/editor/useEditorResourceActions.ts
index 78807154..48fca14e 100644
--- a/apps/web/src/components/editor/useEditorResourceActions.ts
+++ b/apps/web/src/components/editor/useEditorResourceActions.ts
@@ -8,6 +8,7 @@ export type ResourceMenuPosition = {
export type AttachmentMenuTarget = {
kind: "attachment";
+ element?: HTMLElement;
url: string;
filename: string;
resourceId: string | null;
diff --git a/apps/web/src/components/pdf/PdfViewer.tsx b/apps/web/src/components/pdf/PdfViewer.tsx
index 6070c62f..5183cf7c 100644
--- a/apps/web/src/components/pdf/PdfViewer.tsx
+++ b/apps/web/src/components/pdf/PdfViewer.tsx
@@ -260,7 +260,7 @@ export const PdfViewer = ({
aria-label={isFullscreen ? t("pdfViewer.fullscreenLabel", { filename: label }) : undefined}
>
<span className={cn("flex min-w-0 flex-col", isFullscreen && "mx-auto h-full w-full max-w-6xl overflow-hidden rounded-xl bg-card shadow-2xl")}>
- <span className="flex min-h-12 shrink-0 items-center gap-2 overflow-x-auto border-b border-slate-200 bg-card px-3">
+ <span data-edgeever-resource-toolbar className="flex min-h-12 shrink-0 items-center gap-2 overflow-x-auto border-b border-slate-200 bg-card px-3">
{isFullscreen && onPrevious ? (
<ButtonTooltip title={t("pdfViewer.previous")}>
<button type="button" className="pdf-viewer-action" aria-label={t("pdfViewer.previous")} onClick={onPrevious}>