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

fix: insert logic and multi node insert issue #1150

Merged
merged 4 commits into from
Feb 27, 2025

Conversation

SonyLeo
Copy link
Contributor

@SonyLeo SonyLeo commented Feb 27, 2025

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?

  1. insert 节点 部分逻辑缺失
  2. 多选节点 复制 后 粘贴 到新容器报错

Issue Number: N/A

What is the new behavior?

  1. insert 节点补充相应逻辑
  2. 多选节点 复制 后 正常粘贴 到新容器

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

  • New Features

    • Clipboard paste now supports inserting multiple nodes, streamlining batch operations on the canvas.
  • Refactor

    • Improved node insertion on the design canvas for more consistent placement.
    • Enhanced clipboard data handling and validation for a more robust user experience.

Copy link
Contributor

coderabbitai bot commented Feb 27, 2025

Walkthrough

The pull request streamlines node insertion and clipboard handling in the canvas modules. In the DesignCanvas API, the operationTypeMap function now uses simplified logic with direct splice insertion. In the container module, a redundant blank line was removed. Additionally, clipboard paste functionality was updated to process multiple nodes, merging the logic of a previously separate function and refining schema handling in utility functions.

Changes

File(s) Change Summary
packages/canvas/DesignCanvas/src/api/useCanvas.js Modified operationTypeMap to simplify node insertion by removing redundant conditionals and using splice for both 'before' and default cases.
packages/canvas/container/src/container.js Removed an unnecessary blank line between functions; no functional change.
packages/canvas/container/src/keyboard.js, packages/canvas/container/src/utils.js Enhanced clipboard handling: updated paste logic to accept multiple nodes, merged handleMultiNodesPaste functionality, and refactored clipboard schema processing and input validation.

Sequence Diagram(s)

Clipboard Paste Flow

sequenceDiagram
    participant U as User
    participant KE as Keyboard Handler
    participant CP as Clipboard Processor
    participant CI as Canvas Inserter
    U->>KE: Trigger Paste Event (e.g., Ctrl+V)
    KE->>CP: Extract node list from clipboard
    CP->>CP: Validate nodes & iterate over list
    CP->>CI: Insert each node via operationTypeMap
    CI-->>CP: Confirm insertion
    CP-->>KE: Return paste result
Loading

Node Insertion Flow

sequenceDiagram
    participant Caller as operationTypeMap
    participant Parent as ParentNode
    Caller->>Parent: Determine insertion index based on position ('before' or default)
    Parent-->>Caller: Provide current children count
    Caller->>Parent: Insert new node at calculated index using splice
Loading

Possibly related PRs

Suggested labels

bug, refactor-main

Suggested reviewers

  • chilingling
  • hexqi
  • rhlin

Poem

I hopped through streams of code with glee,
Simplifying nodes as swift as can be.
Carrots and splices in a joyful blend,
Clipboard tales now reach a smoother end.
With each small change, my heart does flip—
A bunny's cheer in every bit of script!

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/canvas/DesignCanvas/src/api/useCanvas.js

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "@typescript-eslint/eslint-plugin".

(The package "@typescript-eslint/eslint-plugin" was not found when loaded as a Node module from the directory "/packages/canvas".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install @typescript-eslint/eslint-plugin@latest --save-dev

The plugin "@typescript-eslint/eslint-plugin" was referenced from the config file in "packages/canvas/.eslintrc.cjs » @vue/eslint-config-typescript".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

packages/canvas/container/src/utils.js

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "@typescript-eslint/eslint-plugin".

(The package "@typescript-eslint/eslint-plugin" was not found when loaded as a Node module from the directory "/packages/canvas".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install @typescript-eslint/eslint-plugin@latest --save-dev

The plugin "@typescript-eslint/eslint-plugin" was referenced from the config file in "packages/canvas/.eslintrc.cjs » @vue/eslint-config-typescript".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

packages/canvas/container/src/keyboard.js

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "@typescript-eslint/eslint-plugin".

(The package "@typescript-eslint/eslint-plugin" was not found when loaded as a Node module from the directory "/packages/canvas".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install @typescript-eslint/eslint-plugin@latest --save-dev

The plugin "@typescript-eslint/eslint-plugin" was referenced from the config file in "packages/canvas/.eslintrc.cjs » @vue/eslint-config-typescript".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

✨ Finishing Touches
  • 📝 Generate Docstrings

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

@github-actions github-actions bot added the bug Something isn't working label Feb 27, 2025
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

🧹 Nitpick comments (2)
packages/canvas/container/src/keyboard.js (1)

105-115: Enhanced clipboard paste to support multiple nodes.

The function now handles a list of nodes instead of a single node, addressing the multi-node insertion issue mentioned in the PR objectives. The logic correctly iterates through each node in the list and applies appropriate insertion logic based on component compatibility.

Consider adding error handling in case any individual node insertion fails, to prevent partial paste operations:

 const handleClipboardPaste = (nodeList, schema, parent) => {
   if (!nodeList.length) return
 
+  try {
     nodeList.forEach((node) => {
       if (node?.componentName && schema?.componentName && allowInsert(getConfigure(schema.componentName), node)) {
         insertNode({ parent, node: schema, data: node }, POSITION.IN)
       } else {
         insertNode({ parent, node: schema, data: node }, POSITION.BOTTOM)
       }
     })
+  } catch (error) {
+    console.error('Error pasting nodes:', error)
+  }
 }
packages/canvas/container/src/utils.js (1)

32-63: Enhanced schema translation with better validation and fallbacks.

The improved function now:

  1. Properly validates input
  2. Handles both array and single object formats
  3. Provides a sensible fallback for invalid data

This makes the clipboard functionality more robust when handling different data formats.

Consider adding optional chaining for better error prevention as suggested by the static analysis:

-    if (Array.isArray(parsedData) && parsedData.every((item) => item && item.componentName)) {
+    if (Array.isArray(parsedData) && parsedData.every((item) => item?.componentName)) {
🧰 Tools
🪛 Biome (1.9.4)

[error] 42-42: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2860d1c and a3e12dd.

📒 Files selected for processing (4)
  • packages/canvas/DesignCanvas/src/api/useCanvas.js (2 hunks)
  • packages/canvas/container/src/container.js (0 hunks)
  • packages/canvas/container/src/keyboard.js (2 hunks)
  • packages/canvas/container/src/utils.js (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/canvas/container/src/container.js
🧰 Additional context used
🪛 Biome (1.9.4)
packages/canvas/container/src/utils.js

[error] 42-42: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: push-check
🔇 Additional comments (6)
packages/canvas/DesignCanvas/src/api/useCanvas.js (2)

325-326: Simplified insertion logic for 'before' position.

The code now handles the case where the reference node isn't found (index === -1) by defaulting to the start of the array, which is more robust than the previous implementation.


338-339: Improved default insertion logic.

The simplified logic now handles the case where the reference node isn't found by appending to the end of the array, and otherwise inserts after the reference node. This is more intuitive and reduces the chance of insertion errors.

packages/canvas/container/src/keyboard.js (3)

117-122: Enhanced copy operation to support multiple nodes.

The copy operation now properly handles multiple selected nodes, storing them as an array in the clipboard. This implementation works well with the updated paste functionality.


126-127: Updated clipboard retrieval to fetch node list.

The code now expects to retrieve a list of nodes from the clipboard rather than a single node, properly aligning with the changes in the utils.js file.


133-133: Updated paste handler to use new multi-node functionality.

The clipboard paste event now correctly calls the enhanced paste function with the node list.

packages/canvas/container/src/utils.js (1)

19-29: Improved clipboard data handling for node lists.

The function now accepts a string representation of nodes rather than a node object, which is more flexible and supports the multi-node clipboard functionality. The validation is also improved to ensure valid string data.

@hexqi hexqi merged commit 9b3bb55 into opentiny:release/v2.3.0 Feb 27, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants