-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (57 loc) · 1.73 KB
/
index.js
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
const currentValueProp = "vLineClampValue";
const truncateText = function (el, bindings) {
let limit = parseInt(bindings.value);
if (isNaN(limit)) {
console.error("Parameter for vue-line-clamp must be a number");
return;
} else if (limit !== el[currentValueProp]) {
el[currentValueProp] = limit;
el.style.webkitLineClamp = limit ? limit : "";
}
};
const VueLineClamp = {
install(app, options) {
options = Object.assign(
{
useClass: false,
useImportant: false,
textOverflow: "",
wordBreak: "break-word",
},
options
);
const imp = options.useImportant ? "!important" : "";
const styles = `
display: -webkit-box ${imp};
-webkit-box-orient: vertical ${imp};
height: fit-content;
overflow: hidden ${imp};
word-break: ${options.wordBreak} ${imp};
text-overflow: ${options.textOverflow} ${imp};
`;
if (options.useClass) {
const stylesheets = window.document.styleSheets;
const rule = `.vue-line-clamp{${styles}}`;
if (stylesheets && stylesheets[0] && stylesheets.insertRule) {
stylesheets.insertRule(rule);
} else {
let link = window.document.createElement("style");
link.appendChild(window.document.createTextNode(rule));
window.document.head.appendChild(link);
}
}
app.directive("line-clamp", {
currentValue: 0,
beforeMount(el) {
if (!options.useClass) {
el.style.cssText += styles;
} else {
el.classList.add("vue-line-clamp");
}
},
mounted: (el, bindings) => truncateText(el, bindings),
updated: (el, bindings) => truncateText(el, bindings),
});
},
};
export default VueLineClamp;