AdminReadMore.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <div>
  3. <div class="mb-0" :style="{ 'font-size':`${fontSize}px` }">{{ contentText }}</div>
  4. <p class="mb-0"><a v-if="canStepExpand || (canExpand && !expanded)" class="font-weight-bold small" href="#" @click="expand()">Read more</a></p>
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. props: {
  10. content: {
  11. type: String
  12. },
  13. maxLength: {
  14. type: Number,
  15. default: 140
  16. },
  17. fontSize: {
  18. type: String,
  19. default: "13"
  20. },
  21. step: {
  22. type: Boolean,
  23. default: false
  24. },
  25. stepLimit: {
  26. type: Number,
  27. default: 140
  28. },
  29. initialLimit: {
  30. type: Number,
  31. default: 10
  32. }
  33. },
  34. computed: {
  35. contentText: {
  36. get() {
  37. if(this.step) {
  38. const len = this.content.length;
  39. const steps = len / this.stepLimit;
  40. if(this.stepIndex == 1 || steps < this.stepIndex) {
  41. this.canStepExpand = true;
  42. }
  43. return this.steppedTruncate();
  44. }
  45. if(this.content && this.content.length > this.maxLength) {
  46. this.canExpand = true;
  47. }
  48. return this.expanded ? this.content : this.truncate();
  49. }
  50. }
  51. },
  52. data() {
  53. return {
  54. expanded: false,
  55. canExpand: false,
  56. canStepExpand: false,
  57. stepIndex: 1,
  58. }
  59. },
  60. methods: {
  61. expand() {
  62. if(this.step) {
  63. this.stepIndex++;
  64. this.canStepExpand = true;
  65. } else {
  66. this.expanded = true;
  67. }
  68. },
  69. truncate() {
  70. if(!this.content || !this.content.length) {
  71. return;
  72. }
  73. if(this.content && this.content.length < this.maxLength) {
  74. return this.content;
  75. }
  76. return this.content.slice(0, this.maxLength) + '...';
  77. },
  78. steppedTruncate() {
  79. if(!this.content || !this.content.length) {
  80. return;
  81. }
  82. const len = this.content.length;
  83. const steps = len / this.stepLimit;
  84. const maxLen = this.stepLimit * this.stepIndex;
  85. if(this.initialLimit != 10 && this.stepIndex === 1 && this.canStepExpand) {
  86. this.canStepExpand = len > this.stepLimit;
  87. return this.content.slice(0, this.initialLimit);
  88. } else if(this.canStepExpand && this.stepIndex < steps) {
  89. return this.content.slice(0, maxLen);
  90. } else {
  91. this.canStepExpand = false;
  92. return this.content;
  93. }
  94. }
  95. }
  96. }
  97. </script>