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
|
<script>
import { GlResizeObserverDirective, GlButton } from '@gitlab/ui';
import { STATES, SHOW_MORE, SHOW_LESS } from './constants';
export default {
name: 'TruncatedText',
components: {
GlButton,
},
directives: {
GlResizeObserver: GlResizeObserverDirective,
},
props: {
lines: {
type: Number,
required: false,
default: 3,
},
mobileLines: {
type: Number,
required: false,
default: 10,
},
},
data() {
return {
state: STATES.INITIAL,
};
},
computed: {
showTruncationToggle() {
return this.state !== STATES.INITIAL;
},
truncationToggleText() {
if (this.state === STATES.TRUNCATED) {
return SHOW_MORE;
}
return SHOW_LESS;
},
styleObject() {
// eslint-disable-next-line @gitlab/require-i18n-strings
return { '--lines': this.lines, '--mobile-lines': this.mobileLines };
},
isTruncated() {
return this.state === STATES.EXTENDED ? null : 'gl-truncate-text-by-line gl-overflow-hidden';
},
},
methods: {
onResize({ target }) {
if (target.scrollHeight > target.offsetHeight) {
this.state = STATES.TRUNCATED;
} else if (this.state === STATES.TRUNCATED) {
this.state = STATES.INITIAL;
}
},
toggleTruncation() {
if (this.state === STATES.TRUNCATED) {
this.state = STATES.EXTENDED;
} else if (this.state === STATES.EXTENDED) {
this.state = STATES.TRUNCATED;
}
},
},
};
</script>
<template>
<section>
<article
ref="content"
v-gl-resize-observer="onResize"
:class="isTruncated"
:style="styleObject"
>
<slot></slot>
</article>
<gl-button v-if="showTruncationToggle" variant="link" @click="toggleTruncation">{{
truncationToggleText
}}</gl-button>
</section>
</template>
|