parse.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 '@/components/app-dev/html2json.js';
  13. import wxParseTemplate from './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. let { content, noData, imageProp, startHandler, endHandler, charsHandler } = this;
  98. let parseData = content || noData;
  99. let customHandler = {
  100. start: startHandler,
  101. end: endHandler,
  102. chars: charsHandler
  103. };
  104. let results = HtmlToJson(parseData, customHandler, imageProp, this);
  105. this.imageUrls = results.imageUrls;
  106. this.nodes = results.nodes;
  107. },
  108. navigate(href, $event) {
  109. this.$emit('navigate', href, $event);
  110. },
  111. preview(src, $event) {
  112. if (!this.imageUrls.length || typeof this.imgOptions === 'boolean'){
  113. } else {
  114. uni.previewImage({
  115. current: src,
  116. urls: this.imageUrls,
  117. loop: this.imgOptions.loop,
  118. indicator: this.imgOptions.indicator,
  119. longPressActions: this.imgOptions.longPressActions
  120. });
  121. }
  122. this.$emit('preview', src, $event);
  123. },
  124. removeImageUrl(src) {
  125. const { imageUrls } = this;
  126. imageUrls.splice(imageUrls.indexOf(src), 1);
  127. }
  128. },
  129. watch: {
  130. content() {
  131. this.setHtml();
  132. }
  133. }
  134. };
  135. </script>