From 25093606c9cf03f9e4c940ab925b9dbf13de220a Mon Sep 17 00:00:00 2001 From: Citali Date: Thu, 5 Feb 2026 16:04:07 +0100 Subject: [PATCH] Fix the about page image display --- src/components/about/Editor.tsx | 2 + src/components/editor/plugins/image-kit.tsx | 15 ++++ src/components/ui/image-node.tsx | 76 +++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 src/components/editor/plugins/image-kit.tsx create mode 100644 src/components/ui/image-node.tsx diff --git a/src/components/about/Editor.tsx b/src/components/about/Editor.tsx index 5b6d43a..ec37495 100644 --- a/src/components/about/Editor.tsx +++ b/src/components/about/Editor.tsx @@ -5,6 +5,7 @@ import { saveAboutMeAction } from "@/actions/about/saveAbout"; import { BasicBlocksKit } from "@/components/editor/plugins/basic-blocks-kit"; import { BasicMarksKit } from "@/components/editor/plugins/basic-marks-kit"; import { CodeBlockKit } from "@/components/editor/plugins/code-block-kit"; +import { ImageKit } from "@/components/editor/plugins/image-kit"; import { ListKit } from "@/components/editor/plugins/list-kit"; import { MarkdownKit } from "@/components/editor/plugins/markdown-kit"; import { Button } from "@/components/ui/button"; @@ -43,6 +44,7 @@ export default function AboutEditor({ markdown }: { markdown: string | null }) { plugins: [ ...BasicBlocksKit, ...CodeBlockKit, + ...ImageKit, ...ListKit, ...BasicMarksKit, ...MarkdownKit, diff --git a/src/components/editor/plugins/image-kit.tsx b/src/components/editor/plugins/image-kit.tsx new file mode 100644 index 0000000..577954e --- /dev/null +++ b/src/components/editor/plugins/image-kit.tsx @@ -0,0 +1,15 @@ +'use client'; + +import { createPlatePlugin } from 'platejs/react'; + +import { ImageElement } from '@/components/ui/image-node'; + +// Minimal image plugin to render markdown images. +export const ImageKit = [ + createPlatePlugin({ + key: 'img', + node: { + isElement: true, + }, + }).withComponent(ImageElement), +]; diff --git a/src/components/ui/image-node.tsx b/src/components/ui/image-node.tsx new file mode 100644 index 0000000..229df96 --- /dev/null +++ b/src/components/ui/image-node.tsx @@ -0,0 +1,76 @@ +'use client'; + +import * as React from 'react'; + +import type { PlateElementProps } from 'platejs/react'; + +import { PlateElement, useEditorRef, useFocused, useReadOnly, useSelected, useElement } from 'platejs/react'; + +import { cn } from '@/lib/utils'; + +type ImageElementData = { + url?: string; + src?: string; + alt?: string; + altText?: string; + caption?: { text: string }[]; +}; + +export function ImageElement(props: PlateElementProps) { + const editor = useEditorRef(); + const element = useElement(); + const readOnly = useReadOnly(); + const selected = useSelected(); + const focused = useFocused(); + const data = element ?? (props.element as ImageElementData); + const src = data.url ?? data.src; + const captionText = + data.caption?.map((c) => c.text).join('') ?? data.alt ?? data.altText ?? ''; + const showCaptionEditor = !readOnly; + const path = showCaptionEditor ? editor.api.findPath(element) : null; + + return ( + +
+ {src ? ( + {captionText} + ) : ( +
Missing image source
+ )} +
e.stopPropagation()} + > + {showCaptionEditor ? ( + { + if (!path) return; + editor.tf.setNodes( + { caption: [{ text: e.target.value }] }, + { at: path } + ); + }} + placeholder="Add caption / alt text" + className="w-full rounded-md border border-border bg-background px-3 py-2 text-sm text-foreground" + onMouseDown={(e) => e.stopPropagation()} + onClick={(e) => e.stopPropagation()} + onFocus={(e) => e.stopPropagation()} + /> + ) : captionText ? ( +
{captionText}
+ ) : null} +
+
+ {props.children} +
+ ); +}