ozdenui 0.1.0

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.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +14 -0
  3. data/NOTICE +5 -0
  4. data/README.md +242 -0
  5. data/exe/ozdenui +17 -0
  6. data/lib/ozdenui/AGENTS.md +210 -0
  7. data/lib/ozdenui/COMPONENTS.md +3017 -0
  8. data/lib/ozdenui/app.rb +749 -0
  9. data/lib/ozdenui/command.rb +107 -0
  10. data/lib/ozdenui/components.rb +368 -0
  11. data/lib/ozdenui/dialogs.rb +212 -0
  12. data/lib/ozdenui/files.rb +124 -0
  13. data/lib/ozdenui/icon.png +0 -0
  14. data/lib/ozdenui/kit.json +1 -0
  15. data/lib/ozdenui/kit.rb +115 -0
  16. data/lib/ozdenui/look.rb +229 -0
  17. data/lib/ozdenui/objects.rb +652 -0
  18. data/lib/ozdenui/server.rb +270 -0
  19. data/lib/ozdenui/values.rb +273 -0
  20. data/lib/ozdenui/version.rb +15 -0
  21. data/lib/ozdenui/web/basic/LICENSE +14 -0
  22. data/lib/ozdenui/web/basic/basic.min.css +1 -0
  23. data/lib/ozdenui/web/basic/basic.min.js +78 -0
  24. data/lib/ozdenui/web/basic/font/open-sans/LICENSE.txt +206 -0
  25. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Bold.ttf +0 -0
  26. data/lib/ozdenui/web/basic/font/open-sans/OpenSans-Regular.ttf +0 -0
  27. data/lib/ozdenui/web/basic/scroll-bar.min.js +3 -0
  28. data/lib/ozdenui/web/bridge.js +584 -0
  29. data/lib/ozdenui/web/comp/accordion.js +659 -0
  30. data/lib/ozdenui/web/comp/action-button/add.png +0 -0
  31. data/lib/ozdenui/web/comp/action-button.js +521 -0
  32. data/lib/ozdenui/web/comp/action-tab-bar.js +383 -0
  33. data/lib/ozdenui/web/comp/app-bar.js +237 -0
  34. data/lib/ozdenui/web/comp/badge.js +292 -0
  35. data/lib/ozdenui/web/comp/bottom-bar.js +555 -0
  36. data/lib/ozdenui/web/comp/breadcrumbs.js +524 -0
  37. data/lib/ozdenui/web/comp/canvas.js +652 -0
  38. data/lib/ozdenui/web/comp/chat-view.js +827 -0
  39. data/lib/ozdenui/web/comp/check-box.js +490 -0
  40. data/lib/ozdenui/web/comp/code-editor.js +965 -0
  41. data/lib/ozdenui/web/comp/color-picker.js +636 -0
  42. data/lib/ozdenui/web/comp/context-menu.js +951 -0
  43. data/lib/ozdenui/web/comp/currency-field.js +209 -0
  44. data/lib/ozdenui/web/comp/data-table/arrow.svg +2 -0
  45. data/lib/ozdenui/web/comp/data-table.js +937 -0
  46. data/lib/ozdenui/web/comp/date-picker.js +1055 -0
  47. data/lib/ozdenui/web/comp/dialog.js +992 -0
  48. data/lib/ozdenui/web/comp/email-field.js +65 -0
  49. data/lib/ozdenui/web/comp/file-picker.js +920 -0
  50. data/lib/ozdenui/web/comp/filters.js +807 -0
  51. data/lib/ozdenui/web/comp/form.js +602 -0
  52. data/lib/ozdenui/web/comp/full-screen-select/arrow.svg +2 -0
  53. data/lib/ozdenui/web/comp/full-screen-select/cancel.svg +2 -0
  54. data/lib/ozdenui/web/comp/full-screen-select.js +614 -0
  55. data/lib/ozdenui/web/comp/gauge.js +439 -0
  56. data/lib/ozdenui/web/comp/graph-box.js +3911 -0
  57. data/lib/ozdenui/web/comp/grid-group.js +185 -0
  58. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash-red.png +0 -0
  59. data/lib/ozdenui/web/comp/hold-to-confirm-button/trash.png +0 -0
  60. data/lib/ozdenui/web/comp/hold-to-confirm-button.js +289 -0
  61. data/lib/ozdenui/web/comp/image-view.js +598 -0
  62. data/lib/ozdenui/web/comp/item-list.js +1740 -0
  63. data/lib/ozdenui/web/comp/json-editor.js +3221 -0
  64. data/lib/ozdenui/web/comp/json-view.js +597 -0
  65. data/lib/ozdenui/web/comp/label-editor.js +1140 -0
  66. data/lib/ozdenui/web/comp/left-menu/data-table.png +0 -0
  67. data/lib/ozdenui/web/comp/left-menu/home.png +0 -0
  68. data/lib/ozdenui/web/comp/left-menu/reports.png +0 -0
  69. data/lib/ozdenui/web/comp/left-menu/setting.png +0 -0
  70. data/lib/ozdenui/web/comp/left-menu.js +548 -0
  71. data/lib/ozdenui/web/comp/line-progress-bar.js +343 -0
  72. data/lib/ozdenui/web/comp/loading-screen.js +413 -0
  73. data/lib/ozdenui/web/comp/log-view.js +530 -0
  74. data/lib/ozdenui/web/comp/login-page.js +685 -0
  75. data/lib/ozdenui/web/comp/markdown-view.js +1045 -0
  76. data/lib/ozdenui/web/comp/menu-bar.js +624 -0
  77. data/lib/ozdenui/web/comp/mini-graph-box.js +492 -0
  78. data/lib/ozdenui/web/comp/minimal-accent.js +208 -0
  79. data/lib/ozdenui/web/comp/notice.js +229 -0
  80. data/lib/ozdenui/web/comp/number-field.js +128 -0
  81. data/lib/ozdenui/web/comp/numeric-keyboard.js +298 -0
  82. data/lib/ozdenui/web/comp/page-control.js +762 -0
  83. data/lib/ozdenui/web/comp/page-header.js +191 -0
  84. data/lib/ozdenui/web/comp/password-field/hide-btn.png +0 -0
  85. data/lib/ozdenui/web/comp/password-field/hide-btn2.png +0 -0
  86. data/lib/ozdenui/web/comp/password-field/show-btn.png +0 -0
  87. data/lib/ozdenui/web/comp/password-field/show-btn2.png +0 -0
  88. data/lib/ozdenui/web/comp/password-field.js +192 -0
  89. data/lib/ozdenui/web/comp/period-bar.js +436 -0
  90. data/lib/ozdenui/web/comp/phone-field.js +322 -0
  91. data/lib/ozdenui/web/comp/progress-bar.js +992 -0
  92. data/lib/ozdenui/web/comp/property-panel.js +3639 -0
  93. data/lib/ozdenui/web/comp/radio-button.js +527 -0
  94. data/lib/ozdenui/web/comp/range-slider.js +569 -0
  95. data/lib/ozdenui/web/comp/rich-text-editor.js +1064 -0
  96. data/lib/ozdenui/web/comp/scroll-area.js +308 -0
  97. data/lib/ozdenui/web/comp/scrolling-text.js +297 -0
  98. data/lib/ozdenui/web/comp/search-input/clear-round.svg +1 -0
  99. data/lib/ozdenui/web/comp/search-input/clear.svg +1 -0
  100. data/lib/ozdenui/web/comp/search-input/filter.png +0 -0
  101. data/lib/ozdenui/web/comp/search-input/filter.svg +1 -0
  102. data/lib/ozdenui/web/comp/search-input/search.svg +1 -0
  103. data/lib/ozdenui/web/comp/search-input.js +725 -0
  104. data/lib/ozdenui/web/comp/search-results.js +596 -0
  105. data/lib/ozdenui/web/comp/select-box.js +1862 -0
  106. data/lib/ozdenui/web/comp/settings-row.js +299 -0
  107. data/lib/ozdenui/web/comp/side-panel.js +616 -0
  108. data/lib/ozdenui/web/comp/slider-field.js +952 -0
  109. data/lib/ozdenui/web/comp/slider.js +838 -0
  110. data/lib/ozdenui/web/comp/smart-button/alert-black.png +0 -0
  111. data/lib/ozdenui/web/comp/smart-button/clock.png +0 -0
  112. data/lib/ozdenui/web/comp/smart-button.js +981 -0
  113. data/lib/ozdenui/web/comp/smart-table/clock.png +0 -0
  114. data/lib/ozdenui/web/comp/smart-table/down.png +0 -0
  115. data/lib/ozdenui/web/comp/smart-table/scroll.png +0 -0
  116. data/lib/ozdenui/web/comp/smart-table/sort.png +0 -0
  117. data/lib/ozdenui/web/comp/smart-table/up.png +0 -0
  118. data/lib/ozdenui/web/comp/smart-table.js +1883 -0
  119. data/lib/ozdenui/web/comp/sortable-list.js +1181 -0
  120. data/lib/ozdenui/web/comp/spark-line-box.js +738 -0
  121. data/lib/ozdenui/web/comp/splitter.js +411 -0
  122. data/lib/ozdenui/web/comp/status-bar.js +408 -0
  123. data/lib/ozdenui/web/comp/step-bar.js +345 -0
  124. data/lib/ozdenui/web/comp/stepper.js +753 -0
  125. data/lib/ozdenui/web/comp/tabs.js +1366 -0
  126. data/lib/ozdenui/web/comp/tag-input.js +1107 -0
  127. data/lib/ozdenui/web/comp/tap-effect.js +170 -0
  128. data/lib/ozdenui/web/comp/text-area-field.js +233 -0
  129. data/lib/ozdenui/web/comp/text-field.js +1006 -0
  130. data/lib/ozdenui/web/comp/time-line.js +398 -0
  131. data/lib/ozdenui/web/comp/time-picker.js +1032 -0
  132. data/lib/ozdenui/web/comp/tiny-select/arrow.svg +1 -0
  133. data/lib/ozdenui/web/comp/tiny-select.js +322 -0
  134. data/lib/ozdenui/web/comp/tiny-table.js +283 -0
  135. data/lib/ozdenui/web/comp/toast.js +891 -0
  136. data/lib/ozdenui/web/comp/toggle.js +757 -0
  137. data/lib/ozdenui/web/comp/tool-bar.js +760 -0
  138. data/lib/ozdenui/web/comp/tooltip.js +534 -0
  139. data/lib/ozdenui/web/comp/tree-view.js +1179 -0
  140. data/lib/ozdenui/web/comp/ui-effects.js +1097 -0
  141. data/lib/ozdenui/web/comp/ui-standards.js +403 -0
  142. data/lib/ozdenui/web/comp/url-field.js +190 -0
  143. data/lib/ozdenui/web/comp/web-view.js +588 -0
  144. data/lib/ozdenui/web/components.js +123 -0
  145. data/lib/ozdenui/web/index.html +39 -0
  146. data/lib/ozdenui/window.rb +188 -0
  147. data/lib/ozdenui/window_cocoa.rb +221 -0
  148. data/lib/ozdenui.rb +32 -0
  149. metadata +200 -0
@@ -0,0 +1,1064 @@
1
+ /* Bismillah */
2
+
3
+ /*
4
+
5
+ RichTextEditor - v26.09
6
+
7
+ UI COMPONENT TEMPLATE
8
+ - A simple rich text editor: a toolbar and a writing area.
9
+ - Tools: bold, italic, underline, strike, heading (h2, h3), bullet / numbered list, quote, link,
10
+ clear formatting, undo, redo. Choose them and their order with "tools" ("|" is a separator).
11
+ - The toolbar shows the format at the cursor (Ex: B is pressed inside bold text).
12
+ - Link: the link bar opens under the toolbar (no browser prompt). Ctrl / Cmd + K opens it too.
13
+ - Keyboard: Ctrl / Cmd + B, I, U (the browser), Ctrl / Cmd + Z undo. Tab leaves the editor (accessibility).
14
+ - Paste: "clean" (default) keeps only the allowed tags (the formats of the toolbar), "text" pastes plain text.
15
+ - getHtmlText() is always cleaned: only b, strong, i, em, u, s, strike, del, h2, h3, p, div, br, ul, ol, li,
16
+ blockquote and a (href: http, https, mailto, tel or a relative address). No scripts, styles or events.
17
+ setHtmlText() cleans its input the same way before it is shown.
18
+ - maxLength (characters of the text, 0: no limit) and a counter at the bottom right (showCounter: 1).
19
+ - placeholder, readOnly, enabled. height: the whole editor. The writing area scrolls with
20
+ basic/scroll-bar.js (if it is loaded).
21
+ - onChange(self) is called while the user writes. It is NOT called when the component is created or by setHtmlText().
22
+ - Everything is drawn with code (no image files needed).
23
+ - Style packages: "light" (default; the old name "classic" works too), "modern", "dark". Select with styleName. (RichTextEditor.styles)
24
+
25
+ USAGE:
26
+ const editor = RichTextEditor({
27
+ width: 560,
28
+ height: 300,
29
+ placeholder: "Write the product description...",
30
+ onChange: function (self) { println(self.getHtmlText()); },
31
+ });
32
+ editor.setHtmlText("<p>Hello <b>world</b></p>");
33
+ editor.getHtmlText(); // "<p>Hello <b>world</b></p>"
34
+ editor.getText(); // "Hello world"
35
+ editor.isEmpty(); // 0
36
+ RichTextEditor({ tools: ["bold", "italic", "|", "ul", "ol"], maxLength: 500, showCounter: 1 });
37
+ RichTextEditor({ styleName: "dark", pasteAs: "text" });
38
+
39
+ Started Date: September 2026
40
+ Webpage: https://bug7a.github.io/js-components/
41
+
42
+ Copyright 2020-2026 Bugra Ozden <bugra.ozden@gmail.com> (https://bug7a.github.io/)
43
+ Licensed under the Apache License, Version 2.0
44
+ SPDX-License-Identifier: Apache-2.0
45
+
46
+ */
47
+
48
+ "use strict";
49
+
50
+ // Default values:
51
+ const RichTextEditorDefaults = {
52
+ key: "0",
53
+ width: 560,
54
+ height: 300,
55
+ htmlText: "", // Start content (HTML, it is cleaned)
56
+ placeholder: "Write something...",
57
+ tools: ["bold", "italic", "underline", "strike", "|", "h2", "h3", "|", "ul", "ol", "quote", "|", "link", "clear", "|", "undo", "redo"],
58
+ pasteAs: "clean", // "clean": keeps the allowed formats, "text": plain text
59
+ maxLength: 0, // Characters of the text. 0: no limit
60
+ showCounter: 0, // 1: "120 / 500" (or "120 characters") at the bottom right
61
+ readOnly: 0,
62
+ enabled: 1,
63
+ spellCheck: 1,
64
+ useScrollBar: 1, // basic/scroll-bar.js for the writing area. 0: the scrollbar of the browser.
65
+ ariaLabel: "Text editor",
66
+ texts: {
67
+ bold: "Bold",
68
+ italic: "Italic",
69
+ underline: "Underline",
70
+ strike: "Strikethrough",
71
+ h2: "Heading",
72
+ h3: "Subheading",
73
+ ul: "Bulleted list",
74
+ ol: "Numbered list",
75
+ quote: "Quote",
76
+ link: "Link",
77
+ clear: "Clear formatting",
78
+ undo: "Undo",
79
+ redo: "Redo",
80
+ linkPlaceholder: "https://example.com",
81
+ linkApply: "Apply",
82
+ linkRemove: "Remove",
83
+ counter: "{count} / {max}", // With maxLength
84
+ counterNoLimit: "{count} characters", // Without maxLength
85
+ },
86
+ onChange: function (self) { }, // self.getHtmlText(), self.getText()
87
+ onFocus: function (self) { },
88
+ onBlur: function (self) { },
89
+ styleName: "light", // "light" ("classic": its old name), "modern", "dark" or a name added to RichTextEditor.styles
90
+ style: { // Light style package (default)
91
+ box: {
92
+ color: White(1),
93
+ border: 1,
94
+ borderColor: Black(0.2),
95
+ round: 8,
96
+ },
97
+ boxFocus: {
98
+ borderColor: "#141414",
99
+ },
100
+ toolbar: {
101
+ color: "#F7F7F6",
102
+ dividerColor: Black(0.1),
103
+ padding: 6,
104
+ gap: 2,
105
+ },
106
+ button: {
107
+ size: 32,
108
+ iconSize: 18,
109
+ iconColor: "#4A4A48",
110
+ hoverColor: Black(0.06),
111
+ activeColor: Black(0.1),
112
+ activeIconColor: "#141414",
113
+ round: 6,
114
+ },
115
+ separator: {
116
+ color: Black(0.12),
117
+ },
118
+ content: {
119
+ fontSize: 15,
120
+ lineHeight: 1.6,
121
+ textColor: "#141414",
122
+ fontFamily: "", // "": opensans
123
+ padding: [16, 14], // [x, y]
124
+ linkColor: "#2F6FB0",
125
+ quoteColor: Black(0.6),
126
+ quoteBorderColor: Black(0.2),
127
+ placeholderColor: Black(0.35),
128
+ selectionColor: "rgba(47, 111, 176, 0.2)",
129
+ },
130
+ linkBar: {
131
+ color: "#F7F7F6",
132
+ inputColor: White(1),
133
+ inputBorderColor: Black(0.2),
134
+ textColor: "#141414",
135
+ buttonColor: "#141414",
136
+ buttonTextColor: White(1),
137
+ secondaryTextColor: Black(0.6),
138
+ },
139
+ counter: {
140
+ fontSize: 12,
141
+ textColor: Black(0.45),
142
+ limitColor: "#C0392B", // At the limit
143
+ },
144
+ disabled: {
145
+ opacity: 0.55,
146
+ },
147
+ scrollBar: {
148
+ bar_color: "#9A9A98",
149
+ bar_mouseOverColor: "#6A6A68",
150
+ bar_width: 4,
151
+ bar_round: 3,
152
+ bar_opacity: 0.4,
153
+ bar_mouseOverOpacity: 0.9,
154
+ bar_padding: 2,
155
+ },
156
+ }
157
+ };
158
+
159
+ const RichTextEditor = function (params = {}) {
160
+
161
+ // Merge style package: params.style > RichTextEditor.styles[styleName] > RichTextEditorDefaults.style (light)
162
+ basic.applyStylePackage(params, RichTextEditor.styles, RichTextEditorDefaults);
163
+
164
+ // Merge params:
165
+ mergeIntoIfMissing(params, RichTextEditorDefaults);
166
+
167
+ // Edit params, if needed:
168
+ const _bs = params.style.box;
169
+ params.color = _bs.color;
170
+ params.border = _bs.border;
171
+ params.borderColor = _bs.borderColor;
172
+ params.round = _bs.round;
173
+
174
+ RichTextEditor.injectCss();
175
+
176
+ // BOX: Component container
177
+ let box = startObject(params);
178
+
179
+ // *** PRIVATE VARIABLES:
180
+ const _s = box.style;
181
+ const buttons = {}; // tool name -> Label
182
+ let savedRange = null; // The selection before the link bar took the focus
183
+ let isFocused = 0;
184
+ let lastHtml = "";
185
+ const BORDER_MOTION = "border-color 0.15s";
186
+ const BUTTON_MOTION = "background-color 0.12s";
187
+ const SEPARATOR_HEIGHT_RATIO = 0.6; // Of the button size
188
+ const TOOLBAR_DISABLED_OPACITY = "0.45";
189
+
190
+ // *** PUBLIC VARIABLES:
191
+ // box.editor: the contenteditable element (a raw <div>, not a basic.js object)
192
+ // NOTE: Default values are also public variables.
193
+
194
+ // *** PRIVATE FUNCTIONS:
195
+
196
+ const isEditable = function () {
197
+ return box.enabled == 1 && box.readOnly != 1;
198
+ };
199
+
200
+ const getTextLength = function () {
201
+ return box.editor.textContent.length;
202
+ };
203
+
204
+ const isSelectionInside = function () {
205
+ const sel = window.getSelection();
206
+ if (!sel || sel.rangeCount == 0) return false;
207
+ return box.editor.contains(sel.getRangeAt(0).commonAncestorContainer);
208
+ };
209
+
210
+ const selectRange = function (range) {
211
+ const sel = window.getSelection();
212
+ sel.removeAllRanges();
213
+ sel.addRange(range);
214
+ };
215
+
216
+ const selectContentsOf = function (node) {
217
+ const range = document.createRange();
218
+ range.selectNodeContents(node);
219
+ selectRange(range);
220
+ };
221
+
222
+ const saveSelection = function () {
223
+ if (isSelectionInside()) savedRange = window.getSelection().getRangeAt(0).cloneRange();
224
+ };
225
+
226
+ const restoreSelection = function () {
227
+ box.editor.focus({ preventScroll: true });
228
+ if (!savedRange) return;
229
+ selectRange(savedRange);
230
+ };
231
+
232
+ // The nearest block tag at the cursor: "h2", "h3", "blockquote", "p"...
233
+ const getCurrentBlock = function () {
234
+ if (!isSelectionInside()) return "";
235
+ let node = window.getSelection().getRangeAt(0).startContainer;
236
+ while (node && node !== box.editor) {
237
+ if (node.nodeType === 1 && /^(H2|H3|BLOCKQUOTE|P|DIV|LI)$/.test(node.nodeName)) {
238
+ // WHY: A list item inside a quote is still a quote.
239
+ if (node.nodeName === "LI") {
240
+ node = node.parentNode;
241
+ continue;
242
+ }
243
+ return node.nodeName.toLowerCase();
244
+ }
245
+ node = node.parentNode;
246
+ }
247
+ return "";
248
+ };
249
+
250
+ const getCurrentLink = function () {
251
+ if (!isSelectionInside()) return null;
252
+ let node = window.getSelection().getRangeAt(0).startContainer;
253
+ while (node && node !== box.editor) {
254
+ if (node.nodeName === "A") return node;
255
+ node = node.parentNode;
256
+ }
257
+ return null;
258
+ };
259
+
260
+ const exec = function (command, value = null) {
261
+ document.execCommand(command, false, value);
262
+ };
263
+
264
+ // Toggles a block format: <h2> on a heading makes it a paragraph again.
265
+ const toggleBlock = function (tag) {
266
+ exec("formatBlock", (getCurrentBlock() === tag) ? "<p>" : "<" + tag + ">");
267
+ };
268
+
269
+ // A tool of one execCommand: it is pressed when the browser says the format is on.
270
+ const createCommandTool = function (command, keys) {
271
+ return {
272
+ run: function () { exec(command); },
273
+ state: function () { return document.queryCommandState(command); },
274
+ keys: keys,
275
+ };
276
+ };
277
+
278
+ // A tool of one block tag (h2, h3, blockquote): it is pressed inside that block.
279
+ const createBlockTool = function (tag) {
280
+ return {
281
+ run: function () { toggleBlock(tag); },
282
+ state: function () { return getCurrentBlock() === tag; },
283
+ };
284
+ };
285
+
286
+ const isNeverPressed = function () {
287
+ return false;
288
+ };
289
+
290
+ const clearFormatting = function () {
291
+ exec("removeFormat");
292
+ exec("unlink");
293
+ exec("formatBlock", "<p>");
294
+ };
295
+
296
+ const TOOLS = {
297
+ bold: createCommandTool("bold", "B"),
298
+ italic: createCommandTool("italic", "I"),
299
+ underline: createCommandTool("underline", "U"),
300
+ strike: createCommandTool("strikeThrough"),
301
+ h2: createBlockTool("h2"),
302
+ h3: createBlockTool("h3"),
303
+ ul: createCommandTool("insertUnorderedList"),
304
+ ol: createCommandTool("insertOrderedList"),
305
+ quote: createBlockTool("blockquote"),
306
+ link: { run: function () { openLinkBar(); }, state: function () { return !!getCurrentLink(); }, keys: "K" },
307
+ clear: { run: clearFormatting, state: isNeverPressed },
308
+ undo: { run: function () { exec("undo"); }, state: isNeverPressed, keys: "Z" },
309
+ redo: { run: function () { exec("redo"); }, state: isNeverPressed },
310
+ };
311
+
312
+ const updateToolbar = function () {
313
+ if (!box) return;
314
+ const inside = isSelectionInside();
315
+ Object.keys(buttons).forEach(function (name) {
316
+ const btn = buttons[name];
317
+ const active = inside && isEditable() && TOOLS[name].state() ? 1 : 0;
318
+ btn.isActive = active;
319
+ btn.color = active ? _s.button.activeColor : "transparent";
320
+ btn.textColor = active ? _s.button.activeIconColor : _s.button.iconColor;
321
+ btn.aria = { pressed: active ? "true" : "false" };
322
+ });
323
+ };
324
+
325
+ const updatePlaceholder = function () {
326
+ box.editor.classList.toggle("is-empty", box.isEmpty() == 1);
327
+ };
328
+
329
+ const updateCounter = function () {
330
+ if (!box.lblCounter) return;
331
+ const count = getTextLength();
332
+ const hasLimit = (box.maxLength > 0);
333
+ const template = (hasLimit) ? box.texts.counter : box.texts.counterNoLimit;
334
+ box.lblCounter.plainText = template
335
+ .replace("{count}", String(count))
336
+ .replace("{max}", String(box.maxLength));
337
+ const isAtLimit = (hasLimit && count >= box.maxLength);
338
+ box.lblCounter.textColor = (isAtLimit) ? _s.counter.limitColor : _s.counter.textColor;
339
+ };
340
+
341
+ const updateBorder = function () {
342
+ box.borderColor = (isFocused && isEditable()) ? _s.boxFocus.borderColor : _s.box.borderColor;
343
+ };
344
+
345
+ // After every change of the content.
346
+ const contentChanged = function (fireChange) {
347
+ updatePlaceholder();
348
+ updateCounter();
349
+ updateToolbar();
350
+ if (box.scrollBar) box.scrollBar.refreshScroll();
351
+ if (!fireChange) {
352
+ lastHtml = box.getHtmlText();
353
+ return;
354
+ }
355
+ const html = box.getHtmlText();
356
+ if (html === lastHtml) return;
357
+ lastHtml = html;
358
+ box.onChange(box);
359
+ };
360
+
361
+ // Characters that can still be written (Infinity: no limit).
362
+ const getRemainingLength = function () {
363
+ if (!(box.maxLength > 0)) return Infinity;
364
+ let selected = 0;
365
+ if (isSelectionInside()) selected = window.getSelection().toString().length;
366
+ return Math.max(0, box.maxLength - getTextLength() + selected);
367
+ };
368
+
369
+ const insertPlainText = function (text) {
370
+ const room = getRemainingLength();
371
+ if (text.length > room) text = text.slice(0, room);
372
+ if (text) exec("insertText", text);
373
+ };
374
+
375
+ // *** LINK BAR:
376
+
377
+ const openLinkBar = function () {
378
+ if (!isEditable()) return;
379
+ saveSelection();
380
+ const link = getCurrentLink();
381
+ box.linkInput.text = link ? link.getAttribute("href") || "" : "";
382
+ box.btnLinkRemove.visible = link ? 1 : 0;
383
+ box.linkBar.visible = 1;
384
+ box.linkInput.inputElement.focus({ preventScroll: true });
385
+ box.linkInput.inputElement.select();
386
+ };
387
+
388
+ const closeLinkBar = function (focusEditor = 1) {
389
+ if (box.linkBar.visible != 1) return;
390
+ box.linkBar.visible = 0;
391
+ if (focusEditor) restoreSelection();
392
+ savedRange = null;
393
+ };
394
+
395
+ // "example.com" -> "https://example.com", "name@site.com" -> "mailto:name@site.com"
396
+ const completeHref = function (href) {
397
+ if (/^[^\s@\/]+@[^\s@\/]+\.[^\s@\/]+$/.test(href)) return "mailto:" + href;
398
+ if (!/^([a-z][a-z0-9+.-]*:|\/|#|\.)/i.test(href)) return "https://" + href;
399
+ return href;
400
+ };
401
+
402
+ // Returns 0 when the link could not be written (the bar is already closed then).
403
+ const writeLink = function (href) {
404
+
405
+ const sel = window.getSelection();
406
+
407
+ if (sel.isCollapsed && !getCurrentLink()) {
408
+ // No selected text: the address becomes the text of the link.
409
+ if (href.length > getRemainingLength()) {
410
+ closeLinkBar(1);
411
+ return 0;
412
+ }
413
+ const safeHref = RichTextEditor.escapeHtml(href);
414
+ exec("insertHTML", '<a href="' + safeHref + '">' + safeHref + "</a>&nbsp;");
415
+ return 1;
416
+ }
417
+
418
+ // Inside a link: change the whole link.
419
+ if (sel.isCollapsed) selectContentsOf(getCurrentLink());
420
+ exec("createLink", href);
421
+ return 1;
422
+
423
+ };
424
+
425
+ const applyLink = function () {
426
+
427
+ const href = String(box.linkInput.text || "").trim();
428
+ restoreSelection();
429
+
430
+ if (!href) {
431
+ exec("unlink");
432
+ } else {
433
+ const fullHref = completeHref(href);
434
+ if (!RichTextEditor.isSafeHref(fullHref)) {
435
+ box.linkInput.inputElement.focus();
436
+ return;
437
+ }
438
+ if (!writeLink(fullHref)) return;
439
+ }
440
+
441
+ box.linkBar.visible = 0;
442
+ savedRange = null;
443
+ contentChanged(1);
444
+
445
+ };
446
+
447
+ const removeLink = function () {
448
+ restoreSelection();
449
+ const link = getCurrentLink();
450
+ if (link) selectContentsOf(link);
451
+ exec("unlink");
452
+ box.linkBar.visible = 0;
453
+ savedRange = null;
454
+ contentChanged(1);
455
+ };
456
+
457
+ // *** EDITOR EVENTS:
458
+
459
+ const onBeforeInput = function (event) {
460
+ if (!isEditable()) {
461
+ event.preventDefault();
462
+ return;
463
+ }
464
+ if (!(box.maxLength > 0)) return;
465
+ const type = event.inputType || "";
466
+ // WHY: Typing, Enter and drop add characters. Paste is handled in onPaste.
467
+ const isInsert = (type.indexOf("insert") === 0 && type !== "insertFromPaste" && type !== "insertLink");
468
+ if (!isInsert) return;
469
+ if (getRemainingLength() > 0) return;
470
+ if (type === "insertParagraph" || type === "insertLineBreak") return; // A new line adds no character to the text.
471
+ event.preventDefault();
472
+ };
473
+
474
+ const onPaste = function (event) {
475
+ event.preventDefault();
476
+ if (!isEditable()) return;
477
+ const data = event.clipboardData;
478
+ if (!data) return;
479
+ const text = data.getData("text/plain") || "";
480
+ const html = (box.pasteAs === "clean") ? data.getData("text/html") : "";
481
+ if (html) {
482
+ const clean = RichTextEditor.cleanHtml(html);
483
+ const cleanText = RichTextEditor.htmlToText(clean);
484
+ if (cleanText.length <= getRemainingLength()) {
485
+ exec("insertHTML", clean);
486
+ return;
487
+ }
488
+ }
489
+ insertPlainText(text);
490
+ };
491
+
492
+ const onDrop = function (event) {
493
+ // WHY: Files and images from outside are not part of this editor.
494
+ const files = event.dataTransfer && event.dataTransfer.files;
495
+ if (files && files.length) event.preventDefault();
496
+ };
497
+
498
+ const onKeyDown = function (event) {
499
+ const hasModifier = event.ctrlKey || event.metaKey;
500
+ if (!hasModifier || event.altKey) return;
501
+ if (event.key !== "k" && event.key !== "K") return;
502
+ event.preventDefault();
503
+ if (buttons.link) openLinkBar();
504
+ };
505
+
506
+ const onEditorFocus = function () {
507
+ isFocused = 1;
508
+ updateBorder();
509
+ updateToolbar();
510
+ box.onFocus(box);
511
+ };
512
+
513
+ const onEditorBlur = function () {
514
+ isFocused = 0;
515
+ updateBorder();
516
+ box.onBlur(box);
517
+ };
518
+
519
+ // WHY: mousedown takes the focus and the selection from the editor. preventDefault keeps them.
520
+ const keepEditorFocus = function (self, event) {
521
+ event.preventDefault();
522
+ };
523
+
524
+ const onSelectionChange = function () {
525
+ if (isSelectionInside()) updateToolbar();
526
+ };
527
+
528
+ // *** PUBLIC FUNCTIONS:
529
+
530
+ box.getHtmlText = function () {
531
+ if (box.isEmpty()) return "";
532
+ return RichTextEditor.cleanHtml(box.editor.innerHTML);
533
+ };
534
+ // USAGE: const html = editor.getHtmlText(); // "" when it is empty
535
+
536
+ // The content is cleaned before it is shown. onChange is not called.
537
+ box.setHtmlText = function (html) {
538
+ box.htmlText = RichTextEditor.cleanHtml(html || "");
539
+ box.editor.innerHTML = box.htmlText || "<p><br></p>"; // WHY: The first line is a paragraph too, not a loose text.
540
+ contentChanged(0);
541
+ };
542
+ // USAGE: editor.setHtmlText("<p>Hello <b>world</b></p>")
543
+
544
+ box.getText = function () {
545
+ return RichTextEditor.htmlToText(box.editor.innerHTML);
546
+ };
547
+ // USAGE: editor.getText() // Plain text, lines separated by "\n"
548
+
549
+ box.getLength = function () {
550
+ return getTextLength();
551
+ };
552
+
553
+ box.isEmpty = function () {
554
+ return (box.editor.textContent.trim() === "" && !box.editor.querySelector("li, a")) ? 1 : 0;
555
+ };
556
+
557
+ box.clear = function () {
558
+ box.setHtmlText("");
559
+ };
560
+
561
+ // Inserts text at the cursor (at the end, when the editor does not have the focus). onChange is called.
562
+ box.insertText = function (text) {
563
+ if (!isEditable()) return;
564
+ if (!isSelectionInside()) box.focusEnd();
565
+ insertPlainText(String(text ?? ""));
566
+ };
567
+
568
+ box.insertHtml = function (html) {
569
+ if (!isEditable()) return;
570
+ if (!isSelectionInside()) box.focusEnd();
571
+ const clean = RichTextEditor.cleanHtml(html || "");
572
+ if (RichTextEditor.htmlToText(clean).length <= getRemainingLength()) exec("insertHTML", clean);
573
+ };
574
+
575
+ // Runs a tool like a click on its button: "bold", "h2", "ul", "link"...
576
+ box.runTool = function (name) {
577
+ if (!isEditable() || !TOOLS[name]) return;
578
+ if (!isSelectionInside()) restoreSelection();
579
+ TOOLS[name].run();
580
+ if (name !== "link") contentChanged(1);
581
+ };
582
+
583
+ box.focus = function () {
584
+ box.editor.focus({ preventScroll: true });
585
+ };
586
+
587
+ box.focusEnd = function () {
588
+ box.editor.focus({ preventScroll: true });
589
+ const range = document.createRange();
590
+ range.selectNodeContents(box.editor);
591
+ range.collapse(false);
592
+ selectRange(range);
593
+ };
594
+
595
+ box.setPlaceholder = function (text) {
596
+ box.placeholder = String(text ?? "");
597
+ box.editor.setAttribute("data-placeholder", box.placeholder);
598
+ };
599
+
600
+ box.setMaxLength = function (maxLength) {
601
+ box.maxLength = Math.max(0, Number(maxLength) || 0);
602
+ updateCounter();
603
+ };
604
+
605
+ box.setReadOnly = function (readOnly) {
606
+ box.readOnly = basic.isOn(readOnly) ? 1 : 0;
607
+ applyEditableState();
608
+ };
609
+ // USAGE: editor.setReadOnly(1) // The text can be selected and copied, not changed.
610
+
611
+ box.setEnabled = function (enabled) {
612
+ box.enabled = basic.isOn(enabled) ? 1 : 0;
613
+ applyEditableState();
614
+ };
615
+ // USAGE: get: editor.enabled, set: editor.setEnabled(0)
616
+
617
+ const applyEditableState = function () {
618
+ const editable = isEditable();
619
+ box.editor.contentEditable = editable ? "true" : "false";
620
+ box.editor.tabIndex = (box.enabled == 1) ? 0 : -1;
621
+ box.editor.setAttribute("aria-readonly", (box.readOnly == 1) ? "true" : "false");
622
+ box.editor.setAttribute("aria-disabled", (box.enabled == 1) ? "false" : "true");
623
+ box.editor.style.userSelect = (box.enabled == 1) ? "text" : "none";
624
+ box.css.opacity = (box.enabled == 1) ? "1" : String(_s.disabled.opacity);
625
+ box.toolbar.css = {
626
+ opacity: editable ? "1" : TOOLBAR_DISABLED_OPACITY,
627
+ pointerEvents: editable ? "" : "none",
628
+ };
629
+ if (!editable && box.linkBar.visible == 1) closeLinkBar(0);
630
+ updateBorder();
631
+ updateToolbar();
632
+ };
633
+
634
+ box.refresh = function () {
635
+ contentChanged(0);
636
+ };
637
+
638
+
639
+ // *** OBJECT VIEW:
640
+
641
+ box.clipContent = 1;
642
+ box.css.transition = BORDER_MOTION;
643
+
644
+ // " (⌘B)" or " (Ctrl+B)", "" for a tool without a shortcut
645
+ const getShortcutText = function (name) {
646
+ const keys = TOOLS[name].keys;
647
+ if (!keys) return "";
648
+ const modifier = (/Mac|iPhone|iPad/.test(navigator.platform)) ? "⌘" : "Ctrl+";
649
+ return " (" + modifier + keys + ")";
650
+ };
651
+
652
+ // LABEL: A toolbar button, the icon is an SVG with currentColor
653
+ const createToolButton = function (name) {
654
+ const toolText = box.texts[name] || name;
655
+ const btn = Label({
656
+ text: RichTextEditor.getIconSvg(name, _s.button.iconSize),
657
+ width: _s.button.size,
658
+ height: _s.button.size,
659
+ round: _s.button.round,
660
+ color: "transparent",
661
+ textColor: _s.button.iconColor,
662
+ clickable: 1,
663
+ });
664
+ btn.elem.style.display = "flex";
665
+ btn.css = { alignItems: "center", justifyContent: "center", transition: BUTTON_MOTION };
666
+ btn.lineHeight = "0";
667
+ btn.shrink = 0;
668
+ btn.cursor = "pointer";
669
+ btn.role = "button";
670
+ btn.aria = { label: toolText };
671
+ btn.elem.setAttribute("title", toolText + getShortcutText(name));
672
+ btn.isActive = 0;
673
+ btn.on("mouseenter", function () {
674
+ if (btn.isActive) return;
675
+ btn.color = _s.button.hoverColor;
676
+ });
677
+ btn.on("mouseleave", function () {
678
+ btn.color = btn.isActive ? _s.button.activeColor : "transparent";
679
+ });
680
+ btn.on("mousedown", keepEditorFocus);
681
+ btn.on("click", function () { box.runTool(name); });
682
+ buttons[name] = btn;
683
+ return btn;
684
+ };
685
+
686
+ // GROUP: Layout (toolbar, link bar, writing area, counter)
687
+ box.layout = VGroup({ width: "100%", height: "100%", align: "left top", gap: 0 });
688
+ box.layout.css.alignItems = "stretch";
689
+
690
+ // GROUP: Toolbar
691
+ box.toolbar = HGroup({
692
+ width: "100%",
693
+ height: "auto",
694
+ align: "left center",
695
+ gap: _s.toolbar.gap,
696
+ padding: _s.toolbar.padding,
697
+ color: _s.toolbar.color,
698
+ wrap: 1,
699
+ });
700
+ box.toolbar.shrink = 0;
701
+ box.toolbar.css.borderBottom = "1px solid " + _s.toolbar.dividerColor;
702
+ box.toolbar.role = "toolbar";
703
+ box.toolbar.aria = { label: box.ariaLabel };
704
+
705
+ box.tools.forEach(function (name) {
706
+ if (name === "|") {
707
+ // BOX: Separator
708
+ Box({ width: 1, height: Math.round(_s.button.size * SEPARATOR_HEIGHT_RATIO), color: _s.separator.color });
709
+ that.css.margin = "0px 4px";
710
+ that.shrink = 0;
711
+ } else if (TOOLS[name]) {
712
+ createToolButton(name);
713
+ } else {
714
+ console.warn("RichTextEditor: Unknown tool: " + name);
715
+ }
716
+ });
717
+
718
+ endGroup();
719
+
720
+ // GROUP: Link bar (hidden until the link tool is used)
721
+ box.linkBar = HGroup({
722
+ width: "100%",
723
+ height: "auto",
724
+ align: "left center",
725
+ gap: 6,
726
+ padding: [_s.toolbar.padding + 2, _s.toolbar.padding],
727
+ color: _s.linkBar.color,
728
+ });
729
+ box.linkBar.shrink = 0;
730
+ box.linkBar.css.borderBottom = "1px solid " + _s.toolbar.dividerColor;
731
+
732
+ // INPUT: Address
733
+ box.linkInput = Input({
734
+ width: "auto",
735
+ height: 32,
736
+ minimal: 1,
737
+ fontSize: 14,
738
+ color: _s.linkBar.inputColor,
739
+ textColor: _s.linkBar.textColor,
740
+ round: 6,
741
+ border: 1,
742
+ borderColor: _s.linkBar.inputBorderColor,
743
+ });
744
+ box.linkInput.css = { flex: "1 1 0", minWidth: "0" };
745
+ box.linkInput.inputElement.style.padding = "0px 10px";
746
+ box.linkInput.inputElement.style.width = "100%";
747
+ box.linkInput.inputElement.style.height = "100%";
748
+ box.linkInput.inputElement.style.boxSizing = "border-box";
749
+ box.linkInput.inputElement.style.backgroundColor = "transparent";
750
+ box.linkInput.inputElement.setAttribute("placeholder", box.texts.linkPlaceholder);
751
+ box.linkInput.inputElement.setAttribute("aria-label", box.texts.link);
752
+ box.linkInput.inputElement.setAttribute("inputmode", "url");
753
+
754
+ // LABEL: Apply
755
+ box.btnLinkApply = Label({
756
+ plainText: box.texts.linkApply,
757
+ height: 32,
758
+ padding: [14, 0],
759
+ fontSize: 13,
760
+ round: 6,
761
+ color: _s.linkBar.buttonColor,
762
+ textColor: _s.linkBar.buttonTextColor,
763
+ clickable: 1,
764
+ });
765
+ box.btnLinkApply.elem.style.display = "flex";
766
+ box.btnLinkApply.css.alignItems = "center";
767
+ box.btnLinkApply.shrink = 0;
768
+ box.btnLinkApply.cursor = "pointer";
769
+ box.btnLinkApply.role = "button";
770
+
771
+ // LABEL: Remove the link
772
+ box.btnLinkRemove = Label({
773
+ plainText: box.texts.linkRemove,
774
+ height: 32,
775
+ padding: [8, 0],
776
+ fontSize: 13,
777
+ color: "transparent",
778
+ textColor: _s.linkBar.secondaryTextColor,
779
+ clickable: 1,
780
+ });
781
+ box.btnLinkRemove.elem.style.display = "flex";
782
+ box.btnLinkRemove.css.alignItems = "center";
783
+ box.btnLinkRemove.shrink = 0;
784
+ box.btnLinkRemove.cursor = "pointer";
785
+ box.btnLinkRemove.role = "button";
786
+
787
+ endGroup();
788
+ box.linkBar.visible = 0; // WHY: Hidden after its children are created (they stay flex items).
789
+
790
+ // BOX: Holder of the writing area (and its ScrollBar)
791
+ box.editHolder = startBox({ width: "100%", height: "auto", color: "transparent" });
792
+ box.editHolder.css = { flex: "1 1 0", minHeight: "0" };
793
+
794
+ // BOX: Scrolling area
795
+ box.editArea = Box(0, 0, "100%", "100%", { color: "transparent", scrollY: 1 });
796
+ box.editArea.cursor = "text";
797
+ box.editArea.clickable = 1;
798
+
799
+ // The writing area: a raw contenteditable <div>
800
+ box.editor = document.createElement("div");
801
+ box.editor.className = "rich-text-editor-content";
802
+ box.editor.setAttribute("role", "textbox");
803
+ box.editor.setAttribute("aria-multiline", "true");
804
+ box.editor.setAttribute("aria-label", box.ariaLabel);
805
+ box.editor.spellcheck = (box.spellCheck == 1);
806
+ const _cs = _s.content;
807
+ const _ed = box.editor.style;
808
+ _ed.minHeight = "100%";
809
+ _ed.boxSizing = "border-box";
810
+ _ed.padding = _cs.padding[1] + "px " + _cs.padding[0] + "px";
811
+ _ed.fontSize = _cs.fontSize + "px";
812
+ _ed.lineHeight = String(_cs.lineHeight);
813
+ _ed.color = _cs.textColor;
814
+ if (_cs.fontFamily) _ed.fontFamily = _cs.fontFamily;
815
+ _ed.setProperty("--rte-link", _cs.linkColor);
816
+ _ed.setProperty("--rte-quote", _cs.quoteColor);
817
+ _ed.setProperty("--rte-quote-border", _cs.quoteBorderColor);
818
+ _ed.setProperty("--rte-placeholder", _cs.placeholderColor);
819
+ _ed.setProperty("--rte-selection", _cs.selectionColor);
820
+ box.editArea.elem.appendChild(box.editor);
821
+
822
+ if (box.useScrollBar == 1 && typeof ScrollBar !== "undefined") {
823
+ const scrollBarParams = { scrollableBox: box.editArea, neverHide: 0, showDots: 0 };
824
+ box.scrollBar = ScrollBar(Object.assign(scrollBarParams, _s.scrollBar));
825
+ }
826
+
827
+ endBox();
828
+
829
+ // LABEL: Counter
830
+ if (box.showCounter == 1) {
831
+ box.lblCounter = Label({
832
+ plainText: "",
833
+ width: "100%",
834
+ fontSize: _s.counter.fontSize,
835
+ textColor: _s.counter.textColor,
836
+ textAlign: "right",
837
+ padding: [_s.content.padding[0], 6],
838
+ });
839
+ box.lblCounter.shrink = 0;
840
+ box.lblCounter.css.fontVariantNumeric = "tabular-nums";
841
+ }
842
+
843
+ endGroup();
844
+
845
+ // *** OBJECT INIT CODE:
846
+
847
+ // WHY: <b>, <i> instead of <span style>. (try: an old browser may not know the command)
848
+ try {
849
+ document.execCommand("styleWithCSS", false, false);
850
+ } catch (e) { }
851
+ try {
852
+ document.execCommand("defaultParagraphSeparator", false, "p");
853
+ } catch (e) { }
854
+
855
+ box.editor.addEventListener("input", function () { contentChanged(1); });
856
+ box.editor.addEventListener("beforeinput", onBeforeInput);
857
+ box.editor.addEventListener("paste", onPaste);
858
+ box.editor.addEventListener("drop", onDrop);
859
+ box.editor.addEventListener("keydown", onKeyDown);
860
+ box.editor.addEventListener("focus", onEditorFocus);
861
+ box.editor.addEventListener("blur", onEditorBlur);
862
+ // WHY: A click under the text (in the empty part of the area) puts the cursor at the end.
863
+ box.editArea.on("mousedown", function (self, event) {
864
+ if (event.target !== box.editArea.elem || !isEditable()) return;
865
+ event.preventDefault();
866
+ box.focusEnd();
867
+ });
868
+ box.listenTo(document, "selectionchange", onSelectionChange); // (removed with the box)
869
+
870
+ box.linkInput.inputElement.addEventListener("keydown", function (event) {
871
+ if (event.key === "Enter") {
872
+ event.preventDefault();
873
+ applyLink();
874
+ return;
875
+ }
876
+ if (event.key === "Escape") {
877
+ event.preventDefault();
878
+ closeLinkBar(1);
879
+ }
880
+ });
881
+ box.btnLinkApply.on("mousedown", keepEditorFocus);
882
+ box.btnLinkApply.on("click", applyLink);
883
+ box.btnLinkRemove.on("mousedown", keepEditorFocus);
884
+ box.btnLinkRemove.on("click", removeLink);
885
+
886
+ box.setPlaceholder(box.placeholder);
887
+ box.setHtmlText(box.htmlText);
888
+ box.setMaxLength(box.maxLength);
889
+ applyEditableState();
890
+
891
+ return endObject(box);
892
+
893
+ };
894
+
895
+ // *** STATIC FUNCTIONS:
896
+
897
+ RichTextEditor.ALLOWED_TAGS = ["B", "STRONG", "I", "EM", "U", "S", "STRIKE", "DEL", "H2", "H3", "P", "DIV", "BR", "UL", "OL", "LI", "BLOCKQUOTE", "A"];
898
+ RichTextEditor.DROPPED_TAGS = ["SCRIPT", "STYLE", "IFRAME", "OBJECT", "EMBED", "NOSCRIPT", "TEMPLATE", "HEAD", "TITLE", "META", "LINK", "SVG", "MATH", "IMG", "VIDEO", "AUDIO", "CANVAS", "INPUT", "TEXTAREA", "SELECT", "BUTTON", "FORM"];
899
+
900
+ RichTextEditor.isSafeHref = function (href) {
901
+ const value = String(href || "").trim();
902
+ if (!value) return false;
903
+ if (/^(https?:|mailto:|tel:)/i.test(value)) return true;
904
+ // Relative addresses (no other scheme like javascript:)
905
+ const compactValue = value.replace(/[\s\u0000-\u001F]/g, "");
906
+ return !/^[a-z][a-z0-9+.-]*:/i.test(compactValue);
907
+ };
908
+
909
+ // Keeps only the allowed tags, without attributes (a: only a safe href). Unknown tags are unwrapped.
910
+ // WHY: DOMParser makes an inert document. Its scripts do not run and its images do not load.
911
+ RichTextEditor.cleanHtml = function (html) {
912
+ const doc = new DOMParser().parseFromString("<body>" + String(html || "") + "</body>", "text/html");
913
+ const out = document.createElement("div");
914
+
915
+ const copy = function (source, target) {
916
+ source.childNodes.forEach(function (node) {
917
+ if (node.nodeType === 3) {
918
+ target.appendChild(document.createTextNode(node.nodeValue));
919
+ return;
920
+ }
921
+ if (node.nodeType !== 1) return; // Comments...
922
+ const tag = node.nodeName.toUpperCase();
923
+ if (RichTextEditor.DROPPED_TAGS.indexOf(tag) > -1) return;
924
+ if (RichTextEditor.ALLOWED_TAGS.indexOf(tag) === -1) {
925
+ copy(node, target); // Unwrap: <span>text</span> -> text
926
+ return;
927
+ }
928
+ const el = document.createElement(tag.toLowerCase());
929
+ if (tag === "A") {
930
+ const href = node.getAttribute("href");
931
+ if (!RichTextEditor.isSafeHref(href)) {
932
+ copy(node, target);
933
+ return;
934
+ }
935
+ el.setAttribute("href", href.trim());
936
+ }
937
+ copy(node, el);
938
+ target.appendChild(el);
939
+ });
940
+ };
941
+
942
+ copy(doc.body, out);
943
+ return out.innerHTML;
944
+ };
945
+ // USAGE: RichTextEditor.cleanHtml('<p onclick="x()">Hi <script>..</script><b>there</b></p>') // "<p>Hi <b>there</b></p>"
946
+
947
+ // Plain text with line breaks for the blocks and the list items.
948
+ RichTextEditor.htmlToText = function (html) {
949
+ const doc = new DOMParser().parseFromString("<body>" + String(html || "") + "</body>", "text/html");
950
+ const lines = [];
951
+ let line = "";
952
+ const endLine = function () {
953
+ lines.push(line);
954
+ line = "";
955
+ };
956
+ const walk = function (node) {
957
+ node.childNodes.forEach(function (child) {
958
+ if (child.nodeType === 3) {
959
+ line += child.nodeValue.replace(/\s+/g, " ");
960
+ return;
961
+ }
962
+ if (child.nodeType !== 1) return;
963
+ const tag = child.nodeName;
964
+ if (tag === "BR") {
965
+ endLine();
966
+ return;
967
+ }
968
+ const isBlock = /^(P|DIV|H[1-6]|LI|BLOCKQUOTE|UL|OL)$/.test(tag);
969
+ if (isBlock && line.trim()) endLine();
970
+ if (tag === "LI") line = "- ";
971
+ walk(child);
972
+ if (isBlock && line.trim()) endLine();
973
+ });
974
+ };
975
+ walk(doc.body);
976
+ if (line.trim()) lines.push(line);
977
+ return lines.map(function (l) { return l.trim(); }).join("\n").trim();
978
+ };
979
+
980
+ RichTextEditor.escapeHtml = basic.escapeHtml; // (kept: the name is used outside)
981
+
982
+ // Icons use currentColor: the color of the button label.
983
+ RichTextEditor.getIconSvg = function (name, size) {
984
+ const open = '<svg xmlns="http://www.w3.org/2000/svg" width="' + size + '" height="' + size + '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">';
985
+ const letter = function (text, extra) {
986
+ return '<text x="12" y="17.5" text-anchor="middle" font-size="16" fill="currentColor" stroke="none" font-family="Georgia, \'Times New Roman\', serif" ' + (extra || "") + ">" + text + "</text>";
987
+ };
988
+ const icons = {
989
+ bold: letter("B", 'font-weight="700"'),
990
+ italic: letter("I", 'font-style="italic"'),
991
+ underline: letter("U", 'y="16"') + '<path d="M6 21h12"/>',
992
+ strike: letter("S") + '<path d="M4 12h16"/>',
993
+ h2: '<text x="12" y="16.5" text-anchor="middle" font-size="12" font-weight="700" fill="currentColor" stroke="none" font-family="system-ui, sans-serif">H2</text>',
994
+ h3: '<text x="12" y="16.5" text-anchor="middle" font-size="12" font-weight="700" fill="currentColor" stroke="none" font-family="system-ui, sans-serif">H3</text>',
995
+ ul: '<path d="M9 6h11M9 12h11M9 18h11"/><circle cx="4.5" cy="6" r="1" fill="currentColor"/><circle cx="4.5" cy="12" r="1" fill="currentColor"/><circle cx="4.5" cy="18" r="1" fill="currentColor"/>',
996
+ ol: '<path d="M10 6h10M10 12h10M10 18h10"/><path d="M4 5l1.5-1v5" stroke-width="1.6"/><path d="M3.5 14.5c0-1 2.5-1.2 2.5 0 0 1-2.5 2-2.5 3.5H6" stroke-width="1.6"/>',
997
+ quote: '<path d="M7 7h3v4c0 3-1.5 5-4 6M15 7h3v4c0 3-1.5 5-4 6"/>',
998
+ link: '<path d="M10 13a5 5 0 0 0 7.5.5l3-3a5 5 0 0 0-7-7l-1.7 1.7"/><path d="M14 11a5 5 0 0 0-7.5-.5l-3 3a5 5 0 0 0 7 7l1.7-1.7"/>',
999
+ clear: letter("T", 'x="10"') + '<path d="M15 14l6 6M21 14l-6 6"/>',
1000
+ undo: '<path d="M9 14L4 9l5-5"/><path d="M4 9h10.5a5.5 5.5 0 0 1 0 11H11"/>',
1001
+ redo: '<path d="M15 14l5-5-5-5"/><path d="M20 9H9.5a5.5 5.5 0 0 0 0 11H13"/>',
1002
+ };
1003
+ return open + (icons[name] || "") + "</svg>";
1004
+ };
1005
+
1006
+ // Styles of the content (headings, lists, quotes, links, the placeholder) can not be set with inline styles.
1007
+ RichTextEditor.injectCss = function () {
1008
+ const c = ".rich-text-editor-content";
1009
+ basic.addCss("rich-text-editor-css",
1010
+ c + " { position: relative; outline: none; pointer-events: auto; user-select: text; -webkit-user-select: text; overflow-wrap: break-word; word-break: break-word; }" +
1011
+ c + " p, " + c + " div { margin: 0 0 0.6em 0; }" +
1012
+ c + " h2 { font-size: 1.45em; line-height: 1.3; margin: 0.4em 0 0.4em 0; font-weight: 700; }" +
1013
+ c + " h3 { font-size: 1.2em; line-height: 1.35; margin: 0.4em 0 0.35em 0; font-weight: 700; }" +
1014
+ // WHY: font-weight must stay bold: execCommand("bold") and queryCommandState("bold") read the computed weight.
1015
+ c + " b, " + c + " strong { font-weight: 700; }" +
1016
+ c + " ul, " + c + " ol { margin: 0 0 0.6em 0; padding-left: 1.6em; }" +
1017
+ c + " li { margin: 0.15em 0; }" +
1018
+ c + " blockquote { margin: 0 0 0.6em 0; padding: 0.1em 0 0.1em 1em; border-left: 3px solid var(--rte-quote-border); color: var(--rte-quote); }" +
1019
+ c + " a { color: var(--rte-link); text-decoration: underline; cursor: text; }" +
1020
+ c + " > :first-child { margin-top: 0; }" +
1021
+ c + " ::selection { background: var(--rte-selection); }" +
1022
+ c + ".is-empty::before { content: attr(data-placeholder); position: absolute; color: var(--rte-placeholder); pointer-events: none; }");
1023
+ };
1024
+
1025
+ // *** STYLE PACKAGES:
1026
+ // USAGE: RichTextEditor({ styleName: "modern" })
1027
+ // USAGE: RichTextEditor({ styleName: "dark", style: { content: { fontSize: 16 } } }) // Change only some keys.
1028
+ // USAGE: RichTextEditor({ style: RichTextEditor.styles.dark }) // The same as styleName: "dark". (The package is copied, never changed.)
1029
+ // NOTE: A new package needs only the keys that differ from the default (classic) style.
1030
+ // USAGE: RichTextEditor.styles.myStyle = { toolbar: { color: "ivory" } };
1031
+ RichTextEditor.styles = {
1032
+
1033
+ // White area, light gray toolbar.
1034
+ light: RichTextEditorDefaults.style,
1035
+
1036
+ // Rounder, cadetblue. Same colors as the modern Stepper, SortableList, CheckBox and Tabs.
1037
+ modern: {
1038
+ box: { color: White(1), border: 1, borderColor: "#D9DADB", round: 14 },
1039
+ boxFocus: { borderColor: "cadetblue" },
1040
+ toolbar: { color: White(1), dividerColor: "#E3ECEC", padding: 8, gap: 2 },
1041
+ button: { size: 32, iconSize: 18, iconColor: "#4F7C7D", hoverColor: "#EEF5F5", activeColor: "#DFECEC", activeIconColor: "#2F5F61", round: 100 },
1042
+ separator: { color: "#E3ECEC" },
1043
+ content: { fontSize: 15, lineHeight: 1.6, textColor: "#2F5F61", fontFamily: "", padding: [18, 14], linkColor: "#2E8B8F", quoteColor: "#5C8A8B", quoteBorderColor: "cadetblue", placeholderColor: "#8FB5B6", selectionColor: "rgba(95, 158, 160, 0.22)" },
1044
+ linkBar: { color: "#F5F9F9", inputColor: White(1), inputBorderColor: "#CFE2E2", textColor: "#2F5F61", buttonColor: "cadetblue", buttonTextColor: White(1), secondaryTextColor: "#5C8A8B" },
1045
+ counter: { fontSize: 12, textColor: "#6E9A9B", limitColor: "#C0392B" },
1046
+ scrollBar: { bar_color: "#CFE2E2", bar_mouseOverColor: "cadetblue", bar_width: 4, bar_round: 3, bar_opacity: 0.6, bar_mouseOverOpacity: 1, bar_padding: 2 },
1047
+ },
1048
+
1049
+ // Dark theme, same colors as SelectBox, TagInput, Tabs and Stepper "dark".
1050
+ dark: {
1051
+ box: { color: "#232322", border: 1, borderColor: "rgba(255, 255, 255, 0.10)", round: 8 },
1052
+ boxFocus: { borderColor: "#65A293" },
1053
+ toolbar: { color: "#1C1C1B", dividerColor: "rgba(255, 255, 255, 0.08)", padding: 6, gap: 2 },
1054
+ button: { size: 32, iconSize: 18, iconColor: "rgba(255, 255, 255, 0.65)", hoverColor: "rgba(255, 255, 255, 0.08)", activeColor: "rgba(101, 162, 147, 0.25)", activeIconColor: "#8FD1BF", round: 6 },
1055
+ separator: { color: "rgba(255, 255, 255, 0.12)" },
1056
+ content: { fontSize: 15, lineHeight: 1.6, textColor: "rgba(255, 255, 255, 0.90)", fontFamily: "", padding: [16, 14], linkColor: "#7FC4B2", quoteColor: "rgba(255, 255, 255, 0.6)", quoteBorderColor: "#65A293", placeholderColor: "rgba(255, 255, 255, 0.35)", selectionColor: "rgba(101, 162, 147, 0.35)" },
1057
+ linkBar: { color: "#1C1C1B", inputColor: "#2A2A29", inputBorderColor: "rgba(255, 255, 255, 0.12)", textColor: "rgba(255, 255, 255, 0.9)", buttonColor: "#3D7A6B", buttonTextColor: White(1), secondaryTextColor: "rgba(255, 255, 255, 0.6)" },
1058
+ counter: { fontSize: 12, textColor: "rgba(255, 255, 255, 0.45)", limitColor: "#E57B6B" },
1059
+ disabled: { opacity: 0.45 },
1060
+ scrollBar: { bar_color: "#8A8A88", bar_mouseOverColor: "#BFBFBD", bar_width: 4, bar_round: 3, bar_opacity: 0.4, bar_mouseOverOpacity: 0.9, bar_padding: 2 },
1061
+ },
1062
+
1063
+ };
1064
+ RichTextEditor.styles.classic = RichTextEditor.styles.light; // the old name of light: the pages made with it keep their look