perf: 优化消息定位

This commit is contained in:
kuaifan 2025-03-13 01:38:35 +08:00
parent bc5343652b
commit 635cc04c50
2 changed files with 37 additions and 4 deletions

View File

@ -1082,13 +1082,14 @@ const timezone = require("dayjs/plugin/timezone");
/** /**
* 滚动到元素并抖动 * 滚动到元素并抖动
* @param element * @param element
* @param viewIfNeeded
*/ */
scrollIntoAndShake(element) { scrollIntoAndShake(element, viewIfNeeded = true) {
if (!element) return; if (!element) return;
const elements = Array.isArray(element) ? element : [element]; const elements = Array.isArray(element) ? element : [element];
elements.forEach(el => { elements.forEach(el => {
if (el) { if (el) {
$A.scrollIntoViewIfNeeded(el); viewIfNeeded && $A.scrollIntoViewIfNeeded(el);
$A.addClassWithTimeout(el, "common-shake", 800); $A.addClassWithTimeout(el, "common-shake", 800);
} }
}); });
@ -1300,6 +1301,28 @@ const timezone = require("dayjs/plugin/timezone");
item === arr2[arr2.length - arr1.length + index] item === arr2[arr2.length - arr1.length + index]
).length)); ).length));
}, },
/**
* 查找元素并在失败时重试
* @param {Function} findElementFn - 查找元素的函数
* @param {number} maxAttempts - 最大尝试次数
* @param {number} delayMs - 每次尝试之间的延迟(毫秒)
* @returns {Promise<*>}
*/
async findElementWithRetry(findElementFn, maxAttempts = 3, delayMs = 500) {
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
const element = findElementFn();
if (element) {
return element;
}
if (attempt < maxAttempts) {
await new Promise(resolve => setTimeout(resolve, delayMs));
}
}
throw new Error(`Element not found after ${maxAttempts} attempts`);
}
}); });
/** /**

View File

@ -1487,8 +1487,7 @@ export default {
msgActiveId(val) { msgActiveId(val) {
if (val > 0) { if (val > 0) {
this.msgActiveId = 0 this.msgActiveId = 0
const element = this.$refs.scroller.$el.querySelector(`[data-id="${val}"]`)?.querySelector(".dialog-head") this.shakeToMsgId(val)
$A.scrollIntoAndShake(element)
} }
}, },
@ -2650,6 +2649,8 @@ export default {
msg_id: this.msgId, msg_id: this.msgId,
msg_type: this.msgType, msg_type: this.msgType,
clear_before: true clear_before: true
}).then(_ => {
this.onToBottom()
}).catch(_ => {}) }).catch(_ => {})
}, },
@ -4382,6 +4383,15 @@ export default {
$A.messageError(msg); $A.messageError(msg);
}); });
}, },
async shakeToMsgId(id) {
try {
const element = await $A.findElementWithRetry(() => this.$refs.scroller.$el.querySelector(`[data-id="${id}"]`)?.querySelector(".dialog-head"));
$A.scrollIntoAndShake(element, false)
} catch (e) {
// console.log(e)
}
}
} }
} }
</script> </script>