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

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

383 384
        this.$emit("change", emitContent);
        this.$emit("input", emitContent);
G
guoweijia 已提交
385 386
      }
    },
璃白.'s avatar
璃白. 已提交
387 388 389 390 391 392 393
    isFocus: {
      handler: function(val) {
        const value = {
          text: this.text,
          html: this.html
        };
        if (val) {
璃白.'s avatar
璃白. 已提交
394
          this.showHelp = false;
璃白.'s avatar
璃白. 已提交
395 396 397 398 399 400
          this.$emit("focus", value);
        } else {
          this.$emit("blur", value);
        }
      }
    },
璃白.'s avatar
璃白. 已提交
401 402 403 404 405 406
    value: {
      immediate: true,
      handler: function(val) {
        this.text = val;
      }
    },
G
guoweijia 已提交
407 408 409 410
    fileList: {
      immediate: false,
      deep: true,
      handler: function(val) {
璃白.'s avatar
璃白. 已提交
411
        const uploadType = this.uploadType;
璃白.'s avatar
璃白. 已提交
412 413 414
        if (!val.length) return;
        this.$emit("upload", {
          val: val[0],
璃白.'s avatar
璃白. 已提交
415
          type: uploadType,
璃白.'s avatar
璃白. 已提交
416 417 418
          callback:
            uploadType === "img"
              ? this.uploadImgCallBack
璃白.'s avatar
璃白. 已提交
419 420
              : uploadType === "video"
              ? this.uploadVideoCallBack
璃白.'s avatar
璃白. 已提交
421
              : this.uploadFileCallBack
璃白.'s avatar
璃白. 已提交
422 423
        });
        this.fileList = [];
G
guoweijia 已提交
424 425
      }
    }
璃白.'s avatar
璃白. 已提交
426 427
  },
  methods: {
璃白.'s avatar
璃白. 已提交
428 429
    handleUpload(type) {
      this.uploadType = type;
璃白.'s avatar
璃白. 已提交
430 431 432
      this.$refs.mdUploadFile.click();
      document.getElementById(this.textareaId).focus();
    },
璃白.'s avatar
fix  
璃白. 已提交
433 434 435
    handleEnter() {
      this.$refs[this.headId].resetUlNum();
    },
436 437
    upload(e) {
      this.fileList = Array.from(e.target.files);
璃白.'s avatar
璃白. 已提交
438
      this.$refs.mdUploadFile.value = "";
439
    },
璃白.'s avatar
璃白. 已提交
440 441 442 443 444
    submit() {
      this.$emit("submit", {
        text: this.text,
        html: this.html
      });
璃白.'s avatar
璃白. 已提交
445
    },
璃白.'s avatar
璃白. 已提交
446 447 448 449 450 451 452 453 454 455
    getVideoList(callback) {
      const virtualDom = document.createElement("div");
      virtualDom.style.height = 0;
      virtualDom.innerHTML = this.html;
      document.body.appendChild(virtualDom);
      const vEls = Array.from(virtualDom.getElementsByTagName("video"));
      const list = vEls.map(item => {
        item.preload = true;
        return new Promise((res, rej) => {
          item.oncanplay = function() {
璃白.'s avatar
璃白. 已提交
456 457 458 459
            res({
              src: item.src,
              duration: item.duration
            });
璃白.'s avatar
璃白. 已提交
460 461 462 463
          };
        });
      });
      document.body.removeChild(virtualDom);
璃白.'s avatar
fix  
璃白. 已提交
464 465 466
      return Promise.all(list).then(res => {
        if (callback) callback(res);
        return res;
璃白.'s avatar
璃白. 已提交
467 468
      });
    },
璃白.'s avatar
璃白. 已提交
469 470 471 472 473 474 475 476 477
    queryUserList(keyWord) {
      const _this = this;
      this.$emit("queryUserList", {
        keyWord,
        callback: function(list) {
          _this.userList = list;
        }
      });
    },
璃白.'s avatar
璃白. 已提交
478
    renderLinksHtml({ vDom, links }) {
璃白.'s avatar
璃白. 已提交
479 480 481 482 483
      // 缓存里没有的链接,就发送请求获取信息
      const emitList = links.filter(
        item => !this.linkList.find(link => link && link.url === item.url)
      );
      console.log("emit", emitList);
璃白.'s avatar
璃白. 已提交
484
      this.$emit("renderLinks", {
璃白.'s avatar
璃白. 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
        links: emitList,
        callback: list => {
          // 用原始数组做循环
          links.forEach(item => {
            const linkItem = this.linkList.find(
              link => link && link.url === item.url
            );
            // 如果
            if (!linkItem) {
              const returnLink = list.find(
                link => link && link.url === item.url
              );
              if (!returnLink) return;
              item.title = returnLink.title;
              item.description = returnLink.description;
              item.icon = returnLink.icon;
              this.linkList.push(returnLink);
            } else {
              item.title = linkItem.title;
              item.description = linkItem.description;
              item.icon = linkItem.icon;
            }
璃白.'s avatar
璃白. 已提交
507 508 509 510 511 512
            // 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;
璃白.'s avatar
璃白. 已提交
513
            // linkEl.className = "md_link_card";
514
            item.linkType = linkEl.getAttribute("data-type");
璃白.'s avatar
璃白. 已提交
515 516
            linkEl.setAttribute("target", "_blank");
            linkEl.setAttribute("href", url);
璃白.'s avatar
璃白. 已提交
517 518 519
            if (item.url.includes(".ipynb")) {
              linkEl.className = "jupyterEl";
            }
璃白.'s avatar
璃白. 已提交
520 521 522
            const title = getLinkTitle(linkEl, item);
            linkEl.innerHTML = renderLinkCard(title, item);
          });
璃白.'s avatar
璃白. 已提交
523 524

          this.html = vDom.innerHTML;
璃白.'s avatar
璃白. 已提交
525
        }
璃白.'s avatar
璃白. 已提交
526
      });
璃白.'s avatar
璃白. 已提交
527
    }
璃白.'s avatar
璃白. 已提交
528 529 530 531 532
  }
};
</script>
<style lang="less" scoped>
.md_container {
璃白.'s avatar
璃白. 已提交
533
  background: var(--md-editor-frame-bg-color);
璃白.'s avatar
璃白. 已提交
534
  border: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
535 536 537
  border-radius: 4px;
  padding: 10px 16px;
  box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
538
  transition: border 0.3s;
539
  position: relative;
璃白.'s avatar
璃白. 已提交
540
  // overflow: hidden; // 注释为了显示@用户的弹窗
541 542 543 544 545 546 547
  &.fullScreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    margin: 0;
璃白.'s avatar
璃白. 已提交
548
    border: 1px solid transparent !important;
549 550
    z-index: var(--md-editor-fullScrren-zIndex);
  }
璃白.'s avatar
璃白. 已提交
551
  &.active {
璃白.'s avatar
璃白. 已提交
552
    border: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
553
  }
璃白.'s avatar
璃白. 已提交
554 555 556
  &.disabled {
    background: var(--md-editor-frame-bg-color-disabled);
  }
557 558 559 560 561 562 563
  .word_limit {
    position: absolute;
    right: 10px;
    bottom: 8px;
    font-size: 12px;
    color: var(--md-editor-text-color);
  }
璃白.'s avatar
璃白. 已提交
564 565
}
</style>