feat(training): captions under lesson images
Authors can now attach a caption to an image in the lesson editor; it is sanitized as figure text and follows the image's alignment in the reader. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -12,6 +12,7 @@ import {
|
||||
ArrowDown,
|
||||
ArrowUp,
|
||||
Bold,
|
||||
Captions,
|
||||
Code,
|
||||
Heading2,
|
||||
Heading3,
|
||||
@@ -31,24 +32,81 @@ import {
|
||||
} from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
/** The <img> for an image node, whether the node parsed from an <img> or a <figure>. */
|
||||
function imageElement(element: HTMLElement): HTMLElement {
|
||||
return element.tagName === "IMG"
|
||||
? element
|
||||
: element.querySelector("img") ?? element;
|
||||
}
|
||||
|
||||
/**
|
||||
* The image node adds alignment to the stock image extension. Width is already
|
||||
* a native attribute, so the toolbar can offer percentage presets without any
|
||||
* inline styles (which the server sanitizer intentionally strips).
|
||||
* The image node adds alignment and a caption to the stock image extension.
|
||||
* Width is already a native attribute and the caption is emitted as the
|
||||
* figcaption text, so the toolbar needs no inline styles (which the server
|
||||
* sanitizer intentionally strips).
|
||||
*/
|
||||
const TrainingImage = Image.extend({
|
||||
addAttributes() {
|
||||
return {
|
||||
...this.parent?.(),
|
||||
src: {
|
||||
default: null,
|
||||
parseHTML: (element: HTMLElement) =>
|
||||
imageElement(element).getAttribute("src"),
|
||||
},
|
||||
alt: {
|
||||
default: null,
|
||||
parseHTML: (element: HTMLElement) =>
|
||||
imageElement(element).getAttribute("alt"),
|
||||
},
|
||||
title: {
|
||||
default: null,
|
||||
parseHTML: (element: HTMLElement) =>
|
||||
imageElement(element).getAttribute("title"),
|
||||
},
|
||||
width: {
|
||||
default: null,
|
||||
parseHTML: (element: HTMLElement) =>
|
||||
imageElement(element).getAttribute("width"),
|
||||
},
|
||||
align: {
|
||||
default: "center",
|
||||
parseHTML: (element) => element.getAttribute("data-align") ?? "center",
|
||||
renderHTML: (attributes) => ({
|
||||
parseHTML: (element: HTMLElement) =>
|
||||
imageElement(element).getAttribute("data-align") ?? "center",
|
||||
renderHTML: (attributes: Record<string, unknown>) => ({
|
||||
"data-align": attributes.align ?? "center",
|
||||
}),
|
||||
},
|
||||
caption: {
|
||||
default: null,
|
||||
parseHTML: (element: HTMLElement) =>
|
||||
element.tagName === "FIGURE"
|
||||
? element.querySelector("figcaption")?.textContent?.trim() || null
|
||||
: null,
|
||||
},
|
||||
};
|
||||
},
|
||||
parseHTML() {
|
||||
return [
|
||||
{ tag: "figure[data-lesson-image]" },
|
||||
{ tag: 'img[src]:not([src^="data:"])' },
|
||||
];
|
||||
},
|
||||
renderHTML({ HTMLAttributes }) {
|
||||
const { caption: captionAttribute, ...attributes } = HTMLAttributes;
|
||||
const caption =
|
||||
typeof captionAttribute === "string" ? captionAttribute.trim() : "";
|
||||
const align = attributes["data-align"] ?? "center";
|
||||
|
||||
if (!caption) {
|
||||
return ["figure", { "data-lesson-image": "true" }, ["img", attributes]];
|
||||
}
|
||||
return [
|
||||
"figure",
|
||||
{ "data-lesson-image": "true" },
|
||||
["img", attributes],
|
||||
["figcaption", { "data-align": align }, caption],
|
||||
];
|
||||
},
|
||||
});
|
||||
|
||||
const IMAGE_WIDTHS = ["25%", "50%", "75%", "100%"] as const;
|
||||
@@ -192,6 +250,7 @@ export function RichTextEditor({
|
||||
? String(imageAttributes.width)
|
||||
: "";
|
||||
const imageAlt = (imageAttributes.alt as string | undefined) ?? "";
|
||||
const imageCaption = (imageAttributes.caption as string | undefined) ?? "";
|
||||
|
||||
function promptLink() {
|
||||
if (!editor) return;
|
||||
@@ -224,6 +283,20 @@ export function RichTextEditor({
|
||||
.run();
|
||||
}
|
||||
|
||||
function promptImageCaption() {
|
||||
if (!editor) return;
|
||||
const entered = window.prompt(
|
||||
"Caption shown under the image (leave empty to remove it)",
|
||||
imageCaption,
|
||||
);
|
||||
if (entered === null) return;
|
||||
editor
|
||||
.chain()
|
||||
.focus()
|
||||
.updateAttributes("image", { caption: entered.trim() || null })
|
||||
.run();
|
||||
}
|
||||
|
||||
async function uploadImage(file: File) {
|
||||
if (!editor) return;
|
||||
if (!file.type.startsWith("image/")) {
|
||||
@@ -489,6 +562,13 @@ export function RichTextEditor({
|
||||
<ToolButton label="Edit image description" onClick={promptImageAlt}>
|
||||
<span className="text-[11px] font-semibold">ALT</span>
|
||||
</ToolButton>
|
||||
<ToolButton
|
||||
label={imageCaption ? "Edit image caption" : "Add image caption"}
|
||||
active={Boolean(imageCaption)}
|
||||
onClick={promptImageCaption}
|
||||
>
|
||||
<Captions aria-hidden="true" />
|
||||
</ToolButton>
|
||||
<ToolButton
|
||||
label="Move image up"
|
||||
onClick={() => editor && moveImage(editor, -1)}
|
||||
@@ -515,7 +595,7 @@ export function RichTextEditor({
|
||||
<div className="flex flex-wrap items-center justify-between gap-2 border-t border-border px-2 py-1.5 text-xs text-muted-foreground">
|
||||
<span>
|
||||
Insert images with the image button. Select an image to align, resize,
|
||||
move, or describe it; you can also drag it to a new position.
|
||||
caption, move, or describe it; you can also drag it to a new position.
|
||||
</span>
|
||||
{uploading && (
|
||||
<span role="status" className="inline-flex items-center gap-1">
|
||||
|
||||
Reference in New Issue
Block a user