diff --git a/editor/inspector/assets/material.js b/editor/inspector/assets/material.js
index 7ee5f61ffa..9d59585ebf 100644
--- a/editor/inspector/assets/material.js
+++ b/editor/inspector/assets/material.js
@@ -3,54 +3,68 @@
const { materialTechniquePolyfill } = require('../utils/material');
const { setDisabled, setReadonly, setHidden, loopSetAssetDumpDataReadonly } = require('../utils/prop');
const { join, sep, normalize } = require('path');
+const { escape } = require('lodash');
const effectGroupNameRE = /^db:\/\/(\w+)\//i; // match root DB name
const effectDirRE = /^effects\//i;
+/**
+ * @param {string} label
+ */
+function formatOptionLabel(label) {
+ // 1. remove group name if matched
+ // 2. remove prefix 'effects'(after 'db://' prefix removed)
+ // 3. escape label string because it will be used as html template string
+ return escape(label.replace(effectGroupNameRE, '').replace(effectDirRE, ''));
+}
+
/**
*
- * @param {{name: string; uuid: string; assetPath: string}[]} sortedEffects
+ * @param {{name: string; uuid: string; assetPath: string}[]} effects
* @returns html template
*/
-function renderGroupEffectOptions(sortedEffects) {
- const groupNames = new Set();
+function renderGroupEffectOptions(effects) {
+ // group effects by group name, and no longer rely on the ordering of the input `effects`.
+ const groups = {};
- let htmlTemplate = '';
- let currGroup = '';
+ /**
+ * ungrouped options. html template string.
+ * @type {string[]}
+ */
+ const extras = [];
- for (const effect of sortedEffects) {
+ for (const effect of effects) {
const groupName = effectGroupNameRE.exec(effect.assetPath)?.[1] ?? '';
- let optionLabel = effect.assetPath;
if (groupName !== '') {
- // complete prev group
- if (currGroup !== '' && currGroup !== groupName) {
- htmlTemplate += '';
+ let group = groups[groupName];
+ // group not found yet, init it
+ if (!Array.isArray(group)) {
+ group = [];
+ groups[groupName] = group;
}
- if (!groupNames.has(groupName)) {
- groupNames.add(groupName);
+ const label = formatOptionLabel(effect.assetPath);
- currGroup = groupName;
+ group.push(``);
- htmlTemplate += `';
+ let htmlTemplate = '';
+
+ for (const extra of extras) {
+ htmlTemplate += extra;
+ }
+
+ for (const name in groups) {
+ const options = groups[name];
+ htmlTemplate += ``;
}
return htmlTemplate;