App.vue 3.1 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2
  <div :class="['md_container', { active: isFocus }]">
璃白.'s avatar
璃白. 已提交
3 4 5 6 7
    <markdown-header
      :text.sync="text"
      :selectionInfo.sync="selectionInfo"
      :showPreview.sync="showPreview"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
8 9
      :canPreview="canPreview"
      :toolsOptions="toolsOptions"
璃白.'s avatar
璃白. 已提交
10 11
      :fullScreen.sync="fullScreen"
    />
璃白.'s avatar
璃白. 已提交
12
  <markdownPreview :text="text" :html.sync="html" v-show="showPreview" />
璃白.'s avatar
璃白. 已提交
13 14 15 16 17 18 19 20 21
    <markdown-editor
      :selectionInfo.sync="selectionInfo"
      :text.sync="text"
      :fileList.sync="fileList"
      :placeholder="placeholder"
      :isFocus.sync="isFocus"
      :fullScreen.sync="fullScreen"
      v-show="!showPreview"
    />
璃白.'s avatar
璃白. 已提交
22
    <markdown-footer
璃白.'s avatar
璃白. 已提交
23 24 25
      :fileList.sync="fileList"
      :canAttachFile="canAttachFile"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
26 27 28
      :can-attach-file="canAttachFile"
      v-if="!showPreview && canAttachFile"
    />
璃白.'s avatar
璃白. 已提交
29 30 31
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
32 33 34 35
import markdownHeader from "./components/header/md-header";
import markdownFooter from "./components/footer/md-footer";
import markdownEditor from "./components/content/md-textarea";
import markdownPreview from "./components/content/md-preview";
璃白.'s avatar
璃白. 已提交
36
import { formatText } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
37
export default {
璃白.'s avatar
璃白. 已提交
38 39 40 41 42
  components: {
    markdownHeader,
    markdownFooter,
    markdownEditor,
    markdownPreview
璃白.'s avatar
璃白. 已提交
43
  },
璃白.'s avatar
璃白. 已提交
44 45 46 47 48 49 50 51
  props: {
    placeholder: {
      type: String,
      default: "请输入内容"
    },
    canAttachFile: {
      type: Boolean,
      default: true
璃白.'s avatar
璃白. 已提交
52 53 54 55 56 57 58 59 60 61 62 63
    },
    value: {
      type: [String, Number],
      default: ""
    },
    canPreview: {
      type: Boolean,
      default: true
    },
    toolsOptions: {
      type: Object,
      default: () => {}
璃白.'s avatar
璃白. 已提交
64
    }
G
guoweijia 已提交
65
  },
璃白.'s avatar
璃白. 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  data() {
    return {
      fullScreen: false,
      isFocus: false,
      showPreview: false,
      fileList: [],
      text: "",
      html: "",
      selectionInfo: {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      }
    };
  },
G
guoweijia 已提交
81 82 83 84 85 86 87 88 89 90
  watch: {
    html: {
      immediate: true,
      handler: function(val) {
        this.$emit("change", {
          text: this.text,
          html: this.html
        });
      }
    },
璃白.'s avatar
璃白. 已提交
91 92 93 94 95 96
    value: {
      immediate: true,
      handler: function(val) {
        this.text = val;
      }
    },
G
guoweijia 已提交
97 98 99 100
    fileList: {
      immediate: false,
      deep: true,
      handler: function(val) {
璃白.'s avatar
璃白. 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        const _this = this;
        if (!val.length) return;
        this.$emit("upload", {
          val: val[0],
          callback: function(url) {
            const originalText = _this.text;
            const selectionInfo = _this.selectionInfo;
            const newText = formatText(
              originalText,
              selectionInfo,
              "\n\n![img](",
              `${url})\n`
            );
            _this.text = newText;
          }
        });
        this.fileList = [];
G
guoweijia 已提交
118 119
      }
    }
璃白.'s avatar
璃白. 已提交
120 121 122 123 124
  }
};
</script>
<style lang="less" scoped>
.md_container {
璃白.'s avatar
璃白. 已提交
125 126
  width: 100%;
  // margin: 200px auto;
璃白.'s avatar
璃白. 已提交
127
  border: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
128 129 130
  border-radius: 4px;
  padding: 10px 16px;
  box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
131 132
  transition: border 0.3s;
  &.active {
璃白.'s avatar
璃白. 已提交
133
    border: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
134 135 136
  }
}
</style>