forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

rich-text-editor.tsx155 lines, 5.3 KiBHistoryRawDownload
1import { createSignal, createEffect } from "solid-js";
2import { cn } from "@/lib/cn";
3import { sanitizeRichText } from "@/lib/rich-text";
4import { useT } from "@/stores/preferences-context";
5
6interface RichTextEditorProps {
7 value: string;
8 onChange: (value: string) => void;
9 placeholder?: string;
10 class?: string;
11 minHeight?: string;
12}
13
14export function RichTextEditor(props: RichTextEditorProps) {
15 const t = useT();
16 let editorRef: HTMLDivElement | undefined;
17 const [isFocused, setIsFocused] = createSignal(false);
18
19 // Sync value from props into editor when changed externally
20 createEffect(() => {
21 if (editorRef && editorRef.innerHTML !== props.value) {
22 // If props value is empty string, clear innerHTML
23 if (!props.value) {
24 editorRef.innerHTML = "";
25 } else if (editorRef.innerHTML !== props.value) {
26 // The value can come back from the server (a saved homework answer),
27 // so it goes through the same sanitizer as every other stored HTML.
28 editorRef.innerHTML = sanitizeRichText(props.value);
29 }
30 }
31 });
32
33 const handleInput = () => {
34 if (editorRef) {
35 const html = editorRef.innerHTML;
36 // If editor contains empty tags like <br> or <p><br></p>, treat as empty
37 if (html === "<br>" || html === "<p><br></p>" || editorRef.innerText.trim() === "") {
38 props.onChange("");
39 } else {
40 props.onChange(html);
41 }
42 }
43 };
44
45 const execCommand = (command: string, value: string | undefined = undefined) => {
46 if (!editorRef) return;
47 editorRef.focus();
48 document.execCommand(command, false, value);
49 handleInput();
50 };
51
52 return (
53 <div
54 class={cn(
55 "flex flex-col overflow-hidden rounded-md border border-input bg-background transition-all",
56 isFocused() && "ring-1 ring-primary border-primary/60",
57 props.class
58 )}
59 >
60 {/* Gmail Style Rich Formatting Toolbar */}
61 <div class="flex flex-wrap items-center gap-1 border-b bg-muted/40 px-2 py-1.5 text-xs select-none">
62 <button
63 type="button"
64 class="flex h-7 w-7 items-center justify-center rounded font-bold text-xs hover:bg-muted text-foreground"
65 onClick={() => execCommand("bold")}
66 title={t("editor.bold")}
67 aria-label={t("editor.bold")}
68 >
69 B
70 </button>
71 <button
72 type="button"
73 class="flex h-7 w-7 items-center justify-center rounded italic text-xs hover:bg-muted text-foreground"
74 onClick={() => execCommand("italic")}
75 title={t("editor.italic")}
76 aria-label={t("editor.italic")}
77 >
78 I
79 </button>
80 <button
81 type="button"
82 class="flex h-7 w-7 items-center justify-center rounded underline text-xs hover:bg-muted text-foreground"
83 onClick={() => execCommand("underline")}
84 title={t("editor.underline")}
85 aria-label={t("editor.underline")}
86 >
87 U
88 </button>
89 <button
90 type="button"
91 class="flex h-7 w-7 items-center justify-center rounded line-through text-xs hover:bg-muted text-foreground"
92 onClick={() => execCommand("strikeThrough")}
93 title={t("editor.strike")}
94 aria-label={t("editor.strike")}
95 >
96 S
97 </button>
98
99 <div class="mx-1 h-4 w-px bg-border" />
100
101 <button
102 type="button"
103 class="flex h-7 px-2 items-center justify-center rounded text-xs font-mono hover:bg-muted text-foreground"
104 onClick={() => execCommand("insertUnorderedList")}
105 title={t("editor.bulletList")}
106 aria-label={t("editor.bulletList")}
107 >
108 • {t("editor.bulletListLabel")}
109 </button>
110 <button
111 type="button"
112 class="flex h-7 px-2 items-center justify-center rounded text-xs font-mono hover:bg-muted text-foreground"
113 onClick={() => execCommand("insertOrderedList")}
114 title={t("editor.numberedList")}
115 aria-label={t("editor.numberedList")}
116 >
117 {t("editor.numberedListLabel")}
118 </button>
119
120 <div class="mx-1 h-4 w-px bg-border" />
121
122 <button
123 type="button"
124 class="flex h-7 px-2 items-center justify-center rounded text-xs hover:bg-muted text-muted-foreground"
125 onClick={() => execCommand("removeFormat")}
126 title={t("editor.clearFormatting")}
127 aria-label={t("editor.clearFormatting")}
128 >
129 {t("editor.clearFormattingLabel")}
130 </button>
131 </div>
132
133 {/* Contenteditable Editor Area */}
134 <div class="relative flex-1 p-3 min-h-0">
135 <div
136 ref={editorRef}
137 contentEditable
138 class={cn(
139 "w-full h-full min-h-[140px] outline-hidden text-xs leading-relaxed text-foreground whitespace-pre-wrap overflow-auto",
140 props.minHeight ? props.minHeight : "min-h-[140px]"
141 )}
142 onInput={handleInput}
143 onFocus={() => setIsFocused(true)}
144 onBlur={() => setIsFocused(false)}
145 />
146 {/* Placeholder overlay */}
147 {!props.value && !isFocused() && (
148 <div class="pointer-events-none absolute left-3 top-3 text-xs text-muted-foreground/60 select-none">
149 {props.placeholder || t("editor.bodyPlaceholder")}
150 </div>
151 )}
152 </div>
153 </div>
154 );
155}