bctos-rich-text.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <view>
  3. <view v-for="(content, index) in contentArr" :key="index">
  4. <rich-text :nodes="content"></rich-text>
  5. <video v-if="videoArr[index] !== null" :src="videoArr[index]" controls :style="{ width }"></video>
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. export default {
  11. name: 'bctos-rich-text',
  12. props: { nodes: {}, width: { type: String, default: '100%' } },
  13. data() {
  14. return {
  15. contentArr: [],
  16. videoArr: []
  17. };
  18. },
  19. watch: {
  20. nodes(val) {
  21. this.parseVideo();
  22. }
  23. },
  24. created() {
  25. this.parseVideo();
  26. },
  27. methods: {
  28. parseVideo() {
  29. if (typeof this.nodes != 'string') {
  30. //不是HTML字符串格式的暂不处理
  31. this.contentArr[0] = this.nodes;
  32. this.videoArr[0] = null;
  33. return false;
  34. }
  35. //同步解决如果图片太大超出手机显示界面的问题
  36. let nodes = this.nodes.replace(/\<img/g, '<img style="max-width:98%!important;height:auto;"');
  37. let arr = nodes.split('</video>');
  38. let reg = /<video([\s\S]*)/g;
  39. for (let i in arr) {
  40. var item = arr[i];
  41. var urlMatch = item.match(/<video[\s\S]*src=\"(.*?)\"/);
  42. if (urlMatch && urlMatch.length > 1) {
  43. this.videoArr[i] = urlMatch[1];
  44. } else {
  45. this.videoArr[i] = null;
  46. }
  47. this.contentArr[i] = item.replace(reg, '');
  48. }
  49. this.$forceUpdate()
  50. }
  51. }
  52. };
  53. </script>
  54. <style></style>