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: add refresh life cycles #898

Merged
merged 6 commits into from
Nov 11, 2024

Conversation

yy-wow
Copy link
Collaborator

@yy-wow yy-wow commented Nov 7, 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?

Issue Number: N/A

What is the new behavior?

  1. 修复源码调试找不到包问题;
  2. 刷新工具栏增加生命周期方法;
  3. 增加画布标识,控制是否允许拖拽、选中组件、允许原生事件触发等

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a design mode feature, allowing users to switch between design and runtime states for enhanced control over canvas behavior.
    • Added notification handling and improved refresh logic in the main component for better user feedback during canvas operations.
  • Bug Fixes

    • Corrected a typo in the refresh function to ensure proper functionality.
  • Improvements

    • Enhanced event handling by conditionally executing based on the current design mode, improving responsiveness and user experience.
  • Documentation

    • Updated constants and functions related to design mode for clarity and usability.

Copy link
Contributor

coderabbitai bot commented Nov 7, 2024

Walkthrough

The changes in this pull request involve multiple modifications across different files, primarily focusing on enhancing the handling of design modes in the canvas-related components. Key updates include the introduction of new functions for managing design modes, improvements to event handling based on the design state, and updates to alias resolution for modules. The devAliasPlugin.js file sees a path update for an alias, while Vue components and JavaScript modules are adjusted to accommodate new functionalities and improved error handling.

Changes

File Path Change Summary
packages/build/vite-config/src/vite-plugins/devAliasPlugin.js Updated alias for @opentiny/tiny-engine-canvas from canvas.js to index.js.
packages/toolbars/refresh/src/Main.vue Enhanced setup function, corrected refreshResource typo, and improved refresh logic with error handling.
packages/canvas/container/src/CanvasContainer.vue Added handleCanvasEvent function to manage event handling based on design mode.
packages/canvas/render/src/RenderMain.js Introduced getDesignMode and setDesignMode functions to manage design mode state.
packages/canvas/render/src/context.js Added DESIGN_MODE constant and designMode variable, along with getter and setter functions for design mode.
packages/canvas/render/src/render.js Integrated design mode checks into getBindProps function for event handling.
packages/canvas/container/src/container.js Added getDesignMode and setDesignMode methods to the canvas API for external access.
packages/canvas/common/src/constant.js Introduced DESIGN_MODE constant with properties for different operation modes.

Possibly related PRs

  • feat: customized canvas entry #850: Modifications to the getDevAlias function in devAliasPlugin.js, adding a new alias mapping for the @opentiny/tiny-engine-canvas/render module, which relates to the canvas rendering enhancements.

Suggested labels

enhancement, ospp-2024

Suggested reviewers

  • hexqi
  • rhlin

🐇 In the canvas, where designs play,
New modes emerge to brighten the day.
With events now handled with care,
Refreshing the art, we joyfully share.
So hop along, let creativity flow,
In this vibrant world, let your ideas grow! 🌼


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 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 refactor-main refactor/develop branch feature label Nov 7, 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

🧹 Outside diff range and nitpick comments (2)
packages/build/vite-config/src/vite-plugins/devAliasPlugin.js (1)

57-57: Document the canvas module restructuring.

The canvas module has been moved from being part of design-core to its own standalone package. This structural change should be documented.

Add a comment above the alias to explain this architectural decision:

+    // Canvas module moved to standalone package for better modularity
     '@opentiny/tiny-engine-canvas': path.resolve(basePath, 'packages/canvas/index.js'),
packages/toolbars/refresh/src/Main.vue (1)

43-51: Await afterRefresh if it is asynchronous, and mark refreshResource as async

If afterRefresh is an asynchronous function that returns a promise, you should await it to ensure it completes before proceeding. Additionally, to use await inside refreshResource, the function needs to be declared as async.

Apply these changes:

  1. Mark refreshResource as async:
- const refreshResouce = () => {
+ const refreshResource = async () => {
  1. Await afterRefresh():
          try {
-            afterRefresh()
+            await afterRefresh()
          } catch (error) {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ea0440b and 3dcfb26.

📒 Files selected for processing (2)
  • packages/build/vite-config/src/vite-plugins/devAliasPlugin.js (1 hunks)
  • packages/toolbars/refresh/src/Main.vue (3 hunks)

packages/toolbars/refresh/src/Main.vue Outdated Show resolved Hide resolved
packages/toolbars/refresh/src/Main.vue 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.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (9)
packages/canvas/render/src/context.js (3)

59-60: Translate comment to English for consistency.

The codebase should maintain consistent use of English in comments for better international collaboration.

-// 是否表现画布内特征的标志,用来控制是否允许拖拽、原生事件是否触发等
+// Canvas feature flag to control drag behavior and native event triggering

64-66: Add type validation in setCanvasFlag.

The setter should validate the input to ensure type safety and prevent invalid states.

 export const setCanvasFlag = (flag) => {
+  if (typeof flag !== 'boolean') {
+    throw new TypeError('Canvas flag must be a boolean value')
+  }
   canvasFlag = flag
 }

60-66: Consider using reactive state for canvasFlag.

Using a plain variable for state that affects multiple components could lead to race conditions in concurrent scenarios. Consider using Vue's reactive state management since this is a Vue application.

-let canvasFlag = true
+import { ref } from 'vue'
+
+const canvasFlag = ref(true)
 
 export const getCanvasFlag = () => canvasFlag
 
 export const setCanvasFlag = (flag) => {
+  if (typeof flag !== 'boolean') {
+    throw new TypeError('Canvas flag must be a boolean value')
+  }
-  canvasFlag = flag
+  canvasFlag.value = flag
 }
packages/canvas/container/src/CanvasContainer.vue (3)

144-154: Add error handling for setCurrentNode call.

While the event handling logic is well-structured, the setCurrentNode call could benefit from error handling.

Consider wrapping the state updates in a try-catch block:

 isCanvasEvent(event, () => {
   // html元素使用scroll和mouseup事件处理
   if (event.target === doc.documentElement) {
     isScrolling = false
     return
   }

   insertPosition.value = false
-  setCurrentNode(event)
-  target.value = event.target
+  try {
+    setCurrentNode(event)
+    target.value = event.target
+  } catch (error) {
+    console.error('Error setting current node:', error)
+    // Reset states on error
+    insertPosition.value = false
+    target.value = null
+  }
 })

183-185: Document the purpose of the boolean parameter in dragMove.

The true parameter passed to dragMove is not self-documenting and its purpose is unclear.

Consider using a named constant or adding a comment:

 isCanvasEvent(ev, (e) => {
-  dragMove(e, true)
+  const IS_MOUSE_MOVE = true  // Indicates event is from mousemove
+  dragMove(e, IS_MOUSE_MOVE)
 })

120-128: Consider extracting event handling pattern into a reusable utility.

The new isCanvasEvent pattern introduces a consistent way to handle canvas events. Consider extracting this pattern into a reusable utility that other components can leverage.

You could create a composable like useCanvasEvent:

// useCanvasEvent.js
export function useCanvasEvent() {
  const handleCanvasEvent = (event, handler) => {
    // Current isCanvasEvent implementation
  }
  
  return {
    handleCanvasEvent
  }
}

// Usage in components
const { handleCanvasEvent } = useCanvasEvent()

This would promote consistency across the codebase and make it easier to maintain the event handling logic in one place.

Also applies to: 144-154, 183-185

packages/canvas/render/src/RenderMain.js (1)

Line range hint 1-458: Consider splitting state management concerns

The file currently handles multiple responsibilities including state management, rendering, method context, props management, and now canvas flags. Consider splitting these concerns into separate modules for better maintainability:

  1. Create a dedicated canvas state manager for flag-related operations
  2. Extract the data source management logic
  3. Separate the props and methods management

This would improve code organization and make the refresh lifecycle management more maintainable.

packages/canvas/render/src/render.js (2)

564-567: Consider making event handling behavior configurable.

The current implementation always stops event propagation when the canvas flag is set. Consider making this behavior configurable to support cases where event propagation might be desired.

Example implementation:

-  if (getCanvasFlag()) {
+  const canvasConfig = configure[componentName]?.canvas || {}
+  if (getCanvasFlag() && canvasConfig.stopEvents !== false) {
     bindProps.onMouseover = stopEvent
     bindProps.onFocus = stopEvent
   }

574-576: Document canvas flag behavior.

The canvas flag now controls multiple event handling behaviors. Please add JSDoc comments explaining:

  • The purpose of the canvas flag
  • How it affects event propagation
  • When it should be enabled/disabled

Example documentation:

+  /**
+   * Conditionally applies click capture based on component configuration and canvas state.
+   * When both conditions are met, clicks will be captured and stopped from propagating.
+   */
   if (clickCapture(componentName) && getCanvasFlag()) {
     bindProps.onClickCapture = stopEvent
   }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3dcfb26 and 53d5e07.

📒 Files selected for processing (4)
  • packages/canvas/container/src/CanvasContainer.vue (3 hunks)
  • packages/canvas/render/src/RenderMain.js (2 hunks)
  • packages/canvas/render/src/context.js (1 hunks)
  • packages/canvas/render/src/render.js (2 hunks)
🔇 Additional comments (3)
packages/canvas/render/src/RenderMain.js (2)

30-32: LGTM: Import changes are well-structured

The new imports for canvas flag management are properly integrated into the existing destructured import statement.


456-458: Verify canvas flag usage in refresh lifecycle

The new canvas flag management functions are properly exported. However, let's verify their integration with the refresh lifecycle.

✅ Verification successful

Let me try another search strategy to verify the canvas flag usage.


Based on the search results, I can now generate the final response:

Canvas flag integration is properly implemented

The canvas flags are well integrated within the codebase:

  • Used in CanvasContainer.vue for conditional rendering
  • Properly integrated in render.js for event handling (mouseover, focus, click capture)
  • Flags are managed through the context module with clear getter/setter implementations
  • No direct coupling found with refresh lifecycle, which is correct as they serve different purposes:
    • Canvas flags control event handling and rendering behavior
    • Refresh functionality is handled separately through toolbar actions
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of canvas flags in refresh lifecycle
# Expected: Find references to getCanvasFlag/setCanvasFlag in lifecycle-related code

echo "Searching for canvas flag usage in lifecycle methods..."
rg -l "getCanvasFlag|setCanvasFlag" --type js --type vue

echo "Searching for refresh lifecycle implementations..."
rg -l "refresh.*lifecycle|lifecycle.*refresh" --type js --type vue

echo "Searching for canvas flag state modifications..."
ast-grep --pattern 'setCanvasFlag($expr)'

Length of output: 566


Script:

#!/bin/bash
# Search for canvas flag usage without vue extension
echo "Searching for canvas flag usage..."
rg -l "getCanvasFlag|setCanvasFlag" --type js

echo "Searching for refresh lifecycle implementations..."
rg -l "refresh.*lifecycle|lifecycle.*refresh" --type js

echo "Looking for canvas flag related code..."
rg -A 5 "getCanvasFlag|setCanvasFlag" --type js

echo "Searching for refresh related code..."
rg -A 5 "\brefresh\b" --type js

Length of output: 14866

packages/canvas/render/src/render.js (1)

23-23: LGTM: Import changes are well-organized.

The addition of getCanvasFlag import is properly placed with related context imports.

packages/canvas/container/src/CanvasContainer.vue Outdated Show resolved Hide resolved
@yy-wow yy-wow added the ready_for_review This PR requires more reviews label Nov 8, 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: 0

🧹 Outside diff range and nitpick comments (2)
packages/toolbars/refresh/src/Main.vue (2)

36-51: Add JSDoc comments to document side effects

The refreshResource function performs important side effects like clearing block cache and reloading the canvas. These should be documented for maintainability.

+/**
+ * Refreshes canvas resources by clearing block cache and reloading the canvas.
+ * Also executes the afterRefresh hook if defined.
+ * @throws {Error} Catches and notifies errors from afterRefresh execution
+ */
 const refreshResource = () => {

75-93: Enhance error handling with specific error types

While the error handling is good, it could be more informative by distinguishing between different types of errors.

 const refresh = async () => {
   try {
     if (typeof beforeRefresh === 'function') {
-      await beforeRefresh()
+      try {
+        await beforeRefresh()
+      } catch (error) {
+        useNotify({
+          type: 'error',
+          message: `Error in beforeRefresh: ${error}`
+        })
+        return
+      }
     }

     if (typeof refreshMethod === 'function') {
-      const stop = await refreshMethod()
+      try {
+        const stop = await refreshMethod()
+        if (stop) {
+          return
+        }
+      } catch (error) {
+        useNotify({
+          type: 'error',
+          message: `Error in refreshMethod: ${error}`
+        })
+        return
+      }
-      if (stop) {
-        return
-      }
     }
   } catch (error) {
     useNotify({
       type: 'error',
-      message: `Error in refreshing: ${error}`
+      message: `Unexpected error during refresh: ${error}`
     })
+    return
   }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 53d5e07 and 7b5ddac.

📒 Files selected for processing (1)
  • packages/toolbars/refresh/src/Main.vue (4 hunks)
🔇 Additional comments (2)
packages/toolbars/refresh/src/Main.vue (2)

7-15: LGTM: Import changes are well-structured

The new imports for useNotify and getOptions are properly organized and support the enhanced error handling and lifecycle hooks.


34-34: Verify meta.id availability

The lifecycle hooks are obtained using meta.id. Let's ensure this ID is always available when needed.

✅ Verification successful

meta.id is properly defined and used

The verification shows that:

  • meta.id is properly defined as 'engine.toolbars.refresh' in packages/toolbars/refresh/meta.js
  • The file is correctly imported in Main.vue
  • getOptions is a registered utility function from '@opentiny/tiny-engine-meta-register'
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if meta.id is properly defined and used consistently
# Test 1: Check meta file definition
rg -A 5 "export default.*id:" packages/toolbars/refresh/src/meta.*

# Test 2: Check other usages of meta.id in the codebase
rg "getOptions\(meta\.id\)" --type vue

Length of output: 216


Script:

#!/bin/bash
# Let's try different approaches to verify meta.id usage

# Check for meta file with any extension
fd "meta\." packages/toolbars/refresh/src/

# Look for meta object definition
rg "meta.*=.*\{.*id:" packages/toolbars/refresh/src/

# Check for getOptions usage
rg "getOptions\(" packages/toolbars/refresh/src/

# Look for meta.id usage
rg "meta\.id" packages/toolbars/refresh/src/

# Check Vue file content to understand the context
cat packages/toolbars/refresh/src/Main.vue

Length of output: 3237


Script:

#!/bin/bash
# Let's check the meta import and its definition

# Find the meta file in the parent directory
fd "meta" packages/toolbars/refresh/

# Check the content of meta file if found
fd "meta" packages/toolbars/refresh/ -x cat {}

# Look for getOptions implementation to understand how it uses the id
rg -g "!*.{json,map}" "export.*getOptions" packages/

Length of output: 534

packages/canvas/render/src/context.js Outdated Show resolved Hide resolved
packages/canvas/container/src/CanvasContainer.vue Outdated Show resolved Hide resolved
packages/canvas/container/src/CanvasContainer.vue 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.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
packages/canvas/render/src/context.js (2)

59-60: Improve variable naming and documentation for better semantics.

The current name isDesignMode could be more descriptive of its specific purpose. Based on the comment, this flag controls multiple canvas behaviors (drag-and-drop, native events).

Consider renaming to something more specific like isCanvasInteractive or isCanvasEditable to better reflect its role in controlling canvas interaction features.

-// 是否表现画布内特征的标志,用来控制是否允许拖拽、原生事件是否触发等
-let isDesignMode = true
+/**
+ * Flag that controls canvas interaction features:
+ * - Enables/disables drag and drop functionality
+ * - Controls native event propagation
+ * - Manages canvas edit capabilities
+ * @type {boolean}
+ */
+let isCanvasInteractive = true

62-62: Add JSDoc documentation for the getter function.

For consistency and better code documentation, add JSDoc comments to the getter function.

+/**
+ * Gets the current canvas interaction mode.
+ * @returns {boolean} Current state of canvas interactions
+ */
 export const getDesignMode = () => isDesignMode
packages/canvas/render/src/render.js (1)

561-568: Consider consolidating event handlers for better maintainability.

While the implementation is correct, we can make it more maintainable by combining the event handlers under a single condition.

Consider this refactoring:

   [DESIGN_UIDKEY]: id,
   [DESIGN_TAGKEY]: componentName
 }

 if (getDesignMode()) {
-  bindProps.onMouseover = stopEvent
-  bindProps.onFocus = stopEvent
+  Object.assign(bindProps, {
+    onMouseover: stopEvent,
+    onFocus: stopEvent
+  })
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7b5ddac and 4b6420b.

📒 Files selected for processing (4)
  • packages/canvas/container/src/CanvasContainer.vue (3 hunks)
  • packages/canvas/render/src/RenderMain.js (2 hunks)
  • packages/canvas/render/src/context.js (1 hunks)
  • packages/canvas/render/src/render.js (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/canvas/container/src/CanvasContainer.vue
  • packages/canvas/render/src/RenderMain.js
🔇 Additional comments (3)
packages/canvas/render/src/render.js (3)

23-23: LGTM: Import statement is correctly placed.

The addition of getDesignMode import is well-organized with related context imports.


574-574: LGTM: Click capture condition is well-implemented.

The combined check for both click capture capability and design mode is correctly implemented and follows a consistent pattern.


564-574: Verify event handling behavior in both modes.

The introduction of design mode checks affects event handling. Let's verify the implementation across the codebase.

packages/canvas/render/src/context.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.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (6)
packages/common/js/constants.js (2)

99-102: Remove or translate the Chinese comments.

The constant definition looks good, but consider removing the inline Chinese comments or translating them to English to maintain consistency with the rest of the codebase.

 export const DESIGN_MODE = {
-  DESIGN: 'design', // 设计态
-  RUNTIME: 'runtime' // 运行态
+  DESIGN: 'design',
+  RUNTIME: 'runtime'
 }

99-102: Add JSDoc documentation for the DESIGN_MODE constant.

Consider adding JSDoc documentation to describe the purpose and usage of this constant, especially since it's used across multiple files for canvas behavior management.

+/**
+ * Represents the operational modes of the canvas.
+ * Used to control canvas behavior in different states.
+ * @constant
+ * @type {{DESIGN: string, RUNTIME: string}}
+ */
 export const DESIGN_MODE = {
   DESIGN: 'design',
   RUNTIME: 'runtime'
 }
packages/canvas/container/src/CanvasContainer.vue (2)

145-155: Consider extracting mousedown logic into a separate function.

The mousedown event handler contains nested logic that could be more maintainable if extracted into a named function.

Consider this refactor:

+const handleMouseDown = (event, doc) => {
+  // html element uses scroll and mouseup events
+  if (event.target === doc.documentElement) {
+    isScrolling = false
+    return
+  }
+
+  insertPosition.value = false
+  setCurrentNode(event)
+  target.value = event.target
+}
+
 win.addEventListener('mousedown', (event) => {
-  handleCanvasEvent(() => {
-    // html element uses scroll and mouseup events
-    if (event.target === doc.documentElement) {
-      isScrolling = false
-      return
-    }
-
-    insertPosition.value = false
-    setCurrentNode(event)
-    target.value = event.target
-  })
+  handleCanvasEvent(() => handleMouseDown(event, doc))
 })

184-186: Consider debouncing mousemove event handler.

The mousemove event can fire frequently during drag operations. Consider debouncing the handler to optimize performance.

Consider this improvement:

+const debouncedDragMove = debounce((ev) => dragMove(ev, true), 16) // ~60fps
+
 win.addEventListener('mousemove', (ev) => {
-  handleCanvasEvent(() => {
-    dragMove(ev, true)
-  })
+  handleCanvasEvent(() => debouncedDragMove(ev))
 })

Don't forget to import the debounce utility:

import { debounce } from '@opentiny/tiny-engine-common/js/utils'
packages/canvas/render/src/render.js (1)

564-568: Consider adding error handling for event handlers.

The event handlers are correctly implemented and the design mode check is explicit. However, consider adding try-catch blocks around the stopEvent calls to ensure robustness.

 if (getDesignMode() === DESIGN_MODE.DESIGN) {
-  bindProps.onMouseover = stopEvent
-  bindProps.onFocus = stopEvent
+  bindProps.onMouseover = (event) => {
+    try {
+      return stopEvent(event)
+    } catch (error) {
+      console.error('Error in mouseover event handler:', error)
+      return false
+    }
+  }
+  bindProps.onFocus = (event) => {
+    try {
+      return stopEvent(event)
+    } catch (error) {
+      console.error('Error in focus event handler:', error)
+      return false
+    }
+  }
 }

 if (clickCapture(componentName) && getDesignMode() === DESIGN_MODE.DESIGN) {
-  bindProps.onClickCapture = stopEvent
+  bindProps.onClickCapture = (event) => {
+    try {
+      return stopEvent(event)
+    } catch (error) {
+      console.error('Error in click capture event handler:', error)
+      return false
+    }
+  }
 }

Also applies to: 574-576

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

71-74: Add JSDoc documentation and mode validation.

The new design mode functions would benefit from:

  1. JSDoc documentation explaining their purpose, parameters, and return values
  2. Input validation for setDesignMode to ensure valid mode values
+/**
+ * Gets the current design mode of the canvas.
+ * @returns {string} The current design mode
+ */
 export const getDesignMode = () => getRenderer()?.getDesignMode()

+/**
+ * Sets the design mode of the canvas.
+ * @param {string} mode - The design mode to set
+ * @throws {Error} If the mode is invalid
+ */
 export const setDesignMode = (mode) => {
+  if (!mode || typeof mode !== 'string') {
+    throw new Error('Invalid design mode')
+  }
   getRenderer()?.setDesignMode(mode)
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4b6420b and c8f899e.

📒 Files selected for processing (6)
  • packages/canvas/container/src/CanvasContainer.vue (4 hunks)
  • packages/canvas/container/src/container.js (2 hunks)
  • packages/canvas/render/src/context.js (1 hunks)
  • packages/canvas/render/src/render.js (2 hunks)
  • packages/common/js/constants.js (1 hunks)
  • packages/toolbars/refresh/src/Main.vue (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/canvas/render/src/context.js
  • packages/toolbars/refresh/src/Main.vue
🔇 Additional comments (5)
packages/canvas/container/src/CanvasContainer.vue (1)

34-34: LGTM: Import of design mode constant.

The import of DESIGN_MODE constant is appropriate for the new design mode checks.

packages/canvas/render/src/render.js (2)

23-23: LGTM: Import changes are well-structured.

The new imports for getDesignMode and DESIGN_MODE are properly organized and align with the design mode event handling changes.


561-563: LGTM: Design mode properties are properly configured.

The design-specific properties (DESIGN_UIDKEY and DESIGN_TAGKEY) are correctly added to bindProps.

packages/canvas/container/src/container.js (2)

900-901: LGTM! API exposure follows established patterns.

The new design mode functions are appropriately exposed through the canvasApi object, maintaining consistency with other canvas operations.


71-74: Verify integration with refresh lifecycles.

Since these changes are part of the "refresh life cycles" feature:

  1. Ensure that design mode changes trigger appropriate lifecycle events
  2. Verify that the renderer properly handles design mode transitions

Also applies to: 900-901

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

🧹 Outside diff range and nitpick comments (3)
packages/canvas/common/src/constant.js (2)

5-8: LGTM! Consider adding JSDoc documentation.

The DESIGN_MODE constant is well-structured and its values clearly indicate their purpose. Since this is a crucial constant used across multiple canvas-related components, consider adding JSDoc documentation to describe its usage and impact.

+/**
+ * Represents the operational modes of the canvas.
+ * @constant
+ * @type {{DESIGN: string, RUNTIME: string}}
+ * @description
+ * - DESIGN: Design mode for editing and configuration
+ * - RUNTIME: Runtime mode for production execution
+ */
 export const DESIGN_MODE = {
   DESIGN: 'design', // 设计态
   RUNTIME: 'runtime' // 运行态
 }

6-7: Maintain consistent language in comments.

For better maintainability and international collaboration, consider using English consistently in comments.

 export const DESIGN_MODE = {
-  DESIGN: 'design', // 设计态
-  RUNTIME: 'runtime' // 运行态
+  DESIGN: 'design', // Design mode
+  RUNTIME: 'runtime' // Runtime mode
 }
packages/canvas/container/src/CanvasContainer.vue (1)

144-154: Consider refactoring event handlers for better organization.

The event handlers could benefit from better separation of concerns and consistency.

Consider these improvements:

  1. Extract mousedown logic into named functions:
 win.addEventListener('mousedown', (event) => {
   handleCanvasEvent(() => {
-    if (event.target === doc.documentElement) {
-      isScrolling = false
-      return
-    }
-
-    insertPosition.value = false
-    setCurrentNode(event)
-    target.value = event.target
+    handleMouseDown(event)
   })
 })

+const handleMouseDown = (event) => {
+  if (event.target === doc.documentElement) {
+    isScrolling = false
+    return
+  }
+
+  insertPosition.value = false
+  setCurrentNode(event)
+  target.value = event.target
+}
  1. Consider applying the same design mode check to mouseup events for consistency:
-win.addEventListener('mouseup', (event) => {
+win.addEventListener('mouseup', (event) => {
+  handleCanvasEvent(() => {
     if (event.target !== doc.documentElement || isScrolling) {
       return
     }
     insertPosition.value = false
     setCurrentNode(event)
     target.value = event.target
+  })
 })

Also applies to: 183-185

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c8f899e and 8c9737f.

📒 Files selected for processing (2)
  • packages/canvas/common/src/constant.js (1 hunks)
  • packages/canvas/container/src/CanvasContainer.vue (4 hunks)
🔇 Additional comments (1)
packages/canvas/container/src/CanvasContainer.vue (1)

35-35: LGTM: Clean import of DESIGN_MODE constant.

The import is well-organized and properly used for design mode validation.

@hexqi hexqi merged commit f135499 into opentiny:refactor/develop Nov 11, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready_for_review This PR requires more reviews refactor-main refactor/develop branch feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants