Skip to content

Commit

Permalink
fix fix fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Feb 12, 2024
1 parent be7aac4 commit 68bf460
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
48 changes: 27 additions & 21 deletions node/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,7 @@ export async function pdf(url, pdf_path, options, screenshot_dir, screenshot_opt
height: 1000,
})

while (true) {
const queued = await page.evaluate(`Array.from(document.getElementsByClassName('queued')).map(x => x.id)`)
const running = await page.evaluate(`Array.from(document.getElementsByClassName('running')).map(x => x.id)`)
const cells = await page.evaluate(`Array.from(document.getElementsByTagName('pluto-cell')).map(x => x.id)`)
const bodyClasses = await page.evaluate(`document.body.getAttribute('class')`)

if (running.length > 0) {
process.stdout.write(`\rRunning cell ${chalk.yellow(`${cells.length - queued.length}/${cells.length}`)} ${chalk.cyan(`[${running[0]}]`)}`)
}

if (!(bodyClasses.includes("loading") || queued.length > 0 || running.length > 0)) {
process.stdout.write(`\rRunning cell ${chalk.yellow(`${cells.length}/${cells.length}`)}`)
console.log()
break
}

await sleep(250)
}
await waitForPlutoBusy(page, false, { timeout: 30 * 1000 })

console.log("Exporting as pdf...")
await page.pdf({
Expand All @@ -63,18 +46,41 @@ export async function pdf(url, pdf_path, options, screenshot_dir, screenshot_opt
* @param {p.Page} page
* @param {string} screenshot_dir
*/
async function screenshot_cells(page, screenshot_dir, { outputOnly, dpi }) {
async function screenshot_cells(page, screenshot_dir, { outputOnly, scale }) {
const cells = /** @type {String[]} */ (await page.evaluate(`Array.from(document.querySelectorAll('pluto-cell')).map(x => x.id)`))

for (let cell_id of cells) {
const cell = await page.$(`#${cell_id}`)
const cell = await page.$(`[id="${cell_id}"]${outputOnly ? " > pluto-output" : ""}`)
if (cell) {
await cell.scrollIntoView()
const rect = await cell.boundingBox()
if (rect == null) {
throw new Error(`Cell ${cell_id} is not visible`)
}
const imgpath = path.join(screenshot_dir, `${cell_id}.png`)

await cell.screenshot({ path: imgpath, clip: rect, omitBackground: false })
await cell.screenshot({ path: imgpath, clip: { ...rect, scale }, omitBackground: false })
console.log(`Screenshot ${cell_id} saved to ${imgpath}`)
}
}
}

const waitForPlutoBusy = async (page, iWantBusiness, options) => {
await page.waitForTimeout(1)
await page.waitForFunction(
(iWantBusiness) => {
let quiet = //@ts-ignore
document?.body?._update_is_ongoing === false &&
//@ts-ignore
document?.body?._js_init_set?.size === 0 &&
document?.body?.classList?.contains("loading") === false &&
document?.querySelector(`#process-status-tab-button.something_is_happening`) == null &&
document?.querySelector(`pluto-cell.running, pluto-cell.queued, pluto-cell.internal_test_queued`) == null

return iWantBusiness ? !quiet : quiet
},
options,
iWantBusiness
)
await page.waitForTimeout(1)
}
2 changes: 1 addition & 1 deletion src/PlutoPDF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const default_options = (

const screenshot_default_options = (
outputOnly=false,
dpi=72,
scale=2,
)

function html_to_pdf(
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ result = pluto_to_pdf(testfile, outfile, outdir; open=is_CI, options=(format="A5

@test isdir(outdir)
filez = readdir(outdir)
@test length(filez) == 32
@test length(filez) == 28
@test all(endswith.(filez, ".png"))
@test length(read(filez[1])) > 1000

Expand Down

0 comments on commit 68bf460

Please sign in to comment.