parse.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <!--基础元素-->
  3. <view class="wxParse" :class="className" :style="'user-select:' + userSelect">
  4. <template v-if="!loading">
  5. <block v-for="(node, index) of nodes" :key="index" >
  6. <wxParseTemplate :node="node" />
  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'//none |text| all | element
  20. },
  21. imgOptions:{
  22. type:[Object,Boolean],
  23. default:function(){
  24. return {
  25. loop: false,
  26. indicator:'number',
  27. longPressActions:false
  28. // longPressActions: {
  29. // itemList: ['发送给朋友', '保存图片', '收藏'],
  30. // success: function (res) {
  31. // },
  32. // fail: function (res) {
  33. // }
  34. // }
  35. // }
  36. }
  37. }
  38. },
  39. loading: {
  40. type: Boolean,
  41. default: false
  42. },
  43. className: {
  44. type: String,
  45. default: ''
  46. },
  47. content: {
  48. type: String,
  49. default: ''
  50. },
  51. noData: {
  52. type: String,
  53. default: ''
  54. },
  55. startHandler: {
  56. type: Function,
  57. default() {
  58. return node => {
  59. node.attr.class = null;
  60. node.attr.style = null;
  61. };
  62. }
  63. },
  64. endHandler: {
  65. type: Function,
  66. default: null
  67. },
  68. charsHandler: {
  69. type: Function,
  70. default: null
  71. },
  72. imageProp: {
  73. type: Object,
  74. default() {
  75. return {
  76. mode: 'aspectFit',
  77. padding: 0,
  78. lazyLoad: false,
  79. domain: ''
  80. };
  81. }
  82. },
  83. },
  84. components: {
  85. wxParseTemplate,
  86. },
  87. data() {
  88. return {
  89. nodes:{},
  90. imageUrls: [],
  91. wxParseWidth:{
  92. value:0
  93. }
  94. };
  95. },
  96. mounted() {
  97. this.setHtml();
  98. },
  99. methods: {
  100. setHtml(){
  101. let { content, noData, imageProp, startHandler, endHandler, charsHandler } = this;
  102. let parseData = content || noData;
  103. let customHandler = {
  104. start: startHandler,
  105. end: endHandler,
  106. chars: charsHandler
  107. };
  108. let results = HtmlToJson(parseData, customHandler, imageProp, this);
  109. this.imageUrls = results.imageUrls;
  110. this.nodes = results.nodes;
  111. },
  112. navigate(href, $event, attr) {
  113. this.$emit('navigate', href, $event);
  114. },
  115. preview(src, $event) {
  116. if (!this.imageUrls.length||typeof this.imgOptions === 'boolean'){
  117. }else{
  118. uni.previewImage({
  119. current: src,
  120. urls: this.imageUrls,
  121. loop: this.imgOptions.loop,
  122. indicator: this.imgOptions.indicator,
  123. longPressActions: this.imgOptions.longPressActions
  124. });
  125. }
  126. this.$emit('preview', src, $event);
  127. },
  128. removeImageUrl(src) {
  129. const { imageUrls } = this;
  130. imageUrls.splice(imageUrls.indexOf(src), 1);
  131. }
  132. },
  133. watch: {
  134. content() {
  135. this.setHtml();
  136. }
  137. }
  138. };
  139. </script>