md-preview.vue 1.7 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
fix  
璃白. 已提交
2
  <div :class="['md_preview', { fullScreen }]">
璃白.'s avatar
璃白. 已提交
3 4 5 6 7 8 9
    <div
      v-html="html"
      :style="{
        height: height > 0 ? height + 'px' : 'auto',
        'min-height': htmlMinHeight + 'px'
      }"
    ></div>
璃白.'s avatar
璃白. 已提交
10 11 12 13 14
  </div>
</template>
<script>
export default {
  data() {
璃白.'s avatar
fix  
璃白. 已提交
15
    return {};
璃白.'s avatar
璃白. 已提交
16
  },
璃白.'s avatar
璃白. 已提交
17
  props: {
璃白.'s avatar
璃白. 已提交
18 19 20 21
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
璃白. 已提交
22
    text: {
璃白.'s avatar
璃白. 已提交
23
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
24 25
      default: ""
    },
璃白.'s avatar
fix  
璃白. 已提交
26 27 28
    htmlMinHeight: {
      default: ""
    },
璃白.'s avatar
璃白. 已提交
29 30 31 32
    height: {
      type: Number,
      default: 0
    },
璃白.'s avatar
璃白. 已提交
33
    html: {
璃白.'s avatar
璃白. 已提交
34
      type: [String, Promise],
璃白.'s avatar
璃白. 已提交
35
      default: ""
璃白.'s avatar
璃白. 已提交
36 37 38 39
    },
    fullScreen: {
      type: Boolean,
      default: false
璃白.'s avatar
璃白. 已提交
40
    }
璃白.'s avatar
璃白. 已提交
41
  },
璃白.'s avatar
fix  
璃白. 已提交
42 43
  mounted() {
    this.addClass();
璃白.'s avatar
璃白. 已提交
44
    this.addControls();
璃白.'s avatar
fix  
璃白. 已提交
45 46 47
  },
  updated() {
    this.addClass();
璃白.'s avatar
璃白. 已提交
48
    this.addControls();
璃白.'s avatar
fix  
璃白. 已提交
49
  },
璃白.'s avatar
璃白. 已提交
50
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
51 52
    addClass() {
      setTimeout(() => {
璃白.'s avatar
fix  
璃白. 已提交
53 54 55
        const previewDomList = document.querySelectorAll(".md_preview code");
        if (!previewDomList.length) return;
        previewDomList.forEach(item => {
56
          item.className += " md_hljs";
璃白.'s avatar
fix  
璃白. 已提交
57
        });
璃白.'s avatar
fix  
璃白. 已提交
58
      }, 0);
璃白.'s avatar
璃白. 已提交
59 60 61 62 63 64 65 66 67
    },
    addControls() {
      setTimeout(() => {
        const videoDomList = document.querySelectorAll(".md_preview video");
        if (!videoDomList.length) return;
        videoDomList.forEach(item => {
          item.setAttribute("preload", "auto");
        });
      }, 0);
璃白.'s avatar
璃白. 已提交
68 69 70 71 72
    }
  }
};
</script>
<style lang="less" scoped>
璃白.'s avatar
fix  
璃白. 已提交
73
.md_preview {
璃白.'s avatar
璃白. 已提交
74 75
  // min-height: 148px;
  padding: 14px 0;
璃白.'s avatar
璃白. 已提交
76 77 78
  box-sizing: border-box;
  color: var(--md-editor-text-color);
  word-break: break-all;
璃白.'s avatar
璃白. 已提交
79 80 81 82
  overflow-y: auto;
  & > div {
    overflow-y: auto;
  }
璃白.'s avatar
璃白. 已提交
83 84 85 86
  &.fullScreen {
    max-height: calc(100% - 42px);
    overflow-y: auto;
  }
璃白.'s avatar
璃白. 已提交
87 88
}
</style>