App.vue 7.8 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
2 3 4 5
  <div
    :class="['md_container', { active: isFocus, fullScreen }]"
    :id="'md_' + id"
  >
璃白.'s avatar
璃白. 已提交
6
    <markdown-header
璃白.'s avatar
fix  
璃白. 已提交
7
      :id="textareaId"
璃白.'s avatar
fix  
璃白. 已提交
8
      :ref="'md_header' + id"
璃白.'s avatar
璃白. 已提交
9 10 11 12
      :text.sync="text"
      :selectionInfo.sync="selectionInfo"
      :showPreview.sync="showPreview"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
13 14
      :canPreview="canPreview"
      :toolsOptions="toolsOptions"
15
      :zIndex="zIndex"
璃白.'s avatar
fix  
璃白. 已提交
16
      :tabSize="tabSize"
璃白.'s avatar
璃白. 已提交
17
      :fullScreen.sync="fullScreen"
18
      :themeOptions="themeOptions"
19 20 21 22 23 24 25 26 27 28
      @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
璃白. 已提交
29
    />
璃白.'s avatar
璃白. 已提交
30 31 32 33
    <markdownPreview
      :id="textareaId"
      :fullScreen.sync="fullScreen"
      :text="text"
璃白.'s avatar
璃白. 已提交
34
      :height="textareaHeight"
璃白.'s avatar
璃白. 已提交
35
      :html.sync="html"
璃白.'s avatar
fix  
璃白. 已提交
36 37
      :htmlMinHeight="htmlMinHeight"
      v-if="showPreview"
璃白.'s avatar
璃白. 已提交
38
    />
璃白.'s avatar
璃白. 已提交
39 40 41 42 43 44
    <markdown-editor
      :selectionInfo.sync="selectionInfo"
      :text.sync="text"
      :fileList.sync="fileList"
      :placeholder="placeholder"
      :isFocus.sync="isFocus"
45
      :throttleTime="throttle"
璃白.'s avatar
fix  
璃白. 已提交
46
      :htmlMinHeight.sync="htmlMinHeight"
璃白.'s avatar
璃白. 已提交
47
      :fullScreen.sync="fullScreen"
48 49 50
      :maxLength="maxLength"
      :textLength.sync="textLength"
      :rows="rows"
璃白.'s avatar
璃白. 已提交
51
      :height="textareaHeight"
璃白.'s avatar
fix  
璃白. 已提交
52
      :html.sync="html"
53
      :id="textareaId"
璃白.'s avatar
fix  
璃白. 已提交
54
      :ref="'md_textarea' + id"
璃白.'s avatar
fix  
璃白. 已提交
55
      @tab="$refs['md_header' + id].tab()"
璃白.'s avatar
璃白. 已提交
56
      @submit="submit"
璃白.'s avatar
fix  
璃白. 已提交
57
      @enter="handleEnter"
璃白.'s avatar
璃白. 已提交
58
      @getFilteredTags="filteredTags = $event"
璃白.'s avatar
fix  
璃白. 已提交
59
      v-else
璃白.'s avatar
璃白. 已提交
60
    />
璃白.'s avatar
璃白. 已提交
61
    <div v-if="maxLength && showWordLimit && !showPreview" class="word_limit">
62 63 64 65
      <span>{{ textLength }}</span
      >/<span>{{ maxLength }}</span>
    </div>
    <!-- <markdown-footer
璃白.'s avatar
璃白. 已提交
66 67 68
      :fileList.sync="fileList"
      :canAttachFile="canAttachFile"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
69 70
      :can-attach-file="canAttachFile"
      v-if="!showPreview && canAttachFile"
71
    /> -->
璃白.'s avatar
璃白. 已提交
72 73 74
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
75 76 77 78
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";
79
import { formatText, checktUrl } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
80
export default {
璃白.'s avatar
璃白. 已提交
81 82 83 84
  components: {
    markdownHeader,
    markdownFooter,
    markdownEditor,
璃白.'s avatar
璃白. 已提交
85
    markdownPreview
璃白.'s avatar
璃白. 已提交
86
  },
璃白.'s avatar
璃白. 已提交
87 88 89 90 91
  props: {
    placeholder: {
      type: String,
      default: "请输入内容"
    },
92 93 94
    id: {
      type: String,
      default: ""
璃白.'s avatar
璃白. 已提交
95
    },
璃白.'s avatar
fix  
璃白. 已提交
96 97 98 99
    tabSize: {
      type: [String, Number],
      default: ""
    },
100 101 102 103 104
    // canAttachFile: {
    //   type: Boolean,
    //   default: true
    // },
    // 初始化时赋值
璃白.'s avatar
璃白. 已提交
105 106
    value: {
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
107
      default: ""
璃白.'s avatar
璃白. 已提交
108
    },
109 110 111 112 113 114
    // 全屏时的z-index
    zIndex: {
      type: [String, Number],
      default: ""
    },
    // input时间节流
115 116
    throttle: {
      type: Number,
117
      default: 0
118
    },
璃白.'s avatar
fix  
璃白. 已提交
119 120 121 122 123 124
    setPreview: {
      type: Boolean,
      default: false
    },
    setFullScreen: {
      // type: Boolean,
璃白.'s avatar
fix  
璃白. 已提交
125 126
      default: ""
    },
127
    // 是否可以预览
璃白.'s avatar
璃白. 已提交
128 129 130 131
    canPreview: {
      type: Boolean,
      default: true
    },
132 133 134 135 136 137 138 139 140 141
    // 主题
    themeOptions: {
      type: Object,
      default: () => {}
    },
    focus: {
      type: Boolean,
      default: false
    },
    // 工具栏
璃白.'s avatar
璃白. 已提交
142 143 144
    toolsOptions: {
      type: Object,
      default: () => {}
145
    },
146
    // 行高度
147 148 149 150
    rows: {
      type: [Number, String],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
151 152 153 154 155
    // 高度
    height: {
      type: Number,
      default: 0
    },
156
    // 最大长度
157 158 159 160
    maxLength: {
      type: [Number, String],
      default: ""
    },
161
    // 显示字数限制
162 163 164 165
    showWordLimit: {
      type: Boolean,
      default: false
    },
166 167
    // 图片路径规则
    filePathRule: {
168
      type: RegExp,
169 170 171 172 173 174
      default: () => {}
    }
  },
  computed: {
    textareaId() {
      return "textarea_" + this.id;
璃白.'s avatar
璃白. 已提交
175
    },
璃白.'s avatar
fix  
璃白. 已提交
176 177 178
    headId() {
      return "md_header" + this.id;
    },
璃白.'s avatar
璃白. 已提交
179 180 181 182
    textareaHeight() {
      const height = this.height;
      if (!height) return 0;
      return height - 83;
璃白.'s avatar
璃白. 已提交
183
    }
G
guoweijia 已提交
184
  },
璃白.'s avatar
璃白. 已提交
185 186 187 188 189 190
  data() {
    return {
      fullScreen: false,
      isFocus: false,
      showPreview: false,
      fileList: [],
璃白.'s avatar
璃白. 已提交
191
      filteredTags: [],
璃白.'s avatar
璃白. 已提交
192 193
      text: "",
      html: "",
璃白.'s avatar
fix  
璃白. 已提交
194
      ulNum: 1,
璃白.'s avatar
fix  
璃白. 已提交
195
      htmlMinHeight: 150,
196
      textLength: "",
璃白.'s avatar
璃白. 已提交
197 198 199 200 201 202 203
      selectionInfo: {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      }
    };
  },
204 205 206 207 208 209 210 211
  created() {
    setTimeout(() => {
      this.$emit("load", {
        text: this.text,
        html: this.html
      });
    }, 0);
  },
G
guoweijia 已提交
212
  watch: {
213 214
    focus: {
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
        setTimeout(() => {
          const textEl = document.getElementById(this.textareaId);
          if (val) {
            textEl.focus();
          } else {
            textEl.blur();
          }
        });
      }
    },
    showPreview: {
      handler: function(val) {
        this.$emit("changeTab", val);
      }
    },
    setPreview: {
      handler: function(val) {
        this.showPreview = val;
233 234
      }
    },
璃白.'s avatar
fix  
璃白. 已提交
235
    setFullScreen: {
璃白.'s avatar
fix  
璃白. 已提交
236
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
237 238 239 240 241 242
        this.fullScreen = val;
      }
    },
    fullScreen: {
      handler: function(val) {
        this.$emit("changeFullScreen", val);
璃白.'s avatar
fix  
璃白. 已提交
243 244
      }
    },
245 246 247 248 249 250
    text: {
      immediate: true,
      handler: function(val) {
        this.textLength = val.length;
      }
    },
G
guoweijia 已提交
251
    html: {
璃白.'s avatar
fix  
璃白. 已提交
252
      immediate: false,
G
guoweijia 已提交
253
      handler: function(val) {
254
        const emitContent = {
255 256
          text: this.text,
          html: this.html
257 258 259 260 261
        };
        if (this.filePathRule) {
          const checkResult = checktUrl(val, this.filePathRule);
          emitContent.invalidList = checkResult;
        }
璃白.'s avatar
璃白. 已提交
262
        emitContent.filteredTags = this.filteredTags;
263 264
        this.$emit("change", emitContent);
        this.$emit("input", emitContent);
G
guoweijia 已提交
265 266
      }
    },
璃白.'s avatar
璃白. 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279
    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
璃白. 已提交
280 281 282 283 284 285
    value: {
      immediate: true,
      handler: function(val) {
        this.text = val;
      }
    },
G
guoweijia 已提交
286 287 288 289
    fileList: {
      immediate: false,
      deep: true,
      handler: function(val) {
璃白.'s avatar
璃白. 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303
        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;
璃白.'s avatar
fix  
璃白. 已提交
304
            _this.$refs.mdUploadFile.value = "";
璃白.'s avatar
璃白. 已提交
305 306 307
          }
        });
        this.fileList = [];
G
guoweijia 已提交
308 309
      }
    }
璃白.'s avatar
璃白. 已提交
310 311
  },
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
312 313 314
    handleEnter() {
      this.$refs[this.headId].resetUlNum();
    },
315 316 317
    upload(e) {
      this.fileList = Array.from(e.target.files);
    },
璃白.'s avatar
璃白. 已提交
318 319 320 321 322 323
    submit() {
      this.$emit("submit", {
        text: this.text,
        html: this.html
      });
    }
璃白.'s avatar
璃白. 已提交
324 325 326 327 328
  }
};
</script>
<style lang="less" scoped>
.md_container {
璃白.'s avatar
璃白. 已提交
329
  // width: 100%;
璃白.'s avatar
璃白. 已提交
330
  background: var(--md-editor-frame-bg-color);
璃白.'s avatar
璃白. 已提交
331
  // margin: 200px auto;
璃白.'s avatar
璃白. 已提交
332
  border: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
333 334 335
  border-radius: 4px;
  padding: 10px 16px;
  box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
336
  transition: border 0.3s;
337
  position: relative;
338 339 340 341 342 343 344
  &.fullScreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    margin: 0;
璃白.'s avatar
璃白. 已提交
345
    border: 1px solid transparent !important;
346 347
    z-index: var(--md-editor-fullScrren-zIndex);
  }
璃白.'s avatar
璃白. 已提交
348
  &.active {
璃白.'s avatar
璃白. 已提交
349
    border: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
350
  }
351 352 353 354 355 356 357
  .word_limit {
    position: absolute;
    right: 10px;
    bottom: 8px;
    font-size: 12px;
    color: var(--md-editor-text-color);
  }
璃白.'s avatar
璃白. 已提交
358 359
}
</style>