Исправить цвета HTML-писем + попап контакта
- MessageBody: постобработка HTML через DOMParser — убирает белый цвет текста когда фон тоже светлый (белый-на-белом), охватывает inline styles и атрибуты color/bgcolor - MessageHeader: адреса «От», «Кому», «Копия» стали кликабельными; по клику открывается liquid glass попап с именем, email и кнопкой копирования Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
8f33725a4a
commit
595d6c9c6a
2 changed files with 339 additions and 37 deletions
|
|
@ -8,20 +8,79 @@ const props = defineProps<{
|
|||
|
||||
const iframeRef = ref<HTMLIFrameElement | null>(null)
|
||||
|
||||
// ── Исправление цветов HTML-писем ─────────────────────────
|
||||
// Некоторые письма используют белый текст в секциях с белым фоном.
|
||||
// Обходим DOM и убираем явный белый цвет текста там, где фон тоже светлый.
|
||||
|
||||
function isNearWhite(s: string): boolean {
|
||||
s = s.trim().toLowerCase().replace(/\s+/g, '')
|
||||
if (!s || s === 'transparent' || s === 'rgba(0,0,0,0)') return false
|
||||
if (s === 'white' || s === '#fff' || s === '#ffffff') return true
|
||||
const h6 = s.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/)
|
||||
if (h6) return parseInt(h6[1], 16) >= 200 && parseInt(h6[2], 16) >= 200 && parseInt(h6[3], 16) >= 200
|
||||
const h3 = s.match(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/)
|
||||
if (h3) return parseInt(h3[1] + h3[1], 16) >= 200 && parseInt(h3[2] + h3[2], 16) >= 200 && parseInt(h3[3] + h3[3], 16) >= 200
|
||||
const rgb = s.match(/^rgb\((\d+),(\d+),(\d+)\)$/)
|
||||
if (rgb) return +rgb[1] >= 200 && +rgb[2] >= 200 && +rgb[3] >= 200
|
||||
const rgba = s.match(/^rgba\((\d+),(\d+),(\d+),([\d.]+)\)$/)
|
||||
if (rgba) return +rgba[1] >= 200 && +rgba[2] >= 200 && +rgba[3] >= 200 && +rgba[4] > 0.3
|
||||
return false
|
||||
}
|
||||
|
||||
function getExplicitBg(el: HTMLElement): string {
|
||||
return el.style.backgroundColor || el.style.background || el.getAttribute('bgcolor') || ''
|
||||
}
|
||||
|
||||
function findNearestBg(el: HTMLElement): string {
|
||||
let node: HTMLElement | null = el
|
||||
while (node) {
|
||||
const bg = getExplicitBg(node)
|
||||
if (bg && bg !== 'transparent' && bg !== 'rgba(0, 0, 0, 0)' && bg !== 'rgba(0,0,0,0)') return bg
|
||||
node = node.parentElement
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
function postProcessHtml(html: string): string {
|
||||
try {
|
||||
const parser = new DOMParser()
|
||||
const doc = parser.parseFromString(html, 'text/html')
|
||||
|
||||
const candidates = doc.querySelectorAll<HTMLElement>('[style*="color"], [color], font[color]')
|
||||
for (const el of candidates) {
|
||||
const fg = el.style.color || el.getAttribute('color') || ''
|
||||
if (!fg || !isNearWhite(fg)) continue
|
||||
|
||||
// Белый текст — проверяем фон в контексте элемента
|
||||
const bg = findNearestBg(el)
|
||||
// Если фон тоже светлый или не задан (дефолт белый) → убираем белый цвет
|
||||
if (!bg || isNearWhite(bg)) {
|
||||
if (el.style.color) el.style.removeProperty('color')
|
||||
el.removeAttribute('color')
|
||||
}
|
||||
}
|
||||
|
||||
return doc.documentElement.outerHTML
|
||||
} catch {
|
||||
return html
|
||||
}
|
||||
}
|
||||
|
||||
// ── Вычисленный HTML ───────────────────────────────────────
|
||||
|
||||
const processedHtml = computed(() => {
|
||||
if (!props.body.html) return null
|
||||
|
||||
let html = props.body.html
|
||||
|
||||
// Replace CID references with base64 data URLs
|
||||
// Подставляем inline-изображения
|
||||
for (const img of props.body.inlineImages) {
|
||||
html = html.replaceAll(
|
||||
`cid:${img.cid}`,
|
||||
`data:${img.contentType};base64,${img.data}`
|
||||
)
|
||||
html = html.replaceAll(`cid:${img.cid}`, `data:${img.contentType};base64,${img.data}`)
|
||||
}
|
||||
|
||||
// Wrap in a dark-themed document
|
||||
// Фиксируем белый текст на белом фоне
|
||||
html = postProcessHtml(html)
|
||||
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
|
@ -110,7 +169,7 @@ const plainTextHtml = computed(() => {
|
|||
|
||||
const srcdoc = computed(() => processedHtml.value ?? plainTextHtml.value ?? '')
|
||||
|
||||
// ── Ambient glow: извлекаем доминирующий цвет фона письма ──
|
||||
// ── 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.]+))?/)
|
||||
|
|
@ -150,14 +209,12 @@ function onIframeLoad(): void {
|
|||
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) {
|
||||
setContentGlow(bodyColor.r, bodyColor.g, bodyColor.b, 0.26)
|
||||
return
|
||||
}
|
||||
|
||||
// Многие HTML-письма кладут фон в первый <table> или <td>
|
||||
const firstEl = doc.body.querySelector<HTMLElement>('table, td, [bgcolor]')
|
||||
if (firstEl) {
|
||||
const raw = firstEl.getAttribute('bgcolor') ?? view.getComputedStyle(firstEl).backgroundColor
|
||||
|
|
@ -172,7 +229,6 @@ function onIframeLoad(): void {
|
|||
clearContentGlow()
|
||||
}
|
||||
|
||||
// Сбрасываем свечение при смене письма
|
||||
watch(() => props.body, () => clearContentGlow())
|
||||
|
||||
onUnmounted(() => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import type { MessageEnvelope } from '@shared/types/message'
|
||||
import { computed, ref, onMounted, onUnmounted } from 'vue'
|
||||
import type { MessageEnvelope, Address } from '@shared/types/message'
|
||||
import { formatFullDate } from '../../utils/date'
|
||||
import AppIcon from '../AppIcon.vue'
|
||||
|
||||
|
|
@ -8,43 +8,155 @@ const props = defineProps<{
|
|||
message: MessageEnvelope
|
||||
}>()
|
||||
|
||||
const fromDisplay = computed(() => {
|
||||
const f = props.message.from
|
||||
return f.name ? `${f.name} <${f.email}>` : f.email
|
||||
})
|
||||
|
||||
const toDisplay = computed(() =>
|
||||
props.message.to.map((a) => a.name || a.email).join(', ')
|
||||
)
|
||||
|
||||
const ccDisplay = computed(() =>
|
||||
props.message.cc.map((a) => a.name || a.email).join(', ')
|
||||
)
|
||||
|
||||
const dateDisplay = computed(() => formatFullDate(props.message.date))
|
||||
|
||||
// ── Contact popup ──────────────────────────────────────────
|
||||
|
||||
const activeContact = ref<Address | null>(null)
|
||||
const popupStyle = ref({ top: '0px', left: '0px' })
|
||||
const popupRef = ref<HTMLElement | null>(null)
|
||||
const copyDone = ref(false)
|
||||
|
||||
const POPUP_WIDTH = 272
|
||||
|
||||
function openContact(addr: Address, event: MouseEvent): void {
|
||||
// Если кликнули по уже открытому — закрываем
|
||||
if (activeContact.value?.email === addr.email) {
|
||||
activeContact.value = null
|
||||
return
|
||||
}
|
||||
|
||||
const btn = event.currentTarget as HTMLElement
|
||||
const header = btn.closest('.message-header') as HTMLElement
|
||||
const btnRect = btn.getBoundingClientRect()
|
||||
const headerRect = header.getBoundingClientRect()
|
||||
|
||||
let left = btnRect.left - headerRect.left
|
||||
// Не выходим за правый край
|
||||
left = Math.min(left, headerRect.width - POPUP_WIDTH - 8)
|
||||
|
||||
activeContact.value = addr
|
||||
popupStyle.value = {
|
||||
top: `${btnRect.bottom - headerRect.top + 6}px`,
|
||||
left: `${Math.max(0, left)}px`,
|
||||
}
|
||||
copyDone.value = false
|
||||
}
|
||||
|
||||
async function copyEmail(): Promise<void> {
|
||||
if (!activeContact.value) return
|
||||
await navigator.clipboard.writeText(activeContact.value.email)
|
||||
copyDone.value = true
|
||||
setTimeout(() => { copyDone.value = false }, 1800)
|
||||
}
|
||||
|
||||
function onDocMousedown(e: MouseEvent): void {
|
||||
if (popupRef.value && !popupRef.value.contains(e.target as Node)) {
|
||||
// Если клик внутри header — не закрываем здесь, openContact разберётся
|
||||
activeContact.value = null
|
||||
}
|
||||
}
|
||||
|
||||
function onKeydown(e: KeyboardEvent): void {
|
||||
if (e.key === 'Escape') activeContact.value = null
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener('mousedown', onDocMousedown)
|
||||
document.addEventListener('keydown', onKeydown)
|
||||
})
|
||||
onUnmounted(() => {
|
||||
document.removeEventListener('mousedown', onDocMousedown)
|
||||
document.removeEventListener('keydown', onKeydown)
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="message-header">
|
||||
<h2 class="subject">{{ message.subject }}</h2>
|
||||
|
||||
<div class="header-details">
|
||||
<!-- От -->
|
||||
<div class="header-row">
|
||||
<span class="label">От:</span>
|
||||
<span class="value">{{ fromDisplay }}</span>
|
||||
<span class="value">
|
||||
<button class="addr-chip" @click="openContact(message.from, $event)">
|
||||
<span class="addr-name">{{ message.from.name || message.from.email }}</span>
|
||||
<span v-if="message.from.name" class="addr-email-hint"><{{ message.from.email }}></span>
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- Кому -->
|
||||
<div class="header-row">
|
||||
<span class="label">Кому:</span>
|
||||
<span class="value">{{ toDisplay }}</span>
|
||||
<span class="value addr-list">
|
||||
<button
|
||||
v-for="addr in message.to"
|
||||
:key="addr.email"
|
||||
class="addr-chip"
|
||||
@click="openContact(addr, $event)"
|
||||
>
|
||||
<span class="addr-name">{{ addr.name || addr.email }}</span>
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
<div v-if="ccDisplay" class="header-row">
|
||||
|
||||
<!-- Копия -->
|
||||
<div v-if="message.cc.length" class="header-row">
|
||||
<span class="label">Копия:</span>
|
||||
<span class="value">{{ ccDisplay }}</span>
|
||||
<span class="value addr-list">
|
||||
<button
|
||||
v-for="addr in message.cc"
|
||||
:key="addr.email"
|
||||
class="addr-chip"
|
||||
@click="openContact(addr, $event)"
|
||||
>
|
||||
<span class="addr-name">{{ addr.name || addr.email }}</span>
|
||||
</button>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<!-- Дата -->
|
||||
<div class="header-row">
|
||||
<span class="label">Дата:</span>
|
||||
<span class="value">{{ dateDisplay }}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Мини-профиль контакта -->
|
||||
<Transition name="popup">
|
||||
<div
|
||||
v-if="activeContact"
|
||||
ref="popupRef"
|
||||
class="contact-popup"
|
||||
:style="popupStyle"
|
||||
>
|
||||
<div v-if="activeContact.name" class="popup-name">{{ activeContact.name }}</div>
|
||||
<div class="popup-email-row">
|
||||
<span class="popup-email">{{ activeContact.email }}</span>
|
||||
<button
|
||||
class="popup-copy"
|
||||
:class="{ done: copyDone }"
|
||||
:title="copyDone ? 'Скопировано!' : 'Скопировать email'"
|
||||
@click.stop="copyEmail"
|
||||
>
|
||||
<!-- Copy icon -->
|
||||
<svg v-if="!copyDone" width="13" height="13" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<rect x="9" y="9" width="13" height="13" rx="2" ry="2"/>
|
||||
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/>
|
||||
</svg>
|
||||
<!-- Checkmark icon -->
|
||||
<svg v-else width="13" height="13" viewBox="0 0 24 24" fill="none"
|
||||
stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round">
|
||||
<polyline points="20 6 9 17 4 12"/>
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</Transition>
|
||||
|
||||
<div class="header-badges">
|
||||
<span v-if="message.hasAttachments" class="badge">
|
||||
<AppIcon name="attachment" :size="11" /> Вложения
|
||||
|
|
@ -60,6 +172,7 @@ const dateDisplay = computed(() => formatFullDate(props.message.date))
|
|||
.message-header {
|
||||
padding: 22px 26px 18px;
|
||||
flex-shrink: 0;
|
||||
position: relative;
|
||||
background:
|
||||
linear-gradient(180deg, rgba(255,255,255,0.045) 0%, rgba(255,255,255,0.012) 100%),
|
||||
rgba(10, 10, 18, 0.5);
|
||||
|
|
@ -81,7 +194,6 @@ const dateDisplay = computed(() => formatFullDate(props.message.date))
|
|||
letter-spacing: -0.01em;
|
||||
}
|
||||
|
||||
/* Детали в стеклянной карточке */
|
||||
.header-details {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
|
@ -102,17 +214,12 @@ const dateDisplay = computed(() => formatFullDate(props.message.date))
|
|||
align-items: baseline;
|
||||
padding: 6px 12px;
|
||||
border-bottom: 1px solid rgba(255, 255, 255, 0.04);
|
||||
transition: background var(--transition-fast);
|
||||
}
|
||||
|
||||
.header-row:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.header-row:hover {
|
||||
background: rgba(255, 255, 255, 0.04);
|
||||
}
|
||||
|
||||
.label {
|
||||
color: var(--text-muted);
|
||||
min-width: 52px;
|
||||
|
|
@ -121,7 +228,7 @@ const dateDisplay = computed(() => formatFullDate(props.message.date))
|
|||
font-weight: 500;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.5px;
|
||||
padding-top: 1px;
|
||||
padding-top: 3px;
|
||||
}
|
||||
|
||||
.value {
|
||||
|
|
@ -131,7 +238,146 @@ const dateDisplay = computed(() => formatFullDate(props.message.date))
|
|||
flex: 1;
|
||||
}
|
||||
|
||||
/* Бейджи */
|
||||
/* Список адресов */
|
||||
.addr-list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 4px;
|
||||
align-items: center;
|
||||
padding: 1px 0;
|
||||
}
|
||||
|
||||
/* Кликабельный адрес */
|
||||
.addr-chip {
|
||||
display: inline-flex;
|
||||
align-items: baseline;
|
||||
gap: 4px;
|
||||
padding: 1px 7px 1px 6px;
|
||||
border-radius: 5px;
|
||||
font-size: 12px;
|
||||
color: var(--text-secondary);
|
||||
background: transparent;
|
||||
border: 1px solid transparent;
|
||||
cursor: pointer;
|
||||
transition:
|
||||
background var(--transition-fast),
|
||||
border-color var(--transition-fast),
|
||||
color var(--transition-fast);
|
||||
max-width: 100%;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.addr-chip:hover {
|
||||
background: rgba(255, 255, 255, 0.07);
|
||||
border-color: rgba(255, 255, 255, 0.10);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.addr-name {
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
max-width: 260px;
|
||||
}
|
||||
|
||||
.addr-email-hint {
|
||||
color: var(--text-muted);
|
||||
font-size: 11px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
/* ── Попап контакта ──────────────────────────────────────── */
|
||||
.contact-popup {
|
||||
position: absolute;
|
||||
width: 272px;
|
||||
background:
|
||||
linear-gradient(135deg, rgba(255,255,255,0.10) 0%, rgba(255,255,255,0.05) 100%);
|
||||
backdrop-filter: blur(32px) saturate(1.9);
|
||||
-webkit-backdrop-filter: blur(32px) saturate(1.9);
|
||||
border: 1px solid rgba(255, 255, 255, 0.14);
|
||||
border-radius: 10px;
|
||||
box-shadow:
|
||||
inset 0 1px 0 rgba(255, 255, 255, 0.18),
|
||||
0 8px 32px rgba(0, 0, 0, 0.40),
|
||||
0 2px 8px rgba(0, 0, 0, 0.25);
|
||||
padding: 12px 14px;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.popup-name {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
margin-bottom: 6px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.popup-email-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.popup-email {
|
||||
flex: 1;
|
||||
font-size: 11.5px;
|
||||
color: var(--text-muted);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
user-select: text;
|
||||
-webkit-user-select: text;
|
||||
}
|
||||
|
||||
.popup-copy {
|
||||
flex-shrink: 0;
|
||||
width: 26px;
|
||||
height: 26px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border-radius: 6px;
|
||||
color: var(--text-muted);
|
||||
background: rgba(255, 255, 255, 0.06);
|
||||
border: 1px solid rgba(255, 255, 255, 0.09);
|
||||
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
|
||||
transition:
|
||||
background var(--transition-fast),
|
||||
color var(--transition-fast),
|
||||
border-color var(--transition-fast);
|
||||
}
|
||||
|
||||
.popup-copy:hover {
|
||||
background: rgba(255, 255, 255, 0.12);
|
||||
border-color: rgba(255, 255, 255, 0.18);
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.popup-copy.done {
|
||||
color: var(--accent);
|
||||
background: color-mix(in srgb, var(--accent) 12%, transparent);
|
||||
border-color: color-mix(in srgb, var(--accent) 28%, transparent);
|
||||
}
|
||||
|
||||
/* ── Анимация попапа ─────────────────────────────────────── */
|
||||
.popup-enter-active {
|
||||
transition: opacity 130ms ease, transform 160ms cubic-bezier(0.34, 1.2, 0.64, 1);
|
||||
}
|
||||
.popup-leave-active {
|
||||
transition: opacity 100ms ease, transform 110ms ease;
|
||||
}
|
||||
.popup-enter-from {
|
||||
opacity: 0;
|
||||
transform: translateY(-5px) scale(0.97);
|
||||
}
|
||||
.popup-leave-to {
|
||||
opacity: 0;
|
||||
transform: translateY(-3px) scale(0.97);
|
||||
}
|
||||
|
||||
/* ── Бейджи ──────────────────────────────────────────────── */
|
||||
.header-badges {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
|
|
|
|||
Loading…
Reference in a new issue