svelte-multistyle-ui 0.0.9-alpha → 0.0.13-alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/cli/registry.js +2 -0
- package/dist/components/CodeEditor.svelte +130 -0
- package/dist/components/CodeEditor.svelte.d.ts +25 -0
- package/dist/components/TextEditor.svelte +187 -0
- package/dist/components/TextEditor.svelte.d.ts +27 -0
- package/dist/components/charts/chart-config.d.ts +2 -2
- package/dist/components/codeeditor-styles.css +292 -0
- package/dist/components/text-editor-core/dom-utils.d.ts +17 -0
- package/dist/components/text-editor-core/dom-utils.js +151 -0
- package/dist/components/text-editor-core/editor-core.d.ts +113 -0
- package/dist/components/text-editor-core/editor-core.js +808 -0
- package/dist/components/text-editor-core/history.d.ts +14 -0
- package/dist/components/text-editor-core/history.js +78 -0
- package/dist/components/text-editor-core/html-formatter.d.ts +4 -0
- package/dist/components/text-editor-core/html-formatter.js +80 -0
- package/dist/components/text-editor-core/index.d.ts +7 -0
- package/dist/components/text-editor-core/index.js +22 -0
- package/dist/components/text-editor-core/sanitizer.d.ts +3 -0
- package/dist/components/text-editor-core/sanitizer.js +161 -0
- package/dist/components/text-editor-core/selection-engine.d.ts +11 -0
- package/dist/components/text-editor-core/selection-engine.js +137 -0
- package/dist/components/text-editor-core/toolbar-config.d.ts +91 -0
- package/dist/components/text-editor-core/toolbar-config.js +138 -0
- package/dist/components/texteditor-styles.css +493 -0
- package/dist/config.svelte.d.ts +3 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/themes/presets.d.ts +21 -21
- package/package.json +1 -1
|
@@ -0,0 +1,808 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TextEditorCore — a lightweight, framework-agnostic WYSIWYG editor.
|
|
3
|
+
* Uses contenteditable and document.execCommand under the hood.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { createHistory } from "./history.js";
|
|
7
|
+
import { getSelectionState } from "./selection-engine.js";
|
|
8
|
+
import { sanitizeHTML, sanitizeURL, plainTextFromHTML } from "./sanitizer.js";
|
|
9
|
+
import {
|
|
10
|
+
createElement,
|
|
11
|
+
ensureParagraph,
|
|
12
|
+
getWordCount,
|
|
13
|
+
getTextLength,
|
|
14
|
+
htmlToText,
|
|
15
|
+
isInsideEditor,
|
|
16
|
+
closest,
|
|
17
|
+
} from "./dom-utils.js";
|
|
18
|
+
import { DEFAULT_TOOLBAR } from "./toolbar-config.js";
|
|
19
|
+
import { formatHTML } from "./html-formatter.js";
|
|
20
|
+
|
|
21
|
+
const BLOCK_TAG_MAP = {
|
|
22
|
+
p: "p",
|
|
23
|
+
h1: "h1",
|
|
24
|
+
h2: "h2",
|
|
25
|
+
h3: "h3",
|
|
26
|
+
h4: "h4",
|
|
27
|
+
h5: "h5",
|
|
28
|
+
h6: "h6",
|
|
29
|
+
blockquote: "blockquote",
|
|
30
|
+
pre: "pre",
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export class TextEditorCore {
|
|
34
|
+
constructor(options = {}) {
|
|
35
|
+
this.container = options.element;
|
|
36
|
+
if (!this.container) {
|
|
37
|
+
throw new Error("TextEditorCore requires a container element");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.onChange = options.onChange || (() => {});
|
|
41
|
+
this.onSelectionChange = options.onSelectionChange || (() => {});
|
|
42
|
+
this.onAction = options.onAction || (() => {});
|
|
43
|
+
this.placeholder = options.placeholder || "";
|
|
44
|
+
this.readOnly = options.readOnly || false;
|
|
45
|
+
this.historyLimit = options.historyLimit || 100;
|
|
46
|
+
|
|
47
|
+
this.toolbarConfig = options.toolbar || DEFAULT_TOOLBAR;
|
|
48
|
+
this.value = "";
|
|
49
|
+
this.history = createHistory(this.historyLimit);
|
|
50
|
+
this.isComposing = false;
|
|
51
|
+
this.lastHtml = "";
|
|
52
|
+
this.focused = false;
|
|
53
|
+
|
|
54
|
+
this._handlers = [];
|
|
55
|
+
this._dialogState = { type: null, data: null };
|
|
56
|
+
this._codeView = false;
|
|
57
|
+
this._codeEl = null;
|
|
58
|
+
this._lastCodeValue = "";
|
|
59
|
+
this._savedSelection = null;
|
|
60
|
+
|
|
61
|
+
this._buildDOM();
|
|
62
|
+
this._attachEvents();
|
|
63
|
+
this._renderToolbar();
|
|
64
|
+
this._updatePlaceholder();
|
|
65
|
+
this._updateStatus();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ============ DOM construction ============
|
|
69
|
+
_buildDOM() {
|
|
70
|
+
this.container.classList.add("s-texteditor");
|
|
71
|
+
if (this.readOnly) this.container.classList.add("readonly");
|
|
72
|
+
|
|
73
|
+
this.toolbarEl = createElement("div", { className: "s-texteditor-toolbar" });
|
|
74
|
+
this.bodyEl = createElement("div", { className: "s-texteditor-body" });
|
|
75
|
+
this.editorEl = createElement("div", {
|
|
76
|
+
className: "s-texteditor-content",
|
|
77
|
+
contenteditable: this.readOnly ? "false" : "true",
|
|
78
|
+
role: "textbox",
|
|
79
|
+
"aria-multiline": "true",
|
|
80
|
+
});
|
|
81
|
+
this.statusEl = createElement("div", { className: "s-texteditor-statusbar" });
|
|
82
|
+
|
|
83
|
+
this.bodyEl.appendChild(this.editorEl);
|
|
84
|
+
this.container.appendChild(this.toolbarEl);
|
|
85
|
+
this.container.appendChild(this.bodyEl);
|
|
86
|
+
this.container.appendChild(this.statusEl);
|
|
87
|
+
|
|
88
|
+
if (this.placeholder) {
|
|
89
|
+
this.container.setAttribute("data-placeholder", this.placeholder);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// ============ Event wiring ============
|
|
94
|
+
_attachEvents() {
|
|
95
|
+
this._listen(this.editorEl, "input", this._onInput.bind(this));
|
|
96
|
+
this._listen(this.editorEl, "keydown", this._onKeyDown.bind(this));
|
|
97
|
+
this._listen(this.editorEl, "focus", this._onFocus.bind(this));
|
|
98
|
+
this._listen(this.editorEl, "blur", this._onBlur.bind(this));
|
|
99
|
+
this._listen(this.editorEl, "compositionstart", this._onCompositionStart.bind(this));
|
|
100
|
+
this._listen(this.editorEl, "compositionend", this._onCompositionEnd.bind(this));
|
|
101
|
+
this._listen(this.editorEl, "paste", this._onPaste.bind(this));
|
|
102
|
+
this._listen(this.editorEl, "drop", this._onDrop.bind(this));
|
|
103
|
+
this._listen(document, "selectionchange", this._onSelectionChange.bind(this));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
_listen(target, event, handler) {
|
|
107
|
+
target.addEventListener(event, handler);
|
|
108
|
+
this._handlers.push({ target, event, handler });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
destroy() {
|
|
112
|
+
for (const { target, event, handler } of this._handlers) {
|
|
113
|
+
target.removeEventListener(event, handler);
|
|
114
|
+
}
|
|
115
|
+
this._handlers = [];
|
|
116
|
+
if (this.toolbarEl) this.toolbarEl.innerHTML = "";
|
|
117
|
+
if (this.container) {
|
|
118
|
+
this.container.classList.remove("s-texteditor", "readonly");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// ============ Input handling ============
|
|
123
|
+
_onInput() {
|
|
124
|
+
if (this.isComposing) return;
|
|
125
|
+
this._emitChange();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
_onCompositionStart() {
|
|
129
|
+
this.isComposing = true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
_onCompositionEnd() {
|
|
133
|
+
this.isComposing = false;
|
|
134
|
+
this._emitChange();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
_emitChange() {
|
|
138
|
+
if (this._codeView) return; // Code view manages value directly.
|
|
139
|
+
const html = this.getHTML();
|
|
140
|
+
if (html === this.lastHtml) return;
|
|
141
|
+
this.lastHtml = html;
|
|
142
|
+
this.value = html;
|
|
143
|
+
this.history.record(html);
|
|
144
|
+
this._updatePlaceholder();
|
|
145
|
+
this._updateStatus();
|
|
146
|
+
this.onChange(html);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
_onFocus() {
|
|
150
|
+
this.focused = true;
|
|
151
|
+
this.container.classList.add("focused");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
_onBlur() {
|
|
155
|
+
this.focused = false;
|
|
156
|
+
this.container.classList.remove("focused");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
_onSelectionChange() {
|
|
160
|
+
const sel = document.getSelection();
|
|
161
|
+
if (!sel || sel.rangeCount === 0) return;
|
|
162
|
+
if (!isInsideEditor(sel.anchorNode, this.editorEl)) return;
|
|
163
|
+
|
|
164
|
+
const state = getSelectionState(this.editorEl);
|
|
165
|
+
this._updateToolbarState(state);
|
|
166
|
+
this._updateStatus(state);
|
|
167
|
+
this.onSelectionChange(state);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ============ Keyboard shortcuts ============
|
|
171
|
+
_onKeyDown(e) {
|
|
172
|
+
const key = e.key;
|
|
173
|
+
const ctrl = e.ctrlKey || e.metaKey;
|
|
174
|
+
const shift = e.shiftKey;
|
|
175
|
+
|
|
176
|
+
if (ctrl) {
|
|
177
|
+
switch (key.toLowerCase()) {
|
|
178
|
+
case "b":
|
|
179
|
+
e.preventDefault();
|
|
180
|
+
this._applyFormat("bold");
|
|
181
|
+
return;
|
|
182
|
+
case "i":
|
|
183
|
+
e.preventDefault();
|
|
184
|
+
this._applyFormat("italic");
|
|
185
|
+
return;
|
|
186
|
+
case "u":
|
|
187
|
+
e.preventDefault();
|
|
188
|
+
this._applyFormat("underline");
|
|
189
|
+
return;
|
|
190
|
+
case "z":
|
|
191
|
+
e.preventDefault();
|
|
192
|
+
if (shift) this.redo();
|
|
193
|
+
else this.undo();
|
|
194
|
+
return;
|
|
195
|
+
case "y":
|
|
196
|
+
e.preventDefault();
|
|
197
|
+
this.redo();
|
|
198
|
+
return;
|
|
199
|
+
case "k":
|
|
200
|
+
e.preventDefault();
|
|
201
|
+
this._openLinkDialog();
|
|
202
|
+
return;
|
|
203
|
+
case "a":
|
|
204
|
+
setTimeout(() => this._onSelectionChange(), 0);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (shift) {
|
|
209
|
+
switch (key.toLowerCase()) {
|
|
210
|
+
case "s":
|
|
211
|
+
e.preventDefault();
|
|
212
|
+
this._applyFormat("strikeThrough");
|
|
213
|
+
return;
|
|
214
|
+
case "l":
|
|
215
|
+
e.preventDefault();
|
|
216
|
+
this.exec("justifyLeft");
|
|
217
|
+
this._emitChange();
|
|
218
|
+
return;
|
|
219
|
+
case "e":
|
|
220
|
+
e.preventDefault();
|
|
221
|
+
this.exec("justifyCenter");
|
|
222
|
+
this._emitChange();
|
|
223
|
+
return;
|
|
224
|
+
case "r":
|
|
225
|
+
e.preventDefault();
|
|
226
|
+
this.exec("justifyRight");
|
|
227
|
+
this._emitChange();
|
|
228
|
+
return;
|
|
229
|
+
case "j":
|
|
230
|
+
e.preventDefault();
|
|
231
|
+
this.exec("justifyFull");
|
|
232
|
+
this._emitChange();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (key === "Tab") {
|
|
239
|
+
e.preventDefault();
|
|
240
|
+
if (shift) this.exec("outdent");
|
|
241
|
+
else this.exec("indent");
|
|
242
|
+
this._emitChange();
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
_applyFormat(command) {
|
|
248
|
+
this.focus();
|
|
249
|
+
this._recordBeforeMutation();
|
|
250
|
+
this.exec(command);
|
|
251
|
+
this._emitChange();
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// ============ Paste / Drop ============
|
|
255
|
+
_onPaste(e) {
|
|
256
|
+
e.preventDefault();
|
|
257
|
+
const html = e.clipboardData.getData("text/html");
|
|
258
|
+
const text = e.clipboardData.getData("text/plain");
|
|
259
|
+
const toInsert = html ? sanitizeHTML(html) : (text ? text.replace(/\n/g, "<br>") : "");
|
|
260
|
+
if (toInsert) {
|
|
261
|
+
this._recordBeforeMutation();
|
|
262
|
+
this.exec("insertHTML", toInsert);
|
|
263
|
+
this._emitChange();
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
_onDrop(e) {
|
|
268
|
+
e.preventDefault();
|
|
269
|
+
const files = Array.from(e.dataTransfer.files);
|
|
270
|
+
for (const file of files) {
|
|
271
|
+
if (file.type.startsWith("image/")) {
|
|
272
|
+
this._insertImageFromFile(file);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
_insertImageFromFile(file) {
|
|
278
|
+
const reader = new FileReader();
|
|
279
|
+
reader.onload = (ev) => {
|
|
280
|
+
this._recordBeforeMutation();
|
|
281
|
+
this.exec("insertHTML", `<img src="${ev.target.result}" alt="" style="max-width:100%;" />`);
|
|
282
|
+
this._emitChange();
|
|
283
|
+
};
|
|
284
|
+
reader.readAsDataURL(file);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// ============ Toolbar rendering ============
|
|
288
|
+
_renderToolbar() {
|
|
289
|
+
this.toolbarEl.innerHTML = "";
|
|
290
|
+
for (const group of this.toolbarConfig) {
|
|
291
|
+
const groupEl = createElement("div", { className: "s-texteditor-toolbar-group" });
|
|
292
|
+
for (const action of group) {
|
|
293
|
+
if (action.type === "separator") {
|
|
294
|
+
groupEl.appendChild(createElement("span", { className: "s-texteditor-toolbar-separator" }));
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
const control = this._createControl(action);
|
|
298
|
+
if (control) groupEl.appendChild(control);
|
|
299
|
+
}
|
|
300
|
+
this.toolbarEl.appendChild(groupEl);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
_createControl(action) {
|
|
305
|
+
if (action.type === "button") {
|
|
306
|
+
const btn = createElement("button", {
|
|
307
|
+
className: `s-texteditor-btn s-texteditor-btn-${action.id}`,
|
|
308
|
+
type: "button",
|
|
309
|
+
title: action.title || action.label,
|
|
310
|
+
"data-command": action.command,
|
|
311
|
+
"data-active": action.active || "",
|
|
312
|
+
...(action.icon
|
|
313
|
+
? { innerHTML: `<span class="${action.icon}">${action.label}</span>` }
|
|
314
|
+
: { textContent: action.label }),
|
|
315
|
+
onclick: (e) => {
|
|
316
|
+
e.preventDefault();
|
|
317
|
+
this._handleCommand(action);
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
return btn;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (action.type === "select") {
|
|
324
|
+
const select = createElement("select", {
|
|
325
|
+
className: "s-texteditor-select",
|
|
326
|
+
title: action.title,
|
|
327
|
+
"data-command": action.command,
|
|
328
|
+
onchange: (e) => {
|
|
329
|
+
this._handleCommand(action, e.target.value);
|
|
330
|
+
e.target.selectedIndex = 0;
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
select.appendChild(createElement("option", { textContent: action.title, value: "" }));
|
|
334
|
+
for (const opt of action.options) {
|
|
335
|
+
select.appendChild(createElement("option", { textContent: opt.label, value: opt.value }));
|
|
336
|
+
}
|
|
337
|
+
return createElement("div", { className: "s-texteditor-select-wrap" }, [select]);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (action.type === "color") {
|
|
341
|
+
const label = createElement("label", {
|
|
342
|
+
className: `s-texteditor-color-btn s-texteditor-color-${action.id}${action.label ? " s-texteditor-color-btn--icon" : ""}`,
|
|
343
|
+
title: action.title,
|
|
344
|
+
});
|
|
345
|
+
if (action.label) {
|
|
346
|
+
label.appendChild(createElement("span", {
|
|
347
|
+
className: "s-texteditor-color-icon",
|
|
348
|
+
textContent: action.label,
|
|
349
|
+
}));
|
|
350
|
+
}
|
|
351
|
+
const input = createElement("input", {
|
|
352
|
+
type: "color",
|
|
353
|
+
"data-command": action.command,
|
|
354
|
+
onchange: (e) => {
|
|
355
|
+
this._handleCommand(action, e.target.value);
|
|
356
|
+
},
|
|
357
|
+
});
|
|
358
|
+
label.appendChild(input);
|
|
359
|
+
return label;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return null;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
_handleCommand(action, value) {
|
|
366
|
+
if (action.id === "toggleCodeView") {
|
|
367
|
+
this.toggleCodeView();
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
this.focus();
|
|
372
|
+
|
|
373
|
+
if (action.id === "undo") {
|
|
374
|
+
this.undo();
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (action.id === "redo") {
|
|
378
|
+
this.redo();
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (action.id === "removeFormat") {
|
|
382
|
+
this._recordBeforeMutation();
|
|
383
|
+
this.exec("removeFormat");
|
|
384
|
+
this._emitChange();
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (action.id === "lineHeight") {
|
|
388
|
+
this._recordBeforeMutation();
|
|
389
|
+
this._applyLineHeight(value);
|
|
390
|
+
this._emitChange();
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (action.id === "formatBlock") {
|
|
394
|
+
this._recordBeforeMutation();
|
|
395
|
+
this._applyBlockFormat(value);
|
|
396
|
+
this._emitChange();
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
if (action.id === "insertLink" || action.id === "createLink") {
|
|
400
|
+
this._openLinkDialog();
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (action.id === "insertImage") {
|
|
404
|
+
this._openImageDialog();
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (action.id === "insertVideo") {
|
|
408
|
+
this._openVideoDialog();
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (action.id === "insertTable") {
|
|
412
|
+
this._openTableDialog();
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// Default execCommand path.
|
|
417
|
+
this._recordBeforeMutation();
|
|
418
|
+
this.exec(action.command, value || null);
|
|
419
|
+
this._emitChange();
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// ============ Command helpers ============
|
|
423
|
+
exec(command, value = null) {
|
|
424
|
+
document.execCommand(command, false, value);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
_applyBlockFormat(tag) {
|
|
428
|
+
if (!tag) return;
|
|
429
|
+
const mapped = BLOCK_TAG_MAP[tag.toLowerCase()] || tag;
|
|
430
|
+
this.exec("formatBlock", `<${mapped}>`);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
_applyLineHeight(value) {
|
|
434
|
+
const sel = document.getSelection();
|
|
435
|
+
if (!sel.rangeCount) return;
|
|
436
|
+
const range = sel.getRangeAt(0);
|
|
437
|
+
const blocks = [];
|
|
438
|
+
let node = range.startContainer;
|
|
439
|
+
while (node) {
|
|
440
|
+
if (node === this.editorEl) break;
|
|
441
|
+
if (node.nodeType === Node.ELEMENT_NODE) blocks.push(node);
|
|
442
|
+
node = node.parentNode;
|
|
443
|
+
}
|
|
444
|
+
if (blocks.length === 0) {
|
|
445
|
+
this.editorEl.style.lineHeight = value;
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
for (const block of blocks) {
|
|
449
|
+
block.style.lineHeight = value;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// ============ Dialog-driven actions ============
|
|
454
|
+
_openLinkDialog() {
|
|
455
|
+
const existing = this._getSelectedLink();
|
|
456
|
+
this._dialogState = {
|
|
457
|
+
type: "link",
|
|
458
|
+
data: existing
|
|
459
|
+
? { text: existing.textContent, href: existing.getAttribute("href") || "", title: existing.getAttribute("title") || "", target: existing.getAttribute("target") || "_self" }
|
|
460
|
+
: { text: document.getSelection().toString(), href: "https://", title: "", target: "_self" },
|
|
461
|
+
};
|
|
462
|
+
this.onAction({ type: "dialog", dialog: "link", data: this._dialogState.data });
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
_openImageDialog() {
|
|
466
|
+
this._dialogState = { type: "image", data: { src: "", alt: "", width: "", caption: "" } };
|
|
467
|
+
this.onAction({ type: "dialog", dialog: "image", data: this._dialogState.data });
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
_openVideoDialog() {
|
|
471
|
+
this._dialogState = { type: "video", data: { src: "", width: "560", height: "315" } };
|
|
472
|
+
this.onAction({ type: "dialog", dialog: "video", data: this._dialogState.data });
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
_openTableDialog() {
|
|
476
|
+
this._dialogState = { type: "table", data: { rows: 3, cols: 3 } };
|
|
477
|
+
this.onAction({ type: "dialog", dialog: "table", data: this._dialogState.data });
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
_getSelectedLink() {
|
|
481
|
+
const sel = document.getSelection();
|
|
482
|
+
if (!sel.rangeCount) return null;
|
|
483
|
+
let node = sel.anchorNode;
|
|
484
|
+
while (node && node !== this.editorEl) {
|
|
485
|
+
if (node.nodeType === Node.ELEMENT_NODE && node.tagName === "A") {
|
|
486
|
+
return node;
|
|
487
|
+
}
|
|
488
|
+
node = node.parentNode;
|
|
489
|
+
}
|
|
490
|
+
return null;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// Public APIs used by the Svelte wrapper to apply dialog results.
|
|
494
|
+
insertLink({ text, href, title, target }) {
|
|
495
|
+
const url = sanitizeURL(href);
|
|
496
|
+
if (!url) return;
|
|
497
|
+
this.focus();
|
|
498
|
+
this._recordBeforeMutation();
|
|
499
|
+
|
|
500
|
+
const existing = this._getSelectedLink();
|
|
501
|
+
if (existing) {
|
|
502
|
+
existing.setAttribute("href", url);
|
|
503
|
+
if (title) existing.setAttribute("title", title);
|
|
504
|
+
else existing.removeAttribute("title");
|
|
505
|
+
existing.setAttribute("target", target || "_self");
|
|
506
|
+
if (text) existing.textContent = text;
|
|
507
|
+
} else {
|
|
508
|
+
const anchor = `<a href="${url}"${title ? ` title="${title}"` : ""} target="${target || "_self"}" rel="noopener noreferrer">${text || url}</a>`;
|
|
509
|
+
this.exec("insertHTML", anchor);
|
|
510
|
+
}
|
|
511
|
+
this._emitChange();
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
removeLink() {
|
|
515
|
+
this.focus();
|
|
516
|
+
this._recordBeforeMutation();
|
|
517
|
+
this.exec("unlink");
|
|
518
|
+
this._emitChange();
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
insertImage({ src, alt, width, align, caption }) {
|
|
522
|
+
if (!src) return;
|
|
523
|
+
this.focus();
|
|
524
|
+
this._recordBeforeMutation();
|
|
525
|
+
|
|
526
|
+
let figure = "";
|
|
527
|
+
const style = [];
|
|
528
|
+
if (width) style.push(`width:${width}`);
|
|
529
|
+
if (align) {
|
|
530
|
+
style.push(`float:${align}`);
|
|
531
|
+
if (align === "left" || align === "right") {
|
|
532
|
+
style.push("margin:0 12px 12px 0");
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
const styleAttr = style.length ? ` style="${style.join(";")}"` : "";
|
|
536
|
+
|
|
537
|
+
if (caption) {
|
|
538
|
+
figure = `<figure${styleAttr}><img src="${src}" alt="${alt || ""}" style="max-width:100%;display:block;" /><figcaption>${caption}</figcaption></figure>`;
|
|
539
|
+
} else {
|
|
540
|
+
figure = `<img src="${src}" alt="${alt || ""}"${styleAttr} style="max-width:100%;" />`;
|
|
541
|
+
}
|
|
542
|
+
this.exec("insertHTML", figure);
|
|
543
|
+
this._emitChange();
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
insertVideo({ src, width = "560", height = "315" }) {
|
|
547
|
+
if (!src) return;
|
|
548
|
+
this.focus();
|
|
549
|
+
this._recordBeforeMutation();
|
|
550
|
+
|
|
551
|
+
let embedUrl = src;
|
|
552
|
+
const youtubeMatch = src.match(/(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]+)/);
|
|
553
|
+
if (youtubeMatch) {
|
|
554
|
+
embedUrl = `https://www.youtube.com/embed/${youtubeMatch[1]}`;
|
|
555
|
+
}
|
|
556
|
+
const vimeoMatch = src.match(/vimeo\.com\/(\d+)/);
|
|
557
|
+
if (vimeoMatch) {
|
|
558
|
+
embedUrl = `https://player.vimeo.com/video/${vimeoMatch[1]}`;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
const iframe = `<iframe src="${embedUrl}" width="${width}" height="${height}" frameborder="0" allowfullscreen></iframe>`;
|
|
562
|
+
this.exec("insertHTML", iframe);
|
|
563
|
+
this._emitChange();
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
insertTable({ rows, cols }) {
|
|
567
|
+
rows = Math.max(1, Math.min(20, Number(rows) || 1));
|
|
568
|
+
cols = Math.max(1, Math.min(20, Number(cols) || 1));
|
|
569
|
+
this.focus();
|
|
570
|
+
this._recordBeforeMutation();
|
|
571
|
+
|
|
572
|
+
let html = `<table style="width:100%;border-collapse:collapse;"><tbody>`;
|
|
573
|
+
for (let r = 0; r < rows; r++) {
|
|
574
|
+
html += `<tr>`;
|
|
575
|
+
for (let c = 0; c < cols; c++) {
|
|
576
|
+
html += `<td style="border:1px solid currentColor;padding:6px 8px;"></td>`;
|
|
577
|
+
}
|
|
578
|
+
html += `</tr>`;
|
|
579
|
+
}
|
|
580
|
+
html += `</tbody></table><p><br></p>`;
|
|
581
|
+
this.exec("insertHTML", html);
|
|
582
|
+
this._emitChange();
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// ============ Image helpers ============
|
|
586
|
+
getSelectedImage() {
|
|
587
|
+
const sel = document.getSelection();
|
|
588
|
+
if (!sel.rangeCount) return null;
|
|
589
|
+
let node = sel.anchorNode;
|
|
590
|
+
if (node.nodeType === Node.TEXT_NODE) node = node.parentElement;
|
|
591
|
+
return closest(node, "img");
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
alignImage(align) {
|
|
595
|
+
const img = this.getSelectedImage();
|
|
596
|
+
if (!img) return;
|
|
597
|
+
this._recordBeforeMutation();
|
|
598
|
+
img.style.float = "";
|
|
599
|
+
img.style.margin = "";
|
|
600
|
+
img.style.display = "";
|
|
601
|
+
if (["left", "right"].includes(align)) {
|
|
602
|
+
img.style.float = align;
|
|
603
|
+
img.style.margin = align === "left" ? "0 12px 12px 0" : "0 0 12px 12px";
|
|
604
|
+
} else if (align === "center") {
|
|
605
|
+
img.style.display = "block";
|
|
606
|
+
img.style.margin = "0 auto";
|
|
607
|
+
} else {
|
|
608
|
+
img.style.display = "inline";
|
|
609
|
+
}
|
|
610
|
+
this._emitChange();
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
removeImage() {
|
|
614
|
+
const img = this.getSelectedImage();
|
|
615
|
+
if (!img) return;
|
|
616
|
+
this._recordBeforeMutation();
|
|
617
|
+
const figure = closest(img, "figure");
|
|
618
|
+
if (figure) figure.remove();
|
|
619
|
+
else img.remove();
|
|
620
|
+
this._emitChange();
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
replaceImage(src) {
|
|
624
|
+
const img = this.getSelectedImage();
|
|
625
|
+
if (!img || !src) return;
|
|
626
|
+
this._recordBeforeMutation();
|
|
627
|
+
img.setAttribute("src", src);
|
|
628
|
+
this._emitChange();
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
setImageAlt(alt) {
|
|
632
|
+
const img = this.getSelectedImage();
|
|
633
|
+
if (!img) return;
|
|
634
|
+
img.setAttribute("alt", alt || "");
|
|
635
|
+
this._emitChange();
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
_recordBeforeMutation() {
|
|
639
|
+
this.history.record(this.getHTML());
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
// ============ History ============
|
|
643
|
+
undo() {
|
|
644
|
+
const html = this.history.undo(this.getHTML());
|
|
645
|
+
if (html !== null) {
|
|
646
|
+
this._setHTMLInternal(html, false);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
redo() {
|
|
651
|
+
const html = this.history.redo();
|
|
652
|
+
if (html !== null) {
|
|
653
|
+
this._setHTMLInternal(html, false);
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
// ============ Code view ============
|
|
658
|
+
toggleCodeView() {
|
|
659
|
+
if (this._codeView) {
|
|
660
|
+
this._exitCodeView();
|
|
661
|
+
} else {
|
|
662
|
+
this._enterCodeView();
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
_enterCodeView() {
|
|
667
|
+
this._codeView = true;
|
|
668
|
+
this.container.classList.add("code-view");
|
|
669
|
+
const html = this.getHTML();
|
|
670
|
+
|
|
671
|
+
this._savedSelection = this._saveSelection();
|
|
672
|
+
|
|
673
|
+
this._lastCodeValue = html;
|
|
674
|
+
const formatted = formatHTML(html);
|
|
675
|
+
this._codeEl = createElement("textarea", {
|
|
676
|
+
className: "s-texteditor-code",
|
|
677
|
+
spellcheck: "false",
|
|
678
|
+
oninput: () => {
|
|
679
|
+
const raw = this._codeEl.value;
|
|
680
|
+
this._lastCodeValue = raw;
|
|
681
|
+
const clean = sanitizeHTML(raw);
|
|
682
|
+
this.value = clean;
|
|
683
|
+
this.lastHtml = clean;
|
|
684
|
+
this.onChange(clean);
|
|
685
|
+
this._updateStatus();
|
|
686
|
+
},
|
|
687
|
+
});
|
|
688
|
+
this._codeEl.value = formatted;
|
|
689
|
+
|
|
690
|
+
this.bodyEl.innerHTML = "";
|
|
691
|
+
this.bodyEl.appendChild(this._codeEl);
|
|
692
|
+
this._codeEl.focus();
|
|
693
|
+
|
|
694
|
+
// Disable toolbar formatting buttons while in code view.
|
|
695
|
+
this.toolbarEl.classList.add("readonly");
|
|
696
|
+
this._updateStatus();
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
_exitCodeView() {
|
|
700
|
+
const raw = this._codeEl ? this._codeEl.value : this._lastCodeValue;
|
|
701
|
+
this._codeView = false;
|
|
702
|
+
this.container.classList.remove("code-view");
|
|
703
|
+
|
|
704
|
+
this._codeEl = null;
|
|
705
|
+
this._lastCodeValue = "";
|
|
706
|
+
this.bodyEl.innerHTML = "";
|
|
707
|
+
this.bodyEl.appendChild(this.editorEl);
|
|
708
|
+
this.toolbarEl.classList.remove("readonly");
|
|
709
|
+
|
|
710
|
+
this._setHTMLInternal(raw, true);
|
|
711
|
+
this.focus();
|
|
712
|
+
this._restoreSelection(this._savedSelection);
|
|
713
|
+
this._savedSelection = null;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
_saveSelection() {
|
|
717
|
+
const sel = document.getSelection();
|
|
718
|
+
if (!sel || sel.rangeCount === 0 || !this.editorEl.contains(sel.anchorNode)) return null;
|
|
719
|
+
return sel.getRangeAt(0).cloneRange();
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
_restoreSelection(range) {
|
|
723
|
+
if (!range) return;
|
|
724
|
+
try {
|
|
725
|
+
const sel = document.getSelection();
|
|
726
|
+
sel.removeAllRanges();
|
|
727
|
+
sel.addRange(range);
|
|
728
|
+
} catch {
|
|
729
|
+
// Range may no longer be valid after HTML replacement.
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
isCodeView() {
|
|
734
|
+
return this._codeView;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// ============ HTML API ============
|
|
738
|
+
getHTML() {
|
|
739
|
+
let html = this.editorEl.innerHTML;
|
|
740
|
+
if (html === "<p><br></p>" || html === "<br>") html = "";
|
|
741
|
+
return sanitizeHTML(html);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
setHTML(html) {
|
|
745
|
+
this._setHTMLInternal(html, true);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
_setHTMLInternal(html, record) {
|
|
749
|
+
const clean = sanitizeHTML(html || "");
|
|
750
|
+
this.editorEl.innerHTML = clean || "<p><br></p>";
|
|
751
|
+
ensureParagraph(this.editorEl);
|
|
752
|
+
this.value = clean;
|
|
753
|
+
this.lastHtml = clean;
|
|
754
|
+
this._updatePlaceholder();
|
|
755
|
+
this._updateStatus();
|
|
756
|
+
if (record) this.history.record(clean);
|
|
757
|
+
this.onChange(clean);
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
getText() {
|
|
761
|
+
return htmlToText(this.getHTML());
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
clear() {
|
|
765
|
+
this.setHTML("");
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
focus() {
|
|
769
|
+
this.editorEl.focus();
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
setReadOnly(readOnly) {
|
|
773
|
+
this.readOnly = readOnly;
|
|
774
|
+
this.editorEl.contentEditable = readOnly ? "false" : "true";
|
|
775
|
+
this.container.classList.toggle("readonly", readOnly);
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
// ============ UI updates ============
|
|
779
|
+
_updateToolbarState(state) {
|
|
780
|
+
const buttons = this.toolbarEl.querySelectorAll(".s-texteditor-btn");
|
|
781
|
+
for (const btn of buttons) {
|
|
782
|
+
const activeKey = btn.dataset.active;
|
|
783
|
+
if (!activeKey) continue;
|
|
784
|
+
const active = state.activeFormats.has(activeKey);
|
|
785
|
+
btn.classList.toggle("active", active);
|
|
786
|
+
btn.setAttribute("aria-pressed", String(active));
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
_updatePlaceholder() {
|
|
791
|
+
const empty = !this.editorEl.textContent.trim();
|
|
792
|
+
this.container.classList.toggle("empty", empty);
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
_updateStatus(state = null) {
|
|
796
|
+
const html = this.getHTML();
|
|
797
|
+
const words = getWordCount(html);
|
|
798
|
+
const chars = getTextLength(html);
|
|
799
|
+
const selected = state?.selectedText?.length || 0;
|
|
800
|
+
const tag = state?.currentTag || "";
|
|
801
|
+
this.statusEl.innerHTML = `
|
|
802
|
+
<span>Words: ${words}</span>
|
|
803
|
+
<span>Chars: ${chars}</span>
|
|
804
|
+
${selected > 0 ? `<span>Selected: ${selected}</span>` : ""}
|
|
805
|
+
${tag ? `<span>Tag: ${tag.toUpperCase()}</span>` : ""}
|
|
806
|
+
`;
|
|
807
|
+
}
|
|
808
|
+
}
|