From 37b02d5c136ac00789c3ff136dc21086a3d9b4c2 Mon Sep 17 00:00:00 2001 From: xyf-mac Date: Sun, 7 Nov 2021 23:03:06 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D3.x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/package.json | 2 ++ source/src/inject/index.ts | 57 ++++++++++++++++++++++++++++++++------ 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/source/package.json b/source/package.json index 65ab458..13f49d4 100644 --- a/source/package.json +++ b/source/package.json @@ -10,12 +10,14 @@ }, "dependencies": { "@types/kind-of": "^6.0.0", + "@types/lodash": "^4.14.176", "babel-eslint": "^10.1.0", "core-js": "^3.6.5", "element-ui": "^2.15.1", "fs-extra": "^9.1.0", "less": "^4.1.1", "less-loader": "^7.3.0", + "lodash": "^4.17.21", "uuid": "^8.3.2", "vue": "^2.6.11", "vue-class-component": "^7.2.3", diff --git a/source/src/inject/index.ts b/source/src/inject/index.ts index f97a204..cc46bd0 100644 --- a/source/src/inject/index.ts +++ b/source/src/inject/index.ts @@ -19,6 +19,8 @@ import { } from "@/devtools/data"; import {Msg, Page, PluginEvent} from "@/core/types" import {BuildArrayOptions, BuildImageOptions, BuildObjectOptions, BuildVecOptions} from "@/inject/types"; +// @ts-ignore +import {uniq} from "lodash" declare const cc: any; @@ -187,22 +189,61 @@ class CCInspector { return typeof cc !== "undefined"; } + getAllPropertyDescriptors(obj: Object): string[] { + let keys: string[] = []; + + function circle(root: Object) { + const descriptors = Object.getOwnPropertyDescriptors(root); + for (let descriptorsKey in descriptors) { + if (Object.hasOwnProperty.call(descriptors, descriptorsKey)) { + const value = descriptors[descriptorsKey]; + // 不可枚举的属性,并且允许修改get set的才有效 + if (!value.enumerable && value.configurable) { + keys.push(descriptorsKey); + } + } + } + const proto = Object.getPrototypeOf(root); + if (proto) { + circle(proto) + } + } + + circle(obj) + return keys; + } + _getNodeKeys(node: any) { - let keys = []; + // 3.x变成了getter let excludeProperty = [ - "children", "quat", "node", + "children", "quat", "node", "components", "parent", // 生命周期函数 "onFocusInEditor", "onRestore", "start", "lateUpdate", "update", "resetInEditor", "onLostFocusInEditor", "onEnable", "onDisable", "onDestroy", "onLoad", ]; - for (let key in node) { - if (!key.startsWith("_") && - !excludeProperty.includes(key) && - typeof node[key] !== "function") { - keys.push(key); + const keyHidden = this.getAllPropertyDescriptors(node); + const keyVisible1 = Object.keys(node); // Object不走原型链 + let keyVisible2: string[] = []; + for (let nodeKey in node) {// 走原型链 + // 这里的判断暂时不能去掉,否则会检索不到很多数据 + if (Object.hasOwnProperty.call(node, nodeKey) || true) { + keyVisible2.push(nodeKey) } } - return keys; + let allKeys: string[] = uniq(keyHidden.concat(keyVisible1, keyVisible2)).sort(); + allKeys = allKeys.filter(key => { + return !key.startsWith("_") && !excludeProperty.includes(key); + }); + + allKeys = allKeys.filter(key => { + try { + return typeof node[key] !== "function" + } catch (e) { + // console.warn(`属性${key}出现异常:\n`, e); + return false; + } + }) + return allKeys; } _getPairProperty(key: string) {