md-header.vue 12.4 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2
  <div :class="['md_header', { active: isFocus }]">
璃白.'s avatar
璃白. 已提交
3
    <div :class="['header_tabs', { disabled }]">
璃白.'s avatar
璃白. 已提交
4
      <div
璃白.'s avatar
璃白. 已提交
5
        :class="['tab_item', { active: canPreview && !showPreview }]"
璃白.'s avatar
璃白. 已提交
6
        @click="setShowPreview(false)"
璃白.'s avatar
璃白. 已提交
7
      >
璃白.'s avatar
feat  
璃白. 已提交
8 9
        <!-- <span :class="['icon iconfont', `icon-bianji`]"></span> -->
        <span>编辑</span>
璃白.'s avatar
璃白. 已提交
10 11
      </div>
      <div
璃白.'s avatar
璃白. 已提交
12
        v-if="canPreview"
璃白.'s avatar
璃白. 已提交
13
        :class="['tab_item', { active: showPreview }]"
璃白.'s avatar
璃白. 已提交
14
        @click="setShowPreview(true)"
璃白.'s avatar
璃白. 已提交
15
      >
璃白.'s avatar
feat  
璃白. 已提交
16 17
        <!-- <span :class="['icon iconfont', `icon-yulan`]"></span> -->
        <span>预览</span>
璃白.'s avatar
璃白. 已提交
18 19
      </div>
    </div>
璃白.'s avatar
璃白. 已提交
20
    <div :class="['header_tools', { disabled }]" v-if="!showPreview">
璃白.'s avatar
璃白. 已提交
21
      <tool-button
璃白.'s avatar
fix  
璃白. 已提交
22 23
        :ref="item.name"
        :ulNum.sync="ulNum"
璃白.'s avatar
璃白. 已提交
24
        :info="item"
璃白.'s avatar
璃白. 已提交
25
        :fullScreen="fullScreen"
璃白.'s avatar
璃白. 已提交
26
        @setFullScreen="$emit('update:fullScreen', $event)"
璃白.'s avatar
fix  
璃白. 已提交
27
        @updateText="handleUpdateText"
璃白.'s avatar
璃白. 已提交
28
        @upload="$emit('upload', $event)"
璃白.'s avatar
璃白. 已提交
29
        @setFormatType="setFormatType"
璃白.'s avatar
璃白. 已提交
30
        @updateShowHelp="$emit('updateShowHelp', $event)"
璃白.'s avatar
璃白. 已提交
31
        :class="{ active: item.name === 'format' && formatType }"
璃白.'s avatar
璃白. 已提交
32
        v-for="(item, index) in toolsShow"
璃白.'s avatar
璃白. 已提交
33
        :key="index"
璃白.'s avatar
璃白. 已提交
34
        :text="text"
璃白.'s avatar
璃白. 已提交
35
        :formatType.sync="formatType"
36 37
        :zIndex="zIndex"
        :themeOptions="themeOptions"
璃白.'s avatar
璃白. 已提交
38
        :selectionInfo="selectionInfo"
璃白.'s avatar
璃白. 已提交
39 40 41 42 43
      />
    </div>
  </div>
</template>
<script>
44 45 46 47
import {
  isNotFalse,
  formatText,
  getPosition,
璃白.'s avatar
璃白. 已提交
48
  removeBlankLine,
璃白.'s avatar
璃白. 已提交
49 50
  copyFormatRules,
  checkBoswer
51
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
52
import toolButton from "./components/tool-button";
璃白.'s avatar
璃白. 已提交
53 54
export default {
  components: { toolButton },
璃白.'s avatar
璃白. 已提交
55
  props: {
璃白.'s avatar
fix  
璃白. 已提交
56 57 58 59
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
璃白. 已提交
60 61 62 63 64 65 66 67 68 69 70 71
    fullScreen: {
      type: Boolean,
      default: false
    },
    isFocus: {
      type: Boolean,
      default: false
    },
    showPreview: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
72 73 74 75
    canPreview: {
      type: Boolean,
      default: true
    },
璃白.'s avatar
璃白. 已提交
76 77 78 79
    disabled: {
      type: Boolean,
      default: false
    },
80 81 82 83
    themeOptions: {
      type: Object,
      default: () => {}
    },
璃白.'s avatar
璃白. 已提交
84 85 86 87
    toolsOptions: {
      type: Object,
      default: () => {}
    },
璃白.'s avatar
璃白. 已提交
88 89 90 91
    registerTools: {
      type: Array,
      default: () => []
    },
92
    zIndex: {
璃白.'s avatar
璃白. 已提交
93 94
      type: [String, Number],
      default: ""
95
    },
璃白.'s avatar
fix  
璃白. 已提交
96 97 98 99
    tabSize: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
100
    text: {
璃白.'s avatar
璃白. 已提交
101
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
102 103 104 105 106 107
      default: ""
    },
    selectionInfo: {
      type: Object,
      default: () => {}
    }
璃白.'s avatar
璃白. 已提交
108
  },
璃白.'s avatar
璃白. 已提交
109 110 111 112 113 114 115 116
  computed: {
    toolsShow() {
      const toolsList = this.toolButtonList;
      const toolsOptions = this.toolsOptions;
      if (!toolsOptions) return toolsList;
      return toolsList.filter(item => {
        return isNotFalse(toolsOptions[item.name]);
      });
璃白.'s avatar
璃白. 已提交
117 118 119 120
    },
    isMobile() {
      const isMobile = checkBoswer();
      return isMobile;
璃白.'s avatar
璃白. 已提交
121 122
    }
  },
璃白.'s avatar
璃白. 已提交
123 124 125 126 127 128 129 130 131 132 133
  watch: {
    fullScreen: {
      handler: function(val) {
        if (val) {
          this.toolButtonList.pop();
          this.toolButtonList.push(this.cancelFullScreenBtn);
        } else {
          this.toolButtonList.pop();
          this.toolButtonList.push(this.fullScreenBtn);
        }
      }
璃白.'s avatar
fix  
璃白. 已提交
134
    },
璃白.'s avatar
璃白. 已提交
135 136 137 138 139 140 141 142 143
    selectionInfo: {
      handler: function(newVal, oldVal) {
        if (
          newVal.selectionStart === oldVal.selectionStart &&
          newVal.selectionEnd === oldVal.selectionEnd
        )
          return;
        this.copyFormat();
      }
璃白.'s avatar
璃白. 已提交
144 145 146 147 148
    },
    formatType: {
      handler: function(val) {
        this.$emit("getFormatType", val);
      }
璃白.'s avatar
璃白. 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162
    },
    registerTools: {
      handler: function(val) {
        const list = this.toolButtonList;
        const tableIndex = list.findIndex(item => item.name === "table");
        this.toolButtonList.splice(
          tableIndex,
          0,
          ...val.map(item => {
            item.register = true;
            return item;
          })
        );
      }
璃白.'s avatar
璃白. 已提交
163 164
    }
  },
璃白.'s avatar
璃白. 已提交
165 166
  data() {
    return {
璃白.'s avatar
fix  
璃白. 已提交
167
      cursorPoint: "",
璃白.'s avatar
璃白. 已提交
168 169 170 171 172
      cancelFullScreenBtn: {
        name: "cancelFullScreen",
        icon: "quxiaoquanping_o",
        tip: "退出全屏"
      },
璃白.'s avatar
璃白. 已提交
173 174
      formatType: "", // 格式刷类型
      lock: false,
璃白.'s avatar
璃白. 已提交
175
      scrollTop: 0,
璃白.'s avatar
fix  
璃白. 已提交
176
      ulNum: 1,
璃白.'s avatar
璃白. 已提交
177 178 179 180 181
      fullScreenBtn: {
        name: "fullScreen",
        icon: "fullScreen",
        tip: "全屏模式"
      },
璃白.'s avatar
璃白. 已提交
182
      toolButtonList: [
璃白.'s avatar
fix  
璃白. 已提交
183 184 185 186 187 188 189
        {
          name: "headline",
          icon: "biaoti",
          tip: "添加标题",
          startStr: "\n",
          endStr: "\n"
        },
璃白.'s avatar
璃白. 已提交
190 191 192 193
        {
          name: "bold",
          icon: "bold",
          tip: "粗体",
璃白.'s avatar
璃白. 已提交
194
          doc: "**内容**",
璃白.'s avatar
璃白. 已提交
195 196 197 198 199 200 201
          startStr: "**",
          endStr: "**"
        },
        {
          name: "italic",
          icon: "italic",
          tip: "斜体",
璃白.'s avatar
璃白. 已提交
202
          doc: "_内容_",
璃白.'s avatar
璃白. 已提交
203 204 205 206 207
          startStr: "_",
          endStr: "_"
        },
        {
          name: "quote",
璃白.'s avatar
璃白. 已提交
208
          icon: "yinyong",
璃白.'s avatar
璃白. 已提交
209
          tip: "插入引用",
璃白.'s avatar
feat  
璃白. 已提交
210
          doc: "> 内容",
璃白.'s avatar
璃白. 已提交
211 212 213
          startStr: "\n> ",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
214 215 216 217 218
        {
          name: "format",
          icon: "geshishua",
          tip: "格式刷"
        },
璃白.'s avatar
璃白. 已提交
219 220 221
        {
          name: "code",
          icon: "code",
222
          tip: "插入代码块",
璃白.'s avatar
璃白. 已提交
223
          startStr: "\n```\n",
224
          endStr: "\n\n\n```"
璃白.'s avatar
璃白. 已提交
225 226 227 228 229
        },
        {
          name: "link",
          icon: "lianjie",
          tip: "添加链接",
璃白.'s avatar
璃白. 已提交
230
          doc: "[标题](链接)",
璃白.'s avatar
璃白. 已提交
231
          startStr: "[](",
璃白.'s avatar
璃白. 已提交
232
          endStr: ")"
璃白.'s avatar
璃白. 已提交
233 234 235 236 237
        },
        {
          name: "ul",
          icon: "unorderedList",
          tip: "添加无序列表",
璃白.'s avatar
璃白. 已提交
238
          doc: "- 空格",
璃白.'s avatar
璃白. 已提交
239 240 241 242 243 244 245
          startStr: "\n- ",
          endStr: ""
        },
        {
          name: "ol",
          icon: "youxuliebiao",
          tip: "添加有序列表",
璃白.'s avatar
feat  
璃白. 已提交
246
          doc: "1. 内容",
璃白.'s avatar
璃白. 已提交
247 248 249
          startStr: "",
          endStr: ""
        },
250
        {
璃白.'s avatar
璃白. 已提交
251
          name: "img",
璃白.'s avatar
璃白. 已提交
252
          icon: "img",
璃白.'s avatar
璃白. 已提交
253
          tip: "上传图片",
254 255 256
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
257 258
        {
          name: "file",
璃白.'s avatar
璃白. 已提交
259
          icon: "file",
璃白.'s avatar
璃白. 已提交
260 261 262 263
          tip: "上传附件",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
264 265
        {
          name: "video",
璃白.'s avatar
璃白. 已提交
266
          icon: "video",
璃白.'s avatar
璃白. 已提交
267 268 269 270
          tip: "上传视频",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
271 272 273 274 275 276 277
        // {
        //   name: "loading",
        //   icon: "loading",
        //   tip: "上传中",
        //   startStr: "",
        //   endStr: ""
        // },
璃白.'s avatar
璃白. 已提交
278 279 280 281
        {
          name: "task",
          icon: "renwu",
          tip: "添加任务列表",
璃白.'s avatar
feat  
璃白. 已提交
282
          doc: "- [空格] 内容",
璃白.'s avatar
璃白. 已提交
283 284 285 286 287 288 289 290
          startStr: "\n- [ ] ",
          endStr: ""
        },
        {
          name: "table",
          icon: "biaoge",
          tip: "添加表格",
          startStr:
璃白.'s avatar
璃白. 已提交
291
            "\n\n| 表头 | 表头 |\n| ------ | ------ |\n| 单元格 | 单元格 |\n| 单元格 | 单元格 |\n\n",
璃白.'s avatar
璃白. 已提交
292 293
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
294 295 296
        {
          name: "help",
          icon: "help",
璃白.'s avatar
feat  
璃白. 已提交
297
          tip: "Markdown语法"
璃白.'s avatar
璃白. 已提交
298
        },
璃白.'s avatar
璃白. 已提交
299 300 301 302 303 304 305 306
        {
          name: "fullScreen",
          icon: "fullScreen",
          tip: "全屏模式"
        }
      ]
    };
  },
璃白.'s avatar
璃白. 已提交
307
  created() {},
璃白.'s avatar
璃白. 已提交
308
  methods: {
璃白.'s avatar
璃白. 已提交
309 310
    loading(type, percent) {
      const list = this.toolButtonList;
璃白.'s avatar
璃白. 已提交
311 312 313 314
      const item = list.find(item => item.name === type);
      this.$set(item, "percent", percent);
      item.icon =
        parseInt(percent) === 100 || parseInt(percent) === 0 ? type : "loading";
璃白.'s avatar
璃白. 已提交
315
    },
璃白.'s avatar
fix  
璃白. 已提交
316 317 318
    resetUlNum() {
      this.ulNum = 1;
    },
璃白.'s avatar
璃白. 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    setFormatType({ lock }) {
      if (this.formatType) {
        this.formatType = "";
        return;
      }
      const selectionInfo = this.selectionInfo;
      const selection = this.text.slice(
        selectionInfo.selectionStart,
        selectionInfo.selectionEnd
      );
      const formatType = copyFormatRules(selection);
      this.formatType = formatType;
      this.lock = lock;
    },
    copyFormat() {
      const selectionInfo = this.selectionInfo;
      const formatType = this.formatType;
      if (
        selectionInfo.selectionStart === selectionInfo.selectionEnd ||
        !formatType
      )
        return;
璃白.'s avatar
璃白. 已提交
341
      this.handleUpdateText({ ...formatType, copy: true });
璃白.'s avatar
璃白. 已提交
342 343 344
      if (this.lock) return;
      this.formatType = "";
    },
璃白.'s avatar
fix  
璃白. 已提交
345
    tab() {
璃白.'s avatar
fix  
璃白. 已提交
346 347
      const textEl = document.getElementById(this.id);
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
348 349 350 351 352
      let startStr = "";
      const tabSize = parseInt(this.tabSize);
      Array.from({ length: tabSize }).forEach(() => {
        startStr += " ";
      });
璃白.'s avatar
fix  
璃白. 已提交
353 354
      const endStr = "";
      const originalText = this.text;
璃白.'s avatar
fix  
璃白. 已提交
355 356 357 358 359
      const cursorPoint = getPosition(this.id);
      const selectionInfo = {
        selectionStart: cursorPoint,
        selectionEnd: cursorPoint
      };
璃白.'s avatar
fix  
璃白. 已提交
360
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
璃白.'s avatar
fix  
璃白. 已提交
361
      const len = newText.length - originalText.length;
璃白.'s avatar
fix  
璃白. 已提交
362
      this.updateText(newText, len, scrollTop);
璃白.'s avatar
fix  
璃白. 已提交
363
    },
璃白.'s avatar
璃白. 已提交
364 365
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
璃白.'s avatar
璃白. 已提交
366 367 368 369 370 371 372 373 374 375 376 377
      if (val) {
        const textEl = document.getElementById(this.id);
        this.scrollTop = textEl.scrollTop;
      } else {
        this.$nextTick(() => {
          const textEl = document.getElementById(this.id);
          if (!textEl) return;
          setTimeout(() => {
            textEl.scrollTop = this.scrollTop;
          }, 0);
        });
      }
璃白.'s avatar
璃白. 已提交
378
    },
璃白.'s avatar
璃白. 已提交
379

璃白.'s avatar
璃白. 已提交
380
    handleUpdateText({ startStr, endStr, type, copy }) {
璃白.'s avatar
璃白. 已提交
381 382
      const originalText = this.text;
      const selectionInfo = this.selectionInfo;
璃白.'s avatar
璃白. 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396
      let newText = formatText(originalText, selectionInfo, startStr, endStr);
      const s = selectionInfo.selectionStart;
      const e = selectionInfo.selectionEnd;
      if (copy) {
        const handleBlank = newText
          .slice(s, e)
          .replace(/(\n{2,})/g, `${endStr}$1${startStr}`);
        if (type !== "code") {
          newText =
            newText.slice(0, s) +
            handleBlank +
            newText.slice(e, newText.length);
        }
      }
璃白.'s avatar
璃白. 已提交
397 398 399 400 401
      const len =
        selectionInfo.selectionEnd -
        selectionInfo.selectionStart +
        startStr.length;
      this.updateText(newText, len);
璃白.'s avatar
fix  
璃白. 已提交
402
    },
璃白.'s avatar
fix  
璃白. 已提交
403
    updateText(val, len = 0) {
璃白.'s avatar
fix  
璃白. 已提交
404
      const textEl = document.getElementById(this.id);
405
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
406
      textEl.blur();
璃白.'s avatar
fix  
璃白. 已提交
407
      const cursorPoint = getPosition(this.id);
408
      this.$emit("update:text", removeBlankLine(val));
璃白.'s avatar
璃白. 已提交
409 410
      this.$emit("update:selectionInfo", {
        selectorId: "",
璃白.'s avatar
fix  
璃白. 已提交
411 412
        selectionStart: "",
        selectionEnd: ""
璃白.'s avatar
璃白. 已提交
413
      });
璃白.'s avatar
璃白. 已提交
414
      this.$nextTick(() => {
璃白.'s avatar
fix  
璃白. 已提交
415 416
        textEl.focus();
        textEl.setSelectionRange(cursorPoint + len, cursorPoint + len);
璃白.'s avatar
fix  
璃白. 已提交
417
        textEl.scrollTop = scrollTop;
璃白.'s avatar
璃白. 已提交
418
      });
璃白.'s avatar
璃白. 已提交
419
    }
璃白.'s avatar
璃白. 已提交
420 421 422 423 424 425 426 427 428
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
429
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
430
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
431
  // overflow-y: hidden;
璃白.'s avatar
璃白. 已提交
432
  &.active {
璃白.'s avatar
璃白. 已提交
433
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
434
  }
璃白.'s avatar
璃白. 已提交
435 436 437 438
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
璃白.'s avatar
璃白. 已提交
439
    // padding-bottom: 10px;
璃白.'s avatar
璃白. 已提交
440
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
441 442 443 444
    &.disabled {
      pointer-events: none;
      opacity: var(--md-editor-disabled-opacity);
    }
璃白.'s avatar
璃白. 已提交
445
    .tab_item {
璃白.'s avatar
璃白. 已提交
446
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
447 448
      cursor: pointer;
      position: relative;
璃白.'s avatar
璃白. 已提交
449 450
      // height: 28px;
      padding: 0 6px 8px;
璃白.'s avatar
璃白. 已提交
451 452 453 454 455
      box-sizing: border-box;
      &::after {
        display: block;
        content: "";
        position: absolute;
璃白.'s avatar
璃白. 已提交
456
        bottom: -2px;
璃白.'s avatar
璃白. 已提交
457
        width: 0;
璃白.'s avatar
璃白. 已提交
458
        height: 2px;
璃白.'s avatar
璃白. 已提交
459 460 461 462
        left: 50%;
        transform: translateX(-50%);
        background: transparent;
        transition: all 0.3s;
璃白.'s avatar
璃白. 已提交
463 464 465
        // @media screen and (max-width: 768px) {
        //   bottom: -2px;
        // }
璃白.'s avatar
璃白. 已提交
466 467 468
      }

      &:hover {
璃白.'s avatar
璃白. 已提交
469
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
470 471
        &::after {
          width: 100%;
璃白.'s avatar
fix  
璃白. 已提交
472
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
473 474 475
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
476
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
477 478 479
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
480
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
481 482 483 484 485
        }
      }
      & + .tab_item {
        margin-left: 10px;
      }
璃白.'s avatar
璃白. 已提交
486 487 488 489
      .iconfont {
        font-size: 14px;
        display: inline-block;
      }
璃白.'s avatar
璃白. 已提交
490 491 492 493 494 495
    }
  }
  .header_tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
璃白.'s avatar
璃白. 已提交
496
    // padding-bottom: 10px;
璃白.'s avatar
璃白. 已提交
497
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
498 499 500 501
    &.disabled {
      pointer-events: none;
      opacity: var(--md-editor-disabled-opacity);
    }
璃白.'s avatar
璃白. 已提交
502 503 504
  }
}
</style>