Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

Export an explainer page as a long PNG image; label print as Print / PDF

tt-a1i0ac268674598
diff --git a/archify/explainer/reader.js b/archify/explainer/reader.js
index e13efc37..ebd8ee9d 100644
--- a/archify/explainer/reader.js
+++ b/archify/explainer/reader.js
@@ -210,6 +210,39 @@
all('[data-theme-choice]').forEach(button=>button.addEventListener('click',()=>{theme = button.dataset.themeChoice; applyTheme();}));
preference.addEventListener?.('change',applyTheme);
document.querySelector('[data-print]').addEventListener('click',()=>window.print());
+ // Long image: the page as laid out now, drawn through an SVG foreignObject at twice the pixel
+ // density. Controls, dialogs and walk-through bars are left out. Browsers that refuse to export a
+ // foreignObject canvas (Safari) get a notice pointing to Print / PDF.
+ async function pageImage() {
+ const shell = document.querySelector('.shell'), width = document.documentElement.clientWidth, height = Math.ceil(shell.getBoundingClientRect().height + 48);
+ const clone = root.cloneNode(true);
+ clone.querySelectorAll('script,dialog,#toast,.skip-link,.tools,.ask-button,.ask-floating,.walk-bar,.walk-start,.interactive,.diagram-fit-status').forEach(element=>element.remove());
+ clone.querySelectorAll('.reader-highlight').forEach(element=>element.classList.remove('reader-highlight'));
+ clone.querySelectorAll('.focusing').forEach(element=>element.classList.remove('focusing'));
+ clone.style.width = `${width}px`;
+ const xhtml = new XMLSerializer().serializeToString(clone);
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}"><foreignObject x="0" y="0" width="${width}" height="${height}">${xhtml}</foreignObject></svg>`;
+ const image = new Image();
+ image.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
+ await image.decode();
+ const scale = Math.min(2,16000 / height), canvas = document.createElement('canvas');
+ canvas.width = Math.round(width * scale); canvas.height = Math.round(height * scale);
+ const context = canvas.getContext('2d');
+ context.fillStyle = getComputedStyle(document.body).backgroundColor; context.fillRect(0,0,canvas.width,canvas.height);
+ context.drawImage(image,0,0,canvas.width,canvas.height);
+ return new Promise((resolve,reject)=>canvas.toBlob(blob=>blob ? resolve(blob) : reject(new Error('empty')),'image/png'));
+ }
+ const imageButton = document.querySelector('[data-export-image]');
+ imageButton?.addEventListener('click',async()=>{
+ imageButton.disabled = true;
+ try {
+ const blob = await pageImage(), link = document.createElement('a');
+ link.href = URL.createObjectURL(blob); link.download = `${(document.querySelector('h1')?.textContent || 'explainer').trim().replace(/[\\/:*?"<>|]+/g,' ').slice(0,80)}.png`;
+ document.body.append(link); link.click(); link.remove(); setTimeout(()=>URL.revokeObjectURL(link.href),4000);
+ } catch { notify(say('This browser cannot export the page as an image. Use Print / PDF instead.','当前浏览器无法导出长图,请用"打印 / PDF"。')); }
+ finally { imageButton.disabled = false; }
+ });
+ window.ArchifyExplainer = {pageImage};
// Only the isolated srcdoc copy receives this bridge. The payload remains byte-for-byte native.
const viewerDialog = byId('diagram-dialog');
let viewer = null;
diff --git a/archify/explainer/render.mjs b/archify/explainer/render.mjs
index 0662548d..53890ca0 100644
--- a/archify/explainer/render.mjs
+++ b/archify/explainer/render.mjs
@@ -13,8 +13,8 @@ const escape = value => String(value ?? '').replace(/[&<>"']/g, character => ({'
const json = value => JSON.stringify(value).replace(/[<>&\u2028\u2029]/g, character => `\\u${character.charCodeAt(0).toString(16).padStart(4, '0')}`);
const hash = text => createHash('sha256').update(text).digest('hex');
const dictionaries = {
- en:{answer:'Answer',answerMark:'A',scope:'Applies to',sources:'Sources',print:'Print',auto:'System',light:'Light',dark:'Dark',typical:'Typical value',explore:'Explore diagram',close:'Close',loading:'Opening diagram…',graph:'Diagram structure',changes:'Structure changes',added:'Added',removed:'Removed',modified:'Modified',copy:'Copy',copied:'Copied',walk:'Step through',walkMessages:'Play messages',prev:'Back',next:'Next',end:'Done',ask:'Ask',askQuote:'Ask about this',askPlaceholder:'What is unclear, or what should this part cover?',askCopy:'Copy question',askCopied:'Copied. Paste it to your agent.',askCancel:'Cancel',askChips:['I do not follow this part','Go deeper','Give an example','What is this based on?'],askPage:'Page',askFile:'File',askWhere:'Where',askQuoteLabel:'Quote',askQuestion:'Question',askPatch:'Revise only this panel with Archify panel replacement (patch explainer), keeping its ID.'},
- zh:{answer:'结论',answerMark:'答',scope:'适用于',sources:'来源',print:'打印',auto:'系统',light:'浅色',dark:'深色',typical:'典型值',explore:'展开探索',close:'关闭',loading:'正在打开交互图…',graph:'图的结构',changes:'结构改动',added:'新增',removed:'删除',modified:'修改',copy:'复制',copied:'已复制',walk:'逐步看',walkMessages:'逐条看消息',prev:'上一步',next:'下一步',end:'结束',ask:'追问',askQuote:'追问这段',askPlaceholder:'哪里没看懂,或者希望这块补充什么?',askCopy:'复制追问',askCopied:'已复制,粘贴给 agent 即可',askCancel:'取消',askChips:['没看懂这块','展开讲讲','举个例子','依据是什么?'],askPage:'页面',askFile:'文件',askWhere:'位置',askQuoteLabel:'引用',askQuestion:'问题',askPatch:'请用 Archify 的面板替换(patch explainer)只更新这个面板,保留它的 ID。'}
+ en:{answer:'Answer',answerMark:'A',scope:'Applies to',sources:'Sources',print:'Print / PDF',image:'Image',auto:'System',light:'Light',dark:'Dark',typical:'Typical value',explore:'Explore diagram',close:'Close',loading:'Opening diagram…',graph:'Diagram structure',changes:'Structure changes',added:'Added',removed:'Removed',modified:'Modified',copy:'Copy',copied:'Copied',walk:'Step through',walkMessages:'Play messages',prev:'Back',next:'Next',end:'Done',ask:'Ask',askQuote:'Ask about this',askPlaceholder:'What is unclear, or what should this part cover?',askCopy:'Copy question',askCopied:'Copied. Paste it to your agent.',askCancel:'Cancel',askChips:['I do not follow this part','Go deeper','Give an example','What is this based on?'],askPage:'Page',askFile:'File',askWhere:'Where',askQuoteLabel:'Quote',askQuestion:'Question',askPatch:'Revise only this panel with Archify panel replacement (patch explainer), keeping its ID.'},
+ zh:{answer:'结论',answerMark:'答',scope:'适用于',sources:'来源',print:'打印 / PDF',image:'长图',auto:'系统',light:'浅色',dark:'深色',typical:'典型值',explore:'展开探索',close:'关闭',loading:'正在打开交互图…',graph:'图的结构',changes:'结构改动',added:'新增',removed:'删除',modified:'修改',copy:'复制',copied:'已复制',walk:'逐步看',walkMessages:'逐条看消息',prev:'上一步',next:'下一步',end:'结束',ask:'追问',askQuote:'追问这段',askPlaceholder:'哪里没看懂,或者希望这块补充什么?',askCopy:'复制追问',askCopied:'已复制,粘贴给 agent 即可',askCancel:'取消',askChips:['没看懂这块','展开讲讲','举个例子','依据是什么?'],askPage:'页面',askFile:'文件',askWhere:'位置',askQuoteLabel:'引用',askQuestion:'问题',askPatch:'请用 Archify 的面板替换(patch explainer)只更新这个面板,保留它的 ID。'}
};
/** Render a validated canonical explainer with separately prepared, immutable resources. */
@@ -97,7 +97,7 @@ export function renderExplainer(document, options = {}) {
manifest.dependencies = (bundle.dependencies || []).map(({contentBase64,...entry})=>entry);
if (!preparedBundle) bundle.digest = hash(JSON.stringify(bundle));
if (bundle.sourceText !== sourceText || JSON.stringify(bundle.document) !== JSON.stringify(document)) throw new Error('Prepared bundle differs from rendered source or canonical document.');
- return `<!doctype html>\n<html lang="${escape(locale)}" data-archify-artifact="explainer" data-artifact="${escape(document.meta.id)}" data-revision="${revision}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="color-scheme" content="light dark"><meta name="description" content="${escape(document.answer.summary)}"><title>${escape(document.meta.title)} · Archify</title><style>${css}</style></head><body><a class="skip-link" href="#board">${locale.startsWith('en')?'Skip to content':'跳至正文'}</a><div class="shell"><header class="masthead"><h1>${escape(document.meta.title)}</h1><div class="tools interactive"><div class="theme-controls" role="group" aria-label="${locale.startsWith('en')?'Theme':'主题'}">${['auto','light','dark'].map(theme=>`<button type="button" data-theme-choice="${theme}" aria-pressed="${theme === 'auto'}">${words[theme]}</button>`).join('')}</div><button type="button" data-print>${words.print}</button></div></header><main id="board"><div class="board">${panels}</div></main><footer>${footnotesHtml}<section class="sources"><h2>${words.sources}</h2><ol>${sourceList}</ol></section><p class="colophon">Archify</p></footer></div><dialog class="viewer-dialog" id="diagram-dialog" aria-labelledby="diagram-dialog-title"><header><h2 id="diagram-dialog-title"></h2><button type="button" data-close-diagram>${words.close} ×</button></header><p class="dialog-loading" role="status">${words.loading}</p></dialog><div id="toast" role="status" aria-live="polite"></div><script type="application/json" id="archify-explainer-prepared">${json(packPageBundle(bundle))}</script><script type="application/json" id="explainer-manifest">${json(manifest)}</script><script type="application/json" id="explainer-reader-words">${json(words)}</script><script>${js}</script></body></html>`;
+ return `<!doctype html>\n<html lang="${escape(locale)}" data-archify-artifact="explainer" data-artifact="${escape(document.meta.id)}" data-revision="${revision}"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="color-scheme" content="light dark"><meta name="description" content="${escape(document.answer.summary)}"><title>${escape(document.meta.title)} · Archify</title><style>${css}</style></head><body><a class="skip-link" href="#board">${locale.startsWith('en')?'Skip to content':'跳至正文'}</a><div class="shell"><header class="masthead"><h1>${escape(document.meta.title)}</h1><div class="tools interactive"><div class="theme-controls" role="group" aria-label="${locale.startsWith('en')?'Theme':'主题'}">${['auto','light','dark'].map(theme=>`<button type="button" data-theme-choice="${theme}" aria-pressed="${theme === 'auto'}">${words[theme]}</button>`).join('')}</div><button type="button" data-export-image>${words.image}</button><button type="button" data-print>${words.print}</button></div></header><main id="board"><div class="board">${panels}</div></main><footer>${footnotesHtml}<section class="sources"><h2>${words.sources}</h2><ol>${sourceList}</ol></section><p class="colophon">Archify</p></footer></div><dialog class="viewer-dialog" id="diagram-dialog" aria-labelledby="diagram-dialog-title"><header><h2 id="diagram-dialog-title"></h2><button type="button" data-close-diagram>${words.close} ×</button></header><p class="dialog-loading" role="status">${words.loading}</p></dialog><div id="toast" role="status" aria-live="polite"></div><script type="application/json" id="archify-explainer-prepared">${json(packPageBundle(bundle))}</script><script type="application/json" id="explainer-manifest">${json(manifest)}</script><script type="application/json" id="explainer-reader-words">${json(words)}</script><script>${js}</script></body></html>`;
}
// Prefix native SVG identifiers in the preview only. Original native HTML is frozen above.
diff --git a/test/explainer-browser.test.mjs b/test/explainer-browser.test.mjs
index 29a0c1af..921bf695 100644
--- a/test/explainer-browser.test.mjs
+++ b/test/explainer-browser.test.mjs
@@ -135,5 +135,7 @@ test('text names light their diagram nodes and walk-throughs step through steps
assert.match(result.copied,/Question: I do not follow this part/);
assert.match(result.copied,/patch explainer/);
assert.equal(result.open,false);
+ const image=await pageValue(browser,`window.ArchifyExplainer.pageImage().then(blob=>({type:blob.type,size:blob.size}))`);
+ assert.equal(image.type,'image/png');assert.ok(image.size>20000,'the long image carries the rendered page');
}finally{await browser.close();}
});