u-parse.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <!--**
  2. * forked from:https://github.com/F-loat/mpvue-wxParse
  3. *
  4. * github地址: https://github.com/dcloudio/uParse
  5. *
  6. * for: uni-app框架下 富文本解析
  7. */-->
  8. <template>
  9. <!--基础元素-->
  10. <div class="wxParse" :class="className" v-if="!loading">
  11. <block v-for="(node, index) of nodes" :key="index"><wxParseTemplate :node="node" /></block>
  12. </div>
  13. </template>
  14. <script>
  15. import HtmlToJson from './libs/html2json';
  16. import wxParseTemplate from './components/wxParseTemplate0';
  17. export default {
  18. name: 'wxParse',
  19. props: {
  20. loading: {
  21. type: Boolean,
  22. default: false
  23. },
  24. className: {
  25. type: String,
  26. default: ''
  27. },
  28. content: {
  29. type: String,
  30. default: ''
  31. },
  32. noData: {
  33. type: String,
  34. default: ''
  35. },
  36. startHandler: {
  37. type: Function,
  38. default() {
  39. return node => {
  40. node.attr.class = null;
  41. node.attr.style = null;
  42. };
  43. }
  44. },
  45. endHandler: {
  46. type: Function,
  47. default: null
  48. },
  49. charsHandler: {
  50. type: Function,
  51. default: null
  52. },
  53. imageProp: {
  54. type: Object,
  55. default() {
  56. return {
  57. mode: 'aspectFit',
  58. padding: 0,
  59. lazyLoad: false,
  60. domain: ''
  61. };
  62. }
  63. }
  64. },
  65. components: {
  66. wxParseTemplate
  67. },
  68. data() {
  69. return {
  70. imageUrls: []
  71. };
  72. },
  73. created() {
  74. console.log(this.content, 'main');
  75. },
  76. computed: {
  77. nodes() {
  78. const { content, noData, imageProp, startHandler, endHandler, charsHandler } = this;
  79. const parseData = content || noData;
  80. const customHandler = {
  81. start: startHandler,
  82. end: endHandler,
  83. chars: charsHandler
  84. };
  85. const results = HtmlToJson(parseData, customHandler, imageProp, this);
  86. this.imageUrls = results.imageUrls;
  87. return results.nodes;
  88. }
  89. },
  90. methods: {
  91. navigate(href, $event) {
  92. this.$emit('navigate', href, $event);
  93. },
  94. preview(src, $event) {
  95. if (!this.imageUrls.length) return;
  96. wx.previewImage({
  97. current: src,
  98. urls: this.imageUrls
  99. });
  100. this.$emit('preview', src, $event);
  101. },
  102. removeImageUrl(src) {
  103. const { imageUrls } = this;
  104. imageUrls.splice(imageUrls.indexOf(src), 1);
  105. }
  106. }
  107. };
  108. </script>