@tooark/wysiwyg 1.0.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.
- package/LICENSE +201 -0
- package/NOTICE +12 -0
- package/README.md +139 -0
- package/README.pt-BR.md +139 -0
- package/dist/index.cjs +1705 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +324 -0
- package/dist/index.d.ts +324 -0
- package/dist/index.js +1684 -0
- package/dist/index.js.map +1 -0
- package/package.json +88 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1705 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var tokens = require('@tooark/tokens');
|
|
4
|
+
var core = require('@tiptap/core');
|
|
5
|
+
var extensionHighlight = require('@tiptap/extension-highlight');
|
|
6
|
+
var extensionImage = require('@tiptap/extension-image');
|
|
7
|
+
var Placeholder = require('@tiptap/extension-placeholder');
|
|
8
|
+
var extensionTextAlign = require('@tiptap/extension-text-align');
|
|
9
|
+
var extensionTextStyle = require('@tiptap/extension-text-style');
|
|
10
|
+
var StarterKit = require('@tiptap/starter-kit');
|
|
11
|
+
|
|
12
|
+
// Regras de segurança do conteúdo: o JSON do Tiptap vem de fora (banco, API,
|
|
13
|
+
// outro usuário), então URLs e cores entram por lista de permissão e nós ou
|
|
14
|
+
// marcas fora do schema caem, em vez de derrubar o documento inteiro.
|
|
15
|
+
const SAFE_PROTOCOLS = new Set(["http:", "https:", "mailto:", "tel:"]);
|
|
16
|
+
// Caracteres de controle e espaços no meio da URL são o truque clássico para passar `java\tscript:`.
|
|
17
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: a faixa de controle é justamente o que se remove
|
|
18
|
+
const STRIP = /[\x00-\x1F\x7F-\x9F\s]/g;
|
|
19
|
+
/**
|
|
20
|
+
* URL aceita em `href`/`src`/`poster`: absoluta em http(s), mailto ou tel, ou relativa (`/caminho`, `#ancora`,
|
|
21
|
+
* `?busca`, `./` e `../`). Fora: `javascript:`, `data:`, `blob:`, `vbscript:` e qualquer outro esquema.
|
|
22
|
+
*/
|
|
23
|
+
function isSafeUrl(value) {
|
|
24
|
+
if (typeof value !== "string")
|
|
25
|
+
return false;
|
|
26
|
+
const url = value.replace(STRIP, "");
|
|
27
|
+
if (!url)
|
|
28
|
+
return false;
|
|
29
|
+
if (/^(\/|#|\?|\.\.?\/)/.test(url))
|
|
30
|
+
return true;
|
|
31
|
+
try {
|
|
32
|
+
return SAFE_PROTOCOLS.has(new URL(url).protocol);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Nome de host sem esquema ("exemplo.com/pagina"), a que o formulário de link acrescenta `https://`. */
|
|
39
|
+
function looksLikeHostname(value) {
|
|
40
|
+
return /^[a-z0-9-]+(\.[a-z0-9-]+)+(:\d+)?([/?#].*)?$/i.test(value.trim());
|
|
41
|
+
}
|
|
42
|
+
/** Valor de cor aceito em `color`: hex, nome CSS ou função rgb/hsl sem `;` (evita injetar outras declarações). */
|
|
43
|
+
function isSafeColor(value) {
|
|
44
|
+
return (typeof value === "string" &&
|
|
45
|
+
/^(#[0-9a-f]{3,8}|[a-z]{3,24}|(rgb|rgba|hsl|hsla)\([0-9.,%\s/]{1,40}\))$/i.test(value.trim()));
|
|
46
|
+
}
|
|
47
|
+
const ALIGNMENTS = new Set(["left", "center", "right", "justify"]);
|
|
48
|
+
function sanitizeMark(mark, schema) {
|
|
49
|
+
if (!mark || typeof mark.type !== "string" || !schema.marks[mark.type])
|
|
50
|
+
return null;
|
|
51
|
+
const attrs = { ...(mark.attrs ?? {}) };
|
|
52
|
+
if (mark.type === "link") {
|
|
53
|
+
if (!isSafeUrl(attrs.href))
|
|
54
|
+
return null;
|
|
55
|
+
attrs.href = attrs.href.replace(STRIP, "");
|
|
56
|
+
}
|
|
57
|
+
if (mark.type === "textStyle" || mark.type === "highlight") {
|
|
58
|
+
if (attrs.color != null && !isSafeColor(attrs.color))
|
|
59
|
+
delete attrs.color;
|
|
60
|
+
if (attrs.backgroundColor != null && !isSafeColor(attrs.backgroundColor))
|
|
61
|
+
delete attrs.backgroundColor;
|
|
62
|
+
}
|
|
63
|
+
const next = { ...mark };
|
|
64
|
+
if (Object.keys(attrs).length > 0) {
|
|
65
|
+
next.attrs = attrs;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
delete next.attrs;
|
|
69
|
+
}
|
|
70
|
+
return next;
|
|
71
|
+
}
|
|
72
|
+
function sanitizeNode(node, schema) {
|
|
73
|
+
if (!node || typeof node.type !== "string" || !schema.nodes[node.type])
|
|
74
|
+
return null;
|
|
75
|
+
const attrs = { ...(node.attrs ?? {}) };
|
|
76
|
+
if (node.type === "image" || node.type === "video") {
|
|
77
|
+
if (!isSafeUrl(attrs.src))
|
|
78
|
+
return null;
|
|
79
|
+
attrs.src = attrs.src.replace(STRIP, "");
|
|
80
|
+
if (attrs.poster != null && !isSafeUrl(attrs.poster))
|
|
81
|
+
delete attrs.poster;
|
|
82
|
+
}
|
|
83
|
+
if (attrs.textAlign != null && !ALIGNMENTS.has(String(attrs.textAlign)))
|
|
84
|
+
delete attrs.textAlign;
|
|
85
|
+
if (node.type === "heading") {
|
|
86
|
+
const level = Number(attrs.level);
|
|
87
|
+
attrs.level = Number.isInteger(level) ? Math.min(4, Math.max(1, level)) : 1;
|
|
88
|
+
}
|
|
89
|
+
const marks = Array.isArray(node.marks)
|
|
90
|
+
? node.marks.map((mark) => sanitizeMark(mark, schema)).filter((mark) => mark !== null)
|
|
91
|
+
: undefined;
|
|
92
|
+
const content = Array.isArray(node.content)
|
|
93
|
+
? node.content.map((child) => sanitizeNode(child, schema)).filter((child) => child !== null)
|
|
94
|
+
: undefined;
|
|
95
|
+
const next = { ...node };
|
|
96
|
+
if (Object.keys(attrs).length > 0) {
|
|
97
|
+
next.attrs = attrs;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
delete next.attrs;
|
|
101
|
+
}
|
|
102
|
+
if (marks)
|
|
103
|
+
next.marks = marks;
|
|
104
|
+
if (content)
|
|
105
|
+
next.content = content;
|
|
106
|
+
return next;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Sanitiza um documento do Tiptap contra o schema do editor: nós e marcas desconhecidos são removidos (com os
|
|
110
|
+
* filhos), links, imagens e vídeos com URL fora da lista de permissão caem, cores inválidas somem e `textAlign`
|
|
111
|
+
* e `level` ficam nos valores previstos. `null` continua `null`.
|
|
112
|
+
*/
|
|
113
|
+
function sanitizeWysiwygContent(content, schema) {
|
|
114
|
+
if (!content)
|
|
115
|
+
return null;
|
|
116
|
+
const doc = sanitizeNode(content, schema);
|
|
117
|
+
if (!doc)
|
|
118
|
+
return null;
|
|
119
|
+
return doc;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Documento vazio do ProseMirror (um parágrafo). */
|
|
123
|
+
const EMPTY_DOC = { type: "doc", content: [{ type: "paragraph" }] };
|
|
124
|
+
/** Tamanho máximo de arquivo aceito por padrão (10 MiB). */
|
|
125
|
+
const DEFAULT_MAX_FILE_SIZE = 10 * 1024 * 1024;
|
|
126
|
+
/** Níveis de título disponíveis no editor. */
|
|
127
|
+
const HEADING_LEVELS = [1, 2, 3, 4];
|
|
128
|
+
/** Resolve "auto" para "light"/"dark" pelo color-scheme computado de `element`, ou pela preferência do sistema. */
|
|
129
|
+
function resolveWysiwygTheme(theme, element) {
|
|
130
|
+
if (theme === "dark")
|
|
131
|
+
return "dark";
|
|
132
|
+
if (theme === "light")
|
|
133
|
+
return "light";
|
|
134
|
+
return tokens.resolveColorScheme(element);
|
|
135
|
+
}
|
|
136
|
+
function applyTheme(element, theme) {
|
|
137
|
+
element.setAttribute("data-ark-theme", theme);
|
|
138
|
+
}
|
|
139
|
+
/** Vídeo externo como bloco atômico: `src` (e `poster`) apontam para fora, nunca para base64. */
|
|
140
|
+
const Video = core.Node.create({
|
|
141
|
+
name: "video",
|
|
142
|
+
group: "block",
|
|
143
|
+
atom: true,
|
|
144
|
+
draggable: true,
|
|
145
|
+
selectable: true,
|
|
146
|
+
addAttributes() {
|
|
147
|
+
return {
|
|
148
|
+
src: { default: null },
|
|
149
|
+
poster: { default: null },
|
|
150
|
+
title: { default: null }
|
|
151
|
+
};
|
|
152
|
+
},
|
|
153
|
+
parseHTML() {
|
|
154
|
+
return [{ tag: "video[src]" }];
|
|
155
|
+
},
|
|
156
|
+
renderHTML({ HTMLAttributes }) {
|
|
157
|
+
return ["video", core.mergeAttributes({ controls: "true", preload: "metadata" }, HTMLAttributes)];
|
|
158
|
+
},
|
|
159
|
+
addCommands() {
|
|
160
|
+
return {
|
|
161
|
+
setVideo: (attrs) => ({ commands }) => commands.insertContent({ type: this.name, attrs })
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
// --- Extensões compartilhadas ---
|
|
166
|
+
/**
|
|
167
|
+
* Extensões do editor e do viewer (o mesmo schema nos dois, senão o conteúdo salvo não renderiza): StarterKit
|
|
168
|
+
* (títulos 1-4, sublinhado e link com URL validada), cor de texto, marca-texto, alinhamento, imagem sem base64
|
|
169
|
+
* e vídeo externo. Em `editable` o link não abre no clique (o clique edita); no viewer abre em nova aba.
|
|
170
|
+
*/
|
|
171
|
+
function createWysiwygExtensions(options) {
|
|
172
|
+
const extensions = [
|
|
173
|
+
StarterKit.configure({
|
|
174
|
+
heading: { levels: [...HEADING_LEVELS] },
|
|
175
|
+
link: {
|
|
176
|
+
openOnClick: !options.editable,
|
|
177
|
+
autolink: true,
|
|
178
|
+
linkOnPaste: true,
|
|
179
|
+
// A lista de permissão do pacote vale para o que o usuário digita, cola ou autolinka; um host sem
|
|
180
|
+
// esquema ("exemplo.com") passa aqui porque o autolink grava o href já com http.
|
|
181
|
+
isAllowedUri: (url) => isSafeUrl(url) || looksLikeHostname(url),
|
|
182
|
+
HTMLAttributes: { target: "_blank", rel: "noopener noreferrer nofollow" }
|
|
183
|
+
}
|
|
184
|
+
}),
|
|
185
|
+
extensionTextStyle.TextStyle,
|
|
186
|
+
extensionTextStyle.Color,
|
|
187
|
+
extensionHighlight.Highlight.configure({ multicolor: true }),
|
|
188
|
+
extensionTextAlign.TextAlign.configure({ types: ["heading", "paragraph"], alignments: ["left", "center", "right", "justify"] }),
|
|
189
|
+
extensionImage.Image.configure({ allowBase64: false, inline: false }),
|
|
190
|
+
Video
|
|
191
|
+
];
|
|
192
|
+
if (options.placeholder) {
|
|
193
|
+
extensions.push(Placeholder.configure({ placeholder: options.placeholder }));
|
|
194
|
+
}
|
|
195
|
+
return extensions;
|
|
196
|
+
}
|
|
197
|
+
function kindOf(file) {
|
|
198
|
+
if (file.type.startsWith("image/"))
|
|
199
|
+
return "image";
|
|
200
|
+
if (file.type.startsWith("video/"))
|
|
201
|
+
return "video";
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
function mediaFiles(list) {
|
|
205
|
+
return Array.from(list ?? []).filter((file) => kindOf(file) !== null);
|
|
206
|
+
}
|
|
207
|
+
function createInstance(element, options) {
|
|
208
|
+
let resolved = resolveWysiwygTheme(options.theme, element);
|
|
209
|
+
let applying = false;
|
|
210
|
+
let uploading = 0;
|
|
211
|
+
const maxFileSize = options.maxFileSize ?? DEFAULT_MAX_FILE_SIZE;
|
|
212
|
+
const fail = (reason, file, error) => {
|
|
213
|
+
options.onUploadError?.({ reason, file, error });
|
|
214
|
+
return false;
|
|
215
|
+
};
|
|
216
|
+
// Arquivos colados ou arrastados só entram pelo gancho; sem ele são consumidos (nada de base64 no JSON).
|
|
217
|
+
const handleFiles = (files, position) => {
|
|
218
|
+
if (files.length === 0)
|
|
219
|
+
return false;
|
|
220
|
+
for (const file of files)
|
|
221
|
+
void insertFile(file, position);
|
|
222
|
+
return true;
|
|
223
|
+
};
|
|
224
|
+
const editor = new core.Editor({
|
|
225
|
+
element,
|
|
226
|
+
editable: options.editable,
|
|
227
|
+
extensions: createWysiwygExtensions({ editable: options.editable, placeholder: options.placeholder }),
|
|
228
|
+
content: EMPTY_DOC,
|
|
229
|
+
editorProps: {
|
|
230
|
+
handlePaste: (_view, event) => handleFiles(mediaFiles(event.clipboardData?.files)),
|
|
231
|
+
handleDrop: (view, event, _slice, moved) => {
|
|
232
|
+
if (moved)
|
|
233
|
+
return false;
|
|
234
|
+
const at = view.posAtCoords({ left: event.clientX, top: event.clientY });
|
|
235
|
+
return handleFiles(mediaFiles(event.dataTransfer?.files), at?.pos);
|
|
236
|
+
},
|
|
237
|
+
// HTML colado com <img>/<video> só vira nó quando há gancho: sem ele, seria um hotlink que o app não controla.
|
|
238
|
+
transformPastedHTML: (html) => (options.uploadFile ? html : html.replace(/<(img|video|source)\b[^>]*>/gi, ""))
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
const setContent = (content) => {
|
|
242
|
+
applying = true;
|
|
243
|
+
try {
|
|
244
|
+
// Fora do historico: Ctrl+Z depois de o app trocar o conteudo nao pode voltar ao documento anterior.
|
|
245
|
+
editor
|
|
246
|
+
.chain()
|
|
247
|
+
.setMeta("addToHistory", false)
|
|
248
|
+
.setContent(sanitizeWysiwygContent(content, editor.schema) ?? EMPTY_DOC, { emitUpdate: false })
|
|
249
|
+
.run();
|
|
250
|
+
}
|
|
251
|
+
finally {
|
|
252
|
+
applying = false;
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
if (options.content)
|
|
256
|
+
setContent(options.content);
|
|
257
|
+
if (options.onChange) {
|
|
258
|
+
editor.on("update", () => {
|
|
259
|
+
if (!applying)
|
|
260
|
+
options.onChange?.(editor.getJSON());
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
const setUploading = (next) => {
|
|
264
|
+
uploading = next;
|
|
265
|
+
options.onUploadingChange?.(next);
|
|
266
|
+
};
|
|
267
|
+
const insertFile = async (file, position) => {
|
|
268
|
+
const kind = kindOf(file);
|
|
269
|
+
if (!kind)
|
|
270
|
+
return fail("unsupported-type", file);
|
|
271
|
+
if (!options.uploadFile)
|
|
272
|
+
return fail("no-uploader", file);
|
|
273
|
+
if (file.size > maxFileSize)
|
|
274
|
+
return fail("too-large", file);
|
|
275
|
+
const at = position ?? editor.state.selection.to;
|
|
276
|
+
setUploading(uploading + 1);
|
|
277
|
+
try {
|
|
278
|
+
const result = await options.uploadFile(file, kind);
|
|
279
|
+
if (!result || !isSafeUrl(result.src))
|
|
280
|
+
return fail("invalid-src", file);
|
|
281
|
+
if (editor.isDestroyed)
|
|
282
|
+
return false;
|
|
283
|
+
const pos = Math.min(at, editor.state.doc.content.size);
|
|
284
|
+
const attrs = kind === "image"
|
|
285
|
+
? { src: result.src, alt: result.alt ?? file.name, title: result.title ?? null }
|
|
286
|
+
: { src: result.src, poster: isSafeUrl(result.poster) ? result.poster : null, title: result.title ?? null };
|
|
287
|
+
editor.chain().focus().insertContentAt(pos, { type: kind, attrs }).run();
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
return fail("failed", file, error);
|
|
292
|
+
}
|
|
293
|
+
finally {
|
|
294
|
+
setUploading(uploading - 1);
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
applyTheme(element, resolved);
|
|
298
|
+
return {
|
|
299
|
+
editor,
|
|
300
|
+
getJSON: () => editor.getJSON(),
|
|
301
|
+
setContent,
|
|
302
|
+
isActive: (name, attrs) => editor.isActive(name, attrs),
|
|
303
|
+
setEditable: (editable) => editor.setEditable(editable),
|
|
304
|
+
setTheme: (theme) => {
|
|
305
|
+
resolved = resolveWysiwygTheme(theme, element);
|
|
306
|
+
applyTheme(element, resolved);
|
|
307
|
+
},
|
|
308
|
+
resolvedTheme: () => resolved,
|
|
309
|
+
insertFile,
|
|
310
|
+
uploading: () => uploading,
|
|
311
|
+
setLink: (raw) => {
|
|
312
|
+
const trimmed = raw.trim();
|
|
313
|
+
const href = looksLikeHostname(trimmed) && !/^[a-z][a-z0-9+.-]*:/i.test(trimmed) ? `https://${trimmed}` : trimmed;
|
|
314
|
+
if (!isSafeUrl(href))
|
|
315
|
+
return false;
|
|
316
|
+
const chain = editor.chain().focus().extendMarkRange("link");
|
|
317
|
+
if (editor.state.selection.empty && !editor.isActive("link")) {
|
|
318
|
+
chain.insertContent({ type: "text", text: href, marks: [{ type: "link", attrs: { href } }] }).run();
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
chain.setLink({ href }).run();
|
|
322
|
+
}
|
|
323
|
+
return true;
|
|
324
|
+
},
|
|
325
|
+
unsetLink: () => {
|
|
326
|
+
editor.chain().focus().extendMarkRange("link").unsetLink().run();
|
|
327
|
+
},
|
|
328
|
+
destroy: () => editor.destroy()
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
/** Cria um editor WYSIWYG (editável) dentro de `element`. */
|
|
332
|
+
function createWysiwygEditor(element, options = {}) {
|
|
333
|
+
return createInstance(element, {
|
|
334
|
+
content: options.content,
|
|
335
|
+
theme: options.theme,
|
|
336
|
+
placeholder: options.placeholder,
|
|
337
|
+
editable: options.editable ?? true,
|
|
338
|
+
uploadFile: options.uploadFile,
|
|
339
|
+
maxFileSize: options.maxFileSize,
|
|
340
|
+
onChange: options.onChange,
|
|
341
|
+
onUploadError: options.onUploadError,
|
|
342
|
+
onUploadingChange: options.onUploadingChange
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
/** Cria um viewer (somente leitura) que renderiza o mesmo JSON do editor. */
|
|
346
|
+
function createWysiwygViewer(element, options = {}) {
|
|
347
|
+
return createInstance(element, {
|
|
348
|
+
content: options.content,
|
|
349
|
+
theme: options.theme,
|
|
350
|
+
editable: false
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Rótulos da toolbar. O pacote não depende de @tooark/core, então carrega o
|
|
355
|
+
// próprio dicionário; `custom` recebe um JSON parcial por cima do inglês.
|
|
356
|
+
const en = {
|
|
357
|
+
textStyle: "Text style",
|
|
358
|
+
paragraph: "Normal",
|
|
359
|
+
heading: "Heading",
|
|
360
|
+
bold: "Bold",
|
|
361
|
+
italic: "Italic",
|
|
362
|
+
underline: "Underline",
|
|
363
|
+
strike: "Strikethrough",
|
|
364
|
+
code: "Code",
|
|
365
|
+
textColor: "Text color",
|
|
366
|
+
highlight: "Highlight",
|
|
367
|
+
noColor: "No color",
|
|
368
|
+
alignLeft: "Align left",
|
|
369
|
+
alignCenter: "Align center",
|
|
370
|
+
alignRight: "Align right",
|
|
371
|
+
alignJustify: "Justify",
|
|
372
|
+
bulletList: "Bulleted list",
|
|
373
|
+
orderedList: "Numbered list",
|
|
374
|
+
indent: "Increase indent",
|
|
375
|
+
outdent: "Decrease indent",
|
|
376
|
+
link: "Link",
|
|
377
|
+
linkUrl: "URL",
|
|
378
|
+
apply: "Apply",
|
|
379
|
+
remove: "Remove",
|
|
380
|
+
invalidUrl: "Enter a valid http(s), mailto or tel address",
|
|
381
|
+
image: "Image",
|
|
382
|
+
video: "Video",
|
|
383
|
+
blockquote: "Quote",
|
|
384
|
+
horizontalRule: "Horizontal rule",
|
|
385
|
+
clearFormat: "Clear formatting",
|
|
386
|
+
undo: "Undo",
|
|
387
|
+
redo: "Redo",
|
|
388
|
+
uploading: "Uploading"
|
|
389
|
+
};
|
|
390
|
+
const pt = {
|
|
391
|
+
textStyle: "Estilo do texto",
|
|
392
|
+
paragraph: "Normal",
|
|
393
|
+
heading: "Título",
|
|
394
|
+
bold: "Negrito",
|
|
395
|
+
italic: "Itálico",
|
|
396
|
+
underline: "Sublinhado",
|
|
397
|
+
strike: "Tachado",
|
|
398
|
+
code: "Código",
|
|
399
|
+
textColor: "Cor do texto",
|
|
400
|
+
highlight: "Marca-texto",
|
|
401
|
+
noColor: "Sem cor",
|
|
402
|
+
alignLeft: "Alinhar à esquerda",
|
|
403
|
+
alignCenter: "Centralizar",
|
|
404
|
+
alignRight: "Alinhar à direita",
|
|
405
|
+
alignJustify: "Justificar",
|
|
406
|
+
bulletList: "Lista com marcadores",
|
|
407
|
+
orderedList: "Lista numerada",
|
|
408
|
+
indent: "Aumentar recuo",
|
|
409
|
+
outdent: "Diminuir recuo",
|
|
410
|
+
link: "Link",
|
|
411
|
+
linkUrl: "URL",
|
|
412
|
+
apply: "Aplicar",
|
|
413
|
+
remove: "Remover",
|
|
414
|
+
invalidUrl: "Informe um endereço http(s), mailto ou tel válido",
|
|
415
|
+
image: "Imagem",
|
|
416
|
+
video: "Vídeo",
|
|
417
|
+
blockquote: "Citação",
|
|
418
|
+
horizontalRule: "Linha horizontal",
|
|
419
|
+
clearFormat: "Limpar formatação",
|
|
420
|
+
undo: "Desfazer",
|
|
421
|
+
redo: "Refazer",
|
|
422
|
+
uploading: "Enviando"
|
|
423
|
+
};
|
|
424
|
+
const es = {
|
|
425
|
+
textStyle: "Estilo del texto",
|
|
426
|
+
paragraph: "Normal",
|
|
427
|
+
heading: "Título",
|
|
428
|
+
bold: "Negrita",
|
|
429
|
+
italic: "Cursiva",
|
|
430
|
+
underline: "Subrayado",
|
|
431
|
+
strike: "Tachado",
|
|
432
|
+
code: "Código",
|
|
433
|
+
textColor: "Color del texto",
|
|
434
|
+
highlight: "Resaltado",
|
|
435
|
+
noColor: "Sin color",
|
|
436
|
+
alignLeft: "Alinear a la izquierda",
|
|
437
|
+
alignCenter: "Centrar",
|
|
438
|
+
alignRight: "Alinear a la derecha",
|
|
439
|
+
alignJustify: "Justificar",
|
|
440
|
+
bulletList: "Lista con viñetas",
|
|
441
|
+
orderedList: "Lista numerada",
|
|
442
|
+
indent: "Aumentar sangría",
|
|
443
|
+
outdent: "Reducir sangría",
|
|
444
|
+
link: "Enlace",
|
|
445
|
+
linkUrl: "URL",
|
|
446
|
+
apply: "Aplicar",
|
|
447
|
+
remove: "Quitar",
|
|
448
|
+
invalidUrl: "Ingrese una dirección http(s), mailto o tel válida",
|
|
449
|
+
image: "Imagen",
|
|
450
|
+
video: "Vídeo",
|
|
451
|
+
blockquote: "Cita",
|
|
452
|
+
horizontalRule: "Línea horizontal",
|
|
453
|
+
clearFormat: "Borrar formato",
|
|
454
|
+
undo: "Deshacer",
|
|
455
|
+
redo: "Rehacer",
|
|
456
|
+
uploading: "Enviando"
|
|
457
|
+
};
|
|
458
|
+
const LOCALES = { en, pt, es };
|
|
459
|
+
/**
|
|
460
|
+
* Resolve os rótulos da toolbar: `en`, `pt` ou `es` (desconhecido cai em `en`); `localeJson` (objeto ou JSON)
|
|
461
|
+
* sobrescreve chave a chave, e é a única fonte de `custom`. Só strings entram; JSON inválido é ignorado.
|
|
462
|
+
*/
|
|
463
|
+
function resolveWysiwygLabels(lang, localeJson) {
|
|
464
|
+
const base = LOCALES[(lang || "en").toLowerCase()] ?? en;
|
|
465
|
+
let overrides = {};
|
|
466
|
+
if (typeof localeJson === "string" && localeJson.trim()) {
|
|
467
|
+
try {
|
|
468
|
+
overrides = JSON.parse(localeJson);
|
|
469
|
+
}
|
|
470
|
+
catch {
|
|
471
|
+
overrides = {};
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
else if (localeJson && typeof localeJson === "object") {
|
|
475
|
+
overrides = localeJson;
|
|
476
|
+
}
|
|
477
|
+
const labels = { ...base };
|
|
478
|
+
for (const key of Object.keys(labels)) {
|
|
479
|
+
const value = overrides[key];
|
|
480
|
+
if (typeof value === "string" && value)
|
|
481
|
+
labels[key] = value;
|
|
482
|
+
}
|
|
483
|
+
return labels;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Ícones da toolbar (traços de 24x24 no estilo Lucide), constantes: o único
|
|
487
|
+
// HTML que o pacote injeta por innerHTML.
|
|
488
|
+
const svg = (body) => `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${body}</svg>`;
|
|
489
|
+
const ICONS = {
|
|
490
|
+
bold: svg('<path d="M6 4h8a4 4 0 0 1 0 8H6z"/><path d="M6 12h9a4 4 0 0 1 0 8H6z"/>'),
|
|
491
|
+
italic: svg('<line x1="19" y1="4" x2="10" y2="4"/><line x1="14" y1="20" x2="5" y2="20"/><line x1="15" y1="4" x2="9" y2="20"/>'),
|
|
492
|
+
underline: svg('<path d="M6 4v6a6 6 0 0 0 12 0V4"/><line x1="4" y1="20" x2="20" y2="20"/>'),
|
|
493
|
+
strike: svg('<path d="M16 4H9a3 3 0 0 0-2.83 4"/><path d="M14 12a4 4 0 0 1 0 8H6"/><line x1="4" y1="12" x2="20" y2="12"/>'),
|
|
494
|
+
code: svg('<polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/>'),
|
|
495
|
+
textColor: svg('<path d="M4 20h16"/><path d="m6 16 6-12 6 12"/><path d="M8 12h8"/>'),
|
|
496
|
+
highlight: svg('<path d="m9 11-6 6v3h9l3-3"/><path d="m22 12-4.6 4.6a2 2 0 0 1-2.8 0l-5.2-5.2a2 2 0 0 1 0-2.8L14 4"/>'),
|
|
497
|
+
alignLeft: svg('<line x1="21" y1="6" x2="3" y2="6"/><line x1="15" y1="12" x2="3" y2="12"/><line x1="17" y1="18" x2="3" y2="18"/>'),
|
|
498
|
+
alignCenter: svg('<line x1="21" y1="6" x2="3" y2="6"/><line x1="17" y1="12" x2="7" y2="12"/><line x1="19" y1="18" x2="5" y2="18"/>'),
|
|
499
|
+
alignRight: svg('<line x1="21" y1="6" x2="3" y2="6"/><line x1="21" y1="12" x2="9" y2="12"/><line x1="21" y1="18" x2="7" y2="18"/>'),
|
|
500
|
+
alignJustify: svg('<line x1="21" y1="6" x2="3" y2="6"/><line x1="21" y1="12" x2="3" y2="12"/><line x1="21" y1="18" x2="3" y2="18"/>'),
|
|
501
|
+
bulletList: svg('<line x1="8" y1="6" x2="21" y2="6"/><line x1="8" y1="12" x2="21" y2="12"/><line x1="8" y1="18" x2="21" y2="18"/><circle cx="4" cy="6" r="1" fill="currentColor"/><circle cx="4" cy="12" r="1" fill="currentColor"/><circle cx="4" cy="18" r="1" fill="currentColor"/>'),
|
|
502
|
+
orderedList: svg('<line x1="10" y1="6" x2="21" y2="6"/><line x1="10" y1="12" x2="21" y2="12"/><line x1="10" y1="18" x2="21" y2="18"/><path d="M4 6h1v4"/><path d="M4 10h2"/><path d="M6 18H4c0-1 2-2 2-3s-1-1.5-2-1"/>'),
|
|
503
|
+
indent: svg('<polyline points="3 8 7 12 3 16"/><line x1="21" y1="12" x2="11" y2="12"/><line x1="21" y1="6" x2="11" y2="6"/><line x1="21" y1="18" x2="11" y2="18"/>'),
|
|
504
|
+
outdent: svg('<polyline points="7 8 3 12 7 16"/><line x1="21" y1="12" x2="11" y2="12"/><line x1="21" y1="6" x2="11" y2="6"/><line x1="21" y1="18" x2="11" y2="18"/>'),
|
|
505
|
+
link: svg('<path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/>'),
|
|
506
|
+
image: svg('<rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/>'),
|
|
507
|
+
video: svg('<rect x="2" y="4" width="14" height="16" rx="2"/><path d="m22 8-6 4 6 4V8z"/>'),
|
|
508
|
+
blockquote: svg('<path d="M10 11H6a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v6a4 4 0 0 1-4 4"/><path d="M20 11h-4a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h2a2 2 0 0 1 2 2v6a4 4 0 0 1-4 4"/>'),
|
|
509
|
+
horizontalRule: svg('<line x1="4" y1="12" x2="20" y2="12"/>'),
|
|
510
|
+
clearFormat: svg('<path d="M4 7V4h16v3"/><path d="M5 20h6"/><path d="M13 4 8 20"/><path d="m15 15 5 5"/><path d="m20 15-5 5"/>'),
|
|
511
|
+
undo: svg('<path d="M3 7v6h6"/><path d="M21 17a9 9 0 0 0-9-9 9 9 0 0 0-6 2.3L3 13"/>'),
|
|
512
|
+
redo: svg('<path d="M21 7v6h-6"/><path d="M3 17a9 9 0 0 1 9-9 9 9 0 0 1 6 2.3l3 2.7"/>'),
|
|
513
|
+
none: svg('<circle cx="12" cy="12" r="9"/><line x1="5.6" y1="5.6" x2="18.4" y2="18.4"/>')
|
|
514
|
+
};
|
|
515
|
+
|
|
516
|
+
const WYSIWYG_STYLE_ID = "ark-wysiwyg-styles";
|
|
517
|
+
/**
|
|
518
|
+
* CSS base do editor/viewer. Plano e independente de Tailwind para que o pacote
|
|
519
|
+
* funcione em qualquer projeto. Temas via atributo `data-ark-theme`.
|
|
520
|
+
*/
|
|
521
|
+
const wysiwygCss = `
|
|
522
|
+
.ark-wysiwyg {
|
|
523
|
+
display: flex;
|
|
524
|
+
flex-direction: column;
|
|
525
|
+
gap: 8px;
|
|
526
|
+
font-family: inherit;
|
|
527
|
+
color: #0f172a;
|
|
528
|
+
}
|
|
529
|
+
.ark-wysiwyg[data-ark-theme="dark"] { color: #e2e8f0; }
|
|
530
|
+
|
|
531
|
+
/* ---------- Toolbar ---------- */
|
|
532
|
+
.ark-wysiwyg__toolbar {
|
|
533
|
+
display: flex;
|
|
534
|
+
flex-wrap: wrap;
|
|
535
|
+
align-items: center;
|
|
536
|
+
gap: 4px;
|
|
537
|
+
padding: 6px;
|
|
538
|
+
border: 1px solid #e2e8f0;
|
|
539
|
+
border-radius: 8px;
|
|
540
|
+
background: #f8fafc;
|
|
541
|
+
}
|
|
542
|
+
.ark-wysiwyg[data-ark-theme="dark"] .ark-wysiwyg__toolbar {
|
|
543
|
+
border-color: #334155;
|
|
544
|
+
background: #1e293b;
|
|
545
|
+
}
|
|
546
|
+
.ark-wysiwyg__group {
|
|
547
|
+
display: flex;
|
|
548
|
+
align-items: center;
|
|
549
|
+
gap: 2px;
|
|
550
|
+
}
|
|
551
|
+
.ark-wysiwyg__item {
|
|
552
|
+
display: inline-flex;
|
|
553
|
+
position: relative;
|
|
554
|
+
}
|
|
555
|
+
.ark-wysiwyg__separator {
|
|
556
|
+
width: 1px;
|
|
557
|
+
align-self: stretch;
|
|
558
|
+
margin: 4px 2px;
|
|
559
|
+
background: #e2e8f0;
|
|
560
|
+
}
|
|
561
|
+
.ark-wysiwyg[data-ark-theme="dark"] .ark-wysiwyg__separator { background: #334155; }
|
|
562
|
+
|
|
563
|
+
.ark-wysiwyg__btn {
|
|
564
|
+
position: relative;
|
|
565
|
+
display: inline-flex;
|
|
566
|
+
align-items: center;
|
|
567
|
+
justify-content: center;
|
|
568
|
+
min-width: 32px;
|
|
569
|
+
height: 32px;
|
|
570
|
+
padding: 0 6px;
|
|
571
|
+
border: 1px solid transparent;
|
|
572
|
+
border-radius: 6px;
|
|
573
|
+
background: transparent;
|
|
574
|
+
color: inherit;
|
|
575
|
+
font-size: 14px;
|
|
576
|
+
font-weight: 600;
|
|
577
|
+
line-height: 1;
|
|
578
|
+
cursor: pointer;
|
|
579
|
+
transition: background 120ms ease, border-color 120ms ease;
|
|
580
|
+
}
|
|
581
|
+
.ark-wysiwyg__btn svg { width: 18px; height: 18px; }
|
|
582
|
+
.ark-wysiwyg__btn--text { padding: 0 10px; font-size: 13px; }
|
|
583
|
+
.ark-wysiwyg__btn:hover { background: rgba(100, 116, 139, 0.15); }
|
|
584
|
+
.ark-wysiwyg__btn:focus-visible { outline: 2px solid #6366f1; outline-offset: 1px; }
|
|
585
|
+
.ark-wysiwyg__btn[aria-pressed="true"] {
|
|
586
|
+
background: rgba(99, 102, 241, 0.16);
|
|
587
|
+
border-color: rgba(99, 102, 241, 0.5);
|
|
588
|
+
}
|
|
589
|
+
.ark-wysiwyg__btn:disabled {
|
|
590
|
+
opacity: 0.45;
|
|
591
|
+
cursor: not-allowed;
|
|
592
|
+
background: transparent;
|
|
593
|
+
}
|
|
594
|
+
/* Barra com a cor atual sob o icone de cor do texto e de marca-texto. */
|
|
595
|
+
.ark-wysiwyg__btn--swatch::after {
|
|
596
|
+
content: "";
|
|
597
|
+
position: absolute;
|
|
598
|
+
left: 7px;
|
|
599
|
+
right: 7px;
|
|
600
|
+
bottom: 4px;
|
|
601
|
+
height: 3px;
|
|
602
|
+
border-radius: 2px;
|
|
603
|
+
background: var(--ark-wysiwyg-swatch, transparent);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
.ark-wysiwyg__select {
|
|
607
|
+
height: 32px;
|
|
608
|
+
padding: 0 8px;
|
|
609
|
+
border: 1px solid #cbd5e1;
|
|
610
|
+
border-radius: 6px;
|
|
611
|
+
background: #ffffff;
|
|
612
|
+
color: inherit;
|
|
613
|
+
font: inherit;
|
|
614
|
+
font-size: 13px;
|
|
615
|
+
cursor: pointer;
|
|
616
|
+
}
|
|
617
|
+
.ark-wysiwyg__select:focus-visible { outline: 2px solid #6366f1; outline-offset: 1px; }
|
|
618
|
+
.ark-wysiwyg__select:disabled { opacity: 0.45; cursor: not-allowed; }
|
|
619
|
+
.ark-wysiwyg[data-ark-theme="dark"] .ark-wysiwyg__select {
|
|
620
|
+
border-color: #475569;
|
|
621
|
+
background: #0f172a;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/* ---------- Popovers (paletas e link) ---------- */
|
|
625
|
+
/* Redefine o que o UA da a [popover]; left/top vem inline, calculados a partir do botao. */
|
|
626
|
+
.ark-wysiwyg__popover {
|
|
627
|
+
position: fixed;
|
|
628
|
+
inset: auto;
|
|
629
|
+
margin: 0;
|
|
630
|
+
padding: 8px;
|
|
631
|
+
border: 1px solid #e2e8f0;
|
|
632
|
+
border-radius: 8px;
|
|
633
|
+
background: #ffffff;
|
|
634
|
+
color: #0f172a;
|
|
635
|
+
box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
|
|
636
|
+
font-family: inherit;
|
|
637
|
+
}
|
|
638
|
+
.ark-wysiwyg[data-ark-theme="dark"] .ark-wysiwyg__popover {
|
|
639
|
+
border-color: #334155;
|
|
640
|
+
background: #1e293b;
|
|
641
|
+
color: #e2e8f0;
|
|
642
|
+
}
|
|
643
|
+
.ark-wysiwyg__swatches {
|
|
644
|
+
display: grid;
|
|
645
|
+
grid-template-columns: repeat(5, 24px);
|
|
646
|
+
gap: 6px;
|
|
647
|
+
}
|
|
648
|
+
.ark-wysiwyg__swatch {
|
|
649
|
+
width: 24px;
|
|
650
|
+
height: 24px;
|
|
651
|
+
padding: 0;
|
|
652
|
+
border: 1px solid rgba(15, 23, 42, 0.15);
|
|
653
|
+
border-radius: 6px;
|
|
654
|
+
cursor: pointer;
|
|
655
|
+
display: inline-flex;
|
|
656
|
+
align-items: center;
|
|
657
|
+
justify-content: center;
|
|
658
|
+
color: inherit;
|
|
659
|
+
background: transparent;
|
|
660
|
+
}
|
|
661
|
+
.ark-wysiwyg__swatch svg { width: 16px; height: 16px; }
|
|
662
|
+
.ark-wysiwyg__swatch:hover { transform: scale(1.1); }
|
|
663
|
+
.ark-wysiwyg__swatch:focus-visible { outline: 2px solid #6366f1; outline-offset: 1px; }
|
|
664
|
+
.ark-wysiwyg__link {
|
|
665
|
+
display: flex;
|
|
666
|
+
align-items: center;
|
|
667
|
+
gap: 6px;
|
|
668
|
+
}
|
|
669
|
+
.ark-wysiwyg__input {
|
|
670
|
+
width: 16rem;
|
|
671
|
+
max-width: 60vw;
|
|
672
|
+
height: 32px;
|
|
673
|
+
padding: 0 8px;
|
|
674
|
+
border: 1px solid #cbd5e1;
|
|
675
|
+
border-radius: 6px;
|
|
676
|
+
background: #ffffff;
|
|
677
|
+
color: inherit;
|
|
678
|
+
font: inherit;
|
|
679
|
+
font-size: 13px;
|
|
680
|
+
}
|
|
681
|
+
.ark-wysiwyg__input:focus-visible { outline: 2px solid #6366f1; outline-offset: 1px; }
|
|
682
|
+
.ark-wysiwyg__input[aria-invalid="true"] { border-color: #dc2626; }
|
|
683
|
+
.ark-wysiwyg[data-ark-theme="dark"] .ark-wysiwyg__input {
|
|
684
|
+
border-color: #475569;
|
|
685
|
+
background: #0f172a;
|
|
686
|
+
}
|
|
687
|
+
.ark-wysiwyg__error {
|
|
688
|
+
margin: 6px 0 0;
|
|
689
|
+
font-size: 12px;
|
|
690
|
+
color: #dc2626;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/* ---------- Conteudo ---------- */
|
|
694
|
+
.ark-wysiwyg__content {
|
|
695
|
+
border: 1px solid #e2e8f0;
|
|
696
|
+
border-radius: 8px;
|
|
697
|
+
background: #ffffff;
|
|
698
|
+
}
|
|
699
|
+
.ark-wysiwyg[data-ark-theme="dark"] .ark-wysiwyg__content {
|
|
700
|
+
border-color: #334155;
|
|
701
|
+
background: #0f172a;
|
|
702
|
+
}
|
|
703
|
+
.ark-wysiwyg--viewer .ark-wysiwyg__content {
|
|
704
|
+
border: none;
|
|
705
|
+
background: transparent;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
.ark-wysiwyg .ProseMirror {
|
|
709
|
+
min-height: 120px;
|
|
710
|
+
padding: 12px 14px;
|
|
711
|
+
outline: none;
|
|
712
|
+
line-height: 1.6;
|
|
713
|
+
}
|
|
714
|
+
.ark-wysiwyg--viewer .ProseMirror { min-height: 0; padding: 0; }
|
|
715
|
+
.ark-wysiwyg .ProseMirror > * + * { margin-top: 0.6em; }
|
|
716
|
+
.ark-wysiwyg .ProseMirror h1 { font-size: 1.6em; font-weight: 700; }
|
|
717
|
+
.ark-wysiwyg .ProseMirror h2 { font-size: 1.35em; font-weight: 700; }
|
|
718
|
+
.ark-wysiwyg .ProseMirror h3 { font-size: 1.15em; font-weight: 700; }
|
|
719
|
+
.ark-wysiwyg .ProseMirror h4 { font-size: 1em; font-weight: 700; }
|
|
720
|
+
.ark-wysiwyg .ProseMirror ul,
|
|
721
|
+
.ark-wysiwyg .ProseMirror ol { padding-left: 1.4em; }
|
|
722
|
+
.ark-wysiwyg .ProseMirror ul { list-style: disc; }
|
|
723
|
+
.ark-wysiwyg .ProseMirror ol { list-style: decimal; }
|
|
724
|
+
.ark-wysiwyg .ProseMirror blockquote {
|
|
725
|
+
border-left: 3px solid #cbd5e1;
|
|
726
|
+
padding-left: 12px;
|
|
727
|
+
color: #475569;
|
|
728
|
+
}
|
|
729
|
+
.ark-wysiwyg[data-ark-theme="dark"] .ProseMirror blockquote {
|
|
730
|
+
border-left-color: #475569;
|
|
731
|
+
color: #94a3b8;
|
|
732
|
+
}
|
|
733
|
+
.ark-wysiwyg .ProseMirror code {
|
|
734
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
|
735
|
+
font-size: 0.9em;
|
|
736
|
+
background: rgba(100, 116, 139, 0.18);
|
|
737
|
+
padding: 0.1em 0.3em;
|
|
738
|
+
border-radius: 4px;
|
|
739
|
+
}
|
|
740
|
+
.ark-wysiwyg .ProseMirror hr {
|
|
741
|
+
border: none;
|
|
742
|
+
border-top: 1px solid #cbd5e1;
|
|
743
|
+
margin: 1em 0;
|
|
744
|
+
}
|
|
745
|
+
.ark-wysiwyg[data-ark-theme="dark"] .ProseMirror hr { border-top-color: #475569; }
|
|
746
|
+
.ark-wysiwyg .ProseMirror a {
|
|
747
|
+
color: #2563eb;
|
|
748
|
+
text-decoration: underline;
|
|
749
|
+
text-underline-offset: 2px;
|
|
750
|
+
}
|
|
751
|
+
.ark-wysiwyg[data-ark-theme="dark"] .ProseMirror a { color: #93c5fd; }
|
|
752
|
+
/* Marca-texto: fundo pastel com texto escuro nos dois temas, para ler sempre. */
|
|
753
|
+
.ark-wysiwyg .ProseMirror mark {
|
|
754
|
+
color: #0f172a;
|
|
755
|
+
border-radius: 2px;
|
|
756
|
+
padding: 0 0.1em;
|
|
757
|
+
}
|
|
758
|
+
.ark-wysiwyg .ProseMirror img,
|
|
759
|
+
.ark-wysiwyg .ProseMirror video {
|
|
760
|
+
display: block;
|
|
761
|
+
max-width: 100%;
|
|
762
|
+
height: auto;
|
|
763
|
+
border-radius: 6px;
|
|
764
|
+
}
|
|
765
|
+
.ark-wysiwyg .ProseMirror img.ProseMirror-selectednode,
|
|
766
|
+
.ark-wysiwyg .ProseMirror video.ProseMirror-selectednode {
|
|
767
|
+
outline: 2px solid #6366f1;
|
|
768
|
+
outline-offset: 2px;
|
|
769
|
+
}
|
|
770
|
+
.ark-wysiwyg[aria-busy="true"] .ark-wysiwyg__content { cursor: progress; }
|
|
771
|
+
|
|
772
|
+
/* Placeholder (extension-placeholder) */
|
|
773
|
+
.ark-wysiwyg .ProseMirror p.is-editor-empty:first-child::before {
|
|
774
|
+
content: attr(data-placeholder);
|
|
775
|
+
float: left;
|
|
776
|
+
height: 0;
|
|
777
|
+
pointer-events: none;
|
|
778
|
+
color: #94a3b8;
|
|
779
|
+
}
|
|
780
|
+
`;
|
|
781
|
+
/** Injeta o CSS base no <head> uma única vez. */
|
|
782
|
+
function ensureWysiwygStyles() {
|
|
783
|
+
if (typeof document === "undefined")
|
|
784
|
+
return;
|
|
785
|
+
if (document.getElementById(WYSIWYG_STYLE_ID))
|
|
786
|
+
return;
|
|
787
|
+
const style = document.createElement("style");
|
|
788
|
+
style.id = WYSIWYG_STYLE_ID;
|
|
789
|
+
style.textContent = wysiwygCss;
|
|
790
|
+
document.head.appendChild(style);
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/** Itens de cada grupo, na ordem em que aparecem. */
|
|
794
|
+
const ARK_WYSIWYG_TOOLBAR_GROUPS = {
|
|
795
|
+
style: ["heading"],
|
|
796
|
+
marks: ["bold", "italic", "underline", "strike", "code"],
|
|
797
|
+
color: ["text-color", "highlight"],
|
|
798
|
+
align: ["align-left", "align-center", "align-right", "align-justify"],
|
|
799
|
+
lists: ["bullet-list", "ordered-list", "outdent", "indent"],
|
|
800
|
+
link: ["link"],
|
|
801
|
+
media: ["image", "video"],
|
|
802
|
+
blocks: ["blockquote", "horizontal-rule"],
|
|
803
|
+
clear: ["clear-format"],
|
|
804
|
+
history: ["undo", "redo"]
|
|
805
|
+
};
|
|
806
|
+
/** Grupos exibidos sem o atributo `toolbar`; cor, alinhamento e mídia são opt-in. */
|
|
807
|
+
const ARK_WYSIWYG_DEFAULT_TOOLBAR = [
|
|
808
|
+
"style",
|
|
809
|
+
"marks",
|
|
810
|
+
"lists",
|
|
811
|
+
"link",
|
|
812
|
+
"blocks",
|
|
813
|
+
"clear",
|
|
814
|
+
"history"
|
|
815
|
+
];
|
|
816
|
+
|
|
817
|
+
const headingAction = (level) => ({
|
|
818
|
+
icon: "bold",
|
|
819
|
+
label: "heading",
|
|
820
|
+
run: (e) => e.chain().focus().toggleHeading({ level }).run(),
|
|
821
|
+
active: (e) => e.isActive("heading", { level })
|
|
822
|
+
});
|
|
823
|
+
// Botões simples: um comando, um estado. Os itens com popover, select ou arquivo têm construção própria.
|
|
824
|
+
const ACTIONS = {
|
|
825
|
+
"heading-1": headingAction(1),
|
|
826
|
+
"heading-2": headingAction(2),
|
|
827
|
+
"heading-3": headingAction(3),
|
|
828
|
+
"heading-4": headingAction(4),
|
|
829
|
+
bold: {
|
|
830
|
+
icon: "bold",
|
|
831
|
+
label: "bold",
|
|
832
|
+
run: (e) => e.chain().focus().toggleBold().run(),
|
|
833
|
+
active: (e) => e.isActive("bold")
|
|
834
|
+
},
|
|
835
|
+
italic: {
|
|
836
|
+
icon: "italic",
|
|
837
|
+
label: "italic",
|
|
838
|
+
run: (e) => e.chain().focus().toggleItalic().run(),
|
|
839
|
+
active: (e) => e.isActive("italic")
|
|
840
|
+
},
|
|
841
|
+
underline: {
|
|
842
|
+
icon: "underline",
|
|
843
|
+
label: "underline",
|
|
844
|
+
run: (e) => e.chain().focus().toggleUnderline().run(),
|
|
845
|
+
active: (e) => e.isActive("underline")
|
|
846
|
+
},
|
|
847
|
+
strike: {
|
|
848
|
+
icon: "strike",
|
|
849
|
+
label: "strike",
|
|
850
|
+
run: (e) => e.chain().focus().toggleStrike().run(),
|
|
851
|
+
active: (e) => e.isActive("strike")
|
|
852
|
+
},
|
|
853
|
+
code: {
|
|
854
|
+
icon: "code",
|
|
855
|
+
label: "code",
|
|
856
|
+
run: (e) => e.chain().focus().toggleCode().run(),
|
|
857
|
+
active: (e) => e.isActive("code")
|
|
858
|
+
},
|
|
859
|
+
"align-left": {
|
|
860
|
+
icon: "alignLeft",
|
|
861
|
+
label: "alignLeft",
|
|
862
|
+
run: (e) => e.chain().focus().setTextAlign("left").run(),
|
|
863
|
+
active: (e) => e.isActive({ textAlign: "left" })
|
|
864
|
+
},
|
|
865
|
+
"align-center": {
|
|
866
|
+
icon: "alignCenter",
|
|
867
|
+
label: "alignCenter",
|
|
868
|
+
run: (e) => e.chain().focus().setTextAlign("center").run(),
|
|
869
|
+
active: (e) => e.isActive({ textAlign: "center" })
|
|
870
|
+
},
|
|
871
|
+
"align-right": {
|
|
872
|
+
icon: "alignRight",
|
|
873
|
+
label: "alignRight",
|
|
874
|
+
run: (e) => e.chain().focus().setTextAlign("right").run(),
|
|
875
|
+
active: (e) => e.isActive({ textAlign: "right" })
|
|
876
|
+
},
|
|
877
|
+
"align-justify": {
|
|
878
|
+
icon: "alignJustify",
|
|
879
|
+
label: "alignJustify",
|
|
880
|
+
run: (e) => e.chain().focus().setTextAlign("justify").run(),
|
|
881
|
+
active: (e) => e.isActive({ textAlign: "justify" })
|
|
882
|
+
},
|
|
883
|
+
"bullet-list": {
|
|
884
|
+
icon: "bulletList",
|
|
885
|
+
label: "bulletList",
|
|
886
|
+
run: (e) => e.chain().focus().toggleBulletList().run(),
|
|
887
|
+
active: (e) => e.isActive("bulletList")
|
|
888
|
+
},
|
|
889
|
+
"ordered-list": {
|
|
890
|
+
icon: "orderedList",
|
|
891
|
+
label: "orderedList",
|
|
892
|
+
run: (e) => e.chain().focus().toggleOrderedList().run(),
|
|
893
|
+
active: (e) => e.isActive("orderedList")
|
|
894
|
+
},
|
|
895
|
+
// Recuo só em listas (aninhar/desaninhar itens): recuo de parágrafo não tem semântica no documento.
|
|
896
|
+
outdent: {
|
|
897
|
+
icon: "outdent",
|
|
898
|
+
label: "outdent",
|
|
899
|
+
run: (e) => e.chain().focus().liftListItem("listItem").run(),
|
|
900
|
+
can: (e) => e.can().liftListItem("listItem")
|
|
901
|
+
},
|
|
902
|
+
indent: {
|
|
903
|
+
icon: "indent",
|
|
904
|
+
label: "indent",
|
|
905
|
+
run: (e) => e.chain().focus().sinkListItem("listItem").run(),
|
|
906
|
+
can: (e) => e.can().sinkListItem("listItem")
|
|
907
|
+
},
|
|
908
|
+
blockquote: {
|
|
909
|
+
icon: "blockquote",
|
|
910
|
+
label: "blockquote",
|
|
911
|
+
run: (e) => e.chain().focus().toggleBlockquote().run(),
|
|
912
|
+
active: (e) => e.isActive("blockquote")
|
|
913
|
+
},
|
|
914
|
+
"horizontal-rule": {
|
|
915
|
+
icon: "horizontalRule",
|
|
916
|
+
label: "horizontalRule",
|
|
917
|
+
run: (e) => e.chain().focus().setHorizontalRule().run()
|
|
918
|
+
},
|
|
919
|
+
"clear-format": {
|
|
920
|
+
icon: "clearFormat",
|
|
921
|
+
label: "clearFormat",
|
|
922
|
+
run: (e) => e.chain().focus().unsetAllMarks().clearNodes().run()
|
|
923
|
+
},
|
|
924
|
+
undo: { icon: "undo", label: "undo", run: (e) => e.chain().focus().undo().run(), can: (e) => e.can().undo() },
|
|
925
|
+
redo: { icon: "redo", label: "redo", run: (e) => e.chain().focus().redo().run(), can: (e) => e.can().redo() }
|
|
926
|
+
};
|
|
927
|
+
const ALL_ITEMS = new Set(Object.values(ARK_WYSIWYG_TOOLBAR_GROUPS).flat());
|
|
928
|
+
function groupOf(item) {
|
|
929
|
+
if (item.startsWith("heading"))
|
|
930
|
+
return "style";
|
|
931
|
+
for (const [group, items] of Object.entries(ARK_WYSIWYG_TOOLBAR_GROUPS)) {
|
|
932
|
+
if (items.includes(item))
|
|
933
|
+
return group;
|
|
934
|
+
}
|
|
935
|
+
return "marks";
|
|
936
|
+
}
|
|
937
|
+
/** Cores oferecidas por padrão: tons médios, legíveis nos dois temas. */
|
|
938
|
+
const DEFAULT_COLORS = [
|
|
939
|
+
"#ef4444",
|
|
940
|
+
"#f97316",
|
|
941
|
+
"#eab308",
|
|
942
|
+
"#22c55e",
|
|
943
|
+
"#06b6d4",
|
|
944
|
+
"#3b82f6",
|
|
945
|
+
"#8b5cf6",
|
|
946
|
+
"#ec4899",
|
|
947
|
+
"#64748b"
|
|
948
|
+
];
|
|
949
|
+
/** Marca-texto por padrão: pastéis; o CSS força texto escuro dentro de `mark` para ler nos dois temas. */
|
|
950
|
+
const DEFAULT_HIGHLIGHTS = ["#fde047", "#fdba74", "#86efac", "#7dd3fc", "#c4b5fd", "#f9a8d4"];
|
|
951
|
+
let popoverSeq = 0;
|
|
952
|
+
/**
|
|
953
|
+
* Editor WYSIWYG (Custom Element) baseado em Tiptap.
|
|
954
|
+
*
|
|
955
|
+
* O conteúdo é trocado pela propriedade `content` (JSON do Tiptap, sanitizado
|
|
956
|
+
* ao entrar), não por atributo. Dispara `ark-wysiwyg-change` (bubbles/composed)
|
|
957
|
+
* com `detail` = JSON a cada edição do usuário.
|
|
958
|
+
*
|
|
959
|
+
* Atributos: `theme`, `placeholder`, `editable` ("false" desabilita), `lang` e
|
|
960
|
+
* `locale-json` (rótulos), `toolbar` (grupos e/ou itens separados por vírgula,
|
|
961
|
+
* `all`, ou "none"/"false" para ocultar; padrão: style, marks, lists, link,
|
|
962
|
+
* blocks, clear, history), `colors` e `highlights` (JSON com a paleta),
|
|
963
|
+
* `max-file-size` (bytes). Propriedade `uploadFile`: sem ela, imagem e vídeo
|
|
964
|
+
* não aparecem na toolbar e arquivos colados ou arrastados são recusados
|
|
965
|
+
* (`ark-wysiwyg-upload-error`), nunca embutidos em base64.
|
|
966
|
+
*/
|
|
967
|
+
class ArkWysiwygEditor extends HTMLElement {
|
|
968
|
+
constructor() {
|
|
969
|
+
super(...arguments);
|
|
970
|
+
this.instance = null;
|
|
971
|
+
this.root = null;
|
|
972
|
+
this.toolbarEl = null;
|
|
973
|
+
this.pendingContent = null;
|
|
974
|
+
this.uploader = null;
|
|
975
|
+
this.labels = resolveWysiwygLabels("en");
|
|
976
|
+
/** Controles da toolbar na ordem visual, para o tabindex circulante. */
|
|
977
|
+
this.controls = [];
|
|
978
|
+
/** Sincronizadores por controle (estado ativo, habilitado, cor atual), rodados a cada mudança de seleção. */
|
|
979
|
+
this.syncers = [];
|
|
980
|
+
/** Para de observar o tema da página; só existe com `theme="auto"`. */
|
|
981
|
+
this.disposeTheme = null;
|
|
982
|
+
// Um tab stop por toolbar: setas movem entre os controles habilitados, Home/End vão às pontas.
|
|
983
|
+
this.handleToolbarKeydown = (event) => {
|
|
984
|
+
if (!["ArrowLeft", "ArrowRight", "Home", "End"].includes(event.key))
|
|
985
|
+
return;
|
|
986
|
+
const target = event.target;
|
|
987
|
+
const enabled = this.controls.filter((control) => !control.disabled);
|
|
988
|
+
const index = enabled.indexOf(target);
|
|
989
|
+
if (index < 0)
|
|
990
|
+
return;
|
|
991
|
+
event.preventDefault();
|
|
992
|
+
let next = index;
|
|
993
|
+
if (event.key === "ArrowLeft")
|
|
994
|
+
next = (index - 1 + enabled.length) % enabled.length;
|
|
995
|
+
if (event.key === "ArrowRight")
|
|
996
|
+
next = (index + 1) % enabled.length;
|
|
997
|
+
if (event.key === "Home")
|
|
998
|
+
next = 0;
|
|
999
|
+
if (event.key === "End")
|
|
1000
|
+
next = enabled.length - 1;
|
|
1001
|
+
this.setCurrent(enabled[next]);
|
|
1002
|
+
enabled[next].focus();
|
|
1003
|
+
};
|
|
1004
|
+
this.handleToolbarFocus = (event) => {
|
|
1005
|
+
const target = event.target;
|
|
1006
|
+
if (this.controls.includes(target))
|
|
1007
|
+
this.setCurrent(target);
|
|
1008
|
+
};
|
|
1009
|
+
this.syncToolbar = () => {
|
|
1010
|
+
if (!this.instance)
|
|
1011
|
+
return;
|
|
1012
|
+
for (const sync of this.syncers)
|
|
1013
|
+
sync();
|
|
1014
|
+
// O controle corrente do tabindex não pode ser um desabilitado, senão a toolbar some da ordem do Tab.
|
|
1015
|
+
const current = this.controls.find((control) => control.tabIndex === 0);
|
|
1016
|
+
if (current && current.disabled) {
|
|
1017
|
+
const first = this.controls.find((control) => !control.disabled);
|
|
1018
|
+
if (first)
|
|
1019
|
+
this.setCurrent(first);
|
|
1020
|
+
}
|
|
1021
|
+
};
|
|
1022
|
+
}
|
|
1023
|
+
static get observedAttributes() {
|
|
1024
|
+
return [
|
|
1025
|
+
"theme",
|
|
1026
|
+
"placeholder",
|
|
1027
|
+
"editable",
|
|
1028
|
+
"toolbar",
|
|
1029
|
+
"lang",
|
|
1030
|
+
"locale-json",
|
|
1031
|
+
"colors",
|
|
1032
|
+
"highlights",
|
|
1033
|
+
"max-file-size"
|
|
1034
|
+
];
|
|
1035
|
+
}
|
|
1036
|
+
/** Conteúdo do editor como JSON do Tiptap. Atribuir substitui (sanitizado) sem disparar `ark-wysiwyg-change`. */
|
|
1037
|
+
get content() {
|
|
1038
|
+
return this.instance ? this.instance.getJSON() : this.pendingContent;
|
|
1039
|
+
}
|
|
1040
|
+
set content(value) {
|
|
1041
|
+
this.pendingContent = value;
|
|
1042
|
+
this.instance?.setContent(value);
|
|
1043
|
+
}
|
|
1044
|
+
/** Gancho de upload de imagem/vídeo (propriedade JS). Sem ele o grupo `media` não aparece e arquivos são recusados. */
|
|
1045
|
+
get uploadFile() {
|
|
1046
|
+
return this.uploader;
|
|
1047
|
+
}
|
|
1048
|
+
set uploadFile(value) {
|
|
1049
|
+
this.uploader = typeof value === "function" ? value : null;
|
|
1050
|
+
if (this.instance)
|
|
1051
|
+
this.build();
|
|
1052
|
+
}
|
|
1053
|
+
/** Paleta da cor do texto (atributo `colors` em JSON ou propriedade). */
|
|
1054
|
+
get colors() {
|
|
1055
|
+
return this.readPalette("colors", DEFAULT_COLORS);
|
|
1056
|
+
}
|
|
1057
|
+
set colors(value) {
|
|
1058
|
+
this.writePalette("colors", value);
|
|
1059
|
+
}
|
|
1060
|
+
/** Paleta do marca-texto (atributo `highlights` em JSON ou propriedade). */
|
|
1061
|
+
get highlights() {
|
|
1062
|
+
return this.readPalette("highlights", DEFAULT_HIGHLIGHTS);
|
|
1063
|
+
}
|
|
1064
|
+
set highlights(value) {
|
|
1065
|
+
this.writePalette("highlights", value);
|
|
1066
|
+
}
|
|
1067
|
+
/** Tema efetivamente aplicado (depois de resolver `auto`); `null` antes de renderizar. */
|
|
1068
|
+
get resolvedTheme() {
|
|
1069
|
+
return this.instance?.resolvedTheme() ?? null;
|
|
1070
|
+
}
|
|
1071
|
+
/** Instância nativa do Tiptap (uso avançado); `null` antes de renderizar. */
|
|
1072
|
+
get editor() {
|
|
1073
|
+
return this.instance?.editor ?? null;
|
|
1074
|
+
}
|
|
1075
|
+
connectedCallback() {
|
|
1076
|
+
ensureWysiwygStyles();
|
|
1077
|
+
this.build();
|
|
1078
|
+
}
|
|
1079
|
+
disconnectedCallback() {
|
|
1080
|
+
this.teardown();
|
|
1081
|
+
}
|
|
1082
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
1083
|
+
if (oldValue === newValue || !this.instance)
|
|
1084
|
+
return;
|
|
1085
|
+
if (name === "theme") {
|
|
1086
|
+
this.applyTheme(this.getTheme());
|
|
1087
|
+
this.syncThemeObserver();
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
1090
|
+
if (name === "editable") {
|
|
1091
|
+
this.instance.setEditable(this.isEditable());
|
|
1092
|
+
this.syncToolbar();
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1095
|
+
// placeholder, toolbar, rótulos, paletas e limite exigem reconstrução.
|
|
1096
|
+
this.build();
|
|
1097
|
+
}
|
|
1098
|
+
/** Envia um arquivo pelo gancho e insere a mídia na seleção; `false` se recusado (o evento diz por quê). */
|
|
1099
|
+
insertFile(file) {
|
|
1100
|
+
return this.instance ? this.instance.insertFile(file) : Promise.resolve(false);
|
|
1101
|
+
}
|
|
1102
|
+
getTheme() {
|
|
1103
|
+
const value = (this.getAttribute("theme") || "auto").toLowerCase();
|
|
1104
|
+
if (value === "light" || value === "dark")
|
|
1105
|
+
return value;
|
|
1106
|
+
return "auto";
|
|
1107
|
+
}
|
|
1108
|
+
isEditable() {
|
|
1109
|
+
return this.getAttribute("editable") !== "false";
|
|
1110
|
+
}
|
|
1111
|
+
readPalette(name, fallback) {
|
|
1112
|
+
const raw = this.getAttribute(name);
|
|
1113
|
+
if (raw === null)
|
|
1114
|
+
return [...fallback];
|
|
1115
|
+
try {
|
|
1116
|
+
const parsed = JSON.parse(raw);
|
|
1117
|
+
return Array.isArray(parsed) ? parsed.filter(isSafeColor) : [...fallback];
|
|
1118
|
+
}
|
|
1119
|
+
catch {
|
|
1120
|
+
return [...fallback];
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
writePalette(name, value) {
|
|
1124
|
+
if (Array.isArray(value)) {
|
|
1125
|
+
this.setAttribute(name, JSON.stringify(value.filter(isSafeColor)));
|
|
1126
|
+
}
|
|
1127
|
+
else {
|
|
1128
|
+
this.removeAttribute(name);
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
getMaxFileSize() {
|
|
1132
|
+
const raw = Number(this.getAttribute("max-file-size"));
|
|
1133
|
+
return this.hasAttribute("max-file-size") && Number.isFinite(raw) && raw > 0 ? raw : DEFAULT_MAX_FILE_SIZE;
|
|
1134
|
+
}
|
|
1135
|
+
// Grupos e itens viram segmentos (um por grupo, na ordem pedida); `media` só entra com o gancho de upload.
|
|
1136
|
+
getToolbar() {
|
|
1137
|
+
const raw = this.getAttribute("toolbar");
|
|
1138
|
+
let tokens;
|
|
1139
|
+
if (raw === null) {
|
|
1140
|
+
tokens = [...ARK_WYSIWYG_DEFAULT_TOOLBAR];
|
|
1141
|
+
}
|
|
1142
|
+
else if (raw === "none" || raw === "false" || raw.trim() === "") {
|
|
1143
|
+
return false;
|
|
1144
|
+
}
|
|
1145
|
+
else if (raw.trim() === "all") {
|
|
1146
|
+
tokens = Object.keys(ARK_WYSIWYG_TOOLBAR_GROUPS);
|
|
1147
|
+
}
|
|
1148
|
+
else {
|
|
1149
|
+
tokens = raw.split(",").map((token) => token.trim());
|
|
1150
|
+
}
|
|
1151
|
+
const segments = [];
|
|
1152
|
+
const push = (group, items) => {
|
|
1153
|
+
const last = segments[segments.length - 1];
|
|
1154
|
+
if (last && last.group === group) {
|
|
1155
|
+
last.items.push(...items);
|
|
1156
|
+
}
|
|
1157
|
+
else {
|
|
1158
|
+
segments.push({ group, items: [...items] });
|
|
1159
|
+
}
|
|
1160
|
+
};
|
|
1161
|
+
for (const token of tokens) {
|
|
1162
|
+
if (token in ARK_WYSIWYG_TOOLBAR_GROUPS) {
|
|
1163
|
+
const group = token;
|
|
1164
|
+
push(group, ARK_WYSIWYG_TOOLBAR_GROUPS[group]);
|
|
1165
|
+
}
|
|
1166
|
+
else if (ALL_ITEMS.has(token) || token in ACTIONS) {
|
|
1167
|
+
const item = token;
|
|
1168
|
+
push(groupOf(item), [item]);
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
const result = segments
|
|
1172
|
+
.map((segment) => ({
|
|
1173
|
+
group: segment.group,
|
|
1174
|
+
items: segment.items.filter((item) => (item === "image" || item === "video" ? this.uploader !== null : true))
|
|
1175
|
+
}))
|
|
1176
|
+
.filter((segment) => segment.items.length > 0);
|
|
1177
|
+
return result.length > 0 ? result : false;
|
|
1178
|
+
}
|
|
1179
|
+
build() {
|
|
1180
|
+
this.teardown();
|
|
1181
|
+
if (this.pendingContent === null) {
|
|
1182
|
+
this.pendingContent = this.content;
|
|
1183
|
+
}
|
|
1184
|
+
this.labels = resolveWysiwygLabels(this.getAttribute("lang"), this.getAttribute("locale-json"));
|
|
1185
|
+
const root = document.createElement("div");
|
|
1186
|
+
root.className = "ark-wysiwyg";
|
|
1187
|
+
const segments = this.getToolbar();
|
|
1188
|
+
if (segments) {
|
|
1189
|
+
this.toolbarEl = this.buildToolbar(segments);
|
|
1190
|
+
root.appendChild(this.toolbarEl);
|
|
1191
|
+
}
|
|
1192
|
+
const mount = document.createElement("div");
|
|
1193
|
+
mount.className = "ark-wysiwyg__content";
|
|
1194
|
+
root.appendChild(mount);
|
|
1195
|
+
this.appendChild(root);
|
|
1196
|
+
this.root = root;
|
|
1197
|
+
this.instance = createWysiwygEditor(mount, {
|
|
1198
|
+
content: this.pendingContent,
|
|
1199
|
+
theme: this.getTheme(),
|
|
1200
|
+
placeholder: this.getAttribute("placeholder") || undefined,
|
|
1201
|
+
editable: this.isEditable(),
|
|
1202
|
+
uploadFile: this.uploader ?? undefined,
|
|
1203
|
+
maxFileSize: this.getMaxFileSize(),
|
|
1204
|
+
onChange: (content) => {
|
|
1205
|
+
this.dispatchEvent(new CustomEvent("ark-wysiwyg-change", {
|
|
1206
|
+
detail: content,
|
|
1207
|
+
bubbles: true,
|
|
1208
|
+
composed: true
|
|
1209
|
+
}));
|
|
1210
|
+
this.syncToolbar();
|
|
1211
|
+
},
|
|
1212
|
+
onUploadError: (error) => {
|
|
1213
|
+
this.dispatchEvent(new CustomEvent("ark-wysiwyg-upload-error", { detail: error, bubbles: true, composed: true }));
|
|
1214
|
+
},
|
|
1215
|
+
onUploadingChange: (count) => {
|
|
1216
|
+
this.root?.toggleAttribute("aria-busy", count > 0);
|
|
1217
|
+
this.root?.setAttribute("data-uploading", String(count));
|
|
1218
|
+
this.syncToolbar();
|
|
1219
|
+
}
|
|
1220
|
+
});
|
|
1221
|
+
this.instance.editor.on("selectionUpdate", this.syncToolbar);
|
|
1222
|
+
this.instance.editor.on("transaction", this.syncToolbar);
|
|
1223
|
+
this.syncToolbar();
|
|
1224
|
+
this.root.setAttribute("data-ark-theme", this.instance.resolvedTheme());
|
|
1225
|
+
this.syncThemeObserver();
|
|
1226
|
+
}
|
|
1227
|
+
// Em `auto`, a troca de tema da página re-resolve o tema do editor; com tema fixo não há o que observar.
|
|
1228
|
+
syncThemeObserver() {
|
|
1229
|
+
this.disposeTheme?.();
|
|
1230
|
+
this.disposeTheme = null;
|
|
1231
|
+
if (this.getTheme() !== "auto")
|
|
1232
|
+
return;
|
|
1233
|
+
this.disposeTheme = tokens.observeColorScheme(this, () => this.applyTheme("auto"));
|
|
1234
|
+
}
|
|
1235
|
+
// O engine marca o mount com data-ark-theme; o CSS do tema lê a raiz .ark-wysiwyg, então ela recebe o mesmo.
|
|
1236
|
+
applyTheme(theme) {
|
|
1237
|
+
if (!this.instance)
|
|
1238
|
+
return;
|
|
1239
|
+
this.instance.setTheme(theme);
|
|
1240
|
+
this.root?.setAttribute("data-ark-theme", this.instance.resolvedTheme());
|
|
1241
|
+
}
|
|
1242
|
+
// --- Toolbar ---
|
|
1243
|
+
buildToolbar(segments) {
|
|
1244
|
+
const toolbar = document.createElement("div");
|
|
1245
|
+
toolbar.className = "ark-wysiwyg__toolbar";
|
|
1246
|
+
toolbar.setAttribute("part", "toolbar");
|
|
1247
|
+
toolbar.setAttribute("role", "toolbar");
|
|
1248
|
+
toolbar.addEventListener("keydown", this.handleToolbarKeydown);
|
|
1249
|
+
toolbar.addEventListener("focusin", this.handleToolbarFocus);
|
|
1250
|
+
this.controls = [];
|
|
1251
|
+
this.syncers = [];
|
|
1252
|
+
segments.forEach((segment, index) => {
|
|
1253
|
+
if (index > 0) {
|
|
1254
|
+
const separator = document.createElement("span");
|
|
1255
|
+
separator.className = "ark-wysiwyg__separator";
|
|
1256
|
+
separator.setAttribute("role", "separator");
|
|
1257
|
+
separator.setAttribute("aria-orientation", "vertical");
|
|
1258
|
+
toolbar.appendChild(separator);
|
|
1259
|
+
}
|
|
1260
|
+
const group = document.createElement("div");
|
|
1261
|
+
group.className = "ark-wysiwyg__group";
|
|
1262
|
+
group.setAttribute("data-group", segment.group);
|
|
1263
|
+
for (const item of segment.items)
|
|
1264
|
+
group.appendChild(this.buildItem(item));
|
|
1265
|
+
toolbar.appendChild(group);
|
|
1266
|
+
});
|
|
1267
|
+
if (this.controls.length > 0)
|
|
1268
|
+
this.controls[0].tabIndex = 0;
|
|
1269
|
+
return toolbar;
|
|
1270
|
+
}
|
|
1271
|
+
buildItem(item) {
|
|
1272
|
+
if (item === "heading")
|
|
1273
|
+
return this.buildHeadingSelect();
|
|
1274
|
+
if (item === "text-color")
|
|
1275
|
+
return this.buildPalette("text-color");
|
|
1276
|
+
if (item === "highlight")
|
|
1277
|
+
return this.buildPalette("highlight");
|
|
1278
|
+
if (item === "link")
|
|
1279
|
+
return this.buildLink();
|
|
1280
|
+
if (item === "image" || item === "video")
|
|
1281
|
+
return this.buildFilePicker(item);
|
|
1282
|
+
const action = ACTIONS[item];
|
|
1283
|
+
const level = item.startsWith("heading-") ? item.slice(-1) : "";
|
|
1284
|
+
const label = level ? `${this.labels.heading} ${level}` : this.labels[action.label];
|
|
1285
|
+
const button = this.createButton(label, action.icon);
|
|
1286
|
+
if (level) {
|
|
1287
|
+
button.textContent = `H${level}`;
|
|
1288
|
+
button.classList.add("ark-wysiwyg__btn--text");
|
|
1289
|
+
}
|
|
1290
|
+
button.addEventListener("click", () => {
|
|
1291
|
+
if (!this.instance)
|
|
1292
|
+
return;
|
|
1293
|
+
action.run(this.instance.editor);
|
|
1294
|
+
this.syncToolbar();
|
|
1295
|
+
});
|
|
1296
|
+
this.syncers.push(() => {
|
|
1297
|
+
if (!this.instance)
|
|
1298
|
+
return;
|
|
1299
|
+
const editor = this.instance.editor;
|
|
1300
|
+
if (action.active)
|
|
1301
|
+
button.setAttribute("aria-pressed", String(action.active(editor)));
|
|
1302
|
+
button.disabled = !editor.isEditable || (action.can ? !action.can(editor) : false);
|
|
1303
|
+
});
|
|
1304
|
+
return button;
|
|
1305
|
+
}
|
|
1306
|
+
createButton(label, icon) {
|
|
1307
|
+
const button = document.createElement("button");
|
|
1308
|
+
button.type = "button";
|
|
1309
|
+
button.className = "ark-wysiwyg__btn";
|
|
1310
|
+
button.innerHTML = ICONS[icon];
|
|
1311
|
+
button.title = label;
|
|
1312
|
+
button.setAttribute("aria-label", label);
|
|
1313
|
+
button.tabIndex = -1;
|
|
1314
|
+
this.controls.push(button);
|
|
1315
|
+
return button;
|
|
1316
|
+
}
|
|
1317
|
+
// Estilo do bloco: um <select> nativo (teclado e leitor de tela de graça) mostrando o estilo atual.
|
|
1318
|
+
buildHeadingSelect() {
|
|
1319
|
+
const select = document.createElement("select");
|
|
1320
|
+
select.className = "ark-wysiwyg__select";
|
|
1321
|
+
select.setAttribute("aria-label", this.labels.textStyle);
|
|
1322
|
+
select.title = this.labels.textStyle;
|
|
1323
|
+
select.tabIndex = -1;
|
|
1324
|
+
const normal = document.createElement("option");
|
|
1325
|
+
normal.value = "paragraph";
|
|
1326
|
+
normal.textContent = this.labels.paragraph;
|
|
1327
|
+
select.appendChild(normal);
|
|
1328
|
+
for (const level of HEADING_LEVELS) {
|
|
1329
|
+
const option = document.createElement("option");
|
|
1330
|
+
option.value = String(level);
|
|
1331
|
+
option.textContent = `${this.labels.heading} ${level}`;
|
|
1332
|
+
select.appendChild(option);
|
|
1333
|
+
}
|
|
1334
|
+
select.addEventListener("change", () => {
|
|
1335
|
+
if (!this.instance)
|
|
1336
|
+
return;
|
|
1337
|
+
const chain = this.instance.editor.chain().focus();
|
|
1338
|
+
if (select.value === "paragraph") {
|
|
1339
|
+
chain.setParagraph().run();
|
|
1340
|
+
}
|
|
1341
|
+
else {
|
|
1342
|
+
chain.setHeading({ level: Number(select.value) }).run();
|
|
1343
|
+
}
|
|
1344
|
+
this.syncToolbar();
|
|
1345
|
+
});
|
|
1346
|
+
this.controls.push(select);
|
|
1347
|
+
this.syncers.push(() => {
|
|
1348
|
+
if (!this.instance)
|
|
1349
|
+
return;
|
|
1350
|
+
const editor = this.instance.editor;
|
|
1351
|
+
const level = HEADING_LEVELS.find((candidate) => editor.isActive("heading", { level: candidate }));
|
|
1352
|
+
const value = level ? String(level) : "paragraph";
|
|
1353
|
+
if (select.value !== value)
|
|
1354
|
+
select.value = value;
|
|
1355
|
+
select.disabled = !editor.isEditable;
|
|
1356
|
+
});
|
|
1357
|
+
return select;
|
|
1358
|
+
}
|
|
1359
|
+
// Cor do texto e marca-texto: botão com a cor atual na barra e uma paleta em popover (light dismiss nativo).
|
|
1360
|
+
buildPalette(item) {
|
|
1361
|
+
const isText = item === "text-color";
|
|
1362
|
+
const wrap = document.createElement("span");
|
|
1363
|
+
wrap.className = "ark-wysiwyg__item";
|
|
1364
|
+
const button = this.createButton(isText ? this.labels.textColor : this.labels.highlight, isText ? "textColor" : "highlight");
|
|
1365
|
+
button.classList.add("ark-wysiwyg__btn--swatch");
|
|
1366
|
+
const popover = this.createPopover();
|
|
1367
|
+
button.setAttribute("popovertarget", popover.id);
|
|
1368
|
+
button.setAttribute("aria-haspopup", "true");
|
|
1369
|
+
const grid = document.createElement("div");
|
|
1370
|
+
grid.className = "ark-wysiwyg__swatches";
|
|
1371
|
+
grid.setAttribute("role", "group");
|
|
1372
|
+
grid.setAttribute("aria-label", button.title);
|
|
1373
|
+
const apply = (color) => {
|
|
1374
|
+
if (!this.instance)
|
|
1375
|
+
return;
|
|
1376
|
+
const chain = this.instance.editor.chain().focus();
|
|
1377
|
+
if (isText) {
|
|
1378
|
+
(color ? chain.setColor(color) : chain.unsetColor()).run();
|
|
1379
|
+
}
|
|
1380
|
+
else {
|
|
1381
|
+
(color ? chain.setHighlight({ color }) : chain.unsetHighlight()).run();
|
|
1382
|
+
}
|
|
1383
|
+
popover.hidePopover();
|
|
1384
|
+
this.syncToolbar();
|
|
1385
|
+
};
|
|
1386
|
+
for (const color of isText ? this.colors : this.highlights) {
|
|
1387
|
+
const swatch = document.createElement("button");
|
|
1388
|
+
swatch.type = "button";
|
|
1389
|
+
swatch.className = "ark-wysiwyg__swatch";
|
|
1390
|
+
swatch.style.backgroundColor = color;
|
|
1391
|
+
swatch.title = color;
|
|
1392
|
+
swatch.setAttribute("aria-label", color);
|
|
1393
|
+
swatch.addEventListener("click", () => apply(color));
|
|
1394
|
+
grid.appendChild(swatch);
|
|
1395
|
+
}
|
|
1396
|
+
const none = document.createElement("button");
|
|
1397
|
+
none.type = "button";
|
|
1398
|
+
none.className = "ark-wysiwyg__swatch ark-wysiwyg__swatch--none";
|
|
1399
|
+
none.innerHTML = ICONS.none;
|
|
1400
|
+
none.title = this.labels.noColor;
|
|
1401
|
+
none.setAttribute("aria-label", this.labels.noColor);
|
|
1402
|
+
none.addEventListener("click", () => apply(null));
|
|
1403
|
+
grid.appendChild(none);
|
|
1404
|
+
popover.appendChild(grid);
|
|
1405
|
+
wrap.append(button, popover);
|
|
1406
|
+
this.syncers.push(() => {
|
|
1407
|
+
if (!this.instance)
|
|
1408
|
+
return;
|
|
1409
|
+
const editor = this.instance.editor;
|
|
1410
|
+
const current = isText ? editor.getAttributes("textStyle").color : editor.getAttributes("highlight").color;
|
|
1411
|
+
button.style.setProperty("--ark-wysiwyg-swatch", isSafeColor(current) ? current : "transparent");
|
|
1412
|
+
button.setAttribute("aria-pressed", String(isSafeColor(current)));
|
|
1413
|
+
button.disabled = !editor.isEditable;
|
|
1414
|
+
});
|
|
1415
|
+
return wrap;
|
|
1416
|
+
}
|
|
1417
|
+
// Link: popover com campo de URL; aplica na seleção (ou insere a URL) e recusa esquemas fora da lista.
|
|
1418
|
+
buildLink() {
|
|
1419
|
+
const wrap = document.createElement("span");
|
|
1420
|
+
wrap.className = "ark-wysiwyg__item";
|
|
1421
|
+
const button = this.createButton(this.labels.link, "link");
|
|
1422
|
+
const popover = this.createPopover();
|
|
1423
|
+
popover.classList.add("ark-wysiwyg__popover--link");
|
|
1424
|
+
button.setAttribute("popovertarget", popover.id);
|
|
1425
|
+
button.setAttribute("aria-haspopup", "dialog");
|
|
1426
|
+
const form = document.createElement("form");
|
|
1427
|
+
form.className = "ark-wysiwyg__link";
|
|
1428
|
+
const input = document.createElement("input");
|
|
1429
|
+
input.type = "text";
|
|
1430
|
+
input.inputMode = "url";
|
|
1431
|
+
input.setAttribute("autocomplete", "url");
|
|
1432
|
+
input.className = "ark-wysiwyg__input";
|
|
1433
|
+
input.placeholder = "https://";
|
|
1434
|
+
input.setAttribute("aria-label", this.labels.linkUrl);
|
|
1435
|
+
const error = document.createElement("p");
|
|
1436
|
+
error.className = "ark-wysiwyg__error";
|
|
1437
|
+
error.id = `${popover.id}-error`;
|
|
1438
|
+
error.hidden = true;
|
|
1439
|
+
error.textContent = this.labels.invalidUrl;
|
|
1440
|
+
const applyBtn = document.createElement("button");
|
|
1441
|
+
applyBtn.type = "submit";
|
|
1442
|
+
applyBtn.className = "ark-wysiwyg__btn ark-wysiwyg__btn--text";
|
|
1443
|
+
applyBtn.textContent = this.labels.apply;
|
|
1444
|
+
const removeBtn = document.createElement("button");
|
|
1445
|
+
removeBtn.type = "button";
|
|
1446
|
+
removeBtn.className = "ark-wysiwyg__btn ark-wysiwyg__btn--text";
|
|
1447
|
+
removeBtn.textContent = this.labels.remove;
|
|
1448
|
+
form.addEventListener("submit", (event) => {
|
|
1449
|
+
event.preventDefault();
|
|
1450
|
+
if (!this.instance)
|
|
1451
|
+
return;
|
|
1452
|
+
const ok = this.instance.setLink(input.value);
|
|
1453
|
+
error.hidden = ok;
|
|
1454
|
+
input.setAttribute("aria-invalid", ok ? "false" : "true");
|
|
1455
|
+
if (ok) {
|
|
1456
|
+
input.removeAttribute("aria-describedby");
|
|
1457
|
+
popover.hidePopover();
|
|
1458
|
+
this.syncToolbar();
|
|
1459
|
+
}
|
|
1460
|
+
else {
|
|
1461
|
+
input.setAttribute("aria-describedby", error.id);
|
|
1462
|
+
input.focus();
|
|
1463
|
+
}
|
|
1464
|
+
});
|
|
1465
|
+
removeBtn.addEventListener("click", () => {
|
|
1466
|
+
this.instance?.unsetLink();
|
|
1467
|
+
popover.hidePopover();
|
|
1468
|
+
this.syncToolbar();
|
|
1469
|
+
});
|
|
1470
|
+
popover.addEventListener("toggle", (event) => {
|
|
1471
|
+
if (event.newState !== "open" || !this.instance)
|
|
1472
|
+
return;
|
|
1473
|
+
const href = this.instance.editor.getAttributes("link").href;
|
|
1474
|
+
input.value = typeof href === "string" ? href : "";
|
|
1475
|
+
error.hidden = true;
|
|
1476
|
+
input.removeAttribute("aria-invalid");
|
|
1477
|
+
input.removeAttribute("aria-describedby");
|
|
1478
|
+
removeBtn.hidden = !this.instance.editor.isActive("link");
|
|
1479
|
+
input.focus();
|
|
1480
|
+
input.select();
|
|
1481
|
+
});
|
|
1482
|
+
form.append(input, applyBtn, removeBtn);
|
|
1483
|
+
popover.append(form, error);
|
|
1484
|
+
wrap.append(button, popover);
|
|
1485
|
+
this.syncers.push(() => {
|
|
1486
|
+
if (!this.instance)
|
|
1487
|
+
return;
|
|
1488
|
+
const editor = this.instance.editor;
|
|
1489
|
+
button.setAttribute("aria-pressed", String(editor.isActive("link")));
|
|
1490
|
+
button.disabled = !editor.isEditable;
|
|
1491
|
+
});
|
|
1492
|
+
return wrap;
|
|
1493
|
+
}
|
|
1494
|
+
// Imagem e vídeo: input de arquivo oculto; o envio é do gancho `uploadFile`, o editor só insere a URL.
|
|
1495
|
+
buildFilePicker(item) {
|
|
1496
|
+
const wrap = document.createElement("span");
|
|
1497
|
+
wrap.className = "ark-wysiwyg__item";
|
|
1498
|
+
const label = item === "image" ? this.labels.image : this.labels.video;
|
|
1499
|
+
const button = this.createButton(label, item === "image" ? "image" : "video");
|
|
1500
|
+
const input = document.createElement("input");
|
|
1501
|
+
input.type = "file";
|
|
1502
|
+
input.accept = `${item}/*`;
|
|
1503
|
+
input.hidden = true;
|
|
1504
|
+
input.tabIndex = -1;
|
|
1505
|
+
input.setAttribute("aria-hidden", "true");
|
|
1506
|
+
input.addEventListener("change", () => {
|
|
1507
|
+
const file = input.files?.[0];
|
|
1508
|
+
input.value = "";
|
|
1509
|
+
if (file)
|
|
1510
|
+
void this.insertFile(file);
|
|
1511
|
+
});
|
|
1512
|
+
button.addEventListener("click", () => input.click());
|
|
1513
|
+
wrap.append(button, input);
|
|
1514
|
+
this.syncers.push(() => {
|
|
1515
|
+
if (!this.instance)
|
|
1516
|
+
return;
|
|
1517
|
+
const busy = this.instance.uploading() > 0;
|
|
1518
|
+
button.disabled = !this.instance.editor.isEditable || busy;
|
|
1519
|
+
button.title = busy ? `${this.labels.uploading}…` : label;
|
|
1520
|
+
});
|
|
1521
|
+
return wrap;
|
|
1522
|
+
}
|
|
1523
|
+
// `popover="auto"` + popovertarget abrem e fecham sem JS (light dismiss nativo); só a posição é calculada aqui.
|
|
1524
|
+
createPopover() {
|
|
1525
|
+
const popover = document.createElement("div");
|
|
1526
|
+
popover.id = `ark-wysiwyg-popover-${++popoverSeq}`;
|
|
1527
|
+
popover.className = "ark-wysiwyg__popover";
|
|
1528
|
+
popover.setAttribute("popover", "auto");
|
|
1529
|
+
popover.addEventListener("toggle", (event) => {
|
|
1530
|
+
if (event.newState !== "open")
|
|
1531
|
+
return;
|
|
1532
|
+
const anchor = this.toolbarEl?.querySelector(`[popovertarget="${popover.id}"]`);
|
|
1533
|
+
if (!anchor)
|
|
1534
|
+
return;
|
|
1535
|
+
const rect = anchor.getBoundingClientRect();
|
|
1536
|
+
const width = popover.offsetWidth;
|
|
1537
|
+
const height = popover.offsetHeight;
|
|
1538
|
+
const maxLeft = Math.max(8, document.documentElement.clientWidth - width - 8);
|
|
1539
|
+
const below = rect.bottom + 4;
|
|
1540
|
+
const top = below + height > document.documentElement.clientHeight - 8 ? Math.max(8, rect.top - height - 4) : below;
|
|
1541
|
+
popover.style.left = `${Math.round(Math.min(Math.max(8, rect.left), maxLeft))}px`;
|
|
1542
|
+
popover.style.top = `${Math.round(top)}px`;
|
|
1543
|
+
});
|
|
1544
|
+
return popover;
|
|
1545
|
+
}
|
|
1546
|
+
setCurrent(control) {
|
|
1547
|
+
for (const candidate of this.controls)
|
|
1548
|
+
candidate.tabIndex = candidate === control ? 0 : -1;
|
|
1549
|
+
}
|
|
1550
|
+
teardown() {
|
|
1551
|
+
this.disposeTheme?.();
|
|
1552
|
+
this.disposeTheme = null;
|
|
1553
|
+
if (this.instance) {
|
|
1554
|
+
this.instance.editor.off("selectionUpdate", this.syncToolbar);
|
|
1555
|
+
this.instance.editor.off("transaction", this.syncToolbar);
|
|
1556
|
+
this.instance.destroy();
|
|
1557
|
+
this.instance = null;
|
|
1558
|
+
}
|
|
1559
|
+
this.controls = [];
|
|
1560
|
+
this.syncers = [];
|
|
1561
|
+
this.toolbarEl = null;
|
|
1562
|
+
if (this.root) {
|
|
1563
|
+
this.root.remove();
|
|
1564
|
+
this.root = null;
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
ArkWysiwygEditor.tagName = "ark-wysiwyg-editor";
|
|
1569
|
+
|
|
1570
|
+
/**
|
|
1571
|
+
* Viewer somente-leitura (Custom Element) que renderiza o mesmo JSON do editor.
|
|
1572
|
+
*
|
|
1573
|
+
* Usa Tiptap em modo `editable: false` com o mesmo schema do editor (links,
|
|
1574
|
+
* cores, alinhamento, imagens e vídeos externos): renderiza a partir do JSON,
|
|
1575
|
+
* sanitizado ao entrar (`sanitizeWysiwygContent`), sem injetar HTML
|
|
1576
|
+
* arbitrário; links abrem em nova aba com `rel="noopener noreferrer nofollow"`.
|
|
1577
|
+
*
|
|
1578
|
+
* Conteúdo via propriedade `content` (JSON do Tiptap). Atributo: `theme`.
|
|
1579
|
+
*/
|
|
1580
|
+
class ArkWysiwygViewer extends HTMLElement {
|
|
1581
|
+
constructor() {
|
|
1582
|
+
super(...arguments);
|
|
1583
|
+
this.instance = null;
|
|
1584
|
+
this.root = null;
|
|
1585
|
+
this.pendingContent = null;
|
|
1586
|
+
/** Para de observar o tema da página; só existe com `theme="auto"`. */
|
|
1587
|
+
this.disposeTheme = null;
|
|
1588
|
+
}
|
|
1589
|
+
static get observedAttributes() {
|
|
1590
|
+
return ["theme"];
|
|
1591
|
+
}
|
|
1592
|
+
/** Conteúdo a renderizar como JSON do Tiptap. */
|
|
1593
|
+
get content() {
|
|
1594
|
+
return this.instance ? this.instance.getJSON() : this.pendingContent;
|
|
1595
|
+
}
|
|
1596
|
+
set content(value) {
|
|
1597
|
+
this.pendingContent = value;
|
|
1598
|
+
this.instance?.setContent(value);
|
|
1599
|
+
}
|
|
1600
|
+
/** Tema efetivamente aplicado (depois de resolver `auto`); `null` antes de renderizar. */
|
|
1601
|
+
get resolvedTheme() {
|
|
1602
|
+
return this.instance?.resolvedTheme() ?? null;
|
|
1603
|
+
}
|
|
1604
|
+
connectedCallback() {
|
|
1605
|
+
ensureWysiwygStyles();
|
|
1606
|
+
this.build();
|
|
1607
|
+
}
|
|
1608
|
+
disconnectedCallback() {
|
|
1609
|
+
this.teardown();
|
|
1610
|
+
}
|
|
1611
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
1612
|
+
if (oldValue === newValue || !this.instance)
|
|
1613
|
+
return;
|
|
1614
|
+
if (name === "theme") {
|
|
1615
|
+
this.applyTheme(this.getTheme());
|
|
1616
|
+
this.syncThemeObserver();
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
getTheme() {
|
|
1620
|
+
const value = (this.getAttribute("theme") || "auto").toLowerCase();
|
|
1621
|
+
if (value === "light" || value === "dark")
|
|
1622
|
+
return value;
|
|
1623
|
+
return "auto";
|
|
1624
|
+
}
|
|
1625
|
+
build() {
|
|
1626
|
+
this.teardown();
|
|
1627
|
+
if (this.pendingContent === null) {
|
|
1628
|
+
this.pendingContent = this.content;
|
|
1629
|
+
}
|
|
1630
|
+
const root = document.createElement("div");
|
|
1631
|
+
root.className = "ark-wysiwyg ark-wysiwyg--viewer";
|
|
1632
|
+
const mount = document.createElement("div");
|
|
1633
|
+
mount.className = "ark-wysiwyg__content";
|
|
1634
|
+
root.appendChild(mount);
|
|
1635
|
+
this.appendChild(root);
|
|
1636
|
+
this.root = root;
|
|
1637
|
+
this.instance = createWysiwygViewer(mount, {
|
|
1638
|
+
content: this.pendingContent,
|
|
1639
|
+
theme: this.getTheme()
|
|
1640
|
+
});
|
|
1641
|
+
this.root.setAttribute("data-ark-theme", this.instance.resolvedTheme());
|
|
1642
|
+
this.syncThemeObserver();
|
|
1643
|
+
}
|
|
1644
|
+
// Em `auto`, a troca de tema da página re-resolve o tema do viewer; com tema fixo não há o que observar.
|
|
1645
|
+
syncThemeObserver() {
|
|
1646
|
+
this.disposeTheme?.();
|
|
1647
|
+
this.disposeTheme = null;
|
|
1648
|
+
if (this.getTheme() !== "auto")
|
|
1649
|
+
return;
|
|
1650
|
+
this.disposeTheme = tokens.observeColorScheme(this, () => this.applyTheme("auto"));
|
|
1651
|
+
}
|
|
1652
|
+
// O engine marca o mount com data-ark-theme; o CSS do tema lê a raiz .ark-wysiwyg, então ela recebe o mesmo.
|
|
1653
|
+
applyTheme(theme) {
|
|
1654
|
+
if (!this.instance)
|
|
1655
|
+
return;
|
|
1656
|
+
this.instance.setTheme(theme);
|
|
1657
|
+
this.root?.setAttribute("data-ark-theme", this.instance.resolvedTheme());
|
|
1658
|
+
}
|
|
1659
|
+
teardown() {
|
|
1660
|
+
this.disposeTheme?.();
|
|
1661
|
+
this.disposeTheme = null;
|
|
1662
|
+
this.instance?.destroy();
|
|
1663
|
+
this.instance = null;
|
|
1664
|
+
if (this.root) {
|
|
1665
|
+
this.root.remove();
|
|
1666
|
+
this.root = null;
|
|
1667
|
+
}
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
ArkWysiwygViewer.tagName = "ark-wysiwyg-viewer";
|
|
1671
|
+
|
|
1672
|
+
/**
|
|
1673
|
+
* Registra os Custom Elements `<ark-wysiwyg-editor>` e `<ark-wysiwyg-viewer>`.
|
|
1674
|
+
* Chame antes de usar em qualquer ambiente (puro, React, Angular, Vue).
|
|
1675
|
+
*/
|
|
1676
|
+
function registerTooarkWysiwyg() {
|
|
1677
|
+
if (!customElements.get(ArkWysiwygEditor.tagName)) {
|
|
1678
|
+
customElements.define(ArkWysiwygEditor.tagName, ArkWysiwygEditor);
|
|
1679
|
+
}
|
|
1680
|
+
if (!customElements.get(ArkWysiwygViewer.tagName)) {
|
|
1681
|
+
customElements.define(ArkWysiwygViewer.tagName, ArkWysiwygViewer);
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
exports.ARK_WYSIWYG_DEFAULT_TOOLBAR = ARK_WYSIWYG_DEFAULT_TOOLBAR;
|
|
1686
|
+
exports.ARK_WYSIWYG_TOOLBAR_GROUPS = ARK_WYSIWYG_TOOLBAR_GROUPS;
|
|
1687
|
+
exports.ArkWysiwygEditor = ArkWysiwygEditor;
|
|
1688
|
+
exports.ArkWysiwygViewer = ArkWysiwygViewer;
|
|
1689
|
+
exports.DEFAULT_MAX_FILE_SIZE = DEFAULT_MAX_FILE_SIZE;
|
|
1690
|
+
exports.EMPTY_DOC = EMPTY_DOC;
|
|
1691
|
+
exports.HEADING_LEVELS = HEADING_LEVELS;
|
|
1692
|
+
exports.Video = Video;
|
|
1693
|
+
exports.WYSIWYG_STYLE_ID = WYSIWYG_STYLE_ID;
|
|
1694
|
+
exports.createWysiwygEditor = createWysiwygEditor;
|
|
1695
|
+
exports.createWysiwygExtensions = createWysiwygExtensions;
|
|
1696
|
+
exports.createWysiwygViewer = createWysiwygViewer;
|
|
1697
|
+
exports.ensureWysiwygStyles = ensureWysiwygStyles;
|
|
1698
|
+
exports.isSafeColor = isSafeColor;
|
|
1699
|
+
exports.isSafeUrl = isSafeUrl;
|
|
1700
|
+
exports.registerTooarkWysiwyg = registerTooarkWysiwyg;
|
|
1701
|
+
exports.resolveWysiwygLabels = resolveWysiwygLabels;
|
|
1702
|
+
exports.resolveWysiwygTheme = resolveWysiwygTheme;
|
|
1703
|
+
exports.sanitizeWysiwygContent = sanitizeWysiwygContent;
|
|
1704
|
+
exports.wysiwygCss = wysiwygCss;
|
|
1705
|
+
//# sourceMappingURL=index.cjs.map
|