Репозитории /
Promptly /
public/js/slide-view.js
slide-view.js
| 1 | // Общий рендерер слайдов: используется в редакторе, демонстрации и превью на главной. |
| 2 | // Элементы позиционируются в процентах, размеры шрифтов — в cqw (1% ширины слайда). |
| 3 | (function () { |
| 4 | const DEFAULT_GRADIENT = 'linear-gradient(135deg, #0f172a 0%, #1e1b4b 55%, #312e81 100%)'; |
| 5 | |
| 6 | const FONT_STACKS = { |
| 7 | sans: "system-ui, -apple-system, 'Segoe UI', sans-serif", |
| 8 | serif: "Georgia, 'Times New Roman', serif", |
| 9 | mono: "ui-monospace, 'Cascadia Code', 'JetBrains Mono', Consolas, monospace", |
| 10 | round: "'Segoe UI Rounded', ui-rounded, 'Nunito', system-ui, sans-serif", |
| 11 | }; |
| 12 | |
| 13 | if (!document.getElementById('slide-view-styles')) { |
| 14 | const style = document.createElement('style'); |
| 15 | style.id = 'slide-view-styles'; |
| 16 | style.textContent = ` |
| 17 | .sv-root { |
| 18 | position: absolute; inset: 0; overflow: hidden; |
| 19 | container-type: inline-size; |
| 20 | font-family: system-ui, -apple-system, 'Segoe UI', sans-serif; |
| 21 | color: #f8fafc; user-select: none; |
| 22 | } |
| 23 | .sv-el { position: absolute; box-sizing: border-box; } |
| 24 | .sv-root.editable .sv-el { cursor: move; } |
| 25 | .sv-root.editable .sv-el:hover { outline: 1px dashed rgba(129, 140, 248, .7); outline-offset: 1px; } |
| 26 | .sv-root.editable .sv-el.sv-selected { outline: 2px solid #818cf8; outline-offset: 1px; } |
| 27 | .sv-root.editable .sv-el.sv-ghost { opacity: .38 !important; outline: 1px dashed rgba(251, 191, 36, .8); } |
| 28 | .sv-text { |
| 29 | display: flex; white-space: pre-wrap; word-break: break-word; |
| 30 | overflow: hidden; box-sizing: border-box; font-weight: 700; |
| 31 | } |
| 32 | .sv-image { width: 100%; height: 100%; display: block; object-fit: cover; } |
| 33 | .sv-image-missing { |
| 34 | width: 100%; height: 100%; display: flex; flex-direction: column; gap: 1cqw; |
| 35 | align-items: center; justify-content: center; border: 1px dashed rgba(148, 163, 184, .5); |
| 36 | border-radius: 10px; color: #94a3b8; font-size: 1.6cqw; background: rgba(15, 23, 42, .35); |
| 37 | } |
| 38 | .sv-image-missing b { font-size: 3cqw; line-height: 1; } |
| 39 | .sv-shape { width: 100%; height: 100%; display: block; overflow: visible; } |
| 40 | .sv-video { |
| 41 | position: relative; width: 100%; height: 100%; overflow: hidden; background: #02040a; |
| 42 | } |
| 43 | .sv-video video { |
| 44 | width: 100%; height: 100%; display: block; background: #02040a; |
| 45 | } |
| 46 | .sv-video .sv-media-missing { |
| 47 | position: absolute; inset: 0; display: flex; flex-direction: column; gap: 1cqw; |
| 48 | align-items: center; justify-content: center; border: 1px dashed rgba(148, 163, 184, .5); |
| 49 | color: #94a3b8; font-size: 1.6cqw; background: rgba(15, 23, 42, .35); text-align: center; |
| 50 | box-sizing: border-box; padding: 1cqw; |
| 51 | } |
| 52 | .sv-video .sv-media-missing b { font-size: 3cqw; line-height: 1; } |
| 53 | .sv-root.editable .sv-video { pointer-events: none; } |
| 54 | .sv-window { |
| 55 | position: relative; width: 100%; height: 100%; overflow: hidden; background: #02040a; |
| 56 | display: flex; align-items: center; justify-content: center; |
| 57 | } |
| 58 | .sv-window img { |
| 59 | width: 100%; height: 100%; display: block; object-fit: fill; |
| 60 | } |
| 61 | .sv-window .sv-window-placeholder { |
| 62 | position: absolute; inset: 0; display: flex; flex-direction: column; gap: .6cqw; |
| 63 | align-items: center; justify-content: center; text-align: center; padding: 2cqw; |
| 64 | color: #94a3b8; font-size: 1.5cqw; border: 1px dashed rgba(148, 163, 184, .45); |
| 65 | background: radial-gradient(circle at 50% 40%, rgba(99, 102, 241, .16), rgba(2, 4, 10, .9)); |
| 66 | } |
| 67 | .sv-window .sv-window-placeholder b { font-size: 2.6cqw; } |
| 68 | .sv-window .sv-window-live { |
| 69 | position: absolute; top: .7cqw; right: .7cqw; display: flex; align-items: center; gap: .5cqw; |
| 70 | padding: .35cqw .8cqw; border-radius: 999px; background: rgba(5, 8, 18, .7); |
| 71 | color: #f0abfc; font-size: 1.1cqw; font-weight: 700; letter-spacing: .5px; |
| 72 | border: 1px solid rgba(232, 121, 249, .35); pointer-events: none; |
| 73 | } |
| 74 | .sv-window .sv-window-live i { width: .55cqw; height: .55cqw; border-radius: 50%; background: #e879f9; } |
| 75 | .sv-root:not(.editable) .sv-window[data-live="1"] { cursor: crosshair; } |
| 76 | .sv-root:not(.editable) .sv-window[data-live="1"]:hover { box-shadow: inset 0 0 0 2px rgba(129, 140, 248, .55); } |
| 77 | .sv-button { |
| 78 | width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; |
| 79 | border: none; font-family: inherit; font-weight: 700; line-height: 1.15; padding: 0 4%; |
| 80 | box-sizing: border-box; cursor: pointer; text-align: center; |
| 81 | transition: transform .12s ease, filter .12s ease, box-shadow .12s ease; |
| 82 | } |
| 83 | .sv-lua { |
| 84 | width: 100%; height: 100%; box-sizing: border-box; overflow: hidden; |
| 85 | font-size: 1.7cqw; line-height: 1.35; |
| 86 | } |
| 87 | .sv-root.editable .sv-lua { pointer-events: none; } |
| 88 | .sv-button:hover { filter: brightness(1.1); } |
| 89 | .sv-button:active { transform: scale(.98); } |
| 90 | .sv-root.editable .sv-button { pointer-events: none; } |
| 91 | .sv-audio { |
| 92 | width: 100%; height: 100%; display: flex; align-items: center; gap: .8em; |
| 93 | padding: 0 1.2em; box-sizing: border-box; border-radius: 999px; |
| 94 | background: rgba(15, 23, 42, .62); border: 1px solid rgba(255, 255, 255, .18); |
| 95 | color: #e2e8f0; font-size: clamp(8px, 1.7cqw, 30px); font-weight: 600; |
| 96 | backdrop-filter: blur(10px); overflow: hidden; white-space: nowrap; |
| 97 | box-shadow: 0 10px 30px rgba(0, 0, 0, .3); |
| 98 | } |
| 99 | .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); } |
| 100 | .sv-root:not(.editable) .sv-audio[data-clickable="1"] { cursor: pointer; } |
| 101 | .sv-root:not(.editable) .sv-audio[data-clickable="1"]:hover { background: rgba(30, 41, 59, .8); } |
| 102 | .sv-audio-icon { |
| 103 | width: 2.1em; height: 2.1em; flex-shrink: 0; border-radius: 50%; |
| 104 | display: flex; align-items: center; justify-content: center; |
| 105 | background: linear-gradient(135deg, #6366f1, #a855f7); color: #fff; font-size: .95em; |
| 106 | } |
| 107 | .sv-audio-label { overflow: hidden; text-overflow: ellipsis; } |
| 108 | .sv-bars { display: flex; align-items: flex-end; gap: .16em; height: 1.1em; margin-left: auto; } |
| 109 | .sv-bars i { width: .2em; background: #a5b4fc; border-radius: 999px; animation: svBar .9s ease-in-out infinite; } |
| 110 | .sv-bars i:nth-child(2) { animation-delay: .15s; } |
| 111 | .sv-bars i:nth-child(3) { animation-delay: .3s; } |
| 112 | .sv-bars i:nth-child(4) { animation-delay: .45s; } |
| 113 | @keyframes svBar { 0%, 100% { height: 25%; } 50% { height: 100%; } } |
| 114 | .sv-empty { |
| 115 | position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; |
| 116 | color: rgba(148, 163, 184, .6); font-size: clamp(11px, 2.4cqw, 30px); font-weight: 600; |
| 117 | text-align: center; padding: 8%; |
| 118 | } |
| 119 | /* Переходы между слайдами */ |
| 120 | .sv-anim-fade { animation: svFade .45s cubic-bezier(.2, .7, .3, 1) both; } |
| 121 | .sv-anim-slide { animation: svSlide .5s cubic-bezier(.2, .7, .3, 1) both; } |
| 122 | .sv-anim-zoom { animation: svZoom .45s cubic-bezier(.2, .7, .3, 1) both; } |
| 123 | .sv-anim-flip { animation: svFlip .55s cubic-bezier(.2, .7, .3, 1) both; } |
| 124 | .sv-anim-rise { animation: svRise .5s cubic-bezier(.2, .7, .3, 1) both; } |
| 125 | @keyframes svFade { from { opacity: 0; } to { opacity: 1; } } |
| 126 | @keyframes svSlide { from { opacity: 0; transform: translateX(5%); } to { opacity: 1; transform: none; } } |
| 127 | @keyframes svZoom { from { opacity: 0; transform: scale(.94); } to { opacity: 1; transform: none; } } |
| 128 | @keyframes svFlip { from { opacity: 0; transform: perspective(1400px) rotateY(32deg); } to { opacity: 1; transform: none; } } |
| 129 | @keyframes svRise { from { opacity: 0; transform: translateY(7%); } to { opacity: 1; transform: none; } } |
| 130 | `; |
| 131 | document.head.appendChild(style); |
| 132 | } |
| 133 | |
| 134 | function esc(value) { |
| 135 | return String(value ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); |
| 136 | } |
| 137 | |
| 138 | function interpolate(text, vars) { |
| 139 | if (!text) return ''; |
| 140 | if (!vars) return String(text); |
| 141 | return String(text).replace(/\{\{\s*([\w.-]+)\s*\}\}/g, (match, key) => { |
| 142 | if (Object.prototype.hasOwnProperty.call(vars, key)) { |
| 143 | const value = vars[key]; |
| 144 | if (value === null || value === undefined) return ''; |
| 145 | return String(value); |
| 146 | } |
| 147 | return match; |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | function backgroundStyle(background) { |
| 152 | const bg = background || {}; |
| 153 | const type = bg.type || (bg.image ? 'image' : bg.gradient ? 'gradient' : 'color'); |
| 154 | if (type === 'image' && bg.image) { |
| 155 | return { |
| 156 | backgroundImage: `url("${String(bg.image).replace(/"/g, '%22')}")`, |
| 157 | backgroundSize: 'cover', |
| 158 | backgroundPosition: 'center', |
| 159 | backgroundColor: '#0f172a', |
| 160 | }; |
| 161 | } |
| 162 | if (type === 'gradient') { |
| 163 | return { background: bg.gradient || DEFAULT_GRADIENT }; |
| 164 | } |
| 165 | return { background: bg.color || '#0f172a' }; |
| 166 | } |
| 167 | |
| 168 | function styleObjectToString(styles) { |
| 169 | return Object.keys(styles) |
| 170 | .map((key) => `${key.replace(/[A-Z]/g, (m) => '-' + m.toLowerCase())}:${styles[key]}`) |
| 171 | .join(';'); |
| 172 | } |
| 173 | |
| 174 | function radius(px) { |
| 175 | const value = Number(px); |
| 176 | if (!Number.isFinite(value) || value <= 0) return ''; |
| 177 | return `min(${value}px, 2cqw)`; |
| 178 | } |
| 179 | |
| 180 | function shadowStyle(on) { |
| 181 | return on ? '0 1.2cqw 2.6cqw rgba(0, 0, 0, .38)' : ''; |
| 182 | } |
| 183 | |
| 184 | function createTextElement(element, vars) { |
| 185 | const node = document.createElement('div'); |
| 186 | node.className = 'sv-text'; |
| 187 | const justify = element.align === 'left' ? 'flex-start' : element.align === 'right' ? 'flex-end' : 'center'; |
| 188 | node.style.justifyContent = justify; |
| 189 | node.style.textAlign = element.align || 'center'; |
| 190 | node.style.fontSize = `${Number(element.fontSize) || 5}cqw`; |
| 191 | node.style.fontWeight = String(element.weight || 700); |
| 192 | node.style.color = element.color || '#f8fafc'; |
| 193 | node.style.lineHeight = String(Number(element.lineHeight) || 1.16); |
| 194 | if (element.font && FONT_STACKS[element.font]) node.style.fontFamily = FONT_STACKS[element.font]; |
| 195 | if (element.italic) node.style.fontStyle = 'italic'; |
| 196 | if (element.shadow) node.style.textShadow = '0 .35cqw .9cqw rgba(0, 0, 0, .5)'; |
| 197 | if (element.bg) { |
| 198 | node.style.background = element.bg; |
| 199 | node.style.borderRadius = 'min(18px, 2cqw)'; |
| 200 | node.style.padding = '1.4cqw 2cqw'; |
| 201 | } |
| 202 | node.textContent = interpolate(element.text || '', vars); |
| 203 | return node; |
| 204 | } |
| 205 | |
| 206 | function createImageElement(element, editable) { |
| 207 | if (!element.src) { |
| 208 | const placeholder = document.createElement('div'); |
| 209 | placeholder.className = 'sv-image-missing'; |
| 210 | placeholder.innerHTML = '<b>фото</b><span>Укажите URL или загрузите файл</span>'; |
| 211 | return placeholder; |
| 212 | } |
| 213 | const img = document.createElement('img'); |
| 214 | img.className = 'sv-image'; |
| 215 | img.src = element.src; |
| 216 | img.alt = ''; |
| 217 | img.draggable = false; |
| 218 | img.loading = 'lazy'; |
| 219 | img.style.objectFit = element.fit === 'contain' ? 'contain' : 'cover'; |
| 220 | img.style.borderRadius = radius(element.radius); |
| 221 | if (element.shadow) img.style.boxShadow = shadowStyle(true); |
| 222 | if (editable) img.style.pointerEvents = 'none'; |
| 223 | return img; |
| 224 | } |
| 225 | |
| 226 | // Фигуры: SVG с viewBox 100×100, preserveAspectRatio none — тянутся под размер элемента. |
| 227 | function shapeBody(shape, element) { |
| 228 | const fill = element.fill || '#6366f1'; |
| 229 | const stroke = element.borderColor ? ` stroke="${esc(element.borderColor)}" stroke-width="${Math.max(0.5, Number(element.borderWidth) || 1)}"` : ''; |
| 230 | const r = Math.max(0, Math.min(45, Number(element.radius) || 0)); |
| 231 | switch (shape) { |
| 232 | case 'ellipse': |
| 233 | return `<ellipse cx="50" cy="50" rx="${50 - (stroke ? 1 : 0)}" ry="${50 - (stroke ? 1 : 0)}" fill="${esc(fill)}"${stroke}/>`; |
| 234 | case 'triangle': |
| 235 | return `<path d="M50 2 L98 98 L2 98 Z" fill="${esc(fill)}"${stroke}/>`; |
| 236 | case 'arrow': |
| 237 | return `<path d="M2 36 H56 V12 L98 50 L56 88 V64 H2 Z" fill="${esc(fill)}"${stroke}/>`; |
| 238 | case 'line': |
| 239 | 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}/>`; |
| 240 | case 'star': |
| 241 | 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}/>`; |
| 242 | case 'hexagon': |
| 243 | return `<path d="M25 4 H75 L99 50 L75 96 H25 L1 50 Z" fill="${esc(fill)}"${stroke}/>`; |
| 244 | case 'diamond': |
| 245 | return `<path d="M50 2 L98 50 L50 98 L2 50 Z" fill="${esc(fill)}"${stroke}/>`; |
| 246 | case 'round': |
| 247 | return `<rect x="1" y="1" width="98" height="98" rx="${Math.max(8, r)}" fill="${esc(fill)}"${stroke}/>`; |
| 248 | case 'rect': |
| 249 | default: |
| 250 | return `<rect x="1" y="1" width="98" height="98" rx="${r}" fill="${esc(fill)}"${stroke}/>`; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | function createShapeElement(element) { |
| 255 | const wrap = document.createElement('div'); |
| 256 | wrap.className = 'sv-shape-wrap'; |
| 257 | wrap.style.width = '100%'; |
| 258 | wrap.style.height = '100%'; |
| 259 | if (element.shadow) wrap.style.filter = 'drop-shadow(0 1cqw 1.8cqw rgba(0,0,0,.4))'; |
| 260 | wrap.innerHTML = `<svg class="sv-shape" viewBox="0 0 100 100" preserveAspectRatio="none">${shapeBody(element.shape || 'rect', element)}</svg>`; |
| 261 | return wrap; |
| 262 | } |
| 263 | |
| 264 | function createVideoElement(element, opts) { |
| 265 | const wrap = document.createElement('div'); |
| 266 | wrap.className = 'sv-video'; |
| 267 | wrap.dataset.mediaId = element.id || ''; |
| 268 | wrap.style.borderRadius = radius(element.radius); |
| 269 | |
| 270 | if (!element.src) { |
| 271 | wrap.innerHTML = '<div class="sv-media-missing"><b>видео</b><span>Добавьте видеофайл или ссылку</span></div>'; |
| 272 | return wrap; |
| 273 | } |
| 274 | |
| 275 | const video = document.createElement('video'); |
| 276 | video.src = element.src; |
| 277 | video.poster = element.poster || ''; |
| 278 | video.muted = element.muted !== false; |
| 279 | video.loop = !!element.loop; |
| 280 | const frozen = opts.editable || opts.staticPreview; |
| 281 | video.controls = !frozen && element.controls !== false; |
| 282 | video.playsInline = true; |
| 283 | video.preload = 'auto'; |
| 284 | video.dataset.mediaId = element.id || ''; |
| 285 | video.style.objectFit = element.fit === 'contain' ? 'contain' : 'cover'; |
| 286 | video.style.borderRadius = radius(element.radius); |
| 287 | if (element.shadow) video.style.boxShadow = shadowStyle(true); |
| 288 | if (frozen) { |
| 289 | video.autoplay = false; |
| 290 | video.controls = false; |
| 291 | video.style.pointerEvents = 'none'; |
| 292 | } else { |
| 293 | video.autoplay = !!element.autoplay; |
| 294 | if (typeof opts.onVideoEvent === 'function') { |
| 295 | const fire = (type) => { |
| 296 | if (video._applyingRemote || video._restoring) return; |
| 297 | opts.onVideoEvent(element, type, video); |
| 298 | }; |
| 299 | video.addEventListener('play', () => fire('play')); |
| 300 | video.addEventListener('pause', () => fire('pause')); |
| 301 | video.addEventListener('seeked', () => fire('seek')); |
| 302 | video.addEventListener('ended', () => fire('ended')); |
| 303 | } |
| 304 | } |
| 305 | wrap.appendChild(video); |
| 306 | return wrap; |
| 307 | } |
| 308 | |
| 309 | function windowDataUrl(data) { |
| 310 | if (!data) return ''; |
| 311 | if (/^data:/.test(data)) return data; |
| 312 | return `data:image/jpeg;base64,${data}`; |
| 313 | } |
| 314 | |
| 315 | function createWindowElement(element, opts) { |
| 316 | const wrap = document.createElement('div'); |
| 317 | wrap.className = 'sv-window'; |
| 318 | wrap.dataset.windowId = element.id || ''; |
| 319 | wrap.style.borderRadius = radius(element.radius); |
| 320 | |
| 321 | const live = !opts.editable && typeof opts.onWindowInput === 'function'; |
| 322 | const frameData = opts.windowFrames && opts.windowFrames[element.id]; |
| 323 | const img = document.createElement('img'); |
| 324 | img.className = 'sv-window-frame'; |
| 325 | img.alt = ''; |
| 326 | img.draggable = false; |
| 327 | if (frameData) { |
| 328 | img.src = windowDataUrl(frameData); |
| 329 | if (live) { |
| 330 | wrap.dataset.live = '1'; |
| 331 | const badge = document.createElement('span'); |
| 332 | badge.className = 'sv-window-live'; |
| 333 | badge.innerHTML = '<i></i>LIVE'; |
| 334 | wrap.appendChild(badge); |
| 335 | } |
| 336 | } else { |
| 337 | const placeholder = document.createElement('div'); |
| 338 | placeholder.className = 'sv-window-placeholder'; |
| 339 | placeholder.innerHTML = `<b>окно</b><span></span>`; |
| 340 | placeholder.querySelector('span').textContent = opts.editable |
| 341 | ? (element.targetTitle || 'Окно Chrome — выберите цель в инспекторе') |
| 342 | : 'Трансляция не запущена'; |
| 343 | wrap.appendChild(placeholder); |
| 344 | } |
| 345 | wrap.insertBefore(img, wrap.firstChild); |
| 346 | |
| 347 | if (live) { |
| 348 | const normalize = (event) => { |
| 349 | const rect = wrap.getBoundingClientRect(); |
| 350 | if (!rect.width || !rect.height) return null; |
| 351 | return { |
| 352 | x: Math.max(0, Math.min(1, (event.clientX - rect.left) / rect.width)), |
| 353 | y: Math.max(0, Math.min(1, (event.clientY - rect.top) / rect.height)), |
| 354 | }; |
| 355 | }; |
| 356 | let lastMove = 0; |
| 357 | wrap.addEventListener('click', (event) => { |
| 358 | event.preventDefault(); |
| 359 | event.stopPropagation(); |
| 360 | const point = normalize(event); |
| 361 | if (point) opts.onWindowInput(element, { kind: 'click', ...point }); |
| 362 | }); |
| 363 | wrap.addEventListener('pointermove', (event) => { |
| 364 | const now = Date.now(); |
| 365 | if (now - lastMove < 120) return; |
| 366 | lastMove = now; |
| 367 | const point = normalize(event); |
| 368 | if (point) opts.onWindowInput(element, { kind: 'move', ...point }); |
| 369 | }); |
| 370 | wrap.addEventListener( |
| 371 | 'wheel', |
| 372 | (event) => { |
| 373 | event.preventDefault(); |
| 374 | const point = normalize(event); |
| 375 | if (point) { |
| 376 | opts.onWindowInput(element, { |
| 377 | kind: 'wheel', |
| 378 | ...point, |
| 379 | deltaX: event.deltaX, |
| 380 | deltaY: event.deltaY, |
| 381 | }); |
| 382 | } |
| 383 | }, |
| 384 | { passive: false } |
| 385 | ); |
| 386 | } |
| 387 | return wrap; |
| 388 | } |
| 389 | |
| 390 | function createLuaElement(element, opts) { |
| 391 | const computed = element.computed || {}; |
| 392 | const node = document.createElement('div'); |
| 393 | node.className = 'sv-lua'; |
| 394 | // Стили приходят из Lua в camelCase — переносим их на элемент как есть. |
| 395 | Object.entries(computed.style || {}).forEach(([key, value]) => { |
| 396 | if (value === null || value === undefined) return; |
| 397 | if (key.startsWith('--')) node.style.setProperty(key, String(value)); |
| 398 | else { |
| 399 | try { node.style[key] = String(value); } catch (error) { /* пропускаем неизвестное свойство */ } |
| 400 | } |
| 401 | }); |
| 402 | if (!computed.at && !Object.keys(computed.style || {}).length && !computed.html && !computed.text) { |
| 403 | 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'; |
| 404 | node.textContent = opts.editable ? 'Lua-элемент — вычислите в инспекторе' : (element.label || 'Lua-элемент'); |
| 405 | return node; |
| 406 | } |
| 407 | const vars = opts.variables || {}; |
| 408 | if (computed.css) { |
| 409 | const style = document.createElement('style'); |
| 410 | style.textContent = computed.css; |
| 411 | node.appendChild(style); |
| 412 | } |
| 413 | if (computed.html) node.innerHTML = interpolate(computed.html, vars); |
| 414 | else if (computed.text) node.textContent = interpolate(computed.text, vars); |
| 415 | else if (element.label) node.textContent = interpolate(element.label, vars); |
| 416 | return node; |
| 417 | } |
| 418 | |
| 419 | function createButtonElement(element, vars, opts) { |
| 420 | const button = document.createElement('button'); |
| 421 | button.type = 'button'; |
| 422 | button.className = 'sv-button'; |
| 423 | button.style.background = element.bg || '#6366f1'; |
| 424 | button.style.color = element.color || '#ffffff'; |
| 425 | button.style.fontSize = `${Number(element.fontSize) || 2.2}cqw`; |
| 426 | button.style.borderRadius = radius(element.radius); |
| 427 | if (element.shadow !== false) button.style.boxShadow = '0 8px 26px rgba(0, 0, 0, .28)'; |
| 428 | button.textContent = interpolate(element.label || '', vars); |
| 429 | if (!opts.editable) { |
| 430 | button.addEventListener('click', (event) => { |
| 431 | event.preventDefault(); |
| 432 | event.stopPropagation(); |
| 433 | if (typeof opts.onAction === 'function' && element.action) opts.onAction(element.action, element); |
| 434 | }); |
| 435 | } |
| 436 | return button; |
| 437 | } |
| 438 | |
| 439 | function createAudioElement(element, opts) { |
| 440 | const playing = typeof opts.isAudioPlaying === 'function' ? !!opts.isAudioPlaying(element.id) : false; |
| 441 | const node = document.createElement('div'); |
| 442 | node.className = 'sv-audio' + (playing ? ' playing' : ''); |
| 443 | const canClick = !opts.editable && typeof opts.onAction === 'function'; |
| 444 | if (canClick) { |
| 445 | node.dataset.clickable = '1'; |
| 446 | node.addEventListener('click', (event) => { |
| 447 | event.preventDefault(); |
| 448 | event.stopPropagation(); |
| 449 | opts.onAction(playing ? { type: 'stop', audioId: element.id } : { type: 'play', audioId: element.id }, element); |
| 450 | }); |
| 451 | } |
| 452 | |
| 453 | const icon = document.createElement('span'); |
| 454 | icon.className = 'sv-audio-icon'; |
| 455 | icon.textContent = playing ? '⏸' : '▶'; |
| 456 | const label = document.createElement('span'); |
| 457 | label.className = 'sv-audio-label'; |
| 458 | label.textContent = interpolate(element.label || 'Аудио', opts.variables); |
| 459 | node.appendChild(icon); |
| 460 | node.appendChild(label); |
| 461 | |
| 462 | if (playing) { |
| 463 | const bars = document.createElement('span'); |
| 464 | bars.className = 'sv-bars'; |
| 465 | for (let i = 0; i < 4; i += 1) bars.appendChild(document.createElement('i')); |
| 466 | node.appendChild(bars); |
| 467 | } |
| 468 | return node; |
| 469 | } |
| 470 | |
| 471 | function applyPosition(node, element) { |
| 472 | node.style.left = `${Number(element.x) || 0}%`; |
| 473 | node.style.top = `${Number(element.y) || 0}%`; |
| 474 | node.style.width = `${Number(element.w) || 10}%`; |
| 475 | node.style.height = `${Number(element.h) || 10}%`; |
| 476 | node.style.zIndex = String(Number(element.z) || 1); |
| 477 | const opacity = Number(element.opacity); |
| 478 | node.style.opacity = Number.isFinite(opacity) ? String(Math.max(0, Math.min(1, opacity))) : ''; |
| 479 | const rotate = Number(element.rotate); |
| 480 | if (Number.isFinite(rotate) && rotate) node.style.transform = `rotate(${rotate}deg)`; |
| 481 | } |
| 482 | |
| 483 | // Сохраняем позиции видео, чтобы перерисовка слайда не сбрасывала воспроизведение. |
| 484 | function captureMediaState(container) { |
| 485 | const states = new Map(); |
| 486 | const oldRoot = container._svRoot; |
| 487 | if (!oldRoot) return states; |
| 488 | oldRoot.querySelectorAll('video[data-media-id]').forEach((video) => { |
| 489 | states.set(video.dataset.mediaId, { |
| 490 | time: video.currentTime || 0, |
| 491 | paused: video.paused, |
| 492 | }); |
| 493 | }); |
| 494 | return states; |
| 495 | } |
| 496 | |
| 497 | function restoreMediaState(root, states) { |
| 498 | if (!states || !states.size) return; |
| 499 | root.querySelectorAll('video[data-media-id]').forEach((video) => { |
| 500 | const prev = states.get(video.dataset.mediaId); |
| 501 | if (!prev) return; |
| 502 | video._restoring = true; |
| 503 | const apply = () => { |
| 504 | try { |
| 505 | if (Math.abs(video.currentTime - prev.time) > 0.4) video.currentTime = prev.time; |
| 506 | if (!prev.paused && video.paused) video.play().catch(() => {}); |
| 507 | } catch (error) { |
| 508 | /* видео ещё не готово */ |
| 509 | } |
| 510 | setTimeout(() => { video._restoring = false; }, 200); |
| 511 | }; |
| 512 | if (video.readyState >= 1) apply(); |
| 513 | else video.addEventListener('loadedmetadata', apply, { once: true }); |
| 514 | }); |
| 515 | } |
| 516 | |
| 517 | function render(container, slide, options = {}) { |
| 518 | if (!container) return { nodes: new Map() }; |
| 519 | const opts = Object.assign({ variables: {}, editable: false }, options); |
| 520 | const root = document.createElement('div'); |
| 521 | root.className = 'sv-root' + (opts.editable ? ' editable' : ''); |
| 522 | const bgStyle = backgroundStyle(slide && slide.background); |
| 523 | root.setAttribute('style', styleObjectToString(bgStyle)); |
| 524 | |
| 525 | const mediaStates = captureMediaState(container); |
| 526 | |
| 527 | if (container._svRoot && container._svRoot.parentNode === container) { |
| 528 | container.removeChild(container._svRoot); |
| 529 | } |
| 530 | if (container._svRoot) container._svRoot.remove(); |
| 531 | container._svRoot = root; |
| 532 | container.appendChild(root); |
| 533 | |
| 534 | const elements = slide && Array.isArray(slide.elements) ? slide.elements : []; |
| 535 | const nodes = new Map(); |
| 536 | const sorted = [...elements].sort((a, b) => (Number(a.z) || 0) - (Number(b.z) || 0)); |
| 537 | |
| 538 | sorted.forEach((element) => { |
| 539 | if (!element || !element.type) return; |
| 540 | // Скрытое аудио не показывается зрителям, но в редакторе видно «призраком». |
| 541 | if (element.type === 'audio' && !element.visible && !opts.editable) return; |
| 542 | |
| 543 | const node = document.createElement('div'); |
| 544 | node.className = 'sv-el'; |
| 545 | node.dataset.el = element.id || ''; |
| 546 | node.dataset.type = element.type; |
| 547 | applyPosition(node, element); |
| 548 | if (opts.selectedId && opts.selectedId === element.id) node.classList.add('sv-selected'); |
| 549 | if (opts.editable && element.type === 'audio' && !element.visible) node.classList.add('sv-ghost'); |
| 550 | |
| 551 | let inner; |
| 552 | if (element.type === 'text') inner = createTextElement(element, opts.variables); |
| 553 | else if (element.type === 'image') inner = createImageElement(element, opts.editable); |
| 554 | else if (element.type === 'shape') inner = createShapeElement(element); |
| 555 | else if (element.type === 'button') inner = createButtonElement(element, opts.variables, opts); |
| 556 | else if (element.type === 'audio') inner = createAudioElement(element, opts); |
| 557 | else if (element.type === 'video') inner = createVideoElement(element, opts); |
| 558 | else if (element.type === 'window') inner = createWindowElement(element, opts); |
| 559 | else if (element.type === 'lua') inner = createLuaElement(element, opts); |
| 560 | else inner = document.createElement('div'); |
| 561 | |
| 562 | node.appendChild(inner); |
| 563 | nodes.set(element.id, node); |
| 564 | root.appendChild(node); |
| 565 | }); |
| 566 | |
| 567 | if (!elements.length && opts.placeholder) { |
| 568 | const hint = document.createElement('div'); |
| 569 | hint.className = 'sv-empty'; |
| 570 | hint.textContent = opts.placeholder; |
| 571 | root.appendChild(hint); |
| 572 | } |
| 573 | |
| 574 | restoreMediaState(root, mediaStates); |
| 575 | |
| 576 | if (opts.animate && slide && slide.transition && slide.transition !== 'none') { |
| 577 | root.classList.add(`sv-anim-${slide.transition}`); |
| 578 | } |
| 579 | |
| 580 | return { root, nodes }; |
| 581 | } |
| 582 | |
| 583 | window.SlideView = { |
| 584 | render, |
| 585 | interpolate, |
| 586 | backgroundStyle, |
| 587 | FONT_STACKS, |
| 588 | DEFAULT_GRADIENT, |
| 589 | }; |
| 590 | })(); |