parse.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <!--基础元素-->
  3. <view class="wxParse" :class="className" :style="'user-select:' + userSelect + ';background-color: ' + background">
  4. <template v-if="!loading">
  5. <block v-for="(node, index) of nodes" :key="index" >
  6. <wxParseTemplate :node="node" :parent-node="nodes"/>
  7. </block>
  8. </template>
  9. </view>
  10. </template>
  11. <script>
  12. import HtmlToJson from './libs/html2json';
  13. import wxParseTemplate from './components/wxParseTemplate0';
  14. export default {
  15. name: 'wxParse',
  16. props: {
  17. userSelect:{
  18. type:String,
  19. default:'text'
  20. },
  21. imgOptions:{
  22. type:[Object,Boolean],
  23. default:function(){
  24. return {
  25. loop: false,
  26. indicator:'number',
  27. longPressActions:false
  28. }
  29. }
  30. },
  31. loading: {
  32. type: Boolean,
  33. default: false
  34. },
  35. background: {
  36. type: String,
  37. default: '#ffffff'
  38. },
  39. className: {
  40. type: String,
  41. default: ''
  42. },
  43. content: {
  44. type: String,
  45. default: ''
  46. },
  47. noData: {
  48. type: String,
  49. default: ''
  50. },
  51. startHandler: {
  52. type: Function,
  53. default() {
  54. return node => {
  55. node.attr.class = null;
  56. node.attr.style = null;
  57. };
  58. }
  59. },
  60. endHandler: {
  61. type: Function,
  62. default: null
  63. },
  64. charsHandler: {
  65. type: Function,
  66. default: null
  67. },
  68. imageProp: {
  69. type: Object,
  70. default() {
  71. return {
  72. mode: 'aspectFit',
  73. padding: 0,
  74. lazyLoad: false,
  75. domain: ''
  76. };
  77. }
  78. },
  79. },
  80. components: {
  81. wxParseTemplate,
  82. },
  83. data() {
  84. return {
  85. nodes:{},
  86. imageUrls: [],
  87. wxParseWidth:{
  88. value:0
  89. }
  90. };
  91. },
  92. mounted() {
  93. this.setHtml();
  94. },
  95. methods: {
  96. setHtml(){
  97. /* console.log(this,'测试接受数据'); */
  98. let { content, noData, imageProp, startHandler, endHandler, charsHandler } = this;
  99. let parseData = content || noData;
  100. let customHandler = {
  101. start: startHandler,
  102. end: endHandler,
  103. chars: charsHandler
  104. };
  105. let results = HtmlToJson(parseData, customHandler, imageProp, this);
  106. this.imageUrls = results.imageUrls;
  107. this.nodes = results.nodes;
  108. },
  109. navigate(href, $event) {
  110. this.$emit('navigate', href, $event);
  111. },
  112. preview(src, $event) {
  113. if (!this.imageUrls.length || typeof this.imgOptions === 'boolean'){
  114. } else {
  115. uni.previewImage({
  116. current: src,
  117. urls: this.imageUrls,
  118. loop: this.imgOptions.loop,
  119. indicator: this.imgOptions.indicator,
  120. longPressActions: this.imgOptions.longPressActions
  121. });
  122. }
  123. this.$emit('preview', src, $event);
  124. },
  125. removeImageUrl(src) {
  126. const { imageUrls } = this;
  127. imageUrls.splice(imageUrls.indexOf(src), 1);
  128. }
  129. },
  130. watch: {
  131. content() {
  132. this.setHtml();
  133. }
  134. }
  135. };
  136. </script>