Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(download-code): support download zip for not support browsers #703

Merged
merged 3 commits into from
Aug 6, 2024

Conversation

wenmine
Copy link
Collaborator

@wenmine wenmine commented Jul 24, 2024

English | 简体中文

PR

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our Commit Message Guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • Built its own designer, fully self-validated

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Background and solution

What is the current behavior?

对于不支持 file system api的浏览器以及内嵌的iframe,提供下载zip的功能

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Introduced functionality for creating and downloading ZIP files directly from the browser.
    • Added fallback mechanisms for file handling in environments that do not support the File System Access API.
  • Bug Fixes

    • Improved error handling for file operations to ensure continuity in unsupported scenarios.
  • Chores

    • Updated package dependencies to streamline project management and enhance capabilities.

Copy link
Contributor

coderabbitai bot commented Jul 24, 2024

Walkthrough

The recent changes reflect a shift in how the jszip library is utilized across the project. The dependency has been removed from design-core and integrated into utils, where a new file, fszip.js, handles ZIP file creation directly in the browser. Enhancements to index.js improve error handling and introduce fallback mechanisms for unsupported environments, significantly boosting the project's file management capabilities.

Changes

File(s) Change Summary
packages/design-core/package.json Removed dependency: "jszip": "^3.10.1"
packages/utils/package.json Added dependency: "jszip": "^3.10.1"
packages/utils/src/fs/fszip.js Introduced new file with functions for ZIP file management: saveAs, createZip, and writeZip.
packages/utils/src/fs/index.js Enhanced file system utility with new imports, improved error handling, and fallback mechanisms for ZIP functionality when the File System Access API is unavailable.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Browser
    participant Utils
    participant Zip

    User->>Browser: Initiates file download
    Browser->>Utils: Check for File System Access
    alt Supported
        Utils->>Zip: Create ZIP file
        Zip-->>Utils: Return ZIP handle
        Utils->>Browser: Prompt to save ZIP
    else Not Supported
        Utils->>Zip: Fallback to ZIP creation
        Zip-->>Utils: Return ZIP handle
        Utils->>Browser: Prompt to save ZIP
    end
Loading

Poem

🐇 In the meadow where files reign,
A ZIP was born, no longer in vain.
With functions bright and paths so clear,
Downloading files brings us cheer!
Let's hop along, and celebrate with glee,
New tools for all, just wait and see! 🌼✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a383421 and 092f06f.

Files selected for processing (4)
  • packages/design-core/package.json (1 hunks)
  • packages/utils/package.json (1 hunks)
  • packages/utils/src/fs/fszip.js (1 hunks)
  • packages/utils/src/fs/index.js (3 hunks)
Files skipped from review due to trivial changes (1)
  • packages/design-core/package.json
Additional comments not posted (10)
packages/utils/package.json (1)

33-35: Dependency addition approved.

The addition of the jszip dependency is necessary for the new ZIP file handling functionality.

packages/utils/src/fs/fszip.js (4)

1-11: File header approved.

The file header includes appropriate copyright and license information.


13-14: Import statement approved.

The import statement for jszip is correct and necessary for the functionality.


16-29: Function saveAsZip looks good.

The function correctly creates a download link for the ZIP file and triggers the download.


31-36: Function createZip looks good.

The function correctly initializes a new JSZIP instance.

packages/utils/src/fs/index.js (5)

15-16: Import statement approved.

The import statement for createZip and writeZip is correct and necessary for the new functionality.


18-19: Constant isSupportFileSystemAccess looks good.

The constant correctly checks for the availability of the File System Access API and ensures the code is not running in an iframe.


29-30: Function getUserBaseDirHandle fallback looks good.

The function correctly uses createZip as a fallback when showOpenFilePicker is not available.


182-186: Function writeFiles fallback looks good.

The function correctly uses writeZip as a fallback when the File System Access API is not supported.


192-192: Function writeFiles approved.

The function correctly writes files using the appropriate directory handle.

packages/utils/src/fs/fszip.js Outdated Show resolved Hide resolved
packages/utils/src/fs/fszip.js Outdated Show resolved Hide resolved
packages/utils/src/fs/fszip.js Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Inline review comments failed to post

Actionable comments posted: 2

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 092f06f and 8122d08.

Files selected for processing (2)
  • packages/utils/src/fs/fszip.js (1 hunks)
  • packages/utils/src/fs/index.js (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/utils/src/fs/fszip.js
Additional comments not posted (2)
packages/utils/src/fs/index.js (2)

Line range hint 139-167:
LGTM!

The function implementation is well-structured and does not require any changes.


18-19: LGTM! But verify the constant usage in the codebase.

The code changes are approved.

However, ensure that all usages of isSupportFileSystemAccess are correctly handled.

Verification successful

The usage of isSupportFileSystemAccess is correctly handled within the codebase.

The constant is used in an if-statement to ensure that certain operations are only performed if the file system access is supported.

  • packages/utils/src/fs/index.js: Lines where isSupportFileSystemAccess is defined and used.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all usages of `isSupportFileSystemAccess` in the codebase.

# Test: Search for the constant usage. Expect: Proper handling of the constant.
rg --type python -A 5 $'isSupportFileSystemAccess'

Length of output: 80


Script:

#!/bin/bash
# Description: Verify all usages of `isSupportFileSystemAccess` in the codebase.

# Test: Search for the constant usage. Expect: Proper handling of the constant.
rg --type js -A 5 'isSupportFileSystemAccess'

Length of output: 776

Comments failed to post (2)
packages/utils/src/fs/index.js

28-29: Add error handling and logging.

The function correctly implements a fallback mechanism but could benefit from additional error handling and logging.

-  if (!window.showOpenFilePicker) {
-    return createZip()
-  }
+  if (!window.showOpenFilePicker) {
+    console.warn('File System Access API not supported, creating zip handle as fallback.')
+    try {
+      return createZip()
+    } catch (error) {
+      console.error('Error creating zip handle:', error)
+      throw error
+    }
+  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

  if (!window.showOpenFilePicker) {
    console.warn('File System Access API not supported, creating zip handle as fallback.')
    try {
      return createZip()
    } catch (error) {
      console.error('Error creating zip handle:', error)
      throw error
    }
  }

178-183: Add error handling and logging.

The function correctly implements a fallback mechanism but could benefit from additional error handling and logging.

-  if (!filesInfo?.length) {
-    return
-  }
+  if (!filesInfo?.length) {
+    console.warn('No files to write.')
+    return
+  }

-  if (!isSupportFileSystemAccess) {
-    const zipInfo = { zipName, zipHandle: supportZipCache && baseDirHandle }
-    await writeZip(filesInfo, zipInfo)
-    return
-  }
+  if (!isSupportFileSystemAccess) {
+    console.warn('File System Access API not supported, using zip fallback.')
+    try {
+      const zipInfo = { zipName, zipHandle: supportZipCache && baseDirHandle }
+      await writeZip(filesInfo, zipInfo)
+    } catch (error) {
+      console.error('Error writing zip files:', error)
+      throw error
+    }
+    return
+  }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

export const writeFiles = async (
  baseDirHandle,
  filesInfo,
  zipName = 'tiny-engine-generate-code',
  supportZipCache = true
) => {
  if (!filesInfo?.length) {
    console.warn('No files to write.')
    return
  }

  if (!isSupportFileSystemAccess) {
    console.warn('File System Access API not supported, using zip fallback.')
    try {
      const zipInfo = { zipName, zipHandle: supportZipCache && baseDirHandle }
      await writeZip(filesInfo, zipInfo)
    } catch (error) {
      console.error('Error writing zip files:', error)
      throw error
    }
    return
  }

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8122d08 and 2916a7e.

Files selected for processing (2)
  • packages/utils/src/fs/fszip.js (1 hunks)
  • packages/utils/src/fs/index.js (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/utils/src/fs/fszip.js
  • packages/utils/src/fs/index.js

chilingling
chilingling previously approved these changes Aug 2, 2024
@chilingling chilingling added enhancement New feature or request ready_for_review This PR requires more reviews labels Aug 2, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2916a7e and 50d218f.

Files selected for processing (1)
  • packages/utils/src/fs/fszip.js (1 hunks)
Additional comments not posted (2)
packages/utils/src/fs/fszip.js (2)

21-29: LGTM!

The saveAs function correctly handles the download process.


34-36: LGTM!

The createZip function correctly returns a new instance of JSZIP.

packages/utils/src/fs/fszip.js Show resolved Hide resolved
packages/utils/src/fs/fszip.js Show resolved Hide resolved
packages/utils/src/fs/fszip.js Show resolved Hide resolved
@hexqi hexqi merged commit 3a20462 into opentiny:develop Aug 6, 2024
1 check passed
chilingling added a commit that referenced this pull request Sep 24, 2024
* fix(mockServer): mockServer page preview can't render element-plus element (#503)

* fix(style): fix render error caused by inline style breaks (#526)

* fix(metaComp): fix bug where metaHtmlText could set value to incorret schema children (#473)

* fix(vue-generator): fix globalstate codegen error (#547)

* fix(material): add componentsMap to app Schema after material build (#527)

* fix: slot params missing double quote (#605)

* fix: slot params missing double quote

* fix: exclude nodemodule test case

* fix: 修复onMouseover拼写错误 (#662)

* fix: esbuild install failed on node v16 (#668)

* fix: esbuild install failed on nodev16

* fix: esbuild install failed on nodev16

* fix: builtin components can't generate import statement with genSFCWithDefaultPlugin method (#656)

* fix: esbuild install failed on nodev16 (#671)

* fix: esbuild install failed on nodev16

* fix: esbuild install failed on nodev16

* fix: remove deps on root pkg.json

* fix(preview): multiple nested blocks cannot preview #663 (#665)

* fix(material): add missing componentsMap to mockServer (#701)

* fix(setting): fix bindEvent dialog visible can't work on tinyvue 3.17 (#715)

* feat(download-code): support download zip for not support browsers (#703)

* feat(download-code): support download zip for not support browsers

* feat(download-code): support download zip for not support browsers - review

* feat(download-code): support download zip for not support browsers - review

* docs: update milestone (#728)

* docs: update milestone

* fix: tab

* fix: abaolute canvas init inlineStyle should be string (#730)

* fix(download): Optimize download logic and adapt to iframe (#739)

* fix(download): Optimize download logic and adapt to iframe

* feat(cdn): change cdn from onmicrosoft to unpkg (#750)

* fix: 隐藏画布根节点的包裹元素的操作选项 (#492)

* fix(script): translate log (#549)

* fix: translate log

* Update scripts/connection.mjs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update scripts/connection.mjs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update scripts/connection.mjs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* fix: reset spacing cannot generate correct source code (#657)

* fix: jsx slot modelvalue can't update value (#734)

* fix: jsx slot modelvalue can't update value

* fix: add unit test for updateModel event

* fix(canvas): absolute dnd update position to schema. close #664 (#751)

* fix(generate-vue):修复出码文件选择界面打包后样式丢失问题 (#789)

Co-authored-by: wangwenbing <11535041@qq.com>

* fix(stylePanel): fix setting border-radius could not work on first time (#481)

* fix(common): fix verify required (#787)

* fix: mixed lifeCyclesContent when empty lifecycles (#810)

close #806
修复生命周期为空时,取当前页面schema生命周期值的 bug

---------

Co-authored-by: chilingling <26962197+chilingling@users.noreply.github.com>
Co-authored-by: yeser <631300329@qq.com>
Co-authored-by: wenmine <wwmmail@foxmail.com>
Co-authored-by: Gene <Pacify.98@gmail.com>
Co-authored-by: yaoyun8 <142570291+yaoyun8@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: bwrong <ibwrong@foxmail.com>
Co-authored-by: wangwenbing <11535041@qq.com>
Co-authored-by: Xie Jay <xiejay97@gmail.com>
yy-wow pushed a commit to yy-wow/tiny-engine that referenced this pull request Oct 10, 2024
…pentiny#703)

* feat(download-code): support download zip for not support browsers

* feat(download-code): support download zip for not support browsers - review

* feat(download-code): support download zip for not support browsers - review
yy-wow pushed a commit to yy-wow/tiny-engine that referenced this pull request Oct 10, 2024
* fix(mockServer): mockServer page preview can't render element-plus element (opentiny#503)

* fix(style): fix render error caused by inline style breaks (opentiny#526)

* fix(metaComp): fix bug where metaHtmlText could set value to incorret schema children (opentiny#473)

* fix(vue-generator): fix globalstate codegen error (opentiny#547)

* fix(material): add componentsMap to app Schema after material build (opentiny#527)

* fix: slot params missing double quote (opentiny#605)

* fix: slot params missing double quote

* fix: exclude nodemodule test case

* fix: 修复onMouseover拼写错误 (opentiny#662)

* fix: esbuild install failed on node v16 (opentiny#668)

* fix: esbuild install failed on nodev16

* fix: esbuild install failed on nodev16

* fix: builtin components can't generate import statement with genSFCWithDefaultPlugin method (opentiny#656)

* fix: esbuild install failed on nodev16 (opentiny#671)

* fix: esbuild install failed on nodev16

* fix: esbuild install failed on nodev16

* fix: remove deps on root pkg.json

* fix(preview): multiple nested blocks cannot preview opentiny#663 (opentiny#665)

* fix(material): add missing componentsMap to mockServer (opentiny#701)

* fix(setting): fix bindEvent dialog visible can't work on tinyvue 3.17 (opentiny#715)

* feat(download-code): support download zip for not support browsers (opentiny#703)

* feat(download-code): support download zip for not support browsers

* feat(download-code): support download zip for not support browsers - review

* feat(download-code): support download zip for not support browsers - review

* docs: update milestone (opentiny#728)

* docs: update milestone

* fix: tab

* fix: abaolute canvas init inlineStyle should be string (opentiny#730)

* fix(download): Optimize download logic and adapt to iframe (opentiny#739)

* fix(download): Optimize download logic and adapt to iframe

* feat(cdn): change cdn from onmicrosoft to unpkg (opentiny#750)

* fix: 隐藏画布根节点的包裹元素的操作选项 (opentiny#492)

* fix(script): translate log (opentiny#549)

* fix: translate log

* Update scripts/connection.mjs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update scripts/connection.mjs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update scripts/connection.mjs

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* fix: reset spacing cannot generate correct source code (opentiny#657)

* fix: jsx slot modelvalue can't update value (opentiny#734)

* fix: jsx slot modelvalue can't update value

* fix: add unit test for updateModel event

* fix(canvas): absolute dnd update position to schema. close opentiny#664 (opentiny#751)

* fix(generate-vue):修复出码文件选择界面打包后样式丢失问题 (opentiny#789)

Co-authored-by: wangwenbing <11535041@qq.com>

* fix(stylePanel): fix setting border-radius could not work on first time (opentiny#481)

* fix(common): fix verify required (opentiny#787)

* fix: mixed lifeCyclesContent when empty lifecycles (opentiny#810)

close opentiny#806
修复生命周期为空时,取当前页面schema生命周期值的 bug

---------

Co-authored-by: chilingling <26962197+chilingling@users.noreply.github.com>
Co-authored-by: yeser <631300329@qq.com>
Co-authored-by: wenmine <wwmmail@foxmail.com>
Co-authored-by: Gene <Pacify.98@gmail.com>
Co-authored-by: yaoyun8 <142570291+yaoyun8@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: bwrong <ibwrong@foxmail.com>
Co-authored-by: wangwenbing <11535041@qq.com>
Co-authored-by: Xie Jay <xiejay97@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready_for_review This PR requires more reviews
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants