From 525b59f99546af06e8690598732a48bf754869c1 Mon Sep 17 00:00:00 2001 From: babblebey Date: Tue, 17 Nov 2026 20:49:55 +0100 Subject: [PATCH 01/14] feat(editor): add markdown formatting toolbar and selection handling --- src/components/islands/word-editor.jsx | 193 ++++++++++++++++++++++++- 1 file changed, 191 insertions(+), 2 deletions(-) diff --git a/src/components/islands/word-editor.jsx b/src/components/islands/word-editor.jsx index ce140e99..bfaf353d 100644 --- a/src/components/islands/word-editor.jsx +++ b/src/components/islands/word-editor.jsx @@ -5,7 +5,7 @@ * @exports TogglePreview - Mobile preview toggle for responsive editor experience */ -import { useEffect } from "react"; +import { useEffect, useRef } from "react"; import Markdown from "react-markdown"; import { useStore } from "@nanostores/react"; import useRouter from "../../lib/hooks/use-router.js"; @@ -121,6 +121,8 @@ function Editor({ eTitle, eContent, eMetadata, className, action, ...props }) { } = useWordEditor(); const isDone = isSubmitLoading || isSubmitted; + const textareaRef = useRef(null); + const pendingSelection = useRef(null); useEffect(() => { setTitle(eTitle); @@ -128,6 +130,45 @@ function Editor({ eTitle, eContent, eMetadata, className, action, ...props }) { setInitialContent(eContent); }, []); + useEffect(() => { + const textarea = textareaRef.current; + const selection = pendingSelection.current; + + if (!textarea || !selection) return; + + textarea.focus(); + textarea.setSelectionRange(selection.start, selection.end); + pendingSelection.current = null; + }, [content]); + + function applyMarkdownWrap(marker) { + const textarea = textareaRef.current; + if (!textarea || isDone) return; + + const result = wrapSelection( + content, + textarea.selectionStart, + textarea.selectionEnd, + marker, + ); + pendingSelection.current = result; + setContent(result.value); + } + + function applyMarkdownListPrefix(prefix) { + const textarea = textareaRef.current; + if (!textarea || isDone) return; + + const result = prefixSelectedLines( + content, + textarea.selectionStart, + textarea.selectionEnd, + prefix, + ); + pendingSelection.current = result; + setContent(result.value); + } + /** * Word Submit Handler * @param {import("react").FormEvent} e @@ -205,9 +246,70 @@ function Editor({ eTitle, eContent, eMetadata, className, action, ...props }) { placeholder="New Word" onChange={(e) => setTitle(e.target.value)} readOnly={action === "edit" || isDone} - className={`${(action === "edit" || isDone) && "cursor-not-allowed"} block w-full pb-2 mb-3 text-gray-900 border-b text-lg font-bold focus:outline-none`} + className={`${(action === "edit" || isDone) && "cursor-not-allowed"} block w-full pb-2 text-gray-900 border-b text-lg font-bold focus:outline-none`} /> +
+ applyMarkdownWrap("**")} + > + + + applyMarkdownWrap("*")} + > + + + applyMarkdownListPrefix("- ")} + > + + + applyMarkdownListPrefix("1. ")} + > + + + applyMarkdownWrap("`")} + > + + +