Skip to content

Commit 0eeac8d

Browse files
authored
feat: added ability to specify ellipsis location (start, middle, end) (#66)
1 parent 999cbf9 commit 0eeac8d

15 files changed

+18349
-1820
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
node_modules
33
/dist
4+
/demo
45

56
# local env files
67
.env.local

demo/css/app.cecbc129.css

-1
This file was deleted.

demo/css/chunk-vendors.fb88a4ae.css

-2
This file was deleted.

demo/favicon.ico

-1.12 KB
Binary file not shown.

demo/index.html

-1
This file was deleted.

demo/js/app.085b4bc6.js

-2
This file was deleted.

demo/js/app.085b4bc6.js.map

-1
This file was deleted.

demo/js/chunk-vendors.f2cec41b.js

-2
This file was deleted.

demo/js/chunk-vendors.f2cec41b.js.map

-1
This file was deleted.

demo/js/vue.ddfe293c.js

-8
This file was deleted.

demo/js/vue.ddfe293c.js.map

-1
This file was deleted.

package-lock.json

+18,240-1,799
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-clamp",
3-
"version": "0.3.2",
3+
"version": "0.4.0",
44
"description": "Clamping multiline text with ease.",
55
"scripts": {
66
"serve": "vue-cli-service serve",

src/App.vue

+92
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,101 @@
2222
<li>{{ zh ? '支持在布局变化时自动更新。' : 'Automatically updates upon layout change.' }}</li>
2323
<li>{{ zh ? '支持展开/收起被截断部分内容。' : 'The clamped text can be expanded/collapsed.' }}</li>
2424
<li>{{ zh ? '支持自定义截断文本前后内容,并且进行响应式更新。' : 'Customizable and responsive content before/after clamped text.' }}</li>
25+
<li>{{ zh ? '支持在文本末尾、中间或开始位置进行截断。' : 'Place elllipsis at the end, middle, or the start of the clamped text' }}</li>
2526
</ul>
2627
<h2 id="demo">
2728
<a href="#demo">#</a> Demo
2829
</h2>
30+
<div class="divider text-center" data-content="↓ customizable ellipsis location & max-lines"/>
31+
<section class="case">
32+
<div class="form-horizontal">
33+
<div v-if="!zh" class="form-group">
34+
<label class="form-label col-5 col-sm-12" for="location">{{ zh ? '地点' : 'Location' }}</label>
35+
<div class="col-3 col-sm-12">
36+
<label class="form-radio">
37+
<input v-model="location" type="radio" value="start">
38+
<i class="form-icon"/>
39+
Start
40+
</label>
41+
</div>
42+
<div class="col-3 col-sm-12">
43+
<label class="form-radio">
44+
<input v-model="location" type="radio" value="middle">
45+
<i class="form-icon"/>
46+
Middle
47+
</label>
48+
</div>
49+
<div class="col-1 col-sm-12">
50+
<label class="form-radio">
51+
<input v-model="location" type="radio" value="end">
52+
<i class="form-icon"/>
53+
End
54+
</label>
55+
</div>
56+
</div>
57+
<div class="form-group">
58+
<label class="form-label col-5 col-sm-12" for="lines">{{ zh ? '最大行数' : 'Max lines' }}</label>
59+
<div class="col-7 col-sm-12">
60+
<input
61+
id="lines"
62+
v-model.number="lines"
63+
class="form-input"
64+
type="number"
65+
min="1"
66+
max="8"
67+
step="1"
68+
>
69+
</div>
70+
</div>
71+
<div class="form-group">
72+
<label
73+
class="form-label col-5 col-sm-12"
74+
for="width1"
75+
>{{ zh ? '容器宽度' : 'Container width' }}</label>
76+
<div class="col-7 col-sm-12 tooltip" :data-tooltip="`${width1}px`">
77+
<input id="width1" v-model="width1" class="slider" type="range" min="240" max="600">
78+
</div>
79+
</div>
80+
<div v-if="!zh" class="form-group">
81+
<div class="col-5 col-sm-12">
82+
<label class="form-checkbox">
83+
<input v-model="hyphens1" type="checkbox">
84+
<i class="form-icon"/>
85+
CSS Hyphens
86+
</label>
87+
</div>
88+
<div class="col-5 col-sm-12">
89+
<label class="form-checkbox">
90+
<input v-model="rtl1" type="checkbox">
91+
<i class="form-icon"/>
92+
RTL
93+
</label>
94+
</div>
95+
</div>
96+
</div>
97+
<v-clamp
98+
:class="{
99+
demo: true,
100+
hyphens: hyphens1,
101+
rtl: rtl1
102+
}"
103+
:max-lines="lines"
104+
:location="location"
105+
autoresize
106+
:style="{
107+
width: `${width1}px`
108+
}"
109+
>
110+
{{ zh ? textZh : text }}
111+
<template #after="{ toggle, expanded, clamped }">
112+
<button
113+
v-if="expanded || clamped"
114+
class="toggle btn btn-sm"
115+
@click="toggle"
116+
>{{ zh ? '切换' : 'Toggle' }}</button>
117+
</template>
118+
</v-clamp>
119+
</section>
29120
<div class="divider text-center" data-content="↓ max-lines & slot `after`"/>
30121
<section class="case">
31122
<div class="form-horizontal">
@@ -533,6 +624,7 @@ export default {
533624
return {
534625
lines: 3,
535626
width1: 600,
627+
location: 'end',
536628
hyphens1: true,
537629
rtl1: false,
538630
expanded1: false,

src/components/Clamp.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ export default {
1717
type: String,
1818
default: '…'
1919
},
20+
location: {
21+
type: String,
22+
default: 'end',
23+
validator (value) {
24+
return ['start', 'middle', 'end'].indexOf(value) !== -1
25+
}
26+
},
2027
expanded: Boolean
2128
},
2229
data () {
@@ -28,7 +35,14 @@ export default {
2835
},
2936
computed: {
3037
clampedText () {
31-
return this.text.slice(0, this.offset) + this.ellipsis
38+
if (this.location === 'start') {
39+
return this.ellipsis + (this.text.slice(0, this.offset) || '').trim()
40+
} else if (this.location === 'middle') {
41+
const split = Math.floor(this.offset / 2)
42+
return (this.text.slice(0, split) || '').trim() + this.ellipsis + (this.text.slice(-split) || '').trim()
43+
}
44+
45+
return (this.text.slice(0, this.offset) || '').trim() + this.ellipsis
3246
},
3347
isClamped () {
3448
if (!this.text) {

0 commit comments

Comments
 (0)