badgen/lib/index.js

61 wiersze
2.8 KiB
JavaScript
Czysty Zwykły widok Historia

2018-07-10 17:51:32 +00:00
const calcWidth = require('./calc-text-width.js').Verdana11
2018-06-22 17:42:12 +00:00
const colorPresets = require('./color-presets.js')
2018-07-20 02:55:51 +00:00
module.exports = function ({subject, status, color, style, emoji, icon}) {
typeAssert(typeof subject === 'string', '<subject> must be string')
typeAssert(typeof status === 'string', '<status> must be string')
2018-07-01 08:13:34 +00:00
color = colorPresets[color] || color || colorPresets['blue']
2018-05-29 08:53:54 +00:00
2018-07-18 16:50:28 +00:00
const stTextWidth = calcWidth(status, emoji)
const sbRectWidth = calcWidth(subject, emoji) + 10 + (icon ? 15 : 0)
2018-07-18 16:35:09 +00:00
const stRectWidth = stTextWidth + 10
2018-05-29 08:53:54 +00:00
const width = sbRectWidth + stRectWidth
2018-07-15 09:22:56 +00:00
if (style === 'flat') {
return `
2018-07-20 02:55:51 +00:00
<svg width="${width}" height="20" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
2018-07-15 09:22:56 +00:00
<g>
<rect width="${sbRectWidth}" height="20" fill="#555"/>
<rect x="${sbRectWidth}" width="${stRectWidth}" height="20" fill="#${color}"/>
</g>
<g fill="#fff" text-anchor="start" font-family="Verdana,DejaVu Sans,sans-serif" font-size="11">
<text x="${icon ? '21.6' : '6'}" y="14.8" fill="#000" opacity="0.1">${subject}</text>
<text x="${icon ? '20.6' : '5'}" y="13.8">${subject}</text>
2018-07-18 16:35:09 +00:00
<text x="${sbRectWidth + 5.5}" y="14.8" fill="#000" opacity="0.1" textLength="${stTextWidth}">${status}</text>
<text x="${sbRectWidth + 4.5}" y="13.8" textLength="${stTextWidth}">${status}</text>
2018-07-20 02:55:51 +00:00
</g>${icon ? genIconMarkup(icon) : ''}
2018-07-15 09:22:56 +00:00
</svg>
`
}
2018-05-29 08:53:54 +00:00
return `
2018-07-20 02:55:51 +00:00
<svg width="${width}" height="20" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
2018-05-29 10:13:52 +00:00
<linearGradient id="a" x2="0" y2="100%">
2018-07-16 14:10:56 +00:00
<stop offset="0" stop-color="#EEE" stop-opacity=".1"/>
2018-05-29 08:53:54 +00:00
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
2018-07-15 09:22:56 +00:00
<mask id="m"><rect width="${width}" height="20" rx="3" fill="#FFF"/></mask>
2018-07-14 18:09:01 +00:00
<g mask="url(#m)">
<rect width="${sbRectWidth}" height="20" fill="#555"/>
<rect x="${sbRectWidth}" width="${stRectWidth}" height="20" fill="#${color}"/>
<rect width="${width}" height="20" fill="url(#a)"/>
</g>
<g fill="#fff" text-anchor="start" font-family="Verdana,DejaVu Sans,sans-serif" font-size="11">
<text x="${icon ? '21.6' : '6'}" y="14.8" fill="#000" opacity="0.25">${subject}</text>
<text x="${icon ? '20.6' : '5'}" y="13.8">${subject}</text>
2018-07-18 16:35:09 +00:00
<text x="${sbRectWidth + 5.5}" y="14.8" fill="#000" opacity="0.25" textLength="${stTextWidth}">${status}</text>
<text x="${sbRectWidth + 4.5}" y="13.8" textLength="${stTextWidth}">${status}</text>
2018-07-20 02:55:51 +00:00
</g>${icon ? genIconMarkup(icon) : ''}
2018-05-29 08:53:54 +00:00
</svg>
`
}
2018-07-20 02:55:51 +00:00
function genIconMarkup (iconB64) {
return `<image x="3.8" y="3.4" width="13.2" height="13.2" xlink:href="${iconB64}"/>`
2018-07-20 02:55:51 +00:00
}
function typeAssert (assertion, message) {
if (!assertion) throw new TypeError(message)
}