Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

refactor(editor): keep <Attachment> image rendering as a pure port of the original ImageView (#2857)

Naiyuan Qingd46e90ee0a07
diff --git a/packages/views/editor/attachment.tsx b/packages/views/editor/attachment.tsx
index 48ab4b59f9..cfad8dd0a1 100644
--- a/packages/views/editor/attachment.tsx
+++ b/packages/views/editor/attachment.tsx
@@ -215,10 +215,12 @@ export function Attachment({
// ImageAttachmentView — inline image with hover toolbar
// ---------------------------------------------------------------------------
//
-// Self-contained Tailwind: works inside the editor surface (where the legacy
-// `.rich-text-editor .image-figure` CSS in content-editor.css continues to
-// apply for backward compatibility) AND in standalone surfaces (chat
-// messages, comment-card AttachmentList) that don't carry that scope.
+// DOM and styling are intentionally a direct port of the original
+// extensions/image-view.tsx <figure> structure. All visual styles live in
+// content-editor.css under `.image-figure / .image-content / .image-toolbar`
+// — the unification step de-scoped those rules from `.rich-text-editor` so
+// standalone surfaces (chat messages, AttachmentList) get identical visuals
+// without each component carrying its own Tailwind tax.
interface ImageAttachmentViewProps {
src: string;
@@ -254,83 +256,52 @@ function ImageAttachmentView({
}
};
- // Click on figure opens the preview only in non-editor surfaces — inside
- // the editor we let ProseMirror own the click for selection / cursor
- // placement and route preview through the explicit Maximize button.
- const figureOnClick = !editable && !uploading ? onView : undefined;
+ // Click on figure opens the preview only in non-editor / non-uploading
+ // surfaces — inside the editor we let ProseMirror own the click for
+ // selection / cursor placement and route preview through the explicit
+ // Maximize button. The CSS rule `.image-figure[data-clickable="true"] {
+ // cursor: zoom-in }` keys off this same flag for the cursor affordance.
+ const clickable = !editable && !uploading;
+ // DOM mirrors the original ReadonlyImage (span-only chain so it stays
+ // valid HTML5 when rendered inside a markdown <p>). In editor surfaces
+ // the NodeViewWrapper still emits its own outer .image-node div around
+ // this — the duplicate `image-node` class is harmless.
return (
- <span
- className={cn(
- "image-node group/image relative inline-block max-w-full",
- className,
- )}
- >
+ <span className="image-node">
<span
className={cn(
- "image-figure relative inline-block max-w-full rounded-md transition-shadow",
- selected && editable && "image-selected ring-2 ring-primary",
- !editable && !uploading && "cursor-zoom-in",
+ "image-figure",
+ selected && editable && "image-selected",
+ className,
)}
- onClick={figureOnClick}
+ data-clickable={clickable || undefined}
+ contentEditable={false}
+ onClick={clickable ? onView : undefined}
>
- {src ? (
- <img
- src={src}
- alt={alt}
- className={cn(
- "image-content block max-w-full rounded-md",
- uploading && "image-uploading opacity-60",
- )}
- draggable={false}
- />
- ) : (
- // Defensive: an image input without a URL is degenerate, but
- // emitting nothing leaves no anchor for the toolbar. Render a
- // small placeholder so the surface is still recognizable.
- <span className="block h-20 w-32 rounded-md bg-muted" />
- )}
+ <img
+ src={src || undefined}
+ alt={alt}
+ className={cn("image-content", uploading && "image-uploading")}
+ draggable={false}
+ />
{!uploading && src && (
<span
- className="image-toolbar absolute right-2 top-2 flex items-center gap-0.5 rounded-md border border-border bg-background/95 p-0.5 opacity-0 shadow-sm transition-opacity group-hover/image:opacity-100"
+ className="image-toolbar"
onMouseDown={(e) => e.stopPropagation()}
onClick={(e) => e.stopPropagation()}
>
- <button
- type="button"
- className="flex h-6 w-6 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
- title={t(($) => $.image.view)}
- aria-label={t(($) => $.image.view)}
- onClick={onView}
- >
+ <button type="button" onClick={onView} title={t(($) => $.image.view)}>
<Maximize2 className="size-3.5" />
</button>
- <button
- type="button"
- className="flex h-6 w-6 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
- title={t(($) => $.image.download)}
- aria-label={t(($) => $.image.download)}
- onClick={onDownload}
- >
+ <button type="button" onClick={onDownload} title={t(($) => $.image.download)}>
<Download className="size-3.5" />
</button>
- <button
- type="button"
- className="flex h-6 w-6 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
- title={t(($) => $.image.copy_link)}
- aria-label={t(($) => $.image.copy_link)}
- onClick={handleCopyLink}
- >
+ <button type="button" onClick={handleCopyLink} title={t(($) => $.image.copy_link)}>
<LinkIcon className="size-3.5" />
</button>
{editable && onDelete && (
- <button
- type="button"
- className="flex h-6 w-6 items-center justify-center rounded text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
- title={t(($) => $.image.delete)}
- aria-label={t(($) => $.image.delete)}
- onClick={onDelete}
- >
+ <button type="button" onClick={onDelete} title={t(($) => $.image.delete)}>
<Trash2 className="size-3.5" />
</button>
)}
diff --git a/packages/views/editor/content-editor.css b/packages/views/editor/content-editor.css
index bcc9ee36a7..68349d37c4 100644
--- a/packages/views/editor/content-editor.css
+++ b/packages/views/editor/content-editor.css
@@ -502,32 +502,34 @@
margin: 0.5rem 0;
}
-/* Image NodeView — centered block with max-width cap */
-.rich-text-editor .image-node {
- display: block !important;
- text-align: center;
-}
+/* Image — shared by the editor NodeView AND standalone surfaces (chat
+ * messages, comment-card AttachmentList). De-scoped from `.rich-text-editor`
+ * during the <Attachment> unification so every call site renders identical
+ * visuals without each component carrying its own Tailwind override. */
-.rich-text-editor .image-figure {
+.image-figure {
position: relative;
display: inline-block;
- max-width: min(100%, 640px);
margin: 0.75rem 0;
}
-.rich-text-editor .image-figure.image-selected .image-content {
+.image-figure[data-clickable="true"] {
+ cursor: zoom-in;
+}
+
+.image-figure.image-selected .image-content {
outline: 2px solid var(--brand);
outline-offset: 2px;
}
-.rich-text-editor .image-content {
+.image-content {
display: block;
width: 100%;
height: auto;
border-radius: var(--radius);
}
-.rich-text-editor .image-uploading {
+.image-uploading {
opacity: 0.5;
animation: rte-upload-pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite;
}
@@ -537,13 +539,7 @@
50% { opacity: 0.3; }
}
-/* Readonly — zoom cursor on clickable images */
-.rich-text-editor.readonly .image-figure {
- cursor: zoom-in;
-}
-
-/* Image toolbar — dark pill, top-right corner, appears on hover */
-.rich-text-editor .image-toolbar {
+.image-toolbar {
position: absolute;
top: 0.5rem;
right: 0.5rem;
@@ -562,7 +558,7 @@
opacity: 1;
}
-.rich-text-editor .image-toolbar button {
+.image-toolbar button {
display: flex;
align-items: center;
justify-content: center;
@@ -573,10 +569,22 @@
transition: background 0.15s;
}
-.rich-text-editor .image-toolbar button:hover {
+.image-toolbar button:hover {
background: color-mix(in srgb, white 15%, transparent);
}
+/* Editor-only overrides: center the figure inside the NodeView and cap
+ * width so an oversize paste doesn't blow out the editor column. */
+
+.rich-text-editor .image-node {
+ display: block !important;
+ text-align: center;
+}
+
+.rich-text-editor .image-figure {
+ max-width: min(100%, 640px);
+}
+
/* Bubble menu — floating toolbar pill */
.bubble-menu {
display: flex;
diff --git a/packages/views/editor/extensions/image-view.tsx b/packages/views/editor/extensions/image-view.tsx
index 3ee38ad9b1..0df143e072 100644
--- a/packages/views/editor/extensions/image-view.tsx
+++ b/packages/views/editor/extensions/image-view.tsx
@@ -17,8 +17,10 @@ function ImageView({ node, editor, selected, deleteNode }: NodeViewProps) {
const alt = (node.attrs.alt as string) || "";
const uploading = node.attrs.uploading as boolean;
+ // <Attachment> emits its own .image-node wrapper, so the NodeViewWrapper
+ // stays unclassed — no double image-node.
return (
- <NodeViewWrapper className="image-node">
+ <NodeViewWrapper>
<Attachment
attachment={{
kind: "url",