-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrich-text-editor.html
More file actions
359 lines (325 loc) · 10.8 KB
/
rich-text-editor.html
File metadata and controls
359 lines (325 loc) · 10.8 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Custom Blocks - CatWeb Tools</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="icon"
type="image/x-icon"
href="./static/images/favicon.ico"
/>
<link rel="stylesheet" href="stylesheet.css" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
/>
</head>
<body>
<header>
<h1>Rich Text Editor</h1>
<div class="description">
Simple WYSIWYG Editor, for editing the html in TextLabels.
(where Rich is turned on.)
</div>
</header>
<main>
<div id="formatting-buttons">
<button id="bold-button" title="Bold">
<i class="fa-solid fa-bold"></i>
</button>
<button id="italic-button" title="Italic">
<i class="fa-solid fa-italic"></i>
</button>
<button id="underline-button" title="Underline">
<i class="fa-solid fa-underline"></i>
</button>
<button id="strikethrough-button" title="Strikethrough">
<i class="fa-solid fa-strikethrough"></i>
</button>
<button id="color-button" title="Color">
<i class="fa-solid fa-palette"></i>
</button>
<button id="text-size-button" title="Text Size">
<i class="fa-solid fa-text-height"></i>
</button>
<button id="comment-button" title="Comment">
<i class="fa-solid fa-comment"></i>
</button>
<button id="highlight-button" title="Highlight">
<i class="fa-solid fa-highlighter"></i>
</button>
<button id="font-weight-button" title="Font Weight">
<i class="fa-solid fa-weight-hanging"></i>
</button>
<button id="font-change-button" title="Font Change">
<i class="fa-solid fa-a"></i>
</button>
<button id="font-transparency-button" title="Font Transparency">
<i class="fa-solid fa-eye-slash"></i>
</button>
<button id="font-outline-button" title="Font Outline">
<i class="fa-solid fa-border-top-left"></i>
</button>
</div>
<textarea
type="text"
placeholder="Text here..."
class="large-input"
id="editor"
></textarea>
<div id="preview" style="text-align: center;" aria-label="Preview of formatted text"></div>
</main>
<!-- Notice added above footer -->
<div style="color: grey; font-size: 14px; margin: 10px 0; padding: 10px; text-align: center;">
In order for this to work, make sure 'Rich' is enabled on your Text.
<a href="https://create.roblox.com/docs/ui/rich-text" target="_blank" style="color: #0066cc; text-decoration: underline;">Learn more</a>
</div>
<footer>
This site is open source.
<a href="https://github.com/TRC-Loop/CatWebTools" target="_blank"
>Improve this page</a
>. This page is not affiliated with Catweb. | Version:
<script src="./static/version.js"></script>
<a href="changelog" target="_blank"> View Changelog</a>.
</footer>
<script>
(() => {
const editor = document.getElementById("editor");
const preview = document.getElementById("preview");
// Roblox Rich Text tags map
const richTextTags = {
bold: ["<b>", "</b>"],
italic: ["<i>", "</i>"],
underline: ["<u>", "</u>"],
strikethrough: ["<s>", "</s>"],
color: (color) => [`<font color="${color}">`, "</font>"],
textSize: (size) => [`<size=${size}>`, "</size>"],
comment: [`<!--`, "-->"],
highlight: (color) => [
`<mark color="${color}">`,
"</mark>",
],
fontWeight: (weight) => [
`<font weight="${weight}">`,
"</font>",
],
fontChange: (font) => [
`<font family="${font}">`,
"</font>",
],
fontTransparency: (transparency) => [
`<font transparency="${transparency}">`,
"</font>",
],
fontOutline: (color) => [
`<stroke color="${color}">`,
"</stroke>",
],
};
function wrapSelectionWith(tagStart, tagEnd) {
const start = editor.selectionStart;
const end = editor.selectionEnd;
if (start === end) {
alert("Please select some text to format.");
return;
}
const selectedText = editor.value.substring(start, end);
const before = editor.value.substring(0, start);
const after = editor.value.substring(end);
editor.value =
before + tagStart + selectedText + tagEnd + after;
// Reset selection to inside the tags to allow chaining formatting
editor.selectionStart = start + tagStart.length;
editor.selectionEnd = end + tagStart.length;
editor.focus();
updatePreview();
}
// Helpers for prompts for parameters or default values
function promptColor(defaultColor = "#ff0000") {
const color = prompt(
"Enter a color name or hex code (e.g. red or #ff0000):",
defaultColor
);
return color ? color.trim() : null;
}
function promptSize(defaultSize = "18") {
let size = prompt(
"Enter text size (number, e.g. 18):",
defaultSize
);
if (size) {
size = size.trim();
if (!/^\d+$/.test(size)) {
alert("Please enter a valid numeric size.");
return null;
}
}
return size;
}
function promptText(defaultText = "") {
return prompt("Enter comment text:", defaultText);
}
function promptTransparency(defaultAlpha = "0.5") {
let alpha = prompt(
"Enter transparency (0 to 1):",
defaultAlpha
);
if (alpha) {
alpha = alpha.trim();
if (!/^(0(\.\d+)?|1(\.0+)?)$/.test(alpha)) {
alert(
"Please enter a valid transparency between 0 and 1."
);
return null;
}
}
return alpha;
}
// On button clicks, apply relevant tags
document.getElementById("bold-button").onclick = () =>
wrapSelectionWith("<b>", "</b>");
document.getElementById("italic-button").onclick = () =>
wrapSelectionWith("<i>", "</i>");
document.getElementById("underline-button").onclick = () =>
wrapSelectionWith("<u>", "</u>");
document.getElementById("strikethrough-button").onclick = () =>
wrapSelectionWith("<s>", "</s>");
document.getElementById("comment-button").onclick = () =>
wrapSelectionWith("<!--", "-->");
document.getElementById("color-button").onclick = () => {
const color = promptColor();
if (color) {
const [start, end] = richTextTags.color(color);
wrapSelectionWith(start, end);
}
};
document.getElementById("text-size-button").onclick = () => {
const size = promptSize();
if (size) {
const [start, end] = richTextTags.textSize(size);
wrapSelectionWith(start, end);
}
};
document.getElementById("highlight-button").onclick = () => {
const color = promptColor("#ffff00"); // default yellow highlight
if (color) {
const [start, end] = richTextTags.highlight(color);
wrapSelectionWith(start, end);
}
};
document.getElementById("font-weight-button").onclick = () => {
const weight = prompt(
"Enter font weight (normal, bold, etc.):",
"bold"
);
if (weight) {
const [start, end] = richTextTags.fontWeight(weight);
wrapSelectionWith(start, end);
}
};
document.getElementById("font-change-button").onclick = () => {
const font = prompt(
"Enter font face (e.g. Arial, SourceSans):",
"Arial"
);
if (font) {
const [start, end] = richTextTags.fontChange(font);
wrapSelectionWith(start, end);
}
};
document.getElementById("font-transparency-button").onclick =
() => {
const alpha = promptTransparency();
if (alpha !== null) {
const [start, end] =
richTextTags.fontTransparency(alpha);
wrapSelectionWith(start, end);
}
};
document.getElementById("font-outline-button").onclick = () => {
const color = promptColor("#000000");
if (color) {
const [start, end] = richTextTags.fontOutline(color);
wrapSelectionWith(start, end);
}
};
// Update preview by converting Roblox rich text markup to HTML for demonstration
// Note: Roblox rich text and HTML differ; this is a rough converter for preview only.
function convertRobloxToHTML(text) {
// Escape HTML special chars first
let safeText = text
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
// Convert basic tags
safeText = safeText
.replace(/<b>/gi, "<strong>")
.replace(/<\/b>/gi, "</strong>")
.replace(/<i>/gi, "<em>")
.replace(/<\/i>/gi, "</em>")
.replace(/<u>/gi, "<u>")
.replace(/<\/u>/gi, "</u>")
.replace(/<s>/gi, "<s>")
.replace(/<\/s>/gi, "</s>");
// Convert <font color="...">...</font>
safeText = safeText.replace(
/<font color="([^"]+)">/gi,
'<span style="color:$1">'
);
// Convert <font family="...">...</font>
safeText = safeText.replace(
/<font family="([^"]+)">/gi,
"<span style=\"font-family:'$1', sans-serif\">"
);
// Convert <font weight="...">...</font>
safeText = safeText.replace(
/<font weight="([^"]+)">/gi,
'<span style="font-weight:$1">'
);
// Convert <font transparency="...">...</font>
safeText = safeText.replace(
/<font transparency="([^"]+)">/gi,
'<span style="opacity:$1">'
);
safeText = safeText.replace(/<\/font>/gi, "</span>");
// Convert <size=...>...</size>
safeText = safeText.replace(
/<size=(\d+)>/gi,
(_, size) => `<span style="font-size:${size}px">`
);
safeText = safeText.replace(/<\/size>/gi, "</span>");
// Convert <stroke color="...">...</stroke> (font outline)
safeText = safeText.replace(
/<stroke color="([^"]+)">/gi,
(_, color) =>
`<span style="text-shadow: -1px -1px 0 ${color}, 1px -1px 0 ${color}, -1px 1px 0 ${color}, 1px 1px 0 ${color}">`
);
safeText = safeText.replace(
/<\/stroke>/gi,
"</span>"
);
// Convert <mark color="...">...</mark> (highlight)
safeText = safeText.replace(
/<mark color="([^"]+)">/gi,
'<span style="background-color:$1">'
);
safeText = safeText.replace(/<\/mark>/gi, "</span>");
safeText = safeText.replace(/<!--[\s\S]*?-->/g, "");
safeText = safeText.replace(/<!--.*?-->/g, "</span>");
// Replace newlines with <br>
safeText = safeText.replace(/\n/g, "<br>");
return safeText;
}
function updatePreview() {
const text = editor.value;
preview.innerHTML = convertRobloxToHTML(text);
}
// Update preview on input
editor.addEventListener("input", updatePreview);
// Initial preview update
updatePreview();
})();
</script>
</body>
</html>