vue-line-clamp.umd.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  3. typeof define === 'function' && define.amd ? define(factory) :
  4. (global.VueLineClamp = factory());
  5. }(this, (function () { 'use strict';
  6. var currentValueProp = "vLineClampValue";
  7. function defaultFallbackFunc(el, bindings, lines) {
  8. if (lines) {
  9. var lineHeight = parseInt(bindings.arg);
  10. if (isNaN(lineHeight)) {
  11. console.warn("line-height argument for vue-line-clamp must be a number (of pixels), falling back to 16px");
  12. lineHeight = 16;
  13. }
  14. var maxHeight = lineHeight * lines;
  15. el.style.maxHeight = maxHeight ? maxHeight + "px" : "";
  16. el.style.overflowX = "hidden";
  17. el.style.lineHeight = lineHeight + "px"; // to ensure consistency
  18. } else {
  19. el.style.maxHeight = el.style.overflowX = "";
  20. }
  21. }
  22. var truncateText = function truncateText(el, bindings, useFallbackFunc) {
  23. var lines = parseInt(bindings.value);
  24. if (isNaN(lines)) {
  25. console.error("Parameter for vue-line-clamp must be a number");
  26. return;
  27. } else if (lines !== el[currentValueProp]) {
  28. el[currentValueProp] = lines;
  29. if (useFallbackFunc) {
  30. useFallbackFunc(el, bindings, lines);
  31. } else {
  32. el.style.webkitLineClamp = lines ? lines : "";
  33. }
  34. }
  35. };
  36. var VueLineClamp = {
  37. install: function install(Vue, options) {
  38. options = Object.assign({ importCss: false, textOverflow: "ellipsis" }, options);
  39. var styles = "display:block;display:-webkit-box;-webkit-box-orient:vertical;overflow:hidden;text-overflow:" + options.textOverflow;
  40. if (options.importCss) {
  41. var stylesheets = window.document.styleSheets,
  42. rule = ".vue-line-clamp{" + styles + "}";
  43. if (stylesheets && stylesheets[0] && stylesheets.insertRule) {
  44. stylesheets.insertRule(rule);
  45. } else {
  46. var link = window.document.createElement("style");
  47. link.id = "vue-line-clamp";
  48. link.appendChild(window.document.createTextNode(rule));
  49. window.document.head.appendChild(link);
  50. }
  51. }
  52. var useFallbackFunc = "webkitLineClamp" in document.body.style ? undefined : options.fallbackFunc || defaultFallbackFunc;
  53. Vue.directive("line-clamp", {
  54. currentValue: 0,
  55. bind: function bind(el) {
  56. if (!options.importCss) {
  57. el.style.cssText += styles;
  58. } else {
  59. el.classList.add("vue-line-clamp");
  60. }
  61. },
  62. inserted: function inserted(el, bindings) {
  63. return truncateText(el, bindings, useFallbackFunc);
  64. },
  65. updated: function updated(el, bindings) {
  66. return truncateText(el, bindings, useFallbackFunc);
  67. },
  68. componentUpdated: function componentUpdated(el, bindings) {
  69. return truncateText(el, bindings, useFallbackFunc);
  70. }
  71. });
  72. }
  73. };
  74. return VueLineClamp;
  75. })));