Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 3x 3x 3x 3x 3x 792x 792x 264x 264x 264x 264x 132x 132x 66x 40x 120x 40x 26x 26x 26x 26x 26x 25x 26x 26x 26x 3x | // @implements UC-SPACE-007.1, UC-SPACE-008.1
/**
* The frame of an email in a flow's look (docs/architecture/FLOW_SPACE.md
* "Emails in the flow's colours"): the flow's name in a band of its primary
* colour, an accent line in its secondary colour, the body on its background
* colour. Text colours are not chosen: dark or white, whichever reads best,
* as on the public pages (flows-ui utils/spaceTheme.ts).
*
* No logo: the flow's images are private S3 objects, and a presigned URL
* expires long before an email is read.
*/
const { escapeHtml } = require('../email')
/** Same defaults as flows-ui DEFAULT_SPACE_THEME */
const DEFAULT_THEME = {
primaryColor: '#689909',
secondaryColor: '#bee476',
backgroundColor: '#ffffff'
}
const COLOR_PATTERN = /^#[0-9a-f]{6}$/i
const DARK_TEXT = '#212529'
const LIGHT_TEXT = '#ffffff'
function channel(value) {
const c = value / 255
return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
}
/** WCAG relative luminance of a #rrggbb color */
function relativeLuminance(hex) {
const r = Number.parseInt(hex.slice(1, 3), 16)
const g = Number.parseInt(hex.slice(3, 5), 16)
const b = Number.parseInt(hex.slice(5, 7), 16)
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b)
}
function contrastRatio(a, b) {
const [light, dark] = [relativeLuminance(a), relativeLuminance(b)].sort((x, y) => y - x)
return (light + 0.05) / (dark + 0.05)
}
/** The text color (dark or white) that reads best on a background */
function readableTextColor(background) {
return contrastRatio(background, DARK_TEXT) >= contrastRatio(background, LIGHT_TEXT)
? DARK_TEXT
: LIGHT_TEXT
}
/** The flow's theme, every missing or malformed colour replaced by the default */
function resolveTheme(theme) {
const pick = (key) =>
typeof theme?.[key] === 'string' && COLOR_PATTERN.test(theme[key])
? theme[key].toLowerCase()
: DEFAULT_THEME[key]
return {
primaryColor: pick('primaryColor'),
secondaryColor: pick('secondaryColor'),
backgroundColor: pick('backgroundColor')
}
}
/**
* @param {object} params
* @param {{ name?: string, space?: { theme?: object } }} params.flow
* @param {string} params.title - Plain text, escaped here
* @param {string[]} params.paragraphs - Plain text, escaped here
* @param {string} [params.highlightHtml] - Trusted HTML shown after the paragraphs
* (the verification code box)
* @param {{ label: string, url: string }} [params.action] - The button
* @param {string} [params.footer] - Plain text, escaped here
* @returns {string} the HTML body
*/
function renderFlowEmailHtml({ flow, title, paragraphs, highlightHtml, action, footer }) {
const theme = resolveTheme(flow?.space?.theme)
const onPrimary = readableTextColor(theme.primaryColor)
const onBackground = readableTextColor(theme.backgroundColor)
const flowName = escapeHtml(flow?.name || '')
const body = paragraphs
.map(
(text) =>
`<p style="color: ${onBackground}; line-height: 1.6; margin: 0 0 16px;">${escapeHtml(text)}</p>`
)
.join('\n ')
const button = action
? `<p style="margin: 24px 0;"><a href="${escapeHtml(action.url)}" style="background-color: ${theme.primaryColor}; color: ${onPrimary}; padding: 12px 20px; border-radius: 6px; text-decoration: none; font-weight: bold; display: inline-block;">${escapeHtml(action.label)}</a></p>`
: ''
const footerHtml = footer
? `<p style="color: ${onBackground}; opacity: 0.8; font-size: 13px; line-height: 1.6; border-top: 1px solid #e2e8f0; padding-top: 16px; margin: 24px 0 0;">${escapeHtml(footer)}</p>`
: ''
return `
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; background-color: #f9f9f9;">
<div style="background-color: ${theme.primaryColor}; color: ${onPrimary}; border-radius: 10px 10px 0 0; padding: 20px; font-size: 22px; font-weight: bold;">${flowName}</div>
<div style="height: 4px; background-color: ${theme.secondaryColor};"></div>
<div style="background-color: ${theme.backgroundColor}; border-radius: 0 0 10px 10px; padding: 30px 20px;">
<h1 style="color: ${onBackground}; margin: 0 0 20px; font-size: 22px;">${escapeHtml(title)}</h1>
${body}
${highlightHtml || ''}
${button}
${footerHtml}
</div>
</div>`
}
module.exports = { DEFAULT_THEME, readableTextColor, resolveTheme, renderFlowEmailHtml }
|