From 42cce3f8eb4bcbcaf110fe07155afb86dcd94c79 Mon Sep 17 00:00:00 2001 From: zhangxin <59186205+chichinohaha@users.noreply.github.com> Date: Wed, 29 Sep 2021 22:23:51 +0800 Subject: [PATCH] replace utils function ,update "sortProp" function (#9425) * replace utils function ,update "sortProp" function replace utils function - "getCustomPropElements" -> "updateCustomPropElements" this function is use to render custom node update "sortProp" function - item may be null * Update particle-system.js --- .../inspector/components/particle-system.js | 4 +- editor/inspector/components/widget.js | 9 +-- editor/inspector/utils/prop.js | 74 +++++++++++++------ 3 files changed, 59 insertions(+), 28 deletions(-) diff --git a/editor/inspector/components/particle-system.js b/editor/inspector/components/particle-system.js index 23132019e6..15b6ca34a5 100644 --- a/editor/inspector/components/particle-system.js +++ b/editor/inspector/components/particle-system.js @@ -514,13 +514,13 @@ const uiElements = { }, customProps: { update() { - this.$.customProps.replaceChildren(...propUtils.getCustomPropElements(excludeList, this.dump, (element, prop) => { + propUtils.updateCustomPropElements(this.$.customProps, excludeList, this.dump, (element, prop) => { element.className = 'customProp'; if (prop.dump.visible) { element.render(prop.dump); } element.hidden = !prop.dump.visible; - })); + }); }, }, }; diff --git a/editor/inspector/components/widget.js b/editor/inspector/components/widget.js index 0ca2ff2ff4..d4992c21fe 100644 --- a/editor/inspector/components/widget.js +++ b/editor/inspector/components/widget.js @@ -964,14 +964,13 @@ const uiElements = { if (!this.$customProps) { this.$customProps = this.$el.querySelector('#customProps'); } - this.$customProps.replaceChildren(...propUtils.getCustomPropElements(excludeList, this.dump, (element, prop) => { + propUtils.updateCustomPropElements(this.$customProps, excludeList, this.dump, (element, prop) => { element.className = 'customProp'; - const isShow = prop.dump.visible; - if (isShow) { + if (prop.dump.visible) { element.render(prop.dump); } - element.style = isShow ? '' : 'display: none;'; - })); + element.hidden = !prop.dump.visible; + }); }, }, }; diff --git a/editor/inspector/utils/prop.js b/editor/inspector/utils/prop.js index 11a16a1899..cbd7730900 100644 --- a/editor/inspector/utils/prop.js +++ b/editor/inspector/utils/prop.js @@ -10,16 +10,18 @@ exports.sortProp = function(propMap) { Object.keys(propMap).forEach((key) => { const item = propMap[key]; - if ('displayOrder' in item) { - orderList.push({ - key, - dump: item, - }); - } else { - normalList.push({ - key, - dump: item, - }); + if (item != null) { + if ('displayOrder' in item) { + orderList.push({ + key, + dump: item, + }); + } else { + normalList.push({ + key, + dump: item, + }); + } } }); @@ -30,25 +32,55 @@ exports.sortProp = function(propMap) { /** * - * This function can filter the contents of the dump and is mainly used to get the user's dump data +* This method is used to update the custom node + * @param {HTMLElement} container * @param {string[]} excludeList * @param {object} dump - * @param {(element,prop)=>void} onElementCreated + * @param {(element,prop)=>void} update */ -exports.getCustomPropElements = function(excludeList, dump, onElementCreated) { - const customPropElements = []; +exports.updateCustomPropElements = function(container, excludeList, dump, update) { const sortedProp = exports.sortProp(dump.value); + container.$ = container.$ || {}; + /** + * @type {Array} + */ + const children = []; sortedProp.forEach((prop) => { if (!excludeList.includes(prop.key)) { - const node = document.createElement('ui-prop'); - node.setAttribute('type', 'dump'); - if (typeof onElementCreated === 'function') { - onElementCreated(node, prop); + if (!prop.dump.visible) { + return; } - customPropElements.push(node); + let node = container.$[prop.key]; + if (!node) { + node = document.createElement('ui-prop'); + node.setAttribute('type', 'dump'); + node.dump = prop.dump; + node.key = prop.key; + container.$[prop.key] = node; + } + + if (typeof update === 'function') { + update(node, prop); + } + + children.push(node); + } + }); + const currentChildren = Array.from(container.children); + children.forEach((child, i) => { + if (child === currentChildren[i]) { + return; + } + + container.appendChild(child); + }); + + // delete extra children + currentChildren.forEach(($child) => { + if (!children.includes($child)) { + $child.remove(); } }); - return customPropElements; }; /** @@ -285,7 +317,7 @@ exports.setTooltip = function(element, dump) { let tooltip = dump.tooltip; if (tooltip.startsWith('i18n:')) { tooltip = Editor.I18n.t('ENGINE.' + tooltip.substr(5)); - // 如果 ENGINE 翻译不出来,就当成插件的翻译数据,尝试直接翻译 + // If ENGINE doesn't translate, use extension's translation data and try to translate directly if (!tooltip || tooltip === dump.tooltip) { tooltip = Editor.I18n.t(dump.tooltip.substr(5)) || dump.tooltip; }