Динамическое фоновое свечение по цвету открытого письма

После загрузки iframe извлекаем цвет фона письма (body или первый
table/td) и записываем в --content-glow. Фоновый radial-gradient
в base.css использует эту переменную — белые письма дают белое свечение,
цветные — соответствующий оттенок. Сброс при смене письма.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
vodyanoysh 2026-04-01 00:04:55 +07:00
parent ba8ff2e4f6
commit 49d6d49412
2 changed files with 49 additions and 2 deletions

View file

@ -21,7 +21,7 @@ body {
/* Rich gradient base — accent-glow is set dynamically via JS */
background:
radial-gradient(ellipse 60% 50% at 15% 15%, var(--accent-glow, rgba(168,230,0,0.08)) 0%, transparent 70%),
radial-gradient(ellipse 55% 65% at 78% 55%, var(--accent-glow, rgba(168,230,0,0.05)) 0%, transparent 65%),
radial-gradient(ellipse 55% 65% at 78% 55%, var(--content-glow, transparent) 0%, transparent 65%),
radial-gradient(ellipse 45% 55% at 88% 90%, rgba(0, 110, 220, 0.05) 0%, transparent 70%),
radial-gradient(ellipse 60% 40% at 50% 50%, rgba(80, 0, 160, 0.04) 0%, transparent 100%),
#09090e;

View file

@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, ref, onMounted, watch } from 'vue'
import { computed, ref, watch, onUnmounted } from 'vue'
import type { MessageBody } from '@shared/types/message'
const props = defineProps<{
@ -109,6 +109,52 @@ const plainTextHtml = computed(() => {
})
const srcdoc = computed(() => processedHtml.value ?? plainTextHtml.value ?? '')
// ── Ambient glow: извлекаем доминирующий цвет фона письма ──
function parseRgb(color: string): { r: number; g: number; b: number; a: number } | null {
const m = color.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?/)
if (!m) return null
return { r: +m[1], g: +m[2], b: +m[3], a: m[4] !== undefined ? +m[4] : 1 }
}
function setContentGlow(color: string): void {
document.documentElement.style.setProperty('--content-glow', color)
}
function onIframeLoad(): void {
try {
const doc = iframeRef.value?.contentDocument
const view = iframeRef.value?.contentWindow
if (!doc?.body || !view) return
// Пробуем body напрямую
const bodyColor = parseRgb(view.getComputedStyle(doc.body).backgroundColor)
if (bodyColor && bodyColor.a > 0.1 && bodyColor.r + bodyColor.g + bodyColor.b > 60) {
const { r, g, b } = bodyColor
setContentGlow(`rgba(${r},${g},${b},0.09)`)
return
}
// Многие HTML-письма кладут фон в первый <table> или <td>
const firstEl = doc.body.querySelector<HTMLElement>('table, td, [bgcolor]')
if (firstEl) {
const raw = firstEl.getAttribute('bgcolor') ?? view.getComputedStyle(firstEl).backgroundColor
const c = parseRgb(raw)
if (c && c.a > 0.1 && c.r + c.g + c.b > 60) {
setContentGlow(`rgba(${c.r},${c.g},${c.b},0.09)`)
return
}
}
} catch { /* cross-origin или sandbox */ }
setContentGlow('transparent')
}
// Сбрасываем свечение при смене письма
watch(() => props.body, () => setContentGlow('transparent'))
onUnmounted(() => setContentGlow('transparent'))
</script>
<template>
@ -119,6 +165,7 @@ const srcdoc = computed(() => processedHtml.value ?? plainTextHtml.value ?? '')
:srcdoc="srcdoc"
sandbox="allow-same-origin"
class="body-frame"
@load="onIframeLoad"
/>
<div v-else class="no-body">
<span>Содержимое письма недоступно</span>