forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

rich-text-editor.tsx148 lines, 5.0 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 >
68 B
69 </button>
70 <button
71 type="button"
72 class="flex h-7 w-7 items-center justify-center rounded italic text-xs hover:bg-muted text-foreground"
73 onClick={() => execCommand("italic")}
74 title={t("editor.italic")}
75 >
76 I
77 </button>
78 <button
79 type="button"
80 class="flex h-7 w-7 items-center justify-center rounded underline text-xs hover:bg-muted text-foreground"
81 onClick={() => execCommand("underline")}
82 title={t("editor.underline")}
83 >
84 U
85 </button>
86 <button
87 type="button"
88 class="flex h-7 w-7 items-center justify-center rounded line-through text-xs hover:bg-muted text-foreground"
89 onClick={() => execCommand("strikeThrough")}
90 title={t("editor.strike")}
91 >
92 S
93 </button>
94
95 <div class="mx-1 h-4 w-px bg-border" />
96
97 <button
98 type="button"
99 class="flex h-7 px-2 items-center justify-center rounded text-xs font-mono hover:bg-muted text-foreground"
100 onClick={() => execCommand("insertUnorderedList")}
101 title={t("editor.bulletList")}
102 >
103 • {t("editor.bulletListLabel")}
104 </button>
105 <button
106 type="button"
107 class="flex h-7 px-2 items-center justify-center rounded text-xs font-mono hover:bg-muted text-foreground"
108 onClick={() => execCommand("insertOrderedList")}
109 title={t("editor.numberedList")}
110 >
111 {t("editor.numberedListLabel")}
112 </button>
113
114 <div class="mx-1 h-4 w-px bg-border" />
115
116 <button
117 type="button"
118 class="flex h-7 px-2 items-center justify-center rounded text-xs hover:bg-muted text-muted-foreground"
119 onClick={() => execCommand("removeFormat")}
120 title={t("editor.clearFormatting")}
121 >
122 {t("editor.clearFormattingLabel")}
123 </button>
124 </div>
125
126 {/* Contenteditable Editor Area */}
127 <div class="relative flex-1 p-3 min-h-0">
128 <div
129 ref={editorRef}
130 contentEditable
131 class={cn(
132 "w-full h-full min-h-[140px] outline-hidden text-xs leading-relaxed text-foreground whitespace-pre-wrap overflow-auto",
133 props.minHeight ? props.minHeight : "min-h-[140px]"
134 )}
135 onInput={handleInput}
136 onFocus={() => setIsFocused(true)}
137 onBlur={() => setIsFocused(false)}
138 />
139 {/* Placeholder overlay */}
140 {!props.value && !isFocused() && (
141 <div class="pointer-events-none absolute left-3 top-3 text-xs text-muted-foreground/60 select-none">
142 {props.placeholder || t("editor.bodyPlaceholder")}
143 </div>
144 )}
145 </div>
146 </div>
147 );
148}