App.vue 4.8 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
      :fullScreen.sync="fullScreen"
11 12 13 14 15 16 17 18 19 20
      @upload="$refs.mdUploadFile.click()"
    />
    <input
      ref="mdUploadFile"
      class="md_upload"
      type="file"
      hidden
      name="md-upload-file"
      id="md-upload-file"
      @change="upload"
璃白.'s avatar
璃白. 已提交
21
    />
璃白.'s avatar
璃白. 已提交
22
    <markdownPreview :text="text" :html.sync="html" v-show="showPreview" />
璃白.'s avatar
璃白. 已提交
23 24 25 26 27 28
    <markdown-editor
      :selectionInfo.sync="selectionInfo"
      :text.sync="text"
      :fileList.sync="fileList"
      :placeholder="placeholder"
      :isFocus.sync="isFocus"
29
      :throttleTime="throttle"
璃白.'s avatar
璃白. 已提交
30
      :fullScreen.sync="fullScreen"
31 32 33
      :maxLength="maxLength"
      :textLength.sync="textLength"
      :rows="rows"
璃白.'s avatar
璃白. 已提交
34
      @submit="submit"
璃白.'s avatar
璃白. 已提交
35 36
      v-show="!showPreview"
    />
37 38 39 40 41
    <div v-if="maxLength && showWordLimit" class="word_limit">
      <span>{{ textLength }}</span
      >/<span>{{ maxLength }}</span>
    </div>
    <!-- <markdown-footer
璃白.'s avatar
璃白. 已提交
42 43 44
      :fileList.sync="fileList"
      :canAttachFile="canAttachFile"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
45 46
      :can-attach-file="canAttachFile"
      v-if="!showPreview && canAttachFile"
47
    /> -->
璃白.'s avatar
璃白. 已提交
48 49 50
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
51 52 53 54
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
璃白. 已提交
55
import { formatText } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
56
export default {
璃白.'s avatar
璃白. 已提交
57 58 59 60 61
  components: {
    markdownHeader,
    markdownFooter,
    markdownEditor,
    markdownPreview
璃白.'s avatar
璃白. 已提交
62
  },
璃白.'s avatar
璃白. 已提交
63 64 65 66 67 68 69 70
  props: {
    placeholder: {
      type: String,
      default: "请输入内容"
    },
    canAttachFile: {
      type: Boolean,
      default: true
璃白.'s avatar
璃白. 已提交
71 72 73 74 75
    },
    value: {
      type: [String, Number],
      default: ""
    },
76 77 78 79
    throttle: {
      type: Number,
      default: 1000
    },
璃白.'s avatar
璃白. 已提交
80 81 82 83 84 85 86
    canPreview: {
      type: Boolean,
      default: true
    },
    toolsOptions: {
      type: Object,
      default: () => {}
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    },
    rows: {
      type: [Number, String],
      default: ""
    },
    maxLength: {
      type: [Number, String],
      default: ""
    },
    showWordLimit: {
      type: Boolean,
      default: false
    },
    rule: {
      type: RegExp,
      default: /./
璃白.'s avatar
璃白. 已提交
103
    }
G
guoweijia 已提交
104
  },
璃白.'s avatar
璃白. 已提交
105 106 107 108 109 110 111 112
  data() {
    return {
      fullScreen: false,
      isFocus: false,
      showPreview: false,
      fileList: [],
      text: "",
      html: "",
113
      textLength: "",
璃白.'s avatar
璃白. 已提交
114 115 116 117 118 119 120
      selectionInfo: {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      }
    };
  },
121

G
guoweijia 已提交
122 123 124 125
  watch: {
    html: {
      immediate: true,
      handler: function(val) {
126
        this.textLength = this.text.length;
G
guoweijia 已提交
127 128 129 130
        this.$emit("change", {
          text: this.text,
          html: this.html
        });
131 132 133 134
        this.$emit("input", {
          text: this.text,
          html: this.html
        });
G
guoweijia 已提交
135 136
      }
    },
璃白.'s avatar
璃白. 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
    isFocus: {
      handler: function(val) {
        const value = {
          text: this.text,
          html: this.html
        };
        if (val) {
          this.$emit("focus", value);
        } else {
          this.$emit("blur", value);
        }
      }
    },
璃白.'s avatar
璃白. 已提交
150 151 152 153 154 155
    value: {
      immediate: true,
      handler: function(val) {
        this.text = val;
      }
    },
G
guoweijia 已提交
156 157 158 159
    fileList: {
      immediate: false,
      deep: true,
      handler: function(val) {
璃白.'s avatar
璃白. 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
        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 已提交
177 178
      }
    }
璃白.'s avatar
璃白. 已提交
179 180
  },
  methods: {
181 182 183
    upload(e) {
      this.fileList = Array.from(e.target.files);
    },
璃白.'s avatar
璃白. 已提交
184 185 186 187 188 189
    submit() {
      this.$emit("submit", {
        text: this.text,
        html: this.html
      });
    }
璃白.'s avatar
璃白. 已提交
190 191 192 193 194
  }
};
</script>
<style lang="less" scoped>
.md_container {
璃白.'s avatar
璃白. 已提交
195
  width: 100%;
璃白.'s avatar
璃白. 已提交
196
  background: var(--md-editor-frame-bg-color);
璃白.'s avatar
璃白. 已提交
197
  // margin: 200px auto;
璃白.'s avatar
璃白. 已提交
198
  border: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
199 200 201
  border-radius: 4px;
  padding: 10px 16px;
  box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
202
  transition: border 0.3s;
203
  position: relative;
璃白.'s avatar
璃白. 已提交
204
  &.active {
璃白.'s avatar
璃白. 已提交
205
    border: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
206
  }
207 208 209 210 211 212 213
  .word_limit {
    position: absolute;
    right: 10px;
    bottom: 8px;
    font-size: 12px;
    color: var(--md-editor-text-color);
  }
璃白.'s avatar
璃白. 已提交
214 215
}
</style>