perf: 文本省略组件逻辑调整

This commit is contained in:
rd
2025-08-27 09:50:22 +08:00
parent dd0c16c32d
commit 4baf054d5c

View File

@ -1,14 +1,21 @@
<template> <template>
<Tooltip <Tooltip
:disabled="isShowBtn || (!isShowBtn && disabled)" :open="open"
:placement="props.placement" :placement="props.placement"
:mouseEnterDelay="props.mouseEnterDelay"
:mouseLeaveDelay="props.mouseLeaveDelay"
> >
<template #title> <template #title>
<div :style="contentStyle" class="tip-content">{{ props.context }}</div> <div :style="contentStyle" class="tip-content">{{ props.context }}</div>
</template> </template>
<div v-bind="$attrs" ref="Text" :class="`${isShow ? '' : `line-${props.line}`} `" class="overflow-text"> <div
v-bind="$attrs"
ref="Text"
:class="`${isShow ? '' : `line-${props.line}`} `"
class="overflow-text"
@mouseenter="handleMouseEnter"
@mouseleave="
handleMouseLeave
"
>
{{ props.context }} {{ props.context }}
</div> </div>
<div <div
@ -49,19 +56,16 @@ const props = defineProps({
}, },
placement: { placement: {
type: String, type: String,
default: 'bottom', default: 'top',
}, },
line: { line: {
type: Number, type: Number,
default: 1, default: 1,
}, },
// 显示延迟时间,毫秒级
mouseEnterDelay: { mouseEnterDelay: {
type: Number, type: Number,
default: 0.01, default: 100,
},
mouseLeaveDelay: {
type: Number,
default: 0,
}, },
maxHeight: { maxHeight: {
type: [String, Number], type: [String, Number],
@ -76,10 +80,17 @@ const props = defineProps({
default: false, default: false,
}, },
}); });
const data = reactive({}); const data = reactive({});
const isShow = ref(false); const isShow = ref(false);
const open = ref(false);
const disabled = ref(false);
const Text = ref(null);
const textWidth = ref();
const timer = ref(null);
const contentStyle = computed(() => { const contentStyle = computed(() => {
let style = { const style = {
'max-height': props.maxHeight + 'px', 'max-height': props.maxHeight + 'px',
'max-width': props.maxWidth + 'px', 'max-width': props.maxWidth + 'px',
overflow: 'auto', overflow: 'auto',
@ -87,9 +98,10 @@ const contentStyle = computed(() => {
}; };
return props.maxHeight || props.maxWidth ? style : {}; return props.maxHeight || props.maxWidth ? style : {};
}); });
const disabled = ref(true); const notShowTooltip = computed(() => {
const Text = ref(null); return props.isShowBtn || (!props.isShowBtn && disabled.value);
const textWidth = ref(); });
watch( watch(
[() => props.context, textWidth], [() => props.context, textWidth],
async () => { async () => {
@ -117,7 +129,24 @@ watch(
immediate: true, immediate: true,
}, },
); );
onBeforeMount(() => {});
const clearTimer = () => {
if (timer.value) {
clearTimeout(timer.value);
}
};
const handleMouseEnter = () => {
clearTimer();
timer.value = setTimeout(() => {
open.value = !notShowTooltip.value;
}, props.mouseEnterDelay);
};
const handleMouseLeave = () => {
clearTimer();
open.value = false;
};
onMounted(async () => { onMounted(async () => {
await nextTick(); await nextTick();
const erd = elementResizeDetectorMaker(); const erd = elementResizeDetectorMaker();
@ -128,7 +157,10 @@ onMounted(async () => {
}); });
} }
}); });
watchEffect(() => {}); onUnmounted(() => {
clearTimer();
});
defineExpose({ defineExpose({
...toRefs(data), ...toRefs(data),
}); });