Skip to content

Conversation

westng
Copy link
Member

@westng westng commented Nov 6, 2024

add:自动加载assets/icons目录下的svg图标

Summary by CodeRabbit

  • New Features

    • Introduced dynamic loading of custom SVG icons for enhanced icon selection.
    • Updated icon display logic to include both standard and custom icons based on tab selection.
  • Bug Fixes

    • Improved handling of icon selection states and click events for custom icons.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 6, 2024
Copy link

coderabbitai bot commented Nov 6, 2024

Walkthrough

The changes in ma-icon-panel.vue enhance the component by enabling the dynamic loading of custom SVG icons. This is achieved through the use of import.meta.glob to import SVG files from a specified directory, creating a list of custom icons. The new appendCustomIcons function constructs an object with metadata for these icons, which is then integrated into the existing icon selection logic. The updated logic ensures that both standard and custom icons can be displayed based on the selected tab.

Changes

File Path Change Summary
src/components/ma-icon-picker/ma-icon-panel.vue - Added const customIcons and const customIconList variables.
- Introduced appendCustomIcons() function.
- Updated const updatedData to include results from appendCustomIcons().
- Modified icon selection logic to incorporate custom icons.

Poem

🐰 In the meadow where icons play,
New SVG friends join the fray!
With a hop and a skip, they gleam so bright,
Custom and standard, a beautiful sight.
Click away, let the icons show,
In our panel, watch them glow! ✨


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.

@dosubot dosubot bot added the enhancement label Nov 6, 2024
Copy link

@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: 2

🧹 Outside diff range and nitpick comments (1)
web/src/components/ma-icon-picker/ma-icon-panel.vue (1)

45-49: Consider performance and error handling improvements for SVG loading.

The eager loading of all SVG files could impact initial load performance. Consider these improvements:

  1. Use lazy loading (eager: false) for better initial load performance
  2. Add validation for SVG files
  3. Add TypeScript types for the imported files
-const customIcons = import.meta.glob('@/assets/icons/*.svg', { eager: true })
+const customIcons = import.meta.glob<{ default: string }>('@/assets/icons/*.svg', { 
+  eager: false,
+  transform: (svg) => {
+    // Basic SVG validation
+    if (!svg.default?.includes('<svg')) {
+      throw new Error('Invalid SVG file');
+    }
+    return svg;
+  }
+})
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 941103f and a15bbbd.

📒 Files selected for processing (1)
  • web/src/components/ma-icon-picker/ma-icon-panel.vue (2 hunks)
🔇 Additional comments (1)
web/src/components/ma-icon-picker/ma-icon-panel.vue (1)

45-49: Verify SVG content security.

Loading external SVG files could pose security risks if they contain malicious scripts. Consider implementing SVG sanitization.

Comment on lines +114 to +126
<template v-for="item in updatedData" :key="item.prefix">
<el-tab-pane :label="item.info.name" :name="item.prefix" class="mt-2">
<div class="relative">
<OverlayScrollbarsComponent class="h-[400px] p-3 pl-2 pr-4">
<div class="icon-content">
<template v-for="icon in pageData">
<div
class="icon-item" :class="{
active: `${item.prefix}:${icon}` === model,
active: currentName === 'custom' ? icon === model : `${item.prefix}:${icon}` === model,
}"
@click="selected(`${item.prefix}:${icon}`)"
@click="selected(currentName === 'custom' ? icon : `${item.prefix}:${icon}`)"
>
<ma-svg-icon :name="`${item.prefix}:${icon}`" :size="26" />
<ma-svg-icon :name="currentName === 'custom' ? icon : `${item.prefix}:${icon}`" :size="26" />
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Simplify custom icon handling logic and add error handling.

The current implementation has repetitive conditions and lacks error handling. Consider:

  1. Extracting the icon name resolution logic
  2. Adding loading states for dynamic icons
  3. Handling icon load failures
+const getIconName = (icon: string) => currentName.value === 'custom' ? icon : `${item.prefix}:${icon}`

 <template v-for="icon in pageData">
   <div
     class="icon-item"
-    :class="{
-      active: currentName === 'custom' ? icon === model : `${item.prefix}:${icon}` === model,
-    }"
-    @click="selected(currentName === 'custom' ? icon : `${item.prefix}:${icon}`)"
+    :class="{ active: getIconName(icon) === model }"
+    @click="selected(getIconName(icon))"
   >
-    <ma-svg-icon :name="currentName === 'custom' ? icon : `${item.prefix}:${icon}`" :size="26" />
+    <ma-svg-icon
+      :name="getIconName(icon)"
+      :size="26"
+      @error="handleIconError"
+      v-loading="isLoading(icon)"
+    />
   </div>
 </template>

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +51 to +67
// 将自定义图标追加到 data 中
function appendCustomIcons() {
const customData = {
prefix: 'custom',
info: {
name: '自定义图标',
total: customIconList.length,
version: '1.0.0',
},
icons: customIconList,
}
return [...data, customData]
}

// 将处理后的数据赋值回 data
const updatedData = appendCustomIcons()

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve custom icon data structure implementation.

Several improvements are needed:

  1. The name "自定义图标" should use i18n
  2. Add validation for icon name collisions
  3. Consider making the version configurable
 function appendCustomIcons() {
+  // Check for duplicate icon names
+  const existingIcons = new Set(data.flatMap(d => d.icons))
+  const duplicates = customIconList.filter(icon => existingIcons.has(icon))
+  if (duplicates.length > 0) {
+    console.warn(`Duplicate icon names found: ${duplicates.join(', ')}`)
+  }
+
   const customData = {
     prefix: 'custom',
     info: {
-      name: '自定义图标',
+      name: t('customIcons'),  // Add this to i18n
       total: customIconList.length,
-      version: '1.0.0',
+      version: import.meta.env.VITE_APP_VERSION || '1.0.0',
     },
     icons: customIconList,
   }
   return [...data, customData]
 }

Committable suggestion skipped: line range outside the PR's diff.

@kanyxmo kanyxmo merged commit e906d4e into mineadmin:master Nov 6, 2024
3 checks passed
Copy link

boring-cyborg bot commented Nov 6, 2024

Awesome work, congrats on your first merged pull request! 祝贺你的第一个拉取请求被合并!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants