mirror of
https://github.com/kuaifan/dootask.git
synced 2025-12-12 03:01:12 +00:00
perf: 优化消息定位
This commit is contained in:
parent
bc5343652b
commit
635cc04c50
27
resources/assets/js/functions/common.js
vendored
27
resources/assets/js/functions/common.js
vendored
@ -1082,13 +1082,14 @@ const timezone = require("dayjs/plugin/timezone");
|
||||
/**
|
||||
* 滚动到元素并抖动
|
||||
* @param element
|
||||
* @param viewIfNeeded
|
||||
*/
|
||||
scrollIntoAndShake(element) {
|
||||
scrollIntoAndShake(element, viewIfNeeded = true) {
|
||||
if (!element) return;
|
||||
const elements = Array.isArray(element) ? element : [element];
|
||||
elements.forEach(el => {
|
||||
if (el) {
|
||||
$A.scrollIntoViewIfNeeded(el);
|
||||
viewIfNeeded && $A.scrollIntoViewIfNeeded(el);
|
||||
$A.addClassWithTimeout(el, "common-shake", 800);
|
||||
}
|
||||
});
|
||||
@ -1300,6 +1301,28 @@ const timezone = require("dayjs/plugin/timezone");
|
||||
item === arr2[arr2.length - arr1.length + index]
|
||||
).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`);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
|
||||
@ -1487,8 +1487,7 @@ export default {
|
||||
msgActiveId(val) {
|
||||
if (val > 0) {
|
||||
this.msgActiveId = 0
|
||||
const element = this.$refs.scroller.$el.querySelector(`[data-id="${val}"]`)?.querySelector(".dialog-head")
|
||||
$A.scrollIntoAndShake(element)
|
||||
this.shakeToMsgId(val)
|
||||
}
|
||||
},
|
||||
|
||||
@ -2650,6 +2649,8 @@ export default {
|
||||
msg_id: this.msgId,
|
||||
msg_type: this.msgType,
|
||||
clear_before: true
|
||||
}).then(_ => {
|
||||
this.onToBottom()
|
||||
}).catch(_ => {})
|
||||
},
|
||||
|
||||
@ -4382,6 +4383,15 @@ export default {
|
||||
$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>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user