All files / src/components/blogs ImageBubbleMenu.tsx

97.67% Statements 42/43
100% Branches 26/26
91.66% Functions 11/12
97.67% Lines 42/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208                                    20x 6x 6x 6x 6x 5x   5x     5x   5x                   20x 9x 9x 9x 9x 8x           8x 8x           8x 8x 3x   5x 3x     2x                 8x                   20x 3x 3x 3x 3x 2x 2x 2x 1x                 20x 1x     20x                             2x                   2x                   2x                         4x                   2x                   3x                                                                  
import type { Editor } from "@tiptap/react";
import { BubbleMenu } from "@tiptap/react/menus";
import {
	AlignLeft,
	AlignCenter,
	AlignRight,
	Maximize2,
	Minimize2,
	Type,
	Trash2,
} from "lucide-react";
import { Button } from "../ui/button";
 
interface ImageBubbleMenuProps {
	editor: Editor;
}
 
export function ImageBubbleMenu({ editor }: ImageBubbleMenuProps) {
	const setImageSize = (width: string) => {
		const { state } = editor;
		const { from } = state.selection;
		const node = state.doc.nodeAt(from);
		if (node?.type.name === "image") {
			const currentStyle = (node.attrs.style as string) || "";
			// Preserve alignment styles, replace width
			const alignMatch = currentStyle.match(
				/(float:[^;]+;[^;]*;[^;]*;?)|(display:\s*block;[^;]*;[^;]*;?)/,
			);
			const alignPart = alignMatch ? alignMatch[0] : "";
 
			editor
				.chain()
				.focus()
				.updateAttributes("image", {
					style: `width: ${width}; max-width: 100%; height: auto; ${alignPart}`.trim(),
				})
				.run();
		}
	};
 
	const setImageAlign = (align: string) => {
		const { state } = editor;
		const { from } = state.selection;
		const node = state.doc.nodeAt(from);
		if (node?.type.name === "image") {
			const currentStyle = (node.attrs.style as string) || "";
			// Preserve width if set. When floating (left/right) without an
			// explicit width, default to 50% — a 100%-width floated image has
			// no space to float around and overflows the content container
			// (reported in #352). Center alignment doesn't have this problem
			// because block+auto-margins respect the container.
			const widthMatch = currentStyle.match(/width:\s*[^;]+;?/);
			const widthPart = widthMatch
				? widthMatch[0]
				: align === "left" || align === "right"
					? "width: 50%;"
					: "";
 
			let alignStyle = "";
			if (align === "left") {
				alignStyle =
					"float: left; margin-right: 1rem; margin-bottom: 0.5rem;";
			} else if (align === "right") {
				alignStyle =
					"float: right; margin-left: 1rem; margin-bottom: 0.5rem;";
			} else {
				alignStyle =
					"display: block; margin-left: auto; margin-right: auto;";
			}
 
			// max-width: 100% is a belt-and-suspenders guard: even if a stale
			// width larger than the container was previously set inline, the
			// image can never overflow. The TipTap image extension already
			// applies max-w-full via a class, but inline styles from previous
			// sessions can and do outrank that class in some prose themes.
			editor
				.chain()
				.focus()
				.updateAttributes("image", {
					style: `${widthPart} max-width: 100%; ${alignStyle} height: auto;`.trim(),
				})
				.run();
		}
	};
 
	const editAltText = () => {
		const { state } = editor;
		const { from } = state.selection;
		const node = state.doc.nodeAt(from);
		if (node?.type.name === "image") {
			const currentAlt = (node.attrs.alt as string) || "";
			const newAlt = window.prompt("Alt text:", currentAlt);
			if (newAlt !== null) {
				editor
					.chain()
					.focus()
					.updateAttributes("image", { alt: newAlt })
					.run();
			}
		}
	};
 
	const deleteImage = () => {
		editor.chain().focus().deleteSelection().run();
	};
 
	return (
		<BubbleMenu
			editor={editor}
			options={{
				placement: "top",
			}}
			shouldShow={({ editor: e }: { editor: Editor }) => {
				return e.isActive("image");
			}}
		>
			<div className="flex items-center gap-1 rounded-lg border border-border-default bg-background-elevated shadow-lg p-1">
				<Button
					type="button"
					variant="ghost"
					size="sm"
					onClick={() => setImageSize("50%")}
					title="Small"
					className="h-8 w-8 p-0"
				>
					<Minimize2 className="h-3.5 w-3.5" />
				</Button>
				<Button
					type="button"
					variant="ghost"
					size="sm"
					onClick={() => setImageSize("75%")}
					title="Medium"
					className="h-8 px-2 text-xs"
				>
					M
				</Button>
				<Button
					type="button"
					variant="ghost"
					size="sm"
					onClick={() => setImageSize("100%")}
					title="Full width"
					className="h-8 w-8 p-0"
				>
					<Maximize2 className="h-3.5 w-3.5" />
				</Button>
 
				<div className="w-px h-5 bg-border-default mx-0.5" />
 
				<Button
					type="button"
					variant="ghost"
					size="sm"
					onClick={() => setImageAlign("left")}
					title="Align left"
					className="h-8 w-8 p-0"
				>
					<AlignLeft className="h-3.5 w-3.5" />
				</Button>
				<Button
					type="button"
					variant="ghost"
					size="sm"
					onClick={() => setImageAlign("center")}
					title="Align center"
					className="h-8 w-8 p-0"
				>
					<AlignCenter className="h-3.5 w-3.5" />
				</Button>
				<Button
					type="button"
					variant="ghost"
					size="sm"
					onClick={() => setImageAlign("right")}
					title="Align right"
					className="h-8 w-8 p-0"
				>
					<AlignRight className="h-3.5 w-3.5" />
				</Button>
 
				<div className="w-px h-5 bg-border-default mx-0.5" />
 
				<Button
					type="button"
					variant="ghost"
					size="sm"
					onClick={editAltText}
					title="Edit alt text"
					className="h-8 w-8 p-0"
				>
					<Type className="h-3.5 w-3.5" />
				</Button>
				<Button
					type="button"
					variant="ghost"
					size="sm"
					onClick={deleteImage}
					title="Delete image"
					className="h-8 w-8 p-0 text-error hover:text-error hover:bg-error-light"
				>
					<Trash2 className="h-3.5 w-3.5" />
				</Button>
			</div>
		</BubbleMenu>
	);
}