ReadMore.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <div class="read-more-component" style="word-break: break-word;">
  3. <div v-html="content"></div>
  4. <!-- <div v-if="status.content.length < 200" v-html="content"></div>
  5. <div v-else>
  6. <span v-html="content"></span>
  7. <a
  8. v-if="cursor == 200 || fullContent.length > cursor"
  9. class="font-weight-bold text-muted" href="#"
  10. style="display: block;white-space: nowrap;"
  11. @click.prevent="readMore">
  12. <i class="d-none fas fa-caret-down"></i> {{ $t('common.readMore') }}...
  13. </a>
  14. </div> -->
  15. </div>
  16. </template>
  17. <script type="text/javascript">
  18. export default {
  19. props: {
  20. status: {
  21. type: Object
  22. },
  23. cursorLimit: {
  24. type: Number,
  25. default: 200
  26. }
  27. },
  28. data() {
  29. return {
  30. preRender: undefined,
  31. fullContent: null,
  32. content: null,
  33. cursor: 200
  34. }
  35. },
  36. mounted() {
  37. this.rewriteLinks();
  38. },
  39. methods: {
  40. readMore() {
  41. this.cursor = this.cursor + 200;
  42. this.content = this.fullContent.substr(0, this.cursor);
  43. },
  44. rewriteLinks() {
  45. let content = this.status.content;
  46. let el = document.createElement('div');
  47. el.innerHTML = content;
  48. el.querySelectorAll('a[class*="hashtag"]')
  49. .forEach(elr => {
  50. let tag = elr.innerText;
  51. if(tag.substr(0, 1) == '#') {
  52. tag = tag.substr(1);
  53. }
  54. elr.removeAttribute('target');
  55. elr.setAttribute('href', '/i/web/hashtag/' + tag);
  56. })
  57. el.querySelectorAll('a:not(.hashtag)[class*="mention"], a:not(.hashtag)[class*="list-slug"]')
  58. .forEach(elr => {
  59. let name = elr.innerText;
  60. if(name.substr(0, 1) == '@') {
  61. name = name.substr(1);
  62. }
  63. if(this.status.account.local == false && !name.includes('@')) {
  64. let domain = document.createElement('a');
  65. domain.href = elr.getAttribute('href');
  66. name = name + '@' + domain.hostname;
  67. }
  68. elr.removeAttribute('target');
  69. elr.setAttribute('href', '/i/web/username/' + name);
  70. })
  71. this.content = el.outerHTML;
  72. this.injectCustomEmoji();
  73. },
  74. injectCustomEmoji() {
  75. // console.log('inecting custom emoji');
  76. // let re = /:\w{1,100}:/g;
  77. // let matches = this.status.content.match(re);
  78. // console.log(matches);
  79. // if(this.status.emojis.length == 0) {
  80. // return;
  81. // }
  82. let self = this;
  83. this.status.emojis.forEach(function(emoji) {
  84. let img = `<img draggable="false" class="emojione custom-emoji" alt="${emoji.shortcode}" title="${emoji.shortcode}" src="${emoji.url}" data-original="${emoji.url}" data-static="${emoji.static_url}" width="18" height="18" onerror="this.onerror=null;this.src='/storage/emoji/missing.png';" />`;
  85. self.content = self.content.replace(`:${emoji.shortcode}:`, img);
  86. });
  87. // this.content = this.content.replace(':fediverse:', '😅');
  88. }
  89. }
  90. }
  91. </script>