/* Nachrichten-Editor mit Discord-Vorschau (Text oder Einbettung). * * const editor = SielEditor.mount(container, message, ctx); * editor.get() -> {mode, content, embed: {...}} fuer das Speichern * * ctx: {botName, botAvatar, userName, userAvatar, guildName, memberCount, * attachment: () => url|null (Bild, das mitgeschickt wird, z. B. die Willkommenskarte)} */ (function () { 'use strict'; const COLORS = ['#c9a227', '#2fbf5a', '#ffffff', '#607d8b', '#e74c3c', '#e67e22', '#f1c40f', '#1abc9c', '#3498db', '#5865f2', '#9b59b6']; function el(tag, attrs, ...children) { const node = document.createElement(tag); for (const [key, value] of Object.entries(attrs || {})) { if (key === 'class') node.className = value; else if (key === 'text') node.textContent = value; else if (key === 'style') node.style.cssText = value; // CSSOM - erlaubt trotz CSP ohne 'unsafe-inline' else if (key.startsWith('on')) node.addEventListener(key.slice(2), value); else if (value === true) node.setAttribute(key, ''); else if (value !== false && value != null) node.setAttribute(key, value); } for (const child of children) if (child != null) node.append(child); return node; } function escapeHtml(text) { return text.replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); } // Platzhalter fuer die Vorschau einsetzen und Discord-Markdown grob nachbilden function renderText(text, ctx, inline) { let html = escapeHtml(text || ''); const pill = (label) => '' + escapeHtml(label) + ''; html = html .replace(/\{user\}/g, pill('@' + ctx.userName)) .replace(/\{user\.name\}/g, escapeHtml(ctx.userName)) .replace(/\{user\.idname\}/g, escapeHtml(ctx.userIdName || ctx.userName)) .replace(/\{user\.id\}/g, escapeHtml(ctx.userId || '0')) .replace(/\{user\.avatar\}/g, escapeHtml(ctx.userAvatar || '')) .replace(/\{server\}/g, escapeHtml(ctx.guildName)) .replace(/\{server\.member_count\}/g, String(ctx.memberCount)) .replace(/\{#([^{}\s]{1,100})\}/g, (m, name) => pill('#' + name)) .replace(/<#(\d+)>/g, (m, id) => pill('#' + (ctx.channelName(id) || 'kanal'))) .replace(/<@&(\d+)>/g, (m, id) => pill('@' + (ctx.roleName(id) || 'rolle'))) .replace(/<@!?(\d+)>/g, () => pill('@nutzer')) .replace(/<a?:(\w+):(\d+)>/g, (m, name, id) => ':' + name + ':'); html = html .replace(/```([\s\S]+?)```/g, '
$1
') .replace(/`([^`\n]+)`/g, '$1') .replace(/\*\*\*(.+?)\*\*\*/g, '$1') .replace(/\*\*(.+?)\*\*/g, '$1') .replace(/__(.+?)__/g, '$1') .replace(/\*(.+?)\*/g, '$1') .replace(/(^|\s)_(.+?)_(?=\s|$)/g, '$1$2') .replace(/~~(.+?)~~/g, '$1') .replace(/\|\|(.+?)\|\|/g, '$1'); if (!inline) { html = html .replace(/^### (.*)$/gm, '$1') .replace(/^## (.*)$/gm, '$1') .replace(/^# (.*)$/gm, '$1') .replace(/^> (.*)$/gm, '$1') .replace(/^[-*] (.*)$/gm, '$1'); } return html.replace(/\n/g, '
'); } function fillUrl(url, ctx) { return (url || '').replace(/\{user\.avatar\}/g, ctx.userAvatar || ''); } function mount(container, message, ctx) { const state = JSON.parse(JSON.stringify(message || {})); state.mode = state.mode === 'embed' ? 'embed' : 'text'; state.content = state.content || ''; state.embed = Object.assign({ color: '#c9a227', author: '', title: '', description: '', fields: [], image: '', thumbnail: '', footer: '' }, state.embed || {}); state.embed.fields = (state.embed.fields || []).map((f) => ({ name: f.name || '', value: f.value || '', inline: !!f.inline })); ctx = Object.assign({ botName: 'Siel', botAvatar: '', userName: 'Neues Mitglied', guildName: 'Server', memberCount: 1, channelName: () => null, roleName: () => null, attachment: () => null, extra: null, }, ctx || {}); container.classList.add('msg-editor'); container.textContent = ''; const form = el('div', { class: 'msg-form' }); const preview = el('div', { class: 'dc-preview' }); container.append(el('div', { class: 'msg-grid' }, form, el('div', { class: 'msg-preview-col' }, el('div', { class: 'msg-preview-label', text: 'Vorschau' }), preview))); // --- Umschalter Text / Einbettung const tabs = el('div', { class: 'seg' }); const tabText = el('button', { type: 'button', text: 'Textnachricht', onclick: () => setMode('text') }); const tabEmbed = el('button', { type: 'button', text: 'Einbettung', onclick: () => setMode('embed') }); tabs.append(tabText, tabEmbed); form.append(tabs); function counter(input, max) { const c = el('small', { class: 'counter' }); const upd = () => { c.textContent = input.value.length + ' / ' + max; c.classList.toggle('over', input.value.length > max); }; input.addEventListener('input', upd); upd(); return c; } function field(label, input, extra) { return el('label', { class: 'field' }, el('span', { text: label }), input, extra || null); } function textInput(value, max, onInput, placeholder) { const i = el('input', { type: 'text', maxlength: String(max), placeholder: placeholder || '' }); i.value = value || ''; i.addEventListener('input', () => { onInput(i.value); draw(); }); return i; } function textArea(value, max, onInput, rows, placeholder) { const t = el('textarea', { rows: String(rows || 4), maxlength: String(max), placeholder: placeholder || '' }); t.value = value || ''; t.addEventListener('input', () => { onInput(t.value); draw(); }); return t; } const content = textArea(state.content, 2000, (v) => { state.content = v; }, 4, 'Nachricht …'); const contentLabel = el('span', { text: 'Text' }); const contentField = el('label', { class: 'field' }, contentLabel, content, counter(content, 2000)); // --- Einbettung const embedBox = el('div', { class: 'embed-form' }); const swatches = el('div', { class: 'swatches' }); const custom = el('input', { type: 'color', value: state.embed.color, title: 'Eigene Farbe' }); function paintSwatches() { swatches.textContent = ''; for (const c of COLORS) { swatches.append(el('button', { type: 'button', class: 'swatch' + (state.embed.color === c ? ' active' : ''), style: 'background:' + c, title: c, onclick: () => { state.embed.color = c; custom.value = c; paintSwatches(); draw(); }, })); } swatches.append(custom); } custom.addEventListener('input', () => { state.embed.color = custom.value; paintSwatches(); draw(); }); paintSwatches(); const fieldsBox = el('div', { class: 'embed-fields' }); function paintFields() { fieldsBox.textContent = ''; state.embed.fields.forEach((f, idx) => { const inline = el('input', { type: 'checkbox' }); inline.checked = f.inline; inline.addEventListener('change', () => { f.inline = inline.checked; draw(); }); fieldsBox.append(el('div', { class: 'embed-field' }, textInput(f.name, 256, (v) => { f.name = v; }, 'Name des Feldes'), textArea(f.value, 1024, (v) => { f.value = v; }, 2, 'Feldwert'), el('div', { class: 'embed-field-actions' }, el('label', { class: 'check' }, inline, ' nebeneinander'), el('button', { type: 'button', class: 'btn-link danger', text: 'Feld entfernen', onclick: () => { state.embed.fields.splice(idx, 1); paintFields(); draw(); } })))); }); if (state.embed.fields.length < 10) { fieldsBox.append(el('button', { type: 'button', class: 'btn btn-sm btn-ghost', text: '+ Feld hinzufügen', onclick: () => { state.embed.fields.push({ name: '', value: '', inline: false }); paintFields(); draw(); } })); } } paintFields(); const desc = textArea(state.embed.description, 4096, (v) => { state.embed.description = v; }, 6); embedBox.append( el('div', { class: 'field' }, el('span', { text: 'Farbe des Streifens' }), swatches), field('Autor', textInput(state.embed.author, 256, (v) => { state.embed.author = v; }, 'Name des Verfassers')), field('Titel', textInput(state.embed.title, 256, (v) => { state.embed.title = v; })), field('Beschreibung', desc, counter(desc, 4096)), el('div', { class: 'field' }, el('span', { text: 'Zusätzliche Felder' }), fieldsBox), field('Bild (Adresse)', textInput(state.embed.image, 500, (v) => { state.embed.image = v; }, 'https://…'), ctx.attachmentHint ? el('small', { class: 'muted', text: ctx.attachmentHint }) : null), field('Vorschaubild oben rechts (Adresse)', textInput(state.embed.thumbnail, 500, (v) => { state.embed.thumbnail = v; }, 'https://… oder {user.avatar}')), field('Fußzeile', textInput(state.embed.footer, 2048, (v) => { state.embed.footer = v; })), ); form.append(contentField, embedBox); function setMode(mode) { state.mode = mode; tabText.classList.toggle('active', mode === 'text'); tabEmbed.classList.toggle('active', mode === 'embed'); embedBox.hidden = mode !== 'embed'; contentLabel.textContent = mode === 'embed' ? 'Text über der Einbettung (optional)' : 'Text'; content.rows = mode === 'embed' ? 2 : 4; draw(); } // --- Vorschau function draw() { preview.textContent = ''; const avatar = ctx.botAvatar ? el('img', { class: 'dc-avatar', src: ctx.botAvatar, alt: '' }) : el('div', { class: 'dc-avatar' }); const body = el('div', { class: 'dc-body' }); body.append(el('div', { class: 'dc-head' }, el('span', { class: 'dc-name', text: ctx.botName }), el('span', { class: 'dc-bot', text: 'BOT' }), el('span', { class: 'dc-time', text: 'Heute' }))); const attachment = ctx.attachment(); if (state.content.trim()) { const t = el('div', { class: 'dc-text' }); t.innerHTML = renderText(state.content, ctx); body.append(t); } if (state.mode === 'embed') { const e = state.embed; const box = el('div', { class: 'dc-embed' }); box.style.borderLeftColor = e.color; const main = el('div', { class: 'dc-embed-main' }); if (e.author) main.append(el('div', { class: 'dc-embed-author', text: e.author.replace(/\{user\.name\}/g, ctx.userName).replace(/\{server\}/g, ctx.guildName) })); if (e.title) { const t = el('div', { class: 'dc-embed-title' }); t.innerHTML = renderText(e.title, ctx, true); main.append(t); } if (e.description) { const d = el('div', { class: 'dc-embed-desc' }); d.innerHTML = renderText(e.description, ctx); main.append(d); } const visible = e.fields.filter((f) => f.name || f.value); if (visible.length) { const grid = el('div', { class: 'dc-embed-fields' }); for (const f of visible) { const cell = el('div', { class: 'dc-embed-field' + (f.inline ? ' inline' : '') }); const n = el('div', { class: 'dc-embed-fname' }); n.innerHTML = renderText(f.name, ctx, true); const v = el('div', { class: 'dc-embed-fvalue' }); v.innerHTML = renderText(f.value, ctx); cell.append(n, v); grid.append(cell); } main.append(grid); } const img = attachment || fillUrl(e.image, ctx); if (img) main.append(el('img', { class: 'dc-embed-image', src: img, alt: '' })); if (e.footer) main.append(el('div', { class: 'dc-embed-footer', text: e.footer.replace(/\{server\}/g, ctx.guildName) })); box.append(main); const thumb = fillUrl(e.thumbnail, ctx); if (thumb) box.append(el('img', { class: 'dc-embed-thumb', src: thumb, alt: '' })); body.append(box); } else if (attachment) { body.append(el('img', { class: 'dc-attachment', src: attachment, alt: '' })); } if (body.children.length === 1) body.append(el('div', { class: 'dc-text muted', text: '(leer)' })); if (ctx.extra) { const extra = ctx.extra(); if (extra) body.append(extra); } preview.append(el('div', { class: 'dc-msg' }, avatar, body)); } setMode(state.mode); return { get() { return JSON.parse(JSON.stringify(state)); }, redraw: draw, }; } window.SielEditor = { mount, renderText, el }; })();