From a2e017be5d2a4edbf291b4197e5c474ba9cc5fac Mon Sep 17 00:00:00 2001 From: Gin Date: Mon, 19 Jun 2023 09:37:32 +0800 Subject: [PATCH] [Optimize] improve: rendering logic of effect select options (#15446) * improve: rendering logic of effect select options * update: escape option label string --- editor/inspector/assets/material.js | 68 +++++++++++++++++------------ 1 file changed, 41 insertions(+), 27 deletions(-) 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 += ``; - } - - // for option label, remove group name if matched - optionLabel = optionLabel.replace(effectGroupNameRE, ''); + continue; } - // remove prefix 'effects'(after 'db://' prefix removed) - if (effectDirRE.test(optionLabel)) { - optionLabel = optionLabel.replace(effectDirRE, ''); - } - - // use full name as option value - htmlTemplate += ``; + // no group name, add to extras and render as ungrouped(before grouped options) + const label = formatOptionLabel(effect.name); + extras.push(``); } - if (currGroup !== '') { - htmlTemplate += ''; + let htmlTemplate = ''; + + for (const extra of extras) { + htmlTemplate += extra; + } + + for (const name in groups) { + const options = groups[name]; + htmlTemplate += `${options.join('')}`; } return htmlTemplate;