ellipsisText.vue
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<template>
<el-popover
placement="top"
trigger="hover"
:content="finalPopoverContent"
:disabled="!isOverflow"
width="400"
>
<span
ref="text"
slot="reference"
class="ellipsis"
@mouseenter="checkOverflow"
>
<slot></slot>
</span>
</el-popover>
</template>
<script>
export default {
name: 'EllipsisText',
props: {
content: [String, Number],
popoverContent: {
type: String,
default: ''
}
},
data() {
return {
isOverflow: false
}
},
computed: {
hasDefaultSlot() {
const slot = this.$slots.default
if (!slot || !slot.length) return false
return slot.some(vnode => {
if (vnode.text !== undefined) return vnode.text.trim().length > 0
return !!(vnode.tag || vnode.componentOptions)
})
},
displayText() {
const val = this.content
if (val === null || val === undefined || val === '') return '--'
return String(val)
},
finalPopoverContent() {
if (this.popoverContent) return this.popoverContent
if (this.hasDefaultSlot) return this.getSlotText() || this.displayText
return this.displayText
}
},
watch: {
content() {
this.updateReferenceText()
this.$nextTick(this.checkOverflow)
},
popoverContent() {
this.$nextTick(this.checkOverflow)
},
'$slots.default': {
handler() {
this.updateReferenceText()
this.$nextTick(this.checkOverflow)
},
deep: true
}
},
mounted() {
this.updateReferenceText()
this.checkOverflow()
},
updated() {
this.updateReferenceText()
},
methods: {
getSlotText() {
const slot = this.$slots.default
if (!slot || !slot.length) return ''
return slot.map(vnode => {
if (vnode.text) return vnode.text
return (vnode.elm && vnode.elm.textContent) || ''
}).join('').trim()
},
updateReferenceText() {
this.$nextTick(() => {
const el = this.$refs.text
if (!el || this.hasDefaultSlot) return
el.textContent = this.displayText
})
},
checkOverflow() {
const el = this.$refs.text
if (el) {
this.isOverflow = el.scrollWidth > el.clientWidth
}
}
}
}
</script>
<style scoped>
.dark-popover {
background-color: #333 !important;
color: #fff !important;
border-color: #333 !important;
max-width: 300px !important;
word-wrap: break-word;
font-size: 14px;
line-height: 1.5;
}
.dark-popover .popper__arrow,
.dark-popover .popper__arrow::after {
border-top-color: #333 !important;
}
.ellipsis {
display: inline-block;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
vertical-align: middle;
}
</style>