From dda754830e5e3ee42c2a15fa85c2f2c7a7f8f2e0 Mon Sep 17 00:00:00 2001 From: Kirigaya <1193466151@qq.com> Date: Sat, 4 Jan 2025 15:14:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20netlist=20=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E4=B8=BA=20svg=20&=20pdf?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- l10n/bundle.l10n.de.json | 6 +++- l10n/bundle.l10n.en.json | 6 +++- l10n/bundle.l10n.ja.json | 6 +++- l10n/bundle.l10n.zh-cn.json | 6 +++- l10n/bundle.l10n.zh-tw.json | 6 +++- src/function/dide-netlist/api.ts | 58 +++++++++++++++++------------- src/function/dide-netlist/index.ts | 21 +++++++++++ 7 files changed, 79 insertions(+), 30 deletions(-) diff --git a/l10n/bundle.l10n.de.json b/l10n/bundle.l10n.de.json index 051ef8c..bc0cd8a 100644 --- a/l10n/bundle.l10n.de.json +++ b/l10n/bundle.l10n.de.json @@ -106,5 +106,9 @@ "info.command.structure.reload-vscode": "Vscode neu starten", "error.look-up-log": "Fehlerprotokoll anzeigen", "netlist.save-as-svg": "Als SVG speichern", - "svg-file": "SVG-Datei" + "svg-file": "SVG-Datei", + "toolbar.save-as-svg": "Aktuelle Ansicht als SVG speichern", + "toolbar.save-as-pdf": "Aktuelle Ansicht als PDF speichern", + "pdf-file": "PDF-Datei", + "export-pdf": "PDF wird exportiert" } \ No newline at end of file diff --git a/l10n/bundle.l10n.en.json b/l10n/bundle.l10n.en.json index 96ad49a..6f44ac0 100644 --- a/l10n/bundle.l10n.en.json +++ b/l10n/bundle.l10n.en.json @@ -106,5 +106,9 @@ "info.command.structure.reload-vscode": "Restart Vscode", "error.look-up-log": "View error log", "netlist.save-as-svg": "Save as SVG", - "svg-file": "SVG file" + "svg-file": "SVG file", + "toolbar.save-as-svg": "Save current view as SVG", + "toolbar.save-as-pdf": "Save current view as PDF", + "pdf-file": "PDF file", + "export-pdf": "Exporting PDF" } \ No newline at end of file diff --git a/l10n/bundle.l10n.ja.json b/l10n/bundle.l10n.ja.json index 8766455..eb55965 100644 --- a/l10n/bundle.l10n.ja.json +++ b/l10n/bundle.l10n.ja.json @@ -106,5 +106,9 @@ "info.command.structure.reload-vscode": "Vscodeを再起動", "error.look-up-log": "エラーログを表示", "netlist.save-as-svg": "SVGとして保存", - "svg-file": "SVGファイル" + "svg-file": "SVGファイル", + "toolbar.save-as-svg": "現在のビューをSVGとして保存", + "toolbar.save-as-pdf": "現在のビューをPDFとして保存", + "pdf-file": "PDFファイル", + "export-pdf": "PDFをエクスポート中" } \ No newline at end of file diff --git a/l10n/bundle.l10n.zh-cn.json b/l10n/bundle.l10n.zh-cn.json index 322ae11..019e12b 100644 --- a/l10n/bundle.l10n.zh-cn.json +++ b/l10n/bundle.l10n.zh-cn.json @@ -106,5 +106,9 @@ "info.command.structure.reload-vscode": "重启 Vscode", "error.look-up-log": "查看错误日志", "netlist.save-as-svg": "保存为 Svg", - "svg-file": "svg 文件" + "svg-file": "svg 文件", + "toolbar.save-as-svg": "将当前视图保存为 svg", + "toolbar.save-as-pdf": "将当前视图保存为 pdf", + "pdf-file": "pdf 文件", + "export-pdf": "正在导出 pdf" } \ No newline at end of file diff --git a/l10n/bundle.l10n.zh-tw.json b/l10n/bundle.l10n.zh-tw.json index 2dcb2e8..88210b8 100644 --- a/l10n/bundle.l10n.zh-tw.json +++ b/l10n/bundle.l10n.zh-tw.json @@ -106,5 +106,9 @@ "info.command.structure.reload-vscode": "重啟 Vscode", "error.look-up-log": "查看錯誤日誌", "netlist.save-as-svg": "保存為Svg", - "svg-file": "SVG 檔案" + "svg-file": "SVG 檔案", + "toolbar.save-as-svg": "將目前視圖儲存為SVG", + "toolbar.save-as-pdf": "將目前視圖儲存為PDF", + "pdf-file": "PDF檔案", + "export-pdf": "正在匯出PDF" } \ No newline at end of file diff --git a/src/function/dide-netlist/api.ts b/src/function/dide-netlist/api.ts index 3f8730f..74f9f2f 100644 --- a/src/function/dide-netlist/api.ts +++ b/src/function/dide-netlist/api.ts @@ -2,7 +2,7 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as fs from 'fs'; -import pako from 'pako'; +import * as pako from 'pako'; import puppeteer, { LowerCasePaperFormat, PDFOptions } from 'puppeteer-core'; import { AbsPath, opeParam } from '../../global'; import { hdlPath } from '../../hdlFs'; @@ -28,12 +28,9 @@ export async function saveAsSvg(data: any, panel: vscode.WebviewPanel) { // 询问新的路径 const defaultFilename = moduleName + '.svg'; const defaultPath = hdlPath.join(opeParam.workspacePath, defaultFilename); - const vcdFilters: Record = {}; - vcdFilters[""] = ['svg']; - vcdFilters[t('info.vcd-viewer.all-file')] = ['*']; const saveUri = await vscode.window.showSaveDialog({ - title: t('info.vcd-viewer.save-as-view'), + title: t('netlist.save-as-svg'), defaultUri: vscode.Uri.file(defaultPath), saveLabel: t('info.vcd-viewer.save'), filters: { @@ -66,48 +63,59 @@ export async function saveAsSvg(data: any, panel: vscode.WebviewPanel) { } } -export async function saveAsPdf(req: Request, res: Response) { +export async function saveAsPdf(data: any, panel: vscode.WebviewPanel) { try { - const { svgBuffer, moduleName, width, height } = req.body; + const { svgBuffer, moduleName, width, height } = data; const svgString = pako.ungzip(svgBuffer, { to: 'string' }); // 询问新的路径 const defaultFilename = moduleName + '.pdf'; - const savePath = await showSaveViewDialog({ - title: 'Save As pdf', - defaultPath: path.resolve(__dirname, '../test', defaultFilename), - buttonLabel: 'Save', - filters: [ - { name: 'pdf', extensions: ['pdf'] }, - { name: 'All Files', extensions: ['*'] }, - ], + const defaultPath = hdlPath.join(opeParam.workspacePath, defaultFilename); + + const saveUri = await vscode.window.showSaveDialog({ + title: t('toolbar.save-as-pdf'), + defaultUri: vscode.Uri.file(defaultPath), + saveLabel: t('info.vcd-viewer.save'), + filters: { + [t('pdf-file')]: ['pdf'], + [t("info.vcd-viewer.all-file")]: ['*'] + } }); - if (savePath) { + if (saveUri) { + const savePath = saveUri.fsPath; // 先保存 html const htmlPath = savePath.slice(0, -4) + '.html'; fs.writeFileSync(htmlPath, svgString); - await html2pdf(htmlPath, savePath, moduleName, width, height); - // removeBlankPages(savePath); + await vscode.window.withProgress({ + title: t('export-pdf'), + location: vscode.ProgressLocation.Notification + }, async () => { + await html2pdf(htmlPath, savePath, moduleName, width, height); + }); + fs.rmSync(htmlPath); - res.send({ - savePath, - success: true + panel.webview.postMessage({ + command: 'save-as-pdf', + arguments: [{ success: true }] }); } else { - res.send({ - success: false + panel.webview.postMessage({ + command: 'save-as-pdf', + arguments: [{ success: false }] }); } } catch (error) { console.log('error happen in /save-as-pdf, ' + error); - res.send({ - success: false + panel.webview.postMessage({ + command: 'save-as-pdf', + arguments: [{ success: false }] }); } } async function html2pdf(htmlPath: string, pdfPath: string, moduleName: string, width: number, height: number) { + const browserPath = getDefaultBrowerPath(); const browser = await puppeteer.launch({ executablePath: browserPath, args: ['--lang=en-US', '--no-sandbox', '--disable-setuid-sandbox'] diff --git a/src/function/dide-netlist/index.ts b/src/function/dide-netlist/index.ts index 30600b2..ad4ea9c 100644 --- a/src/function/dide-netlist/index.ts +++ b/src/function/dide-netlist/index.ts @@ -12,6 +12,7 @@ import { t } from '../../i18n'; import { HdlLangID } from '../../global/enum'; import { getIconConfig } from '../../hdlFs/icons'; import { PathSet } from '../../global/util'; +import { saveAsPdf, saveAsSvg } from './api'; type SynthMode = 'before' | 'after' | 'RTL'; @@ -240,6 +241,8 @@ class Netlist { .replace('dide.skin', skin); this.panel.webview.html = preprocessHtml; + + registerMessageEvent(this.panel); } else { YosysOutput.report('preview html in is empty', { level: ReportType.Warn @@ -296,4 +299,22 @@ class Netlist { export async function openNetlistViewer(context: vscode.ExtensionContext, uri: vscode.Uri, moduleName: string) { const viewer = new Netlist(context); viewer.open(uri, moduleName); +} + + +function registerMessageEvent(panel: vscode.WebviewPanel) { + panel.webview.onDidReceiveMessage(message => { + const { command, data } = message; + + switch (command) { + case 'save-as-svg': + saveAsSvg(data, panel); + break; + case 'save-as-pdf': + saveAsPdf(data, panel); + break; + default: + break; + } + }); } \ No newline at end of file