index.vue 2.57 KB
Newer Older
李嘉林 committed
1 2 3 4
<template>
  <!-- 富文本组件 -->
  <div
    class="rich-text"
李嘉林 committed
5
    :style="{ 'padding': padding + 'px', 'background': backgroundColor }"
李嘉林 committed
6
  >
李嘉林 committed
7
    <rich-text :nodes="richText" :space="'nbsp'"></rich-text>
李嘉林 committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  </div>
</template>

<script type="text/ecmascript-6">
/**
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性:max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * @param html
 * @returns {void|string|*}
 */
function formatRichText(html) {
  let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
李嘉林 committed
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    // match = match.replace(/style="[^"]+"/gi, "").replace(/style='[^']+'/gi, "");
    // match = match.replace(/width="[^"]+"/gi, "").replace(/width='[^']+'/gi, "");
    // match = match
    //   .replace(/height="[^"]+"/gi, "")
    //   .replace(/height='[^']+'/gi, "");
    let maxWidth = 375;
    match.replace(/width="(.+?)"/g, function (val, val1) {
      if (val1 > maxWidth) {
        match = match
          .replace(/width="[^"]+"/gi, "")
          .replace(/width='[^']+'/gi, "");
        match = match.replace(
          /\<img/gi,
          '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
        );
      }
    });
李嘉林 committed
40 41 42 43
    return match;
  });
  newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
    match = match
李嘉林 committed
44 45 46 47 48
      .replace(/ width:[^;]+;/gi, "max-width:100%;")
      .replace(/ width:[^;]+;/gi, "max-width:100%;");
    match = match
      .replace(/max-width:[^;]+;/gi, "max-width:100%;")
      .replace(/max-width:[^;]+;/gi, "max-width:100%;");
李嘉林 committed
49 50
    return match;
  });
李嘉林 committed
51 52 53 54 55
  // newContent = newContent.replace(/<br[^>]*\/>/gi, "");
  // newContent = newContent.replace(
  //   /\<img/gi,
  //   '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
  // );
李嘉林 committed
56 57 58 59 60 61 62 63 64 65
  return newContent;
}
export default {
  props: {
    render: {
      type: Boolean,
      default: false,
    },
    datas: {
      type: Object,
李嘉林 committed
66 67 68
      default: ()=>{
        return {}
      },
李嘉林 committed
69 70 71 72 73 74 75 76
    },
  },
  data() {
    return {};
  },
  components: {},
  computed: {
    padding() {
李嘉林 committed
77
      return this.datas.componentData.padding;
李嘉林 committed
78 79
    },
    backgroundColor() {
李嘉林 committed
80
      return this.datas.componentData.backgroundColor;
李嘉林 committed
81 82
    },
    richText() {
李嘉林 committed
83
      return formatRichText(this.datas.componentData.richText);
李嘉林 committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    },
  },
  created() {},
  mounted() {},
  methods: {},
};
</script>

<style lang="scss" scoped>
.rich-text {
  /deep/ img {
    max-width: 100% !important;
    height: auto;
  }
}
</style>