richTextView.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // component/richTextView/richTextView.ts
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. text: {
  8. type: String,
  9. value: ''
  10. }
  11. },
  12. observers: {
  13. text: function (value: string) {
  14. if (value) {
  15. const videoArr: (string | null)[] = []
  16. const contentArr: string[] = []
  17. // 同步解决如果图片太大超出手机显示界面的问题
  18. const nodes = value.replace(/<blockquote/g, "<blockquote style=\"width:100%;background-color: #f5f2f0;border-left: 8px solid #B4D5FF;display: block;font-size: 100%;line-height: 1.5;margin: 10px 0;padding: 10px;box-sizing: border-box;\"").replace(/<hr\/>/g, "<div style=\"border-radius: 3px;margin: 20px auto;padding: 20px 10px;\"><hr style=\"background-color:#ccc;border: 0;display: block;height: 1px;\"/></div>").replace(/<img [^>]*style=['"]*([^'"]+)[^/>]*/g, (match) => {
  19. return match.replace(/ style=['"]([^'"]+)*/g, " style=\"$1max-width:100%;height:auto;");
  20. });
  21. const arr = nodes.split("</video>");
  22. const reg = /<video([\s\S]*)/g;
  23. for (const i in arr) {
  24. const item = arr[i];
  25. const urlMatch = item.match(/<video[\s\S]*src="(.*?)"/);
  26. if (urlMatch && urlMatch.length > 1) {
  27. videoArr[i] = urlMatch[1];
  28. } else {
  29. videoArr[i] = null;
  30. }
  31. contentArr[i] = item.replace(reg, "");
  32. }
  33. this.setData({
  34. videoArr,
  35. contentArr
  36. })
  37. }
  38. }
  39. },
  40. /**
  41. * 组件的初始数据
  42. */
  43. data: {
  44. },
  45. /**
  46. * 组件的方法列表
  47. */
  48. methods: {
  49. }
  50. })