diff --git a/PNG-Export-and-iframe-Architecture-Analysis.md b/PNG-Export-and-iframe-Architecture-Analysis.md new file mode 100644 index 00000000..0d437c99 --- /dev/null +++ b/PNG-Export-and-iframe-Architecture-Analysis.md @@ -0,0 +1,184 @@ +# ZenUML Web Sequence - PNG Export and iframe Architecture Analysis + +## 1. Overview + +ZenUML Web Sequence uses an iframe architecture to render sequence diagrams and implements PNG export functionality through the `toBlob()` method provided by the @zenuml/core library. This document provides a detailed analysis of these two core mechanisms. + +## 2. PNG Export Mechanism + +### 2.1 Export Flow + +The complete PNG export flow is as follows: + +1. **User Triggers Export** + * Click download button triggers `exportPngClickHandler()` (ContentWrap.jsx:540) + * Or click copy button triggers `copyImageClickHandler()` (ContentWrap.jsx:559) + +2. **Permission Check** + ```javascript + if (!window.user) { + this.props.onLogin(); + return; + } + ``` + +3. **Get PNG Blob** + ```javascript + async getPngBlob() { + // Use the getPng method exposed by the iframe + const pngDataUrl = await this.frame.contentWindow.getPng(); + if (!pngDataUrl) { + throw new Error('Failed to get PNG from diagram'); + } + + // Convert data URL to Blob + const response = await fetch(pngDataUrl); + return await response.blob(); + } + ``` + +4. **Process Export** + * Download: Uses FileSaver.js's `saveAs(png, 'zenuml.png')` + * Copy: Uses Clipboard API to write to clipboard + +### 2.2 Key Technical Points + +1. **Official API Method**: Uses the `getPng()` method provided by @zenuml/core library +2. **Data URL to Blob Conversion**: The `getPng()` method returns a data URL which is converted to Blob using the Fetch API +3. **Cross-iframe Access**: Access iframe's exposed methods through `this.frame.contentWindow` +4. **No Vue Internals**: No longer relies on accessing Vue instance internals (`__vue__`), making it more stable and future-proof + +### 2.3 Chrome Extension Special Handling + +Chrome extension uses a different screenshot mechanism (takeScreenshot.js): +- Uses Chrome tabs API for screenshots +- Saves to filesystem through WriteFile.js + +## 3. iframe Architecture Analysis + +### 3.1 iframe Creation + +The iframe is created in ContentWrap.jsx: +```jsx +