Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
feat(editor): add link hover card with URL preview and actions
Naiyuan Qing97755ae45dbb
diff --git a/packages/views/editor/content-editor.css b/packages/views/editor/content-editor.cssindex ebd28948d3..4ef1d8df2e 100644--- a/packages/views/editor/content-editor.css+++ b/packages/views/editor/content-editor.css@@ -489,4 +489,20 @@min-width: 300px;}+/* Link hover card — shows URL + actions on link hover */+.link-hover-card {+ display: flex;+ align-items: center;+ gap: 0.25rem;+ padding: 0.25rem 0.25rem 0.25rem 0.5rem;+ background: var(--popover);+ border: 1px solid color-mix(in srgb, var(--foreground) 10%, transparent);+ border-radius: var(--radius);+ box-shadow:+ 0 4px 12px color-mix(in srgb, black 12%, transparent),+ 0 0 0 1px color-mix(in srgb, black 4%, transparent);+ max-width: min(360px, calc(100vw - 2rem));+ white-space: nowrap;+}+diff --git a/packages/views/editor/content-editor.tsx b/packages/views/editor/content-editor.tsxindex 9052438604..b3dc00fbcb 100644--- a/packages/views/editor/content-editor.tsx+++ b/packages/views/editor/content-editor.tsx@@ -40,6 +40,7 @@ import { createEditorExtensions } from "./extensions";import { uploadAndInsertFile } from "./extensions/file-upload";import { preprocessMarkdown } from "./utils/preprocess";import { EditorBubbleMenu } from "./bubble-menu";+import { useLinkHover, LinkHoverCard } from "./link-hover-card";import "./content-editor.css";// ---------------------------------------------------------------------------@@ -214,6 +215,11 @@ const ContentEditor = forwardRef<ContentEditorRef, ContentEditorProps>(},}));+ // Link hover card — disabled when BubbleMenu is active (has selection)+ const wrapperRef = useRef<HTMLDivElement>(null);+ const hoverDisabled = !editor?.state.selection.empty;+ const hover = useLinkHover(wrapperRef, hoverDisabled);+const handleContainerMouseDown = (event: ReactMouseEvent<HTMLDivElement>) => {if (!editable || !editor) return;@@ -229,15 +235,13 @@ const ContentEditor = forwardRef<ContentEditorRef, ContentEditorProps>(return (<div+ ref={wrapperRef}className="relative flex min-h-full flex-col"onMouseDown={handleContainerMouseDown}><EditorContent className="flex-1 min-h-full" editor={editor} />- {editable && (- <>- <EditorBubbleMenu editor={editor} />- </>- )}+ {editable && <EditorBubbleMenu editor={editor} />}+ <LinkHoverCard {...hover} /></div>);},diff --git a/packages/views/editor/link-hover-card.tsx b/packages/views/editor/link-hover-card.tsxnew file mode 100644index 0000000000..a129a804d9--- /dev/null+++ b/packages/views/editor/link-hover-card.tsx@@ -0,0 +1,236 @@+"use client";++/**+ * LinkHoverCard — floating card shown on link hover.+ *+ * Displays the URL with Copy and Open actions. Portaled to body+ * with position:fixed to escape overflow:hidden containers.+ * Shows after 300ms hover delay, hides after 150ms mouse-out+ * (cancelled if mouse enters the card).+ */++import { useState, useEffect, useCallback, useRef } from "react";+import { createPortal } from "react-dom";+import { computePosition, offset, flip, shift } from "@floating-ui/dom";+import { ExternalLink, Copy } from "lucide-react";+import { toast } from "sonner";+import { Button } from "@multica/ui/components/ui/button";++// ---------------------------------------------------------------------------+// Helpers+// ---------------------------------------------------------------------------++function openLink(href: string) {+ if (href.startsWith("/")) {+ window.dispatchEvent(+ new CustomEvent("multica:navigate", { detail: { path: href } }),+ );+ } else {+ window.open(href, "_blank", "noopener,noreferrer");+ }+}++function truncateUrl(url: string, max = 48): string {+ if (url.length <= max) return url;+ try {+ const u = new URL(url);+ const origin = u.origin;+ const rest = url.slice(origin.length);+ if (rest.length <= 10) return url;+ return `${origin}${rest.slice(0, max - origin.length - 1)}…`;+ } catch {+ return `${url.slice(0, max - 1)}…`;+ }+}++// ---------------------------------------------------------------------------+// Hook — manages hover state with enter/leave delays+// ---------------------------------------------------------------------------++const SHOW_DELAY = 300;+const HIDE_DELAY = 150;++interface HoverState {+ visible: boolean;+ href: string;+ anchorEl: HTMLAnchorElement | null;+}++function useLinkHover(containerRef: React.RefObject<HTMLElement | null>, disabled?: boolean) {+ const [state, setState] = useState<HoverState>({ visible: false, href: "", anchorEl: null });+ const showTimer = useRef(0);+ const hideTimer = useRef(0);+ const cardRef = useRef<HTMLDivElement>(null);++ const clearTimers = useCallback(() => {+ clearTimeout(showTimer.current);+ clearTimeout(hideTimer.current);+ }, []);++ // Container mouse events — detect <a> hover+ useEffect(() => {+ const container = containerRef.current;+ if (!container || disabled) return;++ const onMouseOver = (e: MouseEvent) => {+ const target = e.target as HTMLElement;+ const link = target.closest("a") as HTMLAnchorElement | null;+ if (!link) return;+ const href = link.getAttribute("href");+ if (!href || href.startsWith("mention://")) return;++ clearTimeout(hideTimer.current);+ showTimer.current = window.setTimeout(() => {+ setState({ visible: true, href, anchorEl: link });+ }, SHOW_DELAY);+ };++ const onMouseOut = (e: MouseEvent) => {+ const related = e.relatedTarget as HTMLElement | null;+ // Don't hide if mouse moved to the hover card+ if (related && cardRef.current?.contains(related)) return;+ // Don't hide if mouse moved to another part of the same link+ const link = (e.target as HTMLElement).closest("a");+ if (link && link.contains(related)) return;++ clearTimeout(showTimer.current);+ hideTimer.current = window.setTimeout(() => {+ setState((s) => ({ ...s, visible: false }));+ }, HIDE_DELAY);+ };++ container.addEventListener("mouseover", onMouseOver);+ container.addEventListener("mouseout", onMouseOut);+ return () => {+ container.removeEventListener("mouseover", onMouseOver);+ container.removeEventListener("mouseout", onMouseOut);+ clearTimers();+ };+ }, [containerRef, disabled, clearTimers]);++ // Card mouse events — keep visible while hovering the card+ const onCardEnter = useCallback(() => {+ clearTimeout(hideTimer.current);+ }, []);++ const onCardLeave = useCallback(() => {+ hideTimer.current = window.setTimeout(() => {+ setState((s) => ({ ...s, visible: false }));+ }, HIDE_DELAY);+ }, []);++ return { ...state, cardRef, onCardEnter, onCardLeave };+}++// ---------------------------------------------------------------------------+// Component+// ---------------------------------------------------------------------------++function LinkHoverCard({+ visible,+ href,+ anchorEl,+ cardRef,+ onCardEnter,+ onCardLeave,+}: {+ visible: boolean;+ href: string;+ anchorEl: HTMLAnchorElement | null;+ cardRef: React.RefObject<HTMLDivElement | null>;+ onCardEnter: () => void;+ onCardLeave: () => void;+}) {+ const [pos, setPos] = useState({ top: 0, left: 0 });+ const [positioned, setPositioned] = useState(false);++ // Position the card when the portal div is mounted (ref callback).+ // Using useEffect would race with portal rendering — the div might+ // not be in the DOM yet when the effect runs.+ const setCardRef = useCallback(+ (node: HTMLDivElement | null) => {+ (cardRef as React.MutableRefObject<HTMLDivElement | null>).current = node;+ if (!node || !anchorEl) {+ setPositioned(false);+ return;+ }+ computePosition(anchorEl, node, {+ placement: "bottom-start",+ strategy: "fixed",+ middleware: [offset(4), flip(), shift({ padding: 8 })],+ }).then(({ x, y }) => {+ setPos({ top: y, left: x });+ setPositioned(true);+ });+ },+ [anchorEl, cardRef],+ );++ // Reset positioned when hidden+ useEffect(() => {+ if (!visible) setPositioned(false);+ }, [visible]);++ if (!visible || !anchorEl) return null;++ const handleCopy = async (e: React.MouseEvent) => {+ e.stopPropagation();+ e.preventDefault();+ try {+ await navigator.clipboard.writeText(href);+ toast.success("Link copied");+ } catch {+ toast.error("Failed to copy");+ }+ };++ const handleOpen = (e: React.MouseEvent) => {+ e.stopPropagation();+ e.preventDefault();+ openLink(href);+ };++ return createPortal(+ <div+ ref={setCardRef}+ className="link-hover-card"+ style={{+ position: "fixed",+ top: pos.top,+ left: pos.left,+ zIndex: 50,+ display: positioned ? undefined : "none",+ }}+ onMouseEnter={onCardEnter}+ onMouseLeave={onCardLeave}+ >+ <span+ className="min-w-0 flex-1 truncate text-xs text-muted-foreground px-1"+ title={href}+ >+ {truncateUrl(href)}+ </span>+ <Button+ size="icon-xs"+ variant="ghost"+ className="text-muted-foreground"+ onClick={handleCopy}+ title="Copy link"+ >+ <Copy className="size-3.5" />+ </Button>+ <Button+ size="icon-xs"+ variant="ghost"+ className="text-muted-foreground"+ onClick={handleOpen}+ title="Open link"+ >+ <ExternalLink className="size-3.5" />+ </Button>+ </div>,+ document.body,+ );+}++export { useLinkHover, LinkHoverCard };diff --git a/packages/views/editor/readonly-content.tsx b/packages/views/editor/readonly-content.tsxindex 540769fced..43b2c045b9 100644--- a/packages/views/editor/readonly-content.tsx+++ b/packages/views/editor/readonly-content.tsx@@ -16,7 +16,7 @@* - Rendering mentions with the same IssueMentionCard component and .mention class*/-import { useMemo, useState } from "react";+import { useMemo, useRef, useState } from "react";import ReactMarkdown, {defaultUrlTransform,type Components,@@ -33,7 +33,7 @@ import { cn } from "@multica/ui/lib/utils";import { useNavigation } from "../navigation";import { IssueMentionCard } from "../issues/components/issue-mention-card";import { ImageLightbox } from "./extensions/image-view";-+import { useLinkHover, LinkHoverCard } from "./link-hover-card";import { preprocessMarkdown } from "./utils/preprocess";import "./content-editor.css";@@ -281,9 +281,11 @@ interface ReadonlyContentProps {export function ReadonlyContent({ content, className }: ReadonlyContentProps) {const processed = useMemo(() => preprocessMarkdown(content), [content]);+ const wrapperRef = useRef<HTMLDivElement>(null);+ const hover = useLinkHover(wrapperRef);return (- <div className={cn("rich-text-editor readonly text-sm", className)}>+ <div ref={wrapperRef} className={cn("rich-text-editor readonly text-sm", className)}><ReactMarkdownremarkPlugins={[[remarkGfm, { singleTilde: false }]]}rehypePlugins={[rehypeRaw, [rehypeSanitize, sanitizeSchema]]}@@ -292,6 +294,7 @@ export function ReadonlyContent({ content, className }: ReadonlyContentProps) {>{processed}</ReactMarkdown>+ <LinkHoverCard {...hover} /></div>);}