parse.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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" :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'//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. }
  29. }
  30. },
  31. loading: {
  32. type: Boolean,
  33. default: false
  34. },
  35. className: {
  36. type: String,
  37. default: ''
  38. },
  39. content: {
  40. type: String,
  41. default: ''
  42. },
  43. noData: {
  44. type: String,
  45. default: ''
  46. },
  47. startHandler: {
  48. type: Function,
  49. default() {
  50. return node => {
  51. node.attr.class = null;
  52. node.attr.style = null;
  53. };
  54. }
  55. },
  56. endHandler: {
  57. type: Function,
  58. default: null
  59. },
  60. charsHandler: {
  61. type: Function,
  62. default: null
  63. },
  64. imageProp: {
  65. type: Object,
  66. default() {
  67. return {
  68. mode: 'aspectFit',
  69. padding: 0,
  70. lazyLoad: false,
  71. domain: ''
  72. };
  73. }
  74. },
  75. },
  76. components: {
  77. wxParseTemplate,
  78. },
  79. data() {
  80. return {
  81. nodes:{},
  82. imageUrls: [],
  83. wxParseWidth:{
  84. value:0
  85. }
  86. };
  87. },
  88. mounted() {
  89. this.setHtml();
  90. },
  91. methods: {
  92. setHtml(){
  93. let { content, noData, imageProp, startHandler, endHandler, charsHandler } = this;
  94. let parseData = content || noData;
  95. let customHandler = {
  96. start: startHandler,
  97. end: endHandler,
  98. chars: charsHandler
  99. };
  100. let results = HtmlToJson(parseData, customHandler, imageProp, this);
  101. this.imageUrls = results.imageUrls;
  102. this.nodes = results.nodes;
  103. },
  104. navigate(href, $event) {
  105. this.$emit('navigate', href, $event);
  106. },
  107. preview(src, $event) {
  108. if (!this.imageUrls.length || typeof this.imgOptions === 'boolean'){
  109. } else {
  110. uni.previewImage({
  111. current: src,
  112. urls: this.imageUrls,
  113. loop: this.imgOptions.loop,
  114. indicator: this.imgOptions.indicator,
  115. longPressActions: this.imgOptions.longPressActions
  116. });
  117. }
  118. this.$emit('preview', src, $event);
  119. },
  120. removeImageUrl(src) {
  121. const { imageUrls } = this;
  122. imageUrls.splice(imageUrls.indexOf(src), 1);
  123. }
  124. },
  125. watch: {
  126. content() {
  127. this.setHtml();
  128. }
  129. }
  130. };
  131. </script>