Skip to content

wordBreak Option, cleaned up logic on what function to use #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ In some upcoming version it may be able to detect this value automatically.
| --- | --- | --- | --- |
| importCss | Boolean | false | Set to `true` in order to import styles into `<head>` automatically, element.style is used by default
| textOverflow | String | `ellipsis` | Set the value for [`text-overflow`](https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow) property in modern browsers
| wordBreak | String | `break-all` | set the value for [`word-break`](https://developer.mozilla.org/en-US/docs/Web/CSS/word-break)
| fallbackFunc | Function | defaultFallbackFunc | Provide your own default method to handle the truncation strategy on unsupported browsers. Accepts all directive params: `element (Node)`, `bindings (Object)`, `lines (Number)`


Expand All @@ -46,6 +47,8 @@ In some upcoming version it may be able to detect this value automatically.

### Changelog

**1.2.5** - Implemented `wordBreak` option, refactored how the backup function is called

**v1.2.4** - Implemented `textOverflow` option.

**v1.2.1** - Implemented `fallbackFunc` options, fixed multiple elements clamping on same page.
Expand Down
26 changes: 11 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const currentValueProp = 'vLineClampValue';

function defaultFallbackFunc(el, bindings, lines) {
function defaultFallbackFunc(el, bindings) {
let lines = parseInt(bindings.value);
if (lines) {
let lineHeight = parseInt(bindings.arg);
if (isNaN(lineHeight)) {
Expand All @@ -20,26 +21,21 @@ function defaultFallbackFunc(el, bindings, lines) {
}
}

const truncateText = function(el, bindings, useFallbackFunc) {
const truncateText = function(el, bindings) {
let lines = parseInt(bindings.value);
if (isNaN(lines)) {
console.error('Parameter for vue-line-clamp must be a number');
return;
} else if (lines !== el[currentValueProp]) {
el[currentValueProp] = lines;

if (useFallbackFunc) {
useFallbackFunc(el, bindings, lines);
} else {
el.style.webkitLineClamp = lines ? lines : '';
}
el.style.webkitLineClamp = lines ? lines : '';
}
};

const VueLineClamp = {
install(Vue, options) {
options = Object.assign(
{ importCss: false, textOverflow: 'ellipsis' },
{ importCss: false, textOverflow: 'ellipsis', wordBreak: 'break-all' },
options
);

Expand All @@ -48,7 +44,7 @@ const VueLineClamp = {
display: -webkit-box;
-webkit-box-orient: vertical;
overflow: hidden;
word-break: break-all;
word-break: ${options.wordBreak};
text-overflow: ${options.textOverflow};
`;

Expand All @@ -65,9 +61,9 @@ const VueLineClamp = {
}
}

const useFallbackFunc =
const clampFunction =
'webkitLineClamp' in document.body.style
? undefined
? truncateText
: options.fallbackFunc || defaultFallbackFunc;

Vue.directive('line-clamp', {
Expand All @@ -79,10 +75,10 @@ const VueLineClamp = {
el.classList.add('vue-line-clamp');
}
},
inserted: (el, bindings) => truncateText(el, bindings, useFallbackFunc),
updated: (el, bindings) => truncateText(el, bindings, useFallbackFunc),
inserted: (el, bindings) => clampFunction(el, bindings),
updated: (el, bindings) => clampFunction(el, bindings),
componentUpdated: (el, bindings) =>
truncateText(el, bindings, useFallbackFunc),
clampFunction(el, bindings),
});
},
};
Expand Down