591 lines
26 KiB
JavaScript
591 lines
26 KiB
JavaScript
// Общий рендерер слайдов: используется в редакторе, демонстрации и превью на главной.
|
||
// Элементы позиционируются в процентах, размеры шрифтов — в cqw (1% ширины слайда).
|
||
(function () {
|
||
const DEFAULT_GRADIENT = 'linear-gradient(135deg, #0f172a 0%, #1e1b4b 55%, #312e81 100%)';
|
||
|
||
const FONT_STACKS = {
|
||
sans: "system-ui, -apple-system, 'Segoe UI', sans-serif",
|
||
serif: "Georgia, 'Times New Roman', serif",
|
||
mono: "ui-monospace, 'Cascadia Code', 'JetBrains Mono', Consolas, monospace",
|
||
round: "'Segoe UI Rounded', ui-rounded, 'Nunito', system-ui, sans-serif",
|
||
};
|
||
|
||
if (!document.getElementById('slide-view-styles')) {
|
||
const style = document.createElement('style');
|
||
style.id = 'slide-view-styles';
|
||
style.textContent = `
|
||
.sv-root {
|
||
position: absolute; inset: 0; overflow: hidden;
|
||
container-type: inline-size;
|
||
font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
|
||
color: #f8fafc; user-select: none;
|
||
}
|
||
.sv-el { position: absolute; box-sizing: border-box; }
|
||
.sv-root.editable .sv-el { cursor: move; }
|
||
.sv-root.editable .sv-el:hover { outline: 1px dashed rgba(129, 140, 248, .7); outline-offset: 1px; }
|
||
.sv-root.editable .sv-el.sv-selected { outline: 2px solid #818cf8; outline-offset: 1px; }
|
||
.sv-root.editable .sv-el.sv-ghost { opacity: .38 !important; outline: 1px dashed rgba(251, 191, 36, .8); }
|
||
.sv-text {
|
||
display: flex; white-space: pre-wrap; word-break: break-word;
|
||
overflow: hidden; box-sizing: border-box; font-weight: 700;
|
||
}
|
||
.sv-image { width: 100%; height: 100%; display: block; object-fit: cover; }
|
||
.sv-image-missing {
|
||
width: 100%; height: 100%; display: flex; flex-direction: column; gap: 1cqw;
|
||
align-items: center; justify-content: center; border: 1px dashed rgba(148, 163, 184, .5);
|
||
border-radius: 10px; color: #94a3b8; font-size: 1.6cqw; background: rgba(15, 23, 42, .35);
|
||
}
|
||
.sv-image-missing b { font-size: 3cqw; line-height: 1; }
|
||
.sv-shape { width: 100%; height: 100%; display: block; overflow: visible; }
|
||
.sv-video {
|
||
position: relative; width: 100%; height: 100%; overflow: hidden; background: #02040a;
|
||
}
|
||
.sv-video video {
|
||
width: 100%; height: 100%; display: block; background: #02040a;
|
||
}
|
||
.sv-video .sv-media-missing {
|
||
position: absolute; inset: 0; display: flex; flex-direction: column; gap: 1cqw;
|
||
align-items: center; justify-content: center; border: 1px dashed rgba(148, 163, 184, .5);
|
||
color: #94a3b8; font-size: 1.6cqw; background: rgba(15, 23, 42, .35); text-align: center;
|
||
box-sizing: border-box; padding: 1cqw;
|
||
}
|
||
.sv-video .sv-media-missing b { font-size: 3cqw; line-height: 1; }
|
||
.sv-root.editable .sv-video { pointer-events: none; }
|
||
.sv-window {
|
||
position: relative; width: 100%; height: 100%; overflow: hidden; background: #02040a;
|
||
display: flex; align-items: center; justify-content: center;
|
||
}
|
||
.sv-window img {
|
||
width: 100%; height: 100%; display: block; object-fit: fill;
|
||
}
|
||
.sv-window .sv-window-placeholder {
|
||
position: absolute; inset: 0; display: flex; flex-direction: column; gap: .6cqw;
|
||
align-items: center; justify-content: center; text-align: center; padding: 2cqw;
|
||
color: #94a3b8; font-size: 1.5cqw; border: 1px dashed rgba(148, 163, 184, .45);
|
||
background: radial-gradient(circle at 50% 40%, rgba(99, 102, 241, .16), rgba(2, 4, 10, .9));
|
||
}
|
||
.sv-window .sv-window-placeholder b { font-size: 2.6cqw; }
|
||
.sv-window .sv-window-live {
|
||
position: absolute; top: .7cqw; right: .7cqw; display: flex; align-items: center; gap: .5cqw;
|
||
padding: .35cqw .8cqw; border-radius: 999px; background: rgba(5, 8, 18, .7);
|
||
color: #f0abfc; font-size: 1.1cqw; font-weight: 700; letter-spacing: .5px;
|
||
border: 1px solid rgba(232, 121, 249, .35); pointer-events: none;
|
||
}
|
||
.sv-window .sv-window-live i { width: .55cqw; height: .55cqw; border-radius: 50%; background: #e879f9; }
|
||
.sv-root:not(.editable) .sv-window[data-live="1"] { cursor: crosshair; }
|
||
.sv-root:not(.editable) .sv-window[data-live="1"]:hover { box-shadow: inset 0 0 0 2px rgba(129, 140, 248, .55); }
|
||
.sv-button {
|
||
width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;
|
||
border: none; font-family: inherit; font-weight: 700; line-height: 1.15; padding: 0 4%;
|
||
box-sizing: border-box; cursor: pointer; text-align: center;
|
||
transition: transform .12s ease, filter .12s ease, box-shadow .12s ease;
|
||
}
|
||
.sv-lua {
|
||
width: 100%; height: 100%; box-sizing: border-box; overflow: hidden;
|
||
font-size: 1.7cqw; line-height: 1.35;
|
||
}
|
||
.sv-root.editable .sv-lua { pointer-events: none; }
|
||
.sv-button:hover { filter: brightness(1.1); }
|
||
.sv-button:active { transform: scale(.98); }
|
||
.sv-root.editable .sv-button { pointer-events: none; }
|
||
.sv-audio {
|
||
width: 100%; height: 100%; display: flex; align-items: center; gap: .8em;
|
||
padding: 0 1.2em; box-sizing: border-box; border-radius: 999px;
|
||
background: rgba(15, 23, 42, .62); border: 1px solid rgba(255, 255, 255, .18);
|
||
color: #e2e8f0; font-size: clamp(8px, 1.7cqw, 30px); font-weight: 600;
|
||
backdrop-filter: blur(10px); overflow: hidden; white-space: nowrap;
|
||
box-shadow: 0 10px 30px rgba(0, 0, 0, .3);
|
||
}
|
||
.sv-audio.playing { border-color: rgba(129, 140, 248, .75); box-shadow: 0 0 0 1px rgba(129, 140, 248, .25), 0 10px 30px rgba(0, 0, 0, .3); }
|
||
.sv-root:not(.editable) .sv-audio[data-clickable="1"] { cursor: pointer; }
|
||
.sv-root:not(.editable) .sv-audio[data-clickable="1"]:hover { background: rgba(30, 41, 59, .8); }
|
||
.sv-audio-icon {
|
||
width: 2.1em; height: 2.1em; flex-shrink: 0; border-radius: 50%;
|
||
display: flex; align-items: center; justify-content: center;
|
||
background: linear-gradient(135deg, #6366f1, #a855f7); color: #fff; font-size: .95em;
|
||
}
|
||
.sv-audio-label { overflow: hidden; text-overflow: ellipsis; }
|
||
.sv-bars { display: flex; align-items: flex-end; gap: .16em; height: 1.1em; margin-left: auto; }
|
||
.sv-bars i { width: .2em; background: #a5b4fc; border-radius: 999px; animation: svBar .9s ease-in-out infinite; }
|
||
.sv-bars i:nth-child(2) { animation-delay: .15s; }
|
||
.sv-bars i:nth-child(3) { animation-delay: .3s; }
|
||
.sv-bars i:nth-child(4) { animation-delay: .45s; }
|
||
@keyframes svBar { 0%, 100% { height: 25%; } 50% { height: 100%; } }
|
||
.sv-empty {
|
||
position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
|
||
color: rgba(148, 163, 184, .6); font-size: clamp(11px, 2.4cqw, 30px); font-weight: 600;
|
||
text-align: center; padding: 8%;
|
||
}
|
||
/* Переходы между слайдами */
|
||
.sv-anim-fade { animation: svFade .45s cubic-bezier(.2, .7, .3, 1) both; }
|
||
.sv-anim-slide { animation: svSlide .5s cubic-bezier(.2, .7, .3, 1) both; }
|
||
.sv-anim-zoom { animation: svZoom .45s cubic-bezier(.2, .7, .3, 1) both; }
|
||
.sv-anim-flip { animation: svFlip .55s cubic-bezier(.2, .7, .3, 1) both; }
|
||
.sv-anim-rise { animation: svRise .5s cubic-bezier(.2, .7, .3, 1) both; }
|
||
@keyframes svFade { from { opacity: 0; } to { opacity: 1; } }
|
||
@keyframes svSlide { from { opacity: 0; transform: translateX(5%); } to { opacity: 1; transform: none; } }
|
||
@keyframes svZoom { from { opacity: 0; transform: scale(.94); } to { opacity: 1; transform: none; } }
|
||
@keyframes svFlip { from { opacity: 0; transform: perspective(1400px) rotateY(32deg); } to { opacity: 1; transform: none; } }
|
||
@keyframes svRise { from { opacity: 0; transform: translateY(7%); } to { opacity: 1; transform: none; } }
|
||
`;
|
||
document.head.appendChild(style);
|
||
}
|
||
|
||
function esc(value) {
|
||
return String(value ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
||
}
|
||
|
||
function interpolate(text, vars) {
|
||
if (!text) return '';
|
||
if (!vars) return String(text);
|
||
return String(text).replace(/\{\{\s*([\w.-]+)\s*\}\}/g, (match, key) => {
|
||
if (Object.prototype.hasOwnProperty.call(vars, key)) {
|
||
const value = vars[key];
|
||
if (value === null || value === undefined) return '';
|
||
return String(value);
|
||
}
|
||
return match;
|
||
});
|
||
}
|
||
|
||
function backgroundStyle(background) {
|
||
const bg = background || {};
|
||
const type = bg.type || (bg.image ? 'image' : bg.gradient ? 'gradient' : 'color');
|
||
if (type === 'image' && bg.image) {
|
||
return {
|
||
backgroundImage: `url("${String(bg.image).replace(/"/g, '%22')}")`,
|
||
backgroundSize: 'cover',
|
||
backgroundPosition: 'center',
|
||
backgroundColor: '#0f172a',
|
||
};
|
||
}
|
||
if (type === 'gradient') {
|
||
return { background: bg.gradient || DEFAULT_GRADIENT };
|
||
}
|
||
return { background: bg.color || '#0f172a' };
|
||
}
|
||
|
||
function styleObjectToString(styles) {
|
||
return Object.keys(styles)
|
||
.map((key) => `${key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase())}:${styles[key]}`)
|
||
.join(';');
|
||
}
|
||
|
||
function radius(px) {
|
||
const value = Number(px);
|
||
if (!Number.isFinite(value) || value <= 0) return '';
|
||
return `min(${value}px, 2cqw)`;
|
||
}
|
||
|
||
function shadowStyle(on) {
|
||
return on ? '0 1.2cqw 2.6cqw rgba(0, 0, 0, .38)' : '';
|
||
}
|
||
|
||
function createTextElement(element, vars) {
|
||
const node = document.createElement('div');
|
||
node.className = 'sv-text';
|
||
const justify = element.align === 'left' ? 'flex-start' : element.align === 'right' ? 'flex-end' : 'center';
|
||
node.style.justifyContent = justify;
|
||
node.style.textAlign = element.align || 'center';
|
||
node.style.fontSize = `${Number(element.fontSize) || 5}cqw`;
|
||
node.style.fontWeight = String(element.weight || 700);
|
||
node.style.color = element.color || '#f8fafc';
|
||
node.style.lineHeight = String(Number(element.lineHeight) || 1.16);
|
||
if (element.font && FONT_STACKS[element.font]) node.style.fontFamily = FONT_STACKS[element.font];
|
||
if (element.italic) node.style.fontStyle = 'italic';
|
||
if (element.shadow) node.style.textShadow = '0 .35cqw .9cqw rgba(0, 0, 0, .5)';
|
||
if (element.bg) {
|
||
node.style.background = element.bg;
|
||
node.style.borderRadius = 'min(18px, 2cqw)';
|
||
node.style.padding = '1.4cqw 2cqw';
|
||
}
|
||
node.textContent = interpolate(element.text || '', vars);
|
||
return node;
|
||
}
|
||
|
||
function createImageElement(element, editable) {
|
||
if (!element.src) {
|
||
const placeholder = document.createElement('div');
|
||
placeholder.className = 'sv-image-missing';
|
||
placeholder.innerHTML = '<b>фото</b><span>Укажите URL или загрузите файл</span>';
|
||
return placeholder;
|
||
}
|
||
const img = document.createElement('img');
|
||
img.className = 'sv-image';
|
||
img.src = element.src;
|
||
img.alt = '';
|
||
img.draggable = false;
|
||
img.loading = 'lazy';
|
||
img.style.objectFit = element.fit === 'contain' ? 'contain' : 'cover';
|
||
img.style.borderRadius = radius(element.radius);
|
||
if (element.shadow) img.style.boxShadow = shadowStyle(true);
|
||
if (editable) img.style.pointerEvents = 'none';
|
||
return img;
|
||
}
|
||
|
||
// Фигуры: SVG с viewBox 100×100, preserveAspectRatio none — тянутся под размер элемента.
|
||
function shapeBody(shape, element) {
|
||
const fill = element.fill || '#6366f1';
|
||
const stroke = element.borderColor ? ` stroke="${esc(element.borderColor)}" stroke-width="${Math.max(0.5, Number(element.borderWidth) || 1)}"` : '';
|
||
const r = Math.max(0, Math.min(45, Number(element.radius) || 0));
|
||
switch (shape) {
|
||
case 'ellipse':
|
||
return `<ellipse cx="50" cy="50" rx="${50 - (stroke ? 1 : 0)}" ry="${50 - (stroke ? 1 : 0)}" fill="${esc(fill)}"${stroke}/>`;
|
||
case 'triangle':
|
||
return `<path d="M50 2 L98 98 L2 98 Z" fill="${esc(fill)}"${stroke}/>`;
|
||
case 'arrow':
|
||
return `<path d="M2 36 H56 V12 L98 50 L56 88 V64 H2 Z" fill="${esc(fill)}"${stroke}/>`;
|
||
case 'line':
|
||
return `<rect x="0" y="${50 - Math.max(1.5, r / 4)}" width="100" height="${Math.max(3, r / 2)}" rx="2" fill="${esc(fill)}"${stroke}/>`;
|
||
case 'star':
|
||
return `<path d="M50 2 L61 38 L98 38 L68 60 L79 96 L50 74 L21 96 L32 60 L2 38 L39 38 Z" fill="${esc(fill)}"${stroke}/>`;
|
||
case 'hexagon':
|
||
return `<path d="M25 4 H75 L99 50 L75 96 H25 L1 50 Z" fill="${esc(fill)}"${stroke}/>`;
|
||
case 'diamond':
|
||
return `<path d="M50 2 L98 50 L50 98 L2 50 Z" fill="${esc(fill)}"${stroke}/>`;
|
||
case 'round':
|
||
return `<rect x="1" y="1" width="98" height="98" rx="${Math.max(8, r)}" fill="${esc(fill)}"${stroke}/>`;
|
||
case 'rect':
|
||
default:
|
||
return `<rect x="1" y="1" width="98" height="98" rx="${r}" fill="${esc(fill)}"${stroke}/>`;
|
||
}
|
||
}
|
||
|
||
function createShapeElement(element) {
|
||
const wrap = document.createElement('div');
|
||
wrap.className = 'sv-shape-wrap';
|
||
wrap.style.width = '100%';
|
||
wrap.style.height = '100%';
|
||
if (element.shadow) wrap.style.filter = 'drop-shadow(0 1cqw 1.8cqw rgba(0,0,0,.4))';
|
||
wrap.innerHTML = `<svg class="sv-shape" viewBox="0 0 100 100" preserveAspectRatio="none">${shapeBody(element.shape || 'rect', element)}</svg>`;
|
||
return wrap;
|
||
}
|
||
|
||
function createVideoElement(element, opts) {
|
||
const wrap = document.createElement('div');
|
||
wrap.className = 'sv-video';
|
||
wrap.dataset.mediaId = element.id || '';
|
||
wrap.style.borderRadius = radius(element.radius);
|
||
|
||
if (!element.src) {
|
||
wrap.innerHTML = '<div class="sv-media-missing"><b>видео</b><span>Добавьте видеофайл или ссылку</span></div>';
|
||
return wrap;
|
||
}
|
||
|
||
const video = document.createElement('video');
|
||
video.src = element.src;
|
||
video.poster = element.poster || '';
|
||
video.muted = element.muted !== false;
|
||
video.loop = !!element.loop;
|
||
const frozen = opts.editable || opts.staticPreview;
|
||
video.controls = !frozen && element.controls !== false;
|
||
video.playsInline = true;
|
||
video.preload = 'auto';
|
||
video.dataset.mediaId = element.id || '';
|
||
video.style.objectFit = element.fit === 'contain' ? 'contain' : 'cover';
|
||
video.style.borderRadius = radius(element.radius);
|
||
if (element.shadow) video.style.boxShadow = shadowStyle(true);
|
||
if (frozen) {
|
||
video.autoplay = false;
|
||
video.controls = false;
|
||
video.style.pointerEvents = 'none';
|
||
} else {
|
||
video.autoplay = !!element.autoplay;
|
||
if (typeof opts.onVideoEvent === 'function') {
|
||
const fire = (type) => {
|
||
if (video._applyingRemote || video._restoring) return;
|
||
opts.onVideoEvent(element, type, video);
|
||
};
|
||
video.addEventListener('play', () => fire('play'));
|
||
video.addEventListener('pause', () => fire('pause'));
|
||
video.addEventListener('seeked', () => fire('seek'));
|
||
video.addEventListener('ended', () => fire('ended'));
|
||
}
|
||
}
|
||
wrap.appendChild(video);
|
||
return wrap;
|
||
}
|
||
|
||
function windowDataUrl(data) {
|
||
if (!data) return '';
|
||
if (/^data:/.test(data)) return data;
|
||
return `data:image/jpeg;base64,${data}`;
|
||
}
|
||
|
||
function createWindowElement(element, opts) {
|
||
const wrap = document.createElement('div');
|
||
wrap.className = 'sv-window';
|
||
wrap.dataset.windowId = element.id || '';
|
||
wrap.style.borderRadius = radius(element.radius);
|
||
|
||
const live = !opts.editable && typeof opts.onWindowInput === 'function';
|
||
const frameData = opts.windowFrames && opts.windowFrames[element.id];
|
||
const img = document.createElement('img');
|
||
img.className = 'sv-window-frame';
|
||
img.alt = '';
|
||
img.draggable = false;
|
||
if (frameData) {
|
||
img.src = windowDataUrl(frameData);
|
||
if (live) {
|
||
wrap.dataset.live = '1';
|
||
const badge = document.createElement('span');
|
||
badge.className = 'sv-window-live';
|
||
badge.innerHTML = '<i></i>LIVE';
|
||
wrap.appendChild(badge);
|
||
}
|
||
} else {
|
||
const placeholder = document.createElement('div');
|
||
placeholder.className = 'sv-window-placeholder';
|
||
placeholder.innerHTML = `<b>окно</b><span></span>`;
|
||
placeholder.querySelector('span').textContent = opts.editable
|
||
? (element.targetTitle || 'Окно Chrome — выберите цель в инспекторе')
|
||
: 'Трансляция не запущена';
|
||
wrap.appendChild(placeholder);
|
||
}
|
||
wrap.insertBefore(img, wrap.firstChild);
|
||
|
||
if (live) {
|
||
const normalize = (event) => {
|
||
const rect = wrap.getBoundingClientRect();
|
||
if (!rect.width || !rect.height) return null;
|
||
return {
|
||
x: Math.max(0, Math.min(1, (event.clientX - rect.left) / rect.width)),
|
||
y: Math.max(0, Math.min(1, (event.clientY - rect.top) / rect.height)),
|
||
};
|
||
};
|
||
let lastMove = 0;
|
||
wrap.addEventListener('click', (event) => {
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
const point = normalize(event);
|
||
if (point) opts.onWindowInput(element, { kind: 'click', ...point });
|
||
});
|
||
wrap.addEventListener('pointermove', (event) => {
|
||
const now = Date.now();
|
||
if (now - lastMove < 120) return;
|
||
lastMove = now;
|
||
const point = normalize(event);
|
||
if (point) opts.onWindowInput(element, { kind: 'move', ...point });
|
||
});
|
||
wrap.addEventListener(
|
||
'wheel',
|
||
(event) => {
|
||
event.preventDefault();
|
||
const point = normalize(event);
|
||
if (point) {
|
||
opts.onWindowInput(element, {
|
||
kind: 'wheel',
|
||
...point,
|
||
deltaX: event.deltaX,
|
||
deltaY: event.deltaY,
|
||
});
|
||
}
|
||
},
|
||
{ passive: false }
|
||
);
|
||
}
|
||
return wrap;
|
||
}
|
||
|
||
function createLuaElement(element, opts) {
|
||
const computed = element.computed || {};
|
||
const node = document.createElement('div');
|
||
node.className = 'sv-lua';
|
||
// Стили приходят из Lua в camelCase — переносим их на элемент как есть.
|
||
Object.entries(computed.style || {}).forEach(([key, value]) => {
|
||
if (value === null || value === undefined) return;
|
||
if (key.startsWith('--')) node.style.setProperty(key, String(value));
|
||
else {
|
||
try { node.style[key] = String(value); } catch (error) { /* пропускаем неизвестное свойство */ }
|
||
}
|
||
});
|
||
if (!computed.at && !Object.keys(computed.style || {}).length && !computed.html && !computed.text) {
|
||
node.style.cssText = 'border:1px dashed rgba(148,163,184,.45); border-radius:min(16px,2cqw); display:flex; align-items:center; justify-content:center; color:#94a3b8; font-size:1.6cqw; text-align:center; padding:1.4cqw';
|
||
node.textContent = opts.editable ? 'Lua-элемент — вычислите в инспекторе' : (element.label || 'Lua-элемент');
|
||
return node;
|
||
}
|
||
const vars = opts.variables || {};
|
||
if (computed.css) {
|
||
const style = document.createElement('style');
|
||
style.textContent = computed.css;
|
||
node.appendChild(style);
|
||
}
|
||
if (computed.html) node.innerHTML = interpolate(computed.html, vars);
|
||
else if (computed.text) node.textContent = interpolate(computed.text, vars);
|
||
else if (element.label) node.textContent = interpolate(element.label, vars);
|
||
return node;
|
||
}
|
||
|
||
function createButtonElement(element, vars, opts) {
|
||
const button = document.createElement('button');
|
||
button.type = 'button';
|
||
button.className = 'sv-button';
|
||
button.style.background = element.bg || '#6366f1';
|
||
button.style.color = element.color || '#ffffff';
|
||
button.style.fontSize = `${Number(element.fontSize) || 2.2}cqw`;
|
||
button.style.borderRadius = radius(element.radius);
|
||
if (element.shadow !== false) button.style.boxShadow = '0 8px 26px rgba(0, 0, 0, .28)';
|
||
button.textContent = interpolate(element.label || '', vars);
|
||
if (!opts.editable) {
|
||
button.addEventListener('click', (event) => {
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
if (typeof opts.onAction === 'function' && element.action) opts.onAction(element.action, element);
|
||
});
|
||
}
|
||
return button;
|
||
}
|
||
|
||
function createAudioElement(element, opts) {
|
||
const playing = typeof opts.isAudioPlaying === 'function' ? !!opts.isAudioPlaying(element.id) : false;
|
||
const node = document.createElement('div');
|
||
node.className = 'sv-audio' + (playing ? ' playing' : '');
|
||
const canClick = !opts.editable && typeof opts.onAction === 'function';
|
||
if (canClick) {
|
||
node.dataset.clickable = '1';
|
||
node.addEventListener('click', (event) => {
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
opts.onAction(playing ? { type: 'stop', audioId: element.id } : { type: 'play', audioId: element.id }, element);
|
||
});
|
||
}
|
||
|
||
const icon = document.createElement('span');
|
||
icon.className = 'sv-audio-icon';
|
||
icon.textContent = playing ? '⏸' : '▶';
|
||
const label = document.createElement('span');
|
||
label.className = 'sv-audio-label';
|
||
label.textContent = interpolate(element.label || 'Аудио', opts.variables);
|
||
node.appendChild(icon);
|
||
node.appendChild(label);
|
||
|
||
if (playing) {
|
||
const bars = document.createElement('span');
|
||
bars.className = 'sv-bars';
|
||
for (let i = 0; i < 4; i += 1) bars.appendChild(document.createElement('i'));
|
||
node.appendChild(bars);
|
||
}
|
||
return node;
|
||
}
|
||
|
||
function applyPosition(node, element) {
|
||
node.style.left = `${Number(element.x) || 0}%`;
|
||
node.style.top = `${Number(element.y) || 0}%`;
|
||
node.style.width = `${Number(element.w) || 10}%`;
|
||
node.style.height = `${Number(element.h) || 10}%`;
|
||
node.style.zIndex = String(Number(element.z) || 1);
|
||
const opacity = Number(element.opacity);
|
||
node.style.opacity = Number.isFinite(opacity) ? String(Math.max(0, Math.min(1, opacity))) : '';
|
||
const rotate = Number(element.rotate);
|
||
if (Number.isFinite(rotate) && rotate) node.style.transform = `rotate(${rotate}deg)`;
|
||
}
|
||
|
||
// Сохраняем позиции видео, чтобы перерисовка слайда не сбрасывала воспроизведение.
|
||
function captureMediaState(container) {
|
||
const states = new Map();
|
||
const oldRoot = container._svRoot;
|
||
if (!oldRoot) return states;
|
||
oldRoot.querySelectorAll('video[data-media-id]').forEach((video) => {
|
||
states.set(video.dataset.mediaId, {
|
||
time: video.currentTime || 0,
|
||
paused: video.paused,
|
||
});
|
||
});
|
||
return states;
|
||
}
|
||
|
||
function restoreMediaState(root, states) {
|
||
if (!states || !states.size) return;
|
||
root.querySelectorAll('video[data-media-id]').forEach((video) => {
|
||
const prev = states.get(video.dataset.mediaId);
|
||
if (!prev) return;
|
||
video._restoring = true;
|
||
const apply = () => {
|
||
try {
|
||
if (Math.abs(video.currentTime - prev.time) > 0.4) video.currentTime = prev.time;
|
||
if (!prev.paused && video.paused) video.play().catch(() => {});
|
||
} catch (error) {
|
||
/* видео ещё не готово */
|
||
}
|
||
setTimeout(() => { video._restoring = false; }, 200);
|
||
};
|
||
if (video.readyState >= 1) apply();
|
||
else video.addEventListener('loadedmetadata', apply, { once: true });
|
||
});
|
||
}
|
||
|
||
function render(container, slide, options = {}) {
|
||
if (!container) return { nodes: new Map() };
|
||
const opts = Object.assign({ variables: {}, editable: false }, options);
|
||
const root = document.createElement('div');
|
||
root.className = 'sv-root' + (opts.editable ? ' editable' : '');
|
||
const bgStyle = backgroundStyle(slide && slide.background);
|
||
root.setAttribute('style', styleObjectToString(bgStyle));
|
||
|
||
const mediaStates = captureMediaState(container);
|
||
|
||
if (container._svRoot && container._svRoot.parentNode === container) {
|
||
container.removeChild(container._svRoot);
|
||
}
|
||
if (container._svRoot) container._svRoot.remove();
|
||
container._svRoot = root;
|
||
container.appendChild(root);
|
||
|
||
const elements = slide && Array.isArray(slide.elements) ? slide.elements : [];
|
||
const nodes = new Map();
|
||
const sorted = [...elements].sort((a, b) => (Number(a.z) || 0) - (Number(b.z) || 0));
|
||
|
||
sorted.forEach((element) => {
|
||
if (!element || !element.type) return;
|
||
// Скрытое аудио не показывается зрителям, но в редакторе видно «призраком».
|
||
if (element.type === 'audio' && !element.visible && !opts.editable) return;
|
||
|
||
const node = document.createElement('div');
|
||
node.className = 'sv-el';
|
||
node.dataset.el = element.id || '';
|
||
node.dataset.type = element.type;
|
||
applyPosition(node, element);
|
||
if (opts.selectedId && opts.selectedId === element.id) node.classList.add('sv-selected');
|
||
if (opts.editable && element.type === 'audio' && !element.visible) node.classList.add('sv-ghost');
|
||
|
||
let inner;
|
||
if (element.type === 'text') inner = createTextElement(element, opts.variables);
|
||
else if (element.type === 'image') inner = createImageElement(element, opts.editable);
|
||
else if (element.type === 'shape') inner = createShapeElement(element);
|
||
else if (element.type === 'button') inner = createButtonElement(element, opts.variables, opts);
|
||
else if (element.type === 'audio') inner = createAudioElement(element, opts);
|
||
else if (element.type === 'video') inner = createVideoElement(element, opts);
|
||
else if (element.type === 'window') inner = createWindowElement(element, opts);
|
||
else if (element.type === 'lua') inner = createLuaElement(element, opts);
|
||
else inner = document.createElement('div');
|
||
|
||
node.appendChild(inner);
|
||
nodes.set(element.id, node);
|
||
root.appendChild(node);
|
||
});
|
||
|
||
if (!elements.length && opts.placeholder) {
|
||
const hint = document.createElement('div');
|
||
hint.className = 'sv-empty';
|
||
hint.textContent = opts.placeholder;
|
||
root.appendChild(hint);
|
||
}
|
||
|
||
restoreMediaState(root, mediaStates);
|
||
|
||
if (opts.animate && slide && slide.transition && slide.transition !== 'none') {
|
||
root.classList.add(`sv-anim-${slide.transition}`);
|
||
}
|
||
|
||
return { root, nodes };
|
||
}
|
||
|
||
window.SlideView = {
|
||
render,
|
||
interpolate,
|
||
backgroundStyle,
|
||
FONT_STACKS,
|
||
DEFAULT_GRADIENT,
|
||
};
|
||
})();
|