|
|
@@ -0,0 +1,111 @@
|
|
|
+import ElementUI from 'element-ui';
|
|
|
+
|
|
|
+/**
|
|
|
+ * element-ui 的 el-select 在 mounted 里会同步读一次内部 input 的几何尺寸:
|
|
|
+ *
|
|
|
+ * node_modules/element-ui/lib/element-ui.common.js
|
|
|
+ * this.initialInputHeight = input.getBoundingClientRect().height || sizeMap[this.selectSize];
|
|
|
+ *
|
|
|
+ * 这行读发生在 Vue 的 patch 过程中——组件刚被插入 DOM、布局还是脏的,
|
|
|
+ * 于是每挂载一个 el-select 都会强制整篇文档重排一次。
|
|
|
+ * 销售订单物品清单每行 8 个下拉,100 行就是 800 个 el-select,
|
|
|
+ * 一次渲染 = 800 次强制重排;文档规模到 6 万个布局对象时,
|
|
|
+ * 单是这 3915 次强制布局就吃掉 14.4s(2026-11-02 性能 trace 实测)。
|
|
|
+ *
|
|
|
+ * 这里把挂载期的那一次几何读取换成常量:
|
|
|
+ * element-ui 自己的 sizeMap(medium 36 / small 32 / mini 28)
|
|
|
+ * 与 theme-chalk 的 .el-input--medium/-small/-mini 高度完全一致,
|
|
|
+ * 数值语义不变,只是不再触发强制布局。
|
|
|
+ * 首次遇到某个尺寸时量一次真实高度兜底(项目改过 input 高度时也能对上),
|
|
|
+ * 之后同尺寸直接复用缓存——整站每个尺寸最多一次强制布局。
|
|
|
+ *
|
|
|
+ * initialInputHeight 只被多选模式的 resetInputHeight 用到(sizeInMap 兜底),
|
|
|
+ * 单选下拉完全不受影响,因此语义等价。
|
|
|
+ */
|
|
|
+
|
|
|
+// element-ui theme-chalk 里 .el-input__inner 的各尺寸高度
|
|
|
+const SIZE_HEIGHT = {
|
|
|
+ large: 40,
|
|
|
+ medium: 36,
|
|
|
+ small: 32,
|
|
|
+ mini: 28
|
|
|
+};
|
|
|
+
|
|
|
+// size -> 真实高度缓存,同一尺寸只量一次
|
|
|
+const heightCache = new Map();
|
|
|
+
|
|
|
+function buildRect(height) {
|
|
|
+ const rect = {
|
|
|
+ x: 0,
|
|
|
+ y: 0,
|
|
|
+ top: 0,
|
|
|
+ left: 0,
|
|
|
+ right: 0,
|
|
|
+ bottom: 0,
|
|
|
+ width: 0,
|
|
|
+ height
|
|
|
+ };
|
|
|
+ rect.toJSON = () => ({ ...rect });
|
|
|
+ return rect;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 给 element-ui 的 el-select 打补丁:挂载期不再做同步几何读取。
|
|
|
+ *
|
|
|
+ * 必须在 Vue.use(EleAdmin) 之前调用:
|
|
|
+ * element-ui 的 install 里执行 Vue.component(name, definition),
|
|
|
+ * 内部会立刻 Vue.extend,把生命周期钩子数组固化下来;
|
|
|
+ * 晚于 install 再改 mounted 不会生效。
|
|
|
+ *
|
|
|
+ * @returns {boolean} 是否成功打上补丁
|
|
|
+ */
|
|
|
+export function patchElementSelectMountLayout() {
|
|
|
+ const Select = ElementUI && ElementUI.Select;
|
|
|
+ if (!Select || typeof Select.mounted !== 'function') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (Select.__mountLayoutPatched) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ const originMounted = Select.mounted;
|
|
|
+
|
|
|
+ Select.mounted = function patchedSelectMounted() {
|
|
|
+ const reference = this.$refs && this.$refs.reference;
|
|
|
+ const input =
|
|
|
+ reference && reference.$el && reference.$el.querySelector('input');
|
|
|
+ if (!input) {
|
|
|
+ return originMounted.call(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ const size = this.selectSize;
|
|
|
+ let height = heightCache.get(size);
|
|
|
+ if (height === undefined) {
|
|
|
+ // 同尺寸第一次出现:量一次真实高度(整站每尺寸仅此一次强制布局)
|
|
|
+ const measured = input.getBoundingClientRect().height;
|
|
|
+ if (measured) {
|
|
|
+ height = measured;
|
|
|
+ } else {
|
|
|
+ // 元素还不可见(挂在隐藏容器里)时量到 0,退回 element-ui 的尺寸表
|
|
|
+ height = SIZE_HEIGHT[size] != null ? SIZE_HEIGHT[size] : 0;
|
|
|
+ }
|
|
|
+ heightCache.set(size, height);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 挂载期间把这次几何读取挡掉,原始实现拿到的数值和之前完全一致
|
|
|
+ const originRect = input.getBoundingClientRect;
|
|
|
+ input.getBoundingClientRect = function () {
|
|
|
+ return buildRect(height);
|
|
|
+ };
|
|
|
+ try {
|
|
|
+ return originMounted.call(this);
|
|
|
+ } finally {
|
|
|
+ input.getBoundingClientRect = originRect;
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ Select.__mountLayoutPatched = true;
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+export default patchElementSelectMountLayout;
|