-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
715 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
|
||
const css = 'display:block;display:-webkit-box;-webkit-box-orient:vertical;overflow:hidden;text-overflow:ellipsis'; | ||
|
||
const truncateText = function (el, bindings, needsFallback) { | ||
let lines = parseInt(bindings.value); | ||
if (isNaN(lines)) { | ||
console.error('Parameter for vue-line-clamp must be a number'); | ||
return | ||
} | ||
else if (lines !== bindings.def.currentValue) { | ||
bindings.def.currentValue = lines; | ||
|
||
if (needsFallback) { | ||
if (lines) { | ||
let lineHeight = parseInt(bindings.arg); | ||
if (isNaN(lineHeight)) { | ||
console.warn('line-height argument for vue-line-clamp must be a number (of pixels), falling back to 16px'); | ||
lineHeight = 16; | ||
} | ||
|
||
let maxHeight = lineHeight * lines; | ||
|
||
el.style.maxHeight = maxHeight ? maxHeight+'px' : ''; | ||
el.style.overflowX = 'hidden'; | ||
el.style.lineHeight = lineHeight+'px'; // to ensure consistency | ||
} else { | ||
el.style.maxHeight = el.style.overflowX = ''; | ||
} | ||
} | ||
else { | ||
el.style.webkitLineClamp = lines ? lines : ''; | ||
} | ||
} | ||
}; | ||
|
||
const VueLineClamp = { | ||
install (Vue, options) { | ||
options = Object.assign({ | ||
importCss: false | ||
}, options); | ||
|
||
if (options.importCss) { | ||
const stylesheets = window.document.styleSheets, | ||
rule = `.vue-line-clamp{${css}}`; | ||
if (stylesheets && stylesheets[0] && stylesheets.insertRule) { | ||
stylesheets.insertRule(rule); | ||
} else { | ||
let link = window.document.createElement('style'); | ||
link.id = 'vue-line-clamp'; | ||
link.appendChild(window.document.createTextNode(rule)); | ||
window.document.head.appendChild(link); | ||
} | ||
} | ||
|
||
const needsFallback = 'webkitLineClamp' in document.body.style ? false : true; | ||
|
||
Vue.directive('line-clamp', { | ||
currentValue: 0, | ||
bind (el) { | ||
if (!options.importCss) { | ||
el.style.cssText += css; | ||
} | ||
else { | ||
el.classList.add('vue-line-clamp'); | ||
} | ||
|
||
}, | ||
inserted: (el, bindings) => truncateText(el, bindings, needsFallback), | ||
updated: (el, bindings) => truncateText(el, bindings, needsFallback), | ||
componentUpdated: (el, bindings) => truncateText(el, bindings, needsFallback) | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = VueLineClamp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const css = 'display:block;display:-webkit-box;-webkit-box-orient:vertical;overflow:hidden;text-overflow:ellipsis'; | ||
|
||
const truncateText = function (el, bindings, needsFallback) { | ||
let lines = parseInt(bindings.value); | ||
if (isNaN(lines)) { | ||
console.error('Parameter for vue-line-clamp must be a number'); | ||
return | ||
} | ||
else if (lines !== bindings.def.currentValue) { | ||
bindings.def.currentValue = lines; | ||
|
||
if (needsFallback) { | ||
if (lines) { | ||
let lineHeight = parseInt(bindings.arg); | ||
if (isNaN(lineHeight)) { | ||
console.warn('line-height argument for vue-line-clamp must be a number (of pixels), falling back to 16px'); | ||
lineHeight = 16; | ||
} | ||
|
||
let maxHeight = lineHeight * lines; | ||
|
||
el.style.maxHeight = maxHeight ? maxHeight+'px' : ''; | ||
el.style.overflowX = 'hidden'; | ||
el.style.lineHeight = lineHeight+'px'; // to ensure consistency | ||
} else { | ||
el.style.maxHeight = el.style.overflowX = ''; | ||
} | ||
} | ||
else { | ||
el.style.webkitLineClamp = lines ? lines : ''; | ||
} | ||
} | ||
}; | ||
|
||
const VueLineClamp = { | ||
install (Vue, options) { | ||
options = Object.assign({ | ||
importCss: false | ||
}, options); | ||
|
||
if (options.importCss) { | ||
const stylesheets = window.document.styleSheets, | ||
rule = `.vue-line-clamp{${css}}`; | ||
if (stylesheets && stylesheets[0] && stylesheets.insertRule) { | ||
stylesheets.insertRule(rule); | ||
} else { | ||
let link = window.document.createElement('style'); | ||
link.id = 'vue-line-clamp'; | ||
link.appendChild(window.document.createTextNode(rule)); | ||
window.document.head.appendChild(link); | ||
} | ||
} | ||
|
||
const needsFallback = 'webkitLineClamp' in document.body.style ? false : true; | ||
|
||
Vue.directive('line-clamp', { | ||
currentValue: 0, | ||
bind (el) { | ||
if (!options.importCss) { | ||
el.style.cssText += css; | ||
} | ||
else { | ||
el.classList.add('vue-line-clamp'); | ||
} | ||
|
||
}, | ||
inserted: (el, bindings) => truncateText(el, bindings, needsFallback), | ||
updated: (el, bindings) => truncateText(el, bindings, needsFallback), | ||
componentUpdated: (el, bindings) => truncateText(el, bindings, needsFallback) | ||
}); | ||
} | ||
}; | ||
|
||
export default VueLineClamp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.VueLineClamp = factory()); | ||
}(this, (function () { 'use strict'; | ||
|
||
const css = 'display:block;display:-webkit-box;-webkit-box-orient:vertical;overflow:hidden;text-overflow:ellipsis'; | ||
|
||
const truncateText = function (el, bindings, needsFallback) { | ||
let lines = parseInt(bindings.value); | ||
if (isNaN(lines)) { | ||
console.error('Parameter for vue-line-clamp must be a number'); | ||
return | ||
} | ||
else if (lines !== bindings.def.currentValue) { | ||
bindings.def.currentValue = lines; | ||
|
||
if (needsFallback) { | ||
if (lines) { | ||
let lineHeight = parseInt(bindings.arg); | ||
if (isNaN(lineHeight)) { | ||
console.warn('line-height argument for vue-line-clamp must be a number (of pixels), falling back to 16px'); | ||
lineHeight = 16; | ||
} | ||
|
||
let maxHeight = lineHeight * lines; | ||
|
||
el.style.maxHeight = maxHeight ? maxHeight+'px' : ''; | ||
el.style.overflowX = 'hidden'; | ||
el.style.lineHeight = lineHeight+'px'; // to ensure consistency | ||
} else { | ||
el.style.maxHeight = el.style.overflowX = ''; | ||
} | ||
} | ||
else { | ||
el.style.webkitLineClamp = lines ? lines : ''; | ||
} | ||
} | ||
}; | ||
|
||
const VueLineClamp = { | ||
install (Vue, options) { | ||
options = Object.assign({ | ||
importCss: false | ||
}, options); | ||
|
||
if (options.importCss) { | ||
const stylesheets = window.document.styleSheets, | ||
rule = `.vue-line-clamp{${css}}`; | ||
if (stylesheets && stylesheets[0] && stylesheets.insertRule) { | ||
stylesheets.insertRule(rule); | ||
} else { | ||
let link = window.document.createElement('style'); | ||
link.id = 'vue-line-clamp'; | ||
link.appendChild(window.document.createTextNode(rule)); | ||
window.document.head.appendChild(link); | ||
} | ||
} | ||
|
||
const needsFallback = 'webkitLineClamp' in document.body.style ? false : true; | ||
|
||
Vue.directive('line-clamp', { | ||
currentValue: 0, | ||
bind (el) { | ||
if (!options.importCss) { | ||
el.style.cssText += css; | ||
} | ||
else { | ||
el.classList.add('vue-line-clamp'); | ||
} | ||
|
||
}, | ||
inserted: (el, bindings) => truncateText(el, bindings, needsFallback), | ||
updated: (el, bindings) => truncateText(el, bindings, needsFallback), | ||
componentUpdated: (el, bindings) => truncateText(el, bindings, needsFallback) | ||
}); | ||
} | ||
}; | ||
|
||
return VueLineClamp; | ||
|
||
}))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<!-- THIS FILE IS USED FOR DEVELOPMENT --> | ||
<meta charset="UTF-8"> | ||
<title>Vue Line Clamp - A multi-line ellipsis for Vue</title> | ||
</head> | ||
<body> | ||
<div id="app"> | ||
<label for="range">Number of lines: {{ lines }} </label> <br> | ||
<input type="range" v-model="lines" min="0" max="5" id="range"> | ||
|
||
<p v-line-clamp:26="lines" style="width: 200px"> | ||
Truncate my lorem ipsum dolor, sit amet consectetur adipisicing elit. Dolore, neque minima libero voluptatibus voluptate enim earum qui tempora sed sequi fugiat aut commodi laboriosam iste cupiditate quo veritatis quam obcaecati. | ||
</p> | ||
</div> | ||
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script> | ||
<script src="dist/vue-line-clamp.umd.js"></script> | ||
<script> | ||
Vue.use(VueLineClamp, { | ||
importCss: false // (default) or true to inline css within element's style attr | ||
}) | ||
|
||
new Vue({ | ||
el: '#app', | ||
data: { | ||
lines: 2 | ||
} | ||
}) | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define(factory) : | ||
(global.VueLineClamp = factory()); | ||
}(this, (function () { 'use strict'; | ||
|
||
const css = 'display:block;display:-webkit-box;-webkit-box-orient:vertical;overflow:hidden;text-overflow:ellipsis'; | ||
|
||
const truncateText = function (el, lines, needsFallback) { | ||
if (needsFallback) { | ||
let lineHeight = parseFloat(window.getComputedStyle(el).lineHeight), | ||
maxHeight = lineHeight * lines; | ||
el.style.maxHeight = maxHeight ? maxHeight+'px' : ''; | ||
el.style.overflowX = 'hidden'; | ||
} | ||
else { | ||
el.style.webkitLineClamp = lines; | ||
} | ||
}; | ||
|
||
const VueLineClamp = { | ||
install (Vue, options) { | ||
options = Object.assign({ | ||
importCss: null | ||
}, options); | ||
|
||
if (options.importCss === true) { | ||
let stylesheet = window.document.styleSheets[0], | ||
rule = `.vue-line-clamp{${css}}`; | ||
if (stylesheet && stylesheet.insertRule) { | ||
stylesheets.insertRule(rule); | ||
} else { | ||
let link = window.document.createElement('style'); | ||
link.id = 'vue-line-clamp'; | ||
link.appendChild(window.document.createTextNode(rule)); | ||
window.document.head.appendChild(link); | ||
} | ||
} | ||
|
||
let needsFallback = 'webkitLineClamp' in document.body.style ? false : true; | ||
Vue.directive('line-clamp', { | ||
bind (el) { | ||
if (!options.importCss) { | ||
el.style.cssText += css; | ||
} | ||
else { | ||
el.classList.add('vue-line-clamp'); | ||
} | ||
|
||
}, | ||
inserted (el, bindings) { | ||
truncateText(el, bindings.value, needsFallback); | ||
}, | ||
updated (el, bindings) { | ||
truncateText(el, bindings.value, needsFallback); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
return VueLineClamp; | ||
|
||
}))); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.