App.vue 11.5 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
      :disabled="disabled"
璃白.'s avatar
璃白. 已提交
18
      :fullScreen.sync="fullScreen"
19
      :themeOptions="themeOptions"
璃白.'s avatar
璃白. 已提交
20
      :uploadPercent="uploadPercent"
璃白.'s avatar
璃白. 已提交
21 22 23
      @upload="handleUpload"
      @getFormatType="formatType = $event"
      @updateShowHelp="showHelp = $event"
璃白.'s avatar
璃白. 已提交
24
      @renderLinks="$emit('renderLinks', $event)"
25 26 27 28 29 30 31 32 33
    />
    <input
      ref="mdUploadFile"
      class="md_upload"
      type="file"
      hidden
      name="md-upload-file"
      id="md-upload-file"
      @change="upload"
璃白.'s avatar
璃白. 已提交
34
    />
璃白.'s avatar
璃白. 已提交
35 36 37 38
    <markdownPreview
      :id="textareaId"
      :fullScreen.sync="fullScreen"
      :text="text"
璃白.'s avatar
璃白. 已提交
39
      :height="textareaHeight"
璃白.'s avatar
璃白. 已提交
40
      :html.sync="html"
璃白.'s avatar
fix  
璃白. 已提交
41 42
      :htmlMinHeight="htmlMinHeight"
      v-if="showPreview"
璃白.'s avatar
璃白. 已提交
43
    />
璃白.'s avatar
璃白. 已提交
44

璃白.'s avatar
璃白. 已提交
45 46 47 48 49 50
    <markdown-editor
      :selectionInfo.sync="selectionInfo"
      :text.sync="text"
      :fileList.sync="fileList"
      :placeholder="placeholder"
      :isFocus.sync="isFocus"
51
      :throttleTime="throttle"
璃白.'s avatar
fix  
璃白. 已提交
52
      :htmlMinHeight.sync="htmlMinHeight"
璃白.'s avatar
璃白. 已提交
53
      :fullScreen.sync="fullScreen"
54 55 56
      :maxLength="maxLength"
      :textLength.sync="textLength"
      :rows="rows"
璃白.'s avatar
璃白. 已提交
57
      :height="textareaHeight"
璃白.'s avatar
fix  
璃白. 已提交
58
      :html.sync="html"
59
      :id="textareaId"
璃白.'s avatar
璃白. 已提交
60
      :disabled="disabled"
璃白.'s avatar
璃白. 已提交
61 62
      :show-help="showHelp"
      :formatType="formatType"
璃白.'s avatar
璃白. 已提交
63
      :userList="userList"
璃白.'s avatar
fix  
璃白. 已提交
64
      :ref="'md_textarea' + id"
璃白.'s avatar
fix  
璃白. 已提交
65
      @tab="$refs['md_header' + id].tab()"
璃白.'s avatar
璃白. 已提交
66
      @submit="submit"
璃白.'s avatar
fix  
璃白. 已提交
67
      @enter="handleEnter"
璃白.'s avatar
璃白. 已提交
68
      @getFilteredTags="filteredTags = $event"
璃白.'s avatar
璃白. 已提交
69
      @updateShowHelp="showHelp = $event"
璃白.'s avatar
璃白. 已提交
70
      @renderLinksHtml="renderLinksHtml"
璃白.'s avatar
璃白. 已提交
71
      @queryUserList="queryUserList"
璃白.'s avatar
璃白. 已提交
72
      @callUserList="callUserList = $event"
璃白.'s avatar
fix  
璃白. 已提交
73
      v-else
璃白.'s avatar
璃白. 已提交
74
    />
璃白.'s avatar
璃白. 已提交
75
    <div v-if="maxLength && showWordLimit && !showPreview" class="word_limit">
76 77 78 79
      <span>{{ textLength }}</span
      >/<span>{{ maxLength }}</span>
    </div>
    <!-- <markdown-footer
璃白.'s avatar
璃白. 已提交
80 81 82
      :fileList.sync="fileList"
      :canAttachFile="canAttachFile"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
83 84
      :can-attach-file="canAttachFile"
      v-if="!showPreview && canAttachFile"
85
    /> -->
璃白.'s avatar
璃白. 已提交
86 87 88
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
89 90 91 92
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
璃白. 已提交
93 94 95 96 97 98
import {
  formatText,
  checktUrl,
  getLinkTitle,
  renderLinkCard
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
99
export default {
璃白.'s avatar
璃白. 已提交
100 101 102 103
  components: {
    markdownHeader,
    markdownFooter,
    markdownEditor,
璃白.'s avatar
璃白. 已提交
104
    markdownPreview
璃白.'s avatar
璃白. 已提交
105
  },
璃白.'s avatar
璃白. 已提交
106 107 108 109 110
  props: {
    placeholder: {
      type: String,
      default: "请输入内容"
    },
111 112 113
    id: {
      type: String,
      default: ""
璃白.'s avatar
璃白. 已提交
114
    },
璃白.'s avatar
fix  
璃白. 已提交
115 116 117 118
    tabSize: {
      type: [String, Number],
      default: ""
    },
119 120 121 122 123
    // canAttachFile: {
    //   type: Boolean,
    //   default: true
    // },
    // 初始化时赋值
璃白.'s avatar
璃白. 已提交
124 125
    value: {
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
126
      default: ""
璃白.'s avatar
璃白. 已提交
127
    },
128 129 130 131 132 133
    // 全屏时的z-index
    zIndex: {
      type: [String, Number],
      default: ""
    },
    // input时间节流
134 135
    throttle: {
      type: Number,
136
      default: 0
137
    },
璃白.'s avatar
fix  
璃白. 已提交
138 139 140 141
    setPreview: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
142 143 144 145
    disabled: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
fix  
璃白. 已提交
146 147
    setFullScreen: {
      // type: Boolean,
璃白.'s avatar
fix  
璃白. 已提交
148 149
      default: ""
    },
150
    // 是否可以预览
璃白.'s avatar
璃白. 已提交
151 152 153 154
    canPreview: {
      type: Boolean,
      default: true
    },
155 156 157 158 159 160 161 162 163 164
    // 主题
    themeOptions: {
      type: Object,
      default: () => {}
    },
    focus: {
      type: Boolean,
      default: false
    },
    // 工具栏
璃白.'s avatar
璃白. 已提交
165 166 167
    toolsOptions: {
      type: Object,
      default: () => {}
168
    },
169
    // 行高度
170 171 172 173
    rows: {
      type: [Number, String],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
174 175 176 177 178
    // 高度
    height: {
      type: Number,
      default: 0
    },
179
    // 最大长度
180 181 182 183
    maxLength: {
      type: [Number, String],
      default: ""
    },
184
    // 显示字数限制
185 186 187 188
    showWordLimit: {
      type: Boolean,
      default: false
    },
189 190
    // 图片路径规则
    filePathRule: {
191
      type: RegExp,
192 193 194 195
      default: () => {}
    }
  },
  computed: {
璃白.'s avatar
璃白. 已提交
196 197 198 199
    // isMobile() {
    //   const isMobile = checkBoswer();
    //   return isMobile;
    // },
200 201
    textareaId() {
      return "textarea_" + this.id;
璃白.'s avatar
璃白. 已提交
202
    },
璃白.'s avatar
fix  
璃白. 已提交
203 204 205
    headId() {
      return "md_header" + this.id;
    },
璃白.'s avatar
璃白. 已提交
206 207 208 209
    textareaHeight() {
      const height = this.height;
      if (!height) return 0;
      return height - 83;
璃白.'s avatar
璃白. 已提交
210 211 212 213 214 215 216 217 218 219
    },
    uploadImgCallBack() {
      const _this = this;
      return function({ url, file: { name } }) {
        const originalText = _this.text;
        const selectionInfo = _this.selectionInfo;
        const newText = formatText(
          originalText,
          selectionInfo,
          "\n![img](",
璃白.'s avatar
璃白. 已提交
220
          `${url} "=600 #left")\n`
璃白.'s avatar
璃白. 已提交
221 222 223 224 225 226 227
        );
        _this.text = newText;
        _this.$refs.mdUploadFile.value = "";
      };
    },
    uploadFileCallBack() {
      const _this = this;
璃白.'s avatar
璃白. 已提交
228
      return function({ url, file: { name, size } }) {
璃白.'s avatar
璃白. 已提交
229 230 231 232 233 234
        const originalText = _this.text;
        const selectionInfo = _this.selectionInfo;
        const newText = formatText(
          originalText,
          selectionInfo,
          "\n![file](",
璃白.'s avatar
璃白. 已提交
235
          `${url.replace(/\s/g, "%20")} '${name} ${size}')\n`
璃白.'s avatar
璃白. 已提交
236 237 238 239 240 241 242 243
        );
        _this.text = newText;
        _this.$refs.mdUploadFile.value = "";
      };
    },
    uploadVideoCallBack() {
      const _this = this;
      return function({ url, file: { name } }) {
璃白.'s avatar
璃白. 已提交
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        if (isNaN(parseInt(url))) {
          const originalText = _this.text;
          const selectionInfo = _this.selectionInfo;
          const newText = formatText(
            originalText,
            selectionInfo,
            "![video](",
            `${url})\n`
          );
          _this.text = newText;
          _this.$refs.mdUploadFile.value = "";
          _this.uploadPercent = 100;
        } else {
          _this.uploadPercent = parseInt(url);
        }
        _this.$refs["md_header" + _this.id].loading(
          "video",
          _this.uploadPercent
璃白.'s avatar
璃白. 已提交
262 263
        );
      };
璃白.'s avatar
璃白. 已提交
264
    }
G
guoweijia 已提交
265
  },
璃白.'s avatar
璃白. 已提交
266 267 268 269 270
  data() {
    return {
      fullScreen: false,
      isFocus: false,
      showPreview: false,
璃白.'s avatar
璃白. 已提交
271
      uploadType: "img",
璃白.'s avatar
璃白. 已提交
272
      fileList: [],
璃白.'s avatar
璃白. 已提交
273
      filteredTags: [],
璃白.'s avatar
璃白. 已提交
274 275
      text: "",
      html: "",
璃白.'s avatar
fix  
璃白. 已提交
276
      ulNum: 1,
璃白.'s avatar
璃白. 已提交
277
      formatType: "",
璃白.'s avatar
fix  
璃白. 已提交
278
      htmlMinHeight: 150,
璃白.'s avatar
璃白. 已提交
279
      showHelp: false,
璃白.'s avatar
璃白. 已提交
280
      uploadPercent: 0,
281
      textLength: "",
璃白.'s avatar
璃白. 已提交
282 283
      userList: false,
      callUserList: [],
璃白.'s avatar
璃白. 已提交
284 285 286 287 288 289 290
      selectionInfo: {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      }
    };
  },
291 292 293 294 295 296 297 298
  created() {
    setTimeout(() => {
      this.$emit("load", {
        text: this.text,
        html: this.html
      });
    }, 0);
  },
G
guoweijia 已提交
299
  watch: {
300 301
    focus: {
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
302 303
        setTimeout(() => {
          const textEl = document.getElementById(this.textareaId);
璃白.'s avatar
璃白. 已提交
304
          if (!textEl) return;
璃白.'s avatar
fix  
璃白. 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
          if (val) {
            textEl.focus();
          } else {
            textEl.blur();
          }
        });
      }
    },
    showPreview: {
      handler: function(val) {
        this.$emit("changeTab", val);
      }
    },
    setPreview: {
      handler: function(val) {
        this.showPreview = val;
321 322
      }
    },
璃白.'s avatar
fix  
璃白. 已提交
323
    setFullScreen: {
璃白.'s avatar
fix  
璃白. 已提交
324
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
325 326 327 328 329 330
        this.fullScreen = val;
      }
    },
    fullScreen: {
      handler: function(val) {
        this.$emit("changeFullScreen", val);
璃白.'s avatar
fix  
璃白. 已提交
331 332
      }
    },
333 334 335 336 337 338
    text: {
      immediate: true,
      handler: function(val) {
        this.textLength = val.length;
      }
    },
G
guoweijia 已提交
339
    html: {
璃白.'s avatar
fix  
璃白. 已提交
340
      immediate: false,
G
guoweijia 已提交
341
      handler: function(val) {
342
        const emitContent = {
343 344
          text: this.text,
          html: this.html
345 346 347 348 349
        };
        if (this.filePathRule) {
          const checkResult = checktUrl(val, this.filePathRule);
          emitContent.invalidList = checkResult;
        }
璃白.'s avatar
璃白. 已提交
350 351
        if (this.callUserList.length) {
          emitContent.callUserList = this.callUserList;
璃白.'s avatar
璃白. 已提交
352 353
        } else {
          emitContent.callUserList = [];
璃白.'s avatar
璃白. 已提交
354
        }
璃白.'s avatar
璃白. 已提交
355
        emitContent.filteredTags = this.filteredTags;
璃白.'s avatar
璃白. 已提交
356

357 358
        this.$emit("change", emitContent);
        this.$emit("input", emitContent);
G
guoweijia 已提交
359 360
      }
    },
璃白.'s avatar
璃白. 已提交
361 362 363 364 365 366 367
    isFocus: {
      handler: function(val) {
        const value = {
          text: this.text,
          html: this.html
        };
        if (val) {
璃白.'s avatar
璃白. 已提交
368
          this.showHelp = false;
璃白.'s avatar
璃白. 已提交
369 370 371 372 373 374
          this.$emit("focus", value);
        } else {
          this.$emit("blur", value);
        }
      }
    },
璃白.'s avatar
璃白. 已提交
375 376 377 378 379 380
    value: {
      immediate: true,
      handler: function(val) {
        this.text = val;
      }
    },
G
guoweijia 已提交
381 382 383 384
    fileList: {
      immediate: false,
      deep: true,
      handler: function(val) {
璃白.'s avatar
璃白. 已提交
385
        const uploadType = this.uploadType;
璃白.'s avatar
璃白. 已提交
386 387 388
        if (!val.length) return;
        this.$emit("upload", {
          val: val[0],
璃白.'s avatar
璃白. 已提交
389
          type: uploadType,
璃白.'s avatar
璃白. 已提交
390 391 392
          callback:
            uploadType === "img"
              ? this.uploadImgCallBack
璃白.'s avatar
璃白. 已提交
393 394
              : uploadType === "video"
              ? this.uploadVideoCallBack
璃白.'s avatar
璃白. 已提交
395
              : this.uploadFileCallBack
璃白.'s avatar
璃白. 已提交
396 397
        });
        this.fileList = [];
G
guoweijia 已提交
398 399
      }
    }
璃白.'s avatar
璃白. 已提交
400 401
  },
  methods: {
璃白.'s avatar
璃白. 已提交
402 403
    handleUpload(type) {
      this.uploadType = type;
璃白.'s avatar
璃白. 已提交
404 405 406
      this.$refs.mdUploadFile.click();
      document.getElementById(this.textareaId).focus();
    },
璃白.'s avatar
fix  
璃白. 已提交
407 408 409
    handleEnter() {
      this.$refs[this.headId].resetUlNum();
    },
410 411
    upload(e) {
      this.fileList = Array.from(e.target.files);
璃白.'s avatar
璃白. 已提交
412
      this.$refs.mdUploadFile.value = "";
413
    },
璃白.'s avatar
璃白. 已提交
414 415 416 417 418
    submit() {
      this.$emit("submit", {
        text: this.text,
        html: this.html
      });
璃白.'s avatar
璃白. 已提交
419
    },
璃白.'s avatar
璃白. 已提交
420 421 422 423 424 425 426 427 428
    queryUserList(keyWord) {
      const _this = this;
      this.$emit("queryUserList", {
        keyWord,
        callback: function(list) {
          _this.userList = list;
        }
      });
    },
璃白.'s avatar
璃白. 已提交
429
    renderLinksHtml({ vDom, links }) {
璃白.'s avatar
璃白. 已提交
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
      const _this = this;
      this.$emit("renderLinks", {
        links,
        callback: function(list) {
          list.forEach(item => {
            // item.csdn = true;
            const linkEl = vDom.querySelector("#" + item.id);
            if (!linkEl) return;
            const url = item.csdn
              ? "https://link.csdn.net/?target=" + item.url
              : item.url;
            // linkEl.id = "md_link_card";
            linkEl.className = "md_link_card";
            linkEl.setAttribute("target", "_blank");
            linkEl.setAttribute("href", url);
            const title = getLinkTitle(linkEl, item);

            linkEl.innerHTML = renderLinkCard(title, item);
          });
          _this.html = vDom.innerHTML;
        }
璃白.'s avatar
璃白. 已提交
451
      });
璃白.'s avatar
璃白. 已提交
452
    }
璃白.'s avatar
璃白. 已提交
453 454 455 456 457
  }
};
</script>
<style lang="less" scoped>
.md_container {
璃白.'s avatar
璃白. 已提交
458
  background: var(--md-editor-frame-bg-color);
璃白.'s avatar
璃白. 已提交
459
  border: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
460 461 462
  border-radius: 4px;
  padding: 10px 16px;
  box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
463
  transition: border 0.3s;
464
  position: relative;
璃白.'s avatar
璃白. 已提交
465
  // overflow: hidden; // 注释为了显示@用户的弹窗
466 467 468 469 470 471 472
  &.fullScreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    margin: 0;
璃白.'s avatar
璃白. 已提交
473
    border: 1px solid transparent !important;
474 475
    z-index: var(--md-editor-fullScrren-zIndex);
  }
璃白.'s avatar
璃白. 已提交
476
  &.active {
璃白.'s avatar
璃白. 已提交
477
    border: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
478
  }
璃白.'s avatar
璃白. 已提交
479 480 481
  &.disabled {
    background: var(--md-editor-frame-bg-color-disabled);
  }
482 483 484 485 486 487 488
  .word_limit {
    position: absolute;
    right: 10px;
    bottom: 8px;
    font-size: 12px;
    color: var(--md-editor-text-color);
  }
璃白.'s avatar
璃白. 已提交
489 490
}
</style>