1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
import { createSignal, onMount, onCleanup, Show } from "solid-js";
import { createEditor, $getSelection, $isRangeSelection, $isElementNode, $createTextNode, $createParagraphNode, $insertNodes, $getRoot, COMMAND_PRIORITY_LOW } from "lexical";
import type { LexicalEditor, TextFormatType } from "lexical";
import { registerRichText } from "@lexical/rich-text";
import { $generateHtmlFromNodes, $generateNodesFromDOM } from "@lexical/html";
import { LinkNode, $createLinkNode, $isLinkNode, TOGGLE_LINK_COMMAND, $toggleLink } from "@lexical/link";
import { registerHistory, createEmptyHistoryState } from "@lexical/history";
import {
IconBold, IconBoldOff,
IconItalic,
IconUnderline,
IconEyeOff,
IconLink, IconLinkOff,
IconMoodSmile,
} from "@tabler/icons-solidjs";
import "emoji-picker-element";
interface MiniEditorProps {
onHtml: (html: string) => void;
initialHtml?: string;
}
export default function MiniEditor(props: MiniEditorProps) {
let editorRef!: HTMLDivElement;
let containerRef!: HTMLDivElement;
let linkUrlRef!: HTMLInputElement;
let editorInstance: LexicalEditor;
const [bold, setBold] = createSignal(false);
const [italic, setItalic] = createSignal(false);
const [underline, setUnderline] = createSignal(false);
const [spoiler, setSpoiler] = createSignal(false);
const [link, setLink] = createSignal(false);
const [showLinkInput, setShowLinkInput] = createSignal(false);
const [linkUrl, setLinkUrl] = createSignal("");
const [linkText, setLinkText] = createSignal("");
const [hasSelection, setHasSelection] = createSignal(false);
const [showEmoji, setShowEmoji] = createSignal(false);
const [emojiPos, setEmojiPos] = createSignal({ top: 0, right: 0 });
function ensureSelection() {
let selection = $getSelection();
if (!$isRangeSelection(selection)) {
const root = $getRoot();
const firstChild = root.getFirstChild();
if ($isElementNode(firstChild)) {
selection = firstChild.select(0, 0);
} else {
const paragraph = $createParagraphNode();
root.append(paragraph);
selection = paragraph.select(0, 0);
}
}
return selection;
}
function ensureFocus() {
editorInstance.update(() => { ensureSelection(); });
editorInstance.focus();
}
function formatText(format: TextFormatType) {
editorInstance.update(() => {
const selection = ensureSelection();
if ($isRangeSelection(selection)) {
selection.formatText(format);
}
});
editorInstance.focus();
}
function openLinkInput() {
setShowEmoji(false);
if (link()) {
ensureFocus();
editorInstance.dispatchCommand(TOGGLE_LINK_COMMAND, null);
return;
}
editorInstance.getEditorState().read(() => {
const selection = $getSelection();
if ($isRangeSelection(selection)) {
const text = selection.getTextContent();
setLinkText(text);
setHasSelection(text.length > 0);
} else {
setLinkText("");
setHasSelection(false);
}
});
setLinkUrl("");
setShowLinkInput(true);
requestAnimationFrame(() => linkUrlRef?.focus());
}
function submitLink() {
const url = linkUrl().trim();
if (!url) return;
const text = linkText().trim();
ensureFocus();
editorInstance.update(() => {
const selection = $getSelection();
if ($isRangeSelection(selection) && hasSelection()) {
$toggleLink(url);
} else {
const linkNode = $createLinkNode(url);
linkNode.append($createTextNode(text || url));
$insertNodes([linkNode]);
}
});
setShowLinkInput(false);
setLinkUrl("");
setLinkText("");
}
function cancelLink() {
setShowLinkInput(false);
setLinkUrl("");
setLinkText("");
ensureFocus();
}
let emojiBtnRef!: HTMLButtonElement;
function toggleEmojiPicker() {
if (!showEmoji()) {
const rect = emojiBtnRef.getBoundingClientRect();
setEmojiPos({ top: rect.bottom + 2, right: window.innerWidth - rect.right });
}
setShowEmoji(!showEmoji());
}
function insertEmoji(unicode: string) {
editorInstance.update(() => {
const selection = ensureSelection();
if ($isRangeSelection(selection)) {
selection.insertText(unicode);
} else {
$insertNodes([$createTextNode(unicode)]);
}
});
editorInstance.focus();
setShowEmoji(false);
}
onMount(() => {
editorInstance = createEditor({
namespace: "pagoda-mini",
nodes: [LinkNode],
theme: {
paragraph: "editor-paragraph",
text: {
bold: "editor-bold",
italic: "editor-italic",
underline: "editor-underline",
highlight: "editor-spoiler",
},
link: "editor-link",
},
onError: (error: Error) => console.error(error),
});
editorInstance.setRootElement(editorRef);
const cleanupRichText = registerRichText(editorInstance);
const cleanupLink = editorInstance.registerCommand(TOGGLE_LINK_COMMAND, (payload) => {
$toggleLink(typeof payload === "string" ? payload : null);
return true;
}, COMMAND_PRIORITY_LOW);
const cleanupHistory = registerHistory(editorInstance, createEmptyHistoryState(), 300);
if (props.initialHtml) {
editorInstance.update(() => {
const parser = new DOMParser();
const dom = parser.parseFromString(props.initialHtml!, "text/html");
const nodes = $generateNodesFromDOM(editorInstance, dom);
const root = $getRoot();
root.clear();
root.append(...nodes);
});
}
editorInstance.registerUpdateListener(({ editorState }) => {
editorState.read(() => {
props.onHtml($generateHtmlFromNodes(editorInstance));
const selection = $getSelection();
if ($isRangeSelection(selection)) {
setBold(selection.hasFormat("bold"));
setItalic(selection.hasFormat("italic"));
setUnderline(selection.hasFormat("underline"));
setSpoiler(selection.hasFormat("highlight"));
const node = selection.anchor.getNode();
const parent = node.getParent();
setLink(parent !== null && $isLinkNode(parent));
}
});
});
function handleClickOutside(event: MouseEvent) {
const target = event.target as HTMLElement;
if (showEmoji() && !containerRef.contains(target) && !target.closest(".editor-emoji-popup")) {
setShowEmoji(false);
}
}
document.addEventListener("mousedown", handleClickOutside);
onCleanup(() => {
cleanupRichText();
cleanupLink();
cleanupHistory();
document.removeEventListener("mousedown", handleClickOutside);
editorInstance.setRootElement(null);
});
});
const s = "18";
const w = "2";
const wActive = "3";
return (
<div class="editor-container editor-mini" ref={containerRef}>
<div class="editor-toolbar">
<button type="button" class="editor-toolbar-btn" classList={{ "editor-toolbar-active": bold() }} onMouseDown={(e) => e.preventDefault()} onClick={() => formatText("bold")} title="Bold">
<Show when={bold()} fallback={<IconBoldOff size={s} stroke={w} />}><IconBold size={s} stroke={w} /></Show>
</button>
<button type="button" class="editor-toolbar-btn" classList={{ "editor-toolbar-active": italic() }} onMouseDown={(e) => e.preventDefault()} onClick={() => formatText("italic")} title="Italic">
<IconItalic size={s} stroke={italic() ? wActive : w} />
</button>
<button type="button" class="editor-toolbar-btn" classList={{ "editor-toolbar-active": underline() }} onMouseDown={(e) => e.preventDefault()} onClick={() => formatText("underline")} title="Underline">
<IconUnderline size={s} stroke={underline() ? wActive : w} />
</button>
<button type="button" class="editor-toolbar-btn" classList={{ "editor-toolbar-active": spoiler() }} onMouseDown={(e) => e.preventDefault()} onClick={() => formatText("highlight")} title="Spoiler">
<IconEyeOff size={s} stroke={spoiler() ? wActive : w} />
</button>
<span class="editor-toolbar-divider" />
<button type="button" class="editor-toolbar-btn" classList={{ "editor-toolbar-active": link() }} onMouseDown={(e) => e.preventDefault()} onClick={openLinkInput} title="Link">
<Show when={link()} fallback={<IconLinkOff size={s} stroke={w} />}><IconLink size={s} stroke={w} /></Show>
</button>
<span class="editor-toolbar-divider" />
<button type="button" ref={emojiBtnRef} class="editor-toolbar-btn" onClick={toggleEmojiPicker} title="Emoji">
<IconMoodSmile size={s} stroke={w} />
</button>
</div>
<Show when={showLinkInput()}>
<div class="editor-link-bar">
<Show when={!hasSelection()}>
<input
type="text"
class="editor-link-input"
placeholder="Link text"
value={linkText()}
onInput={(e) => setLinkText(e.currentTarget.value)}
onKeyDown={(e) => { if (e.key === "Escape") cancelLink(); }}
/>
</Show>
<input
ref={linkUrlRef}
type="text"
class="editor-link-input"
placeholder="https://"
value={linkUrl()}
onInput={(e) => setLinkUrl(e.currentTarget.value)}
onKeyDown={(e) => {
if (e.key === "Enter") submitLink();
if (e.key === "Escape") cancelLink();
}}
/>
<button type="button" class="editor-link-btn editor-link-apply" onClick={submitLink}>Apply</button>
<button type="button" class="editor-link-btn" onClick={cancelLink}>Cancel</button>
</div>
</Show>
<div ref={editorRef} class="editor-root" contentEditable />
<Show when={showEmoji()}>
<div class="editor-emoji-popup" style={{ top: `${emojiPos().top}px`, right: `${emojiPos().right}px` }}>
<emoji-picker
class="editor-emoji-picker"
on:emoji-click={(e: CustomEvent) => insertEmoji(e.detail.unicode)}
/>
</div>
</Show>
</div>
);
}
|