This commit is contained in:
lowlighter
2022-01-20 23:54:16 -05:00
7 changed files with 61 additions and 271 deletions

View File

@@ -435,41 +435,45 @@ export const svg = {
console.debug("metrics/svg/resize > resizing svg")
let height, resized, width
try {
({resized, width, height} = await page.evaluate(async (padding, js) => {
//Execute user JavaScript if provided
if (js) {
try {
console.debug(`metrics/svg/resize > executing ${js}`)
await new Function("document", `return (async () => {${js}})()`)(document) //eslint-disable-line no-new-func
console.debug("metrics/svg/resize > successfully executed user javascript")
({resized, width, height} = await page.evaluate(
async (padding, js) => {
//Execute user JavaScript if provided
if (js) {
try {
console.debug(`metrics/svg/resize > executing ${js}`)
await new Function("document", `return (async () => {${js}})()`)(document) //eslint-disable-line no-new-func
console.debug("metrics/svg/resize > successfully executed user javascript")
}
catch (error) {
console.debug(`an error occured while evaluating user js: ${error}`)
}
}
catch (error) {
console.debug(`an error occured while evaluating user js: ${error}`)
}
}
//Disable animations
const animated = !document.querySelector("svg").classList.contains("no-animations")
if (animated)
document.querySelector("svg").classList.add("no-animations")
console.debug(`animations are ${animated ? "enabled" : "disabled"}`)
await new Promise(solve => setTimeout(solve, 2400))
//Get bounds and resize
let {y:height, width} = document.querySelector("svg #metrics-end").getBoundingClientRect()
console.debug(`bounds width=${width}, height=${height}`)
height = Math.ceil(height * padding.height + padding.absolute.height)
width = Math.ceil(width * padding.width + padding.absolute.width)
console.debug(`bounds after applying padding width=${width} (*${padding.width}+${padding.absolute.width}), height=${height} (*${padding.height}+${padding.absolute.height})`)
//Resize svg
if (document.querySelector("svg").getAttribute("height") === "auto")
console.debug('skipped height resizing because it was set to "auto"')
else
document.querySelector("svg").setAttribute("height", height)
//Enable animations
if (animated)
document.querySelector("svg").classList.remove("no-animations")
//Result
return {resized:new XMLSerializer().serializeToString(document.querySelector("svg")), height, width}
}, padding, js))
//Disable animations
const animated = !document.querySelector("svg").classList.contains("no-animations")
if (animated)
document.querySelector("svg").classList.add("no-animations")
console.debug(`animations are ${animated ? "enabled" : "disabled"}`)
await new Promise(solve => setTimeout(solve, 2400))
//Get bounds and resize
let {y:height, width} = document.querySelector("svg #metrics-end").getBoundingClientRect()
console.debug(`bounds width=${width}, height=${height}`)
height = Math.ceil(height * padding.height + padding.absolute.height)
width = Math.ceil(width * padding.width + padding.absolute.width)
console.debug(`bounds after applying padding width=${width} (*${padding.width}+${padding.absolute.width}), height=${height} (*${padding.height}+${padding.absolute.height})`)
//Resize svg
if (document.querySelector("svg").getAttribute("height") === "auto")
console.debug('skipped height resizing because it was set to "auto"')
else
document.querySelector("svg").setAttribute("height", height)
//Enable animations
if (animated)
document.querySelector("svg").classList.remove("no-animations")
//Result
return {resized:new XMLSerializer().serializeToString(document.querySelector("svg")), height, width}
},
padding,
js,
))
}
catch (error) {
console.error(error)

View File

@@ -557,6 +557,21 @@ Useful to avoid creating a new template just to tweak some styling</p>
<tr>
<td nowrap="nowrap">🌐 Web instances must configure <code>settings.json</code><br>
<b>type:</b> <code>string</code>
<br></td>
</tr>
<tr>
<td nowrap="nowrap"><code>extras_js</code></td>
<td rowspan="2"><p>Extra JavaScript</p>
<p>Custom JavaScript that will be executed during puppeteer rendering.
Useful to avoid creating a new template just to tweak some content.</p>
<p>Note that is it executed within puppeteer context and <strong>not</strong> <em>metrics</em> context.
It is run after transformations and optimizations, but just before resizing.</p>
<img width="900" height="1" alt=""></td>
</tr>
<tr>
<td nowrap="nowrap">✨ On <code>master</code>/<code>main</code><br>
🌐 Web instances must configure <code>settings.json</code><br>
<b>type:</b> <code>string</code>
<br></td>
</tr>
<tr>