wxParse.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <!--**
  2. * forked from:https://github.com/F-loat/mpvue-wxParse
  3. *
  4. * github地址: https://github.com/dcloudio/uParse
  5. *
  6. * uni-app框架下 富文本解析
  7. * 全局引入css文件,@import '/.../wxParse.css';
  8. *
  9. */-->
  10. <template>
  11. <!--基础元素-->
  12. <div class="wxParse" :class="className" :style="'user-select:' + userSelect">
  13. <block v-for="(node, index) of nodes" :key="index" v-if="!loading">
  14. <wxParseTemplate :node="node" />
  15. </block>
  16. </div>
  17. </template>
  18. <script>
  19. import HtmlToJson from './libs/html2json';
  20. import wxParseTemplate from './components/wxParseTemplate0';
  21. export default {
  22. name: 'wxParse',
  23. emits: ['navigate','preview'],
  24. props: {
  25. userSelect: {
  26. type: String,
  27. default: 'text' //none |text| all | element
  28. },
  29. imgOptions: {
  30. type: [Object, Boolean],
  31. default: function() {
  32. return {
  33. loop: false,
  34. indicator: 'number',
  35. longPressActions: false
  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: '<text></text>'
  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. computed: {},
  97. mounted() {
  98. this.setHtml()
  99. },
  100. methods: {
  101. setHtml() {
  102. this.getWidth().then((data) => {
  103. this.wxParseWidth.value = data;
  104. })
  105. let {
  106. content,
  107. noData,
  108. imageProp,
  109. startHandler,
  110. endHandler,
  111. charsHandler
  112. } = this;
  113. let parseData = content || noData;
  114. let customHandler = {
  115. start: startHandler,
  116. end: endHandler,
  117. chars: charsHandler
  118. };
  119. let results = HtmlToJson(parseData, customHandler, imageProp, this);
  120. this.imageUrls = results.imageUrls;
  121. this.nodes = [];
  122. setTimeout(()=>{
  123. this.nodes = results.nodes;
  124. },20)
  125. // results.nodes.forEach((item) => {
  126. // setTimeout(() => {
  127. // this.nodes.push(item)
  128. // }, 0);
  129. // })
  130. },
  131. getWidth() {
  132. return new Promise((res, rej) => {
  133. // #ifndef MP-ALIPAY || MP-BAIDU
  134. uni.createSelectorQuery()
  135. .in(this)
  136. .select('.wxParse')
  137. .fields({
  138. size: true,
  139. scrollOffset: true
  140. },
  141. data => {
  142. res(data.width);
  143. }
  144. ).exec();
  145. // #endif
  146. // #ifdef MP-BAIDU
  147. return new Promise((res, rej) => {
  148. swan.createSelectorQuery().select('.wxParse').boundingClientRect(function(rect) {
  149. res(rect.width)
  150. }).exec()
  151. });
  152. // #endif
  153. // #ifdef MP-ALIPAY
  154. my.createSelectorQuery()
  155. .select('.wxParse')
  156. .boundingClientRect().exec((ret) => {
  157. res(ret[0].width);
  158. });
  159. // #endif
  160. });
  161. },
  162. navigate(href, $event, attr) {
  163. this.$emit('navigate', href, $event);
  164. },
  165. preview(src, $event) {
  166. if (!this.imageUrls.length || typeof this.imgOptions === 'boolean') {
  167. } else {
  168. uni.previewImage({
  169. current: src,
  170. urls: this.imageUrls,
  171. loop: this.imgOptions.loop,
  172. indicator: this.imgOptions.indicator,
  173. longPressActions: this.imgOptions.longPressActions
  174. });
  175. }
  176. this.$emit('preview', src, $event);
  177. },
  178. removeImageUrl(src) {
  179. const {
  180. imageUrls
  181. } = this;
  182. imageUrls.splice(imageUrls.indexOf(src), 1);
  183. }
  184. },
  185. // 父组件中提供
  186. provide() {
  187. return {
  188. parseWidth: this.wxParseWidth,
  189. parseSelect: this.userSelect
  190. // 提示:provide 和 inject 绑定并不是可响应的。这是刻意为之的。然而,如果你传入了一个可监听的对象,那么其对象的属性还是可响应的。
  191. };
  192. },
  193. watch: {
  194. content() {
  195. this.setHtml()
  196. }
  197. }
  198. };
  199. </script>