Skip to content

Commit af0f50d

Browse files
authored
Merge pull request #40 from CurryPaste/main
组件更新
2 parents fd6495a + 2b8b1d0 commit af0f50d

36 files changed

Lines changed: 2023 additions & 242 deletions

example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
node_modules
33
/dist
4+
/docs
45

56

67
# local env files

example/package-lock.json

Lines changed: 217 additions & 237 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,36 @@
55
"scripts": {
66
"serve": "vue-cli-service serve",
77
"build": "vue-cli-service build",
8+
"build:self": "npm run build:ellipsis",
9+
"build:cropper": "vue-cli-service build --target lib --name vueCropper ./src/components/UploadImg.vue",
10+
"build:ellipsis": "vue-cli-service build --target lib --name ellipsis ./src/components/EllipsisFun.vue",
11+
"build:fluctuation": "vue-cli-service build --target lib --name vueFluctuation ./src/components/Fluctuation.vue",
12+
"build:docs": "vue-cli-service build --dest docs",
813
"test:unit": "vue-cli-service test:unit",
914
"lint": "vue-cli-service lint",
1015
"getTypes": " vue-cli-service getTypes"
1116
},
1217
"dependencies": {
1318
"@ant-design-vue/use": "^0.0.1-alpha.8",
19+
"@findsoft/vue-fluctuation": "^1.0.3",
20+
"@findsoft/vue-fluctuation-v3": "^1.0.0",
1421
"ant-design-vue": "^2.0.0-rc.5",
1522
"axios": "^0.21.1",
1623
"core-js": "^3.6.5",
24+
"cropperjs": "^1.5.11",
1725
"mockjs": "^1.1.0",
1826
"moment": "^2.29.1",
1927
"register-service-worker": "^1.7.1",
28+
"to-top-vue": "^1.0.7",
2029
"vue": "^3.0.0",
30+
"vue-cropperjs": "^5.0.0",
2131
"vue-router": "^4.0.0-0",
2232
"vuex": "^4.0.0-0"
2333
},
2434
"devDependencies": {
2535
"@types/jest": "^24.0.19",
2636
"@types/mockjs": "^1.0.3",
37+
"@types/vue-cropperjs": "^4.1.1",
2738
"@typescript-eslint/eslint-plugin": "^2.33.0",
2839
"@typescript-eslint/parser": "^2.33.0",
2940
"@vue/cli-plugin-babel": "~4.5.0",
33.2 KB
Loading
3.43 KB
Loading
9.67 KB
Loading
5.34 KB
Loading
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<template lang="pug">
2+
a-tooltip(:title="text")
3+
span.ellipsis(
4+
v-if='type === "length"'
5+
) {{showText}}
6+
7+
span.ellipsis.text-flow-ellipsis-multiple(
8+
v-else-if='type === "rows"'
9+
:style='typeRows()') {{showText}}
10+
11+
.ellipsis.box(
12+
v-else-if='type === "height"'
13+
ref='ellipsis'
14+
:style='typeHeight()') {{showText}}
15+
</template>
16+
17+
<script lang="ts">
18+
import { defineComponent, onMounted, ref } from 'vue';
19+
import { Tooltip } from 'ant-design-vue';
20+
21+
export default defineComponent({
22+
components: {
23+
aTooltip: Tooltip,
24+
},
25+
props: {
26+
/** 要省略的文本 */
27+
text: {
28+
type: String,
29+
default: '',
30+
},
31+
/** 省略类型 length、rows、height */
32+
type: {
33+
type: String,
34+
default: 'length',
35+
},
36+
/***/
37+
number: {
38+
type: Number,
39+
default: 1,
40+
},
41+
42+
/** */
43+
},
44+
setup(props) {
45+
const ellipsis = ref(null);
46+
const showText = ref(''); /** 组件上显示的文本 */
47+
48+
const typeLength = () => {
49+
if (props.number < props.text.length) return `${props.text.substr(0, props.number)}...`;
50+
return props.text;
51+
};
52+
const typeRows = () => ({
53+
'-webkit-line-clamp': props.number,
54+
});
55+
const typeHeight = () => ({
56+
height: `${props.number}px`,
57+
});
58+
59+
onMounted(() => {
60+
let res = '';
61+
if (props.type === 'length') res = typeLength();
62+
if (['rows', 'height'].includes(props.type)) res = props.text;
63+
showText.value = res;
64+
65+
console.log(ellipsis.value);
66+
});
67+
68+
return {
69+
ellipsis,
70+
showText,
71+
typeRows,
72+
typeHeight,
73+
};
74+
},
75+
});
76+
</script>
77+
78+
<style lang="stylus" scoped>
79+
/** 多行省略 */
80+
.text-flow-ellipsis-multiple
81+
word-break break-all
82+
text-overflow ellipsis
83+
display -webkit-box
84+
-webkit-box-orient: vertical
85+
// -webkit-line-clamp: 2
86+
overflow hidden
87+
/** 高度省略 */
88+
.box
89+
position relative
90+
overflow hidden
91+
&:after
92+
content "..."
93+
position absolute
94+
bottom 0px
95+
right 0px
96+
padding-left 10px
97+
background transparent
98+
// background -webkit-linear-gradient(left, transparent, #fff 55%)
99+
// background -o-linear-gradient(right, transparent, #fff 55%)
100+
// background -moz-linear-gradient(right, transparent, #fff 55%)
101+
// background linear-gradient(to right, transparent, #fff 55%)
102+
</style>
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
<template lang="pug">
2+
.main
3+
template(v-if='showAll')
4+
a-tooltip(:title="text")
5+
span.ellipsis(
6+
ref='ellipsis'
7+
) {{showText}}
8+
template(v-else)
9+
span.ellipsis(
10+
ref='ellipsis'
11+
) {{showText}}
12+
</template>
13+
14+
<script lang="ts">
15+
/* eslint-disable @typescript-eslint/no-explicit-any */
16+
import { defineComponent, onMounted, ref } from 'vue';
17+
import { Tooltip } from 'ant-design-vue';
18+
19+
export default defineComponent({
20+
components: {
21+
aTooltip: Tooltip,
22+
},
23+
props: {
24+
/** 长文本 */
25+
text: {
26+
type: String,
27+
default: '',
28+
},
29+
/** 超出时显示的内容 */
30+
ellipsisType: {
31+
type: String,
32+
default: '...',
33+
},
34+
/** 悬停是否显示全文内容 */
35+
showAll: {
36+
type: Boolean,
37+
default: true,
38+
},
39+
40+
/** 字体大小 这个api也不重要 */
41+
fontSize: {
42+
type: Number,
43+
default: 14,
44+
},
45+
46+
/** 限制的高度 */
47+
limitHeightNumber: {
48+
type: Number,
49+
default: 0,
50+
},
51+
/** 行高 直接获得实际高度,不从api获得 */
52+
// lineHeight: {
53+
// type: Number,
54+
// default: 18,
55+
// },
56+
/** 是否通过 高度限制 */
57+
isHeightLimit: {
58+
type: Boolean,
59+
default: false,
60+
},
61+
62+
/** 限制的行数 —— 行数*行高 与 容器对比 */
63+
limitLineNumber: {
64+
type: Number,
65+
default: 1,
66+
},
67+
/** 是否通过 行数控制 */
68+
isLineLimit: {
69+
type: Boolean,
70+
default: false,
71+
},
72+
73+
/** 是否根据长度限制 */
74+
isCharLimit: {
75+
type: Boolean,
76+
default: false,
77+
},
78+
/** 长度 */
79+
maxLength: {
80+
type: Number,
81+
default: 20,
82+
},
83+
84+
},
85+
setup(props) {
86+
const ellipsis = ref(null);
87+
const showText = ref(''); /** 组件上显示的文本 */
88+
89+
/** getFontWidth 只处理了汉字。字母和数字也要优化 */
90+
const getFontWidth = () => {
91+
let FW = 0;
92+
/** 如果没传,dom获取 */
93+
const e = document.createElement('span');
94+
const t = document.createTextNode('');
95+
e.appendChild(t);
96+
document.body.appendChild(e);
97+
const domRect = (e as any).getBoundingClientRect();
98+
const { left: FL, right: FR } = domRect;
99+
if (FL && FR) FW = FR - FL;
100+
document.body.removeChild(e);
101+
102+
return FW;
103+
};
104+
105+
/** getLineHeight 高度是一致的。不需要考虑字母汉字 */
106+
const getLineHeight = () => {
107+
let FH = 0;
108+
/** 如果没传,dom获取 */
109+
const e = document.createElement('span');
110+
const t = document.createTextNode('');
111+
e.appendChild(t);
112+
document.body.appendChild(e);
113+
const domRect = (e as any).getBoundingClientRect();
114+
// console.log(domRect, 'domRect');
115+
const { height: DRH } = domRect;
116+
if (DRH) FH = DRH;
117+
document.body.removeChild(e);
118+
119+
return FH;
120+
};
121+
122+
/** lengthLimit */
123+
const lengthLimit = () => {
124+
const { text, maxLength, ellipsisType } = props;
125+
if (text.length > maxLength) return text.substr(0, maxLength) + ellipsisType;
126+
return text;
127+
};
128+
129+
/** lineLimit */
130+
const lineLimit = () => {
131+
const {
132+
text, limitLineNumber, ellipsisType, fontSize,
133+
} = props;
134+
/** 获得单个字体大小 */
135+
const FW = fontSize || getFontWidth(); // 字体宽度&字体大小
136+
137+
/** 获得父容器宽度 */
138+
const containerWidth = (ellipsis.value as any).parentNode.offsetWidth;
139+
140+
/** 获得每行文字个数 —— 需要优化 字母和数字情况 */
141+
const strNum = Math.floor(containerWidth / FW);
142+
143+
/** 判断长度 */
144+
if (strNum * limitLineNumber < text.length) {
145+
return text.substr(0, strNum * limitLineNumber - 1) + ellipsisType;
146+
}
147+
return text;
148+
};
149+
150+
/** heightLimit */
151+
const heightLimit = () => {
152+
const {
153+
text, limitHeightNumber, ellipsisType, fontSize,
154+
} = props;
155+
156+
/** 行高度 */
157+
const FH = getLineHeight();
158+
159+
/** 获得单个字体大小 */
160+
const FW = fontSize || getFontWidth(); // 字体宽度&字体大小
161+
162+
/** 获得父容器宽度 */
163+
const containerWidth = (ellipsis.value as any).parentNode.offsetWidth;
164+
165+
/** 获得每行文字个数 —— 需要优化 字母和数字情况 */
166+
const strNum = Math.floor(containerWidth / FW);
167+
168+
/** 计算最大行数 如果一行都不够,返回1行 */
169+
const maxLen = Math.floor(limitHeightNumber / FH) || 1;
170+
// console.log(maxLen, 'maxLen', ellipsisType);
171+
172+
/** 判断长度 */
173+
if (strNum * maxLen < text.length) {
174+
return text.substr(0, strNum * maxLen - 1) + ellipsisType;
175+
}
176+
return text;
177+
};
178+
179+
const getShowText = () => {
180+
const { isCharLimit, isLineLimit, isHeightLimit } = props;
181+
let res = '';
182+
if (isCharLimit) {
183+
res = lengthLimit();
184+
} else if (isLineLimit) {
185+
res = lineLimit();
186+
} else if (isHeightLimit) {
187+
res = heightLimit();
188+
}
189+
showText.value = res;
190+
};
191+
192+
onMounted(() => {
193+
// console.log(ellipsis.value);
194+
getShowText();
195+
});
196+
197+
return {
198+
ellipsis,
199+
showText,
200+
};
201+
},
202+
});
203+
</script>
204+
205+
<style lang="stylus" scoped>
206+
207+
</style>

0 commit comments

Comments
 (0)