-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailView.svelte
More file actions
212 lines (191 loc) · 6.57 KB
/
Copy pathEmailView.svelte
File metadata and controls
212 lines (191 loc) · 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<script>
import { run } from 'svelte/legacy'
import { onMount } from 'svelte'
import * as email from './email'
import { emails, currSelected } from './../fallback/stores.js'
import { _, locale } from 'svelte-i18n'
import Icon from '@iconify/svelte'
let parsed = $state()
let date = $state()
let isDark = $state(false)
onMount(() => {
const html = document.documentElement
isDark = html.classList.contains('dark')
const obs = new MutationObserver(() => {
isDark = html.classList.contains('dark')
})
obs.observe(html, { attributes: true, attributeFilter: ['class'] })
return () => obs.disconnect()
})
run(() => {
if ($currSelected >= 0) {
const mail = $emails.find((e) => e.id === $currSelected)
if (mail) {
date = new Date(mail.date)
email.parseMail(mail.raw).then((x) => {
parsed = x
})
}
}
})
function escapeHtml(s) {
return s
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
}
// HTML emails carry their own design — leave their styling untouched.
// Plain-text emails have no styling of their own, so mirror the site
// theme so the text stays legible when dark mode is toggled.
let bodyDoc = $derived.by(() => {
if (!parsed) return ''
if (parsed.html) {
return `<!doctype html><html><body style="margin:1rem">${parsed.html}</body></html>`
}
const bg = isDark ? '#061b2d' : '#ffffff'
const fg = isDark ? '#ffffff' : '#030e17'
const scheme = isDark ? 'dark' : 'light'
const text = escapeHtml(parsed.text ?? '')
const bodyStyle = `margin:1rem;background:${bg};color:${fg};color-scheme:${scheme};font-family:system-ui,-apple-system,sans-serif;font-size:14px;line-height:1.5`
const preStyle =
'margin:0;white-space:pre-wrap;word-break:break-word;font-family:inherit'
return `<!doctype html><html><body style="${bodyStyle}"><pre style="${preStyle}">${text}</pre></body></html>`
})
</script>
{#if parsed}
<div class="email-view">
<div class="email-header">
{#if parsed.from}
<div class="header-row">
<span class="header-label"
>{$_('fallback.email.from')}:</span
>
<span class="header-value">
{#if parsed.from.name}{parsed.from.name} <{parsed
.from.address}>{:else}{parsed.from
.address}{/if}
</span>
</div>
{/if}
{#if parsed.to && parsed.to.length > 0}
<div class="header-row">
<span class="header-label">{$_('fallback.email.to')}:</span>
<span class="header-value">
{#each parsed.to as { name, address } (address)}
{#if name}{name} <{address}>{:else}{address}{/if}
{/each}
</span>
</div>
{/if}
{#if parsed.subject}
<div class="header-row">
<span class="header-label"
>{$_('fallback.email.subject')}:</span
>
<span class="header-value">{parsed.subject}</span>
</div>
{/if}
{#if date && !isNaN(date.getTime())}
<div class="header-row">
<span class="header-label"
>{$_('fallback.email.date')}:</span
>
<span class="header-value"
>{date.toLocaleString($locale)}</span
>
</div>
{/if}
</div>
<div class="email-body">
<iframe srcdoc={bodyDoc} title="Mail message" sandbox=""></iframe>
</div>
{#if parsed.attachments && parsed.attachments.length > 0}
<div class="email-attachments">
{#each parsed.attachments as att (att.filename)}
<button
class="attachment-chip"
onclick={(e) => {
e.preventDefault()
email.downloadAttachment(
att.content,
att.mimeType,
att.filename
)
}}
type="button"
>
<Icon icon="mdi:attachment" width="16px" />
{att.filename}
</button>
{/each}
</div>
{/if}
</div>
{/if}
<style lang="scss">
.email-view {
height: 100%;
display: flex;
flex-direction: column;
}
.email-header {
display: flex;
flex-direction: column;
padding: 0.75rem 1rem;
border-bottom: 1px solid var(--pg-input-normal);
}
.header-row {
display: flex;
gap: 0.5rem;
padding: 0.1rem 0;
font-size: var(--pg-font-size-sm);
line-height: 1.3;
}
.header-label {
font-weight: var(--pg-font-weight-bold);
white-space: nowrap;
flex-shrink: 0;
}
.header-value {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.email-body {
flex: 1;
min-height: 0;
iframe {
border: none;
width: 100%;
height: 100%;
}
}
.email-attachments {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
padding: 0.75rem 1rem;
border-top: 1px solid var(--pg-input-normal);
}
.attachment-chip {
all: unset;
cursor: pointer;
display: inline-flex;
align-items: center;
gap: 0.35rem;
padding: 0.3rem 0.75rem;
font-size: var(--pg-font-size-sm);
background: var(--pg-general-background);
border: 1px solid var(--pg-input-normal);
border-radius: var(--pg-border-radius-md);
transition: all 0.2s ease;
&:hover {
border-color: var(--pg-primary);
color: var(--pg-primary);
}
&:focus-visible {
outline: 2px solid var(--pg-primary);
outline-offset: 2px;
}
}
</style>