md-header.vue 13.1 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>
郭维嘉 已提交
20
    <div :class="['header_tools', { disabled }]">
璃白.'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"
郭维嘉 已提交
30
        @updateShowDoc="$emit('updateShowDoc', $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
  copyFormatRules,
郭维嘉 已提交
50 51
  checkBoswer,
  pick
52
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
53
import toolButton from "./components/tool-button";
璃白.'s avatar
璃白. 已提交
54 55
export default {
  components: { toolButton },
璃白.'s avatar
璃白. 已提交
56
  props: {
璃白.'s avatar
fix  
璃白. 已提交
57 58 59 60
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
璃白. 已提交
61 62 63 64 65 66 67 68 69 70 71 72
    fullScreen: {
      type: Boolean,
      default: false
    },
    isFocus: {
      type: Boolean,
      default: false
    },
    showPreview: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
73 74 75 76
    canPreview: {
      type: Boolean,
      default: true
    },
璃白.'s avatar
璃白. 已提交
77 78 79 80
    disabled: {
      type: Boolean,
      default: false
    },
81 82 83 84
    themeOptions: {
      type: Object,
      default: () => {}
    },
璃白.'s avatar
璃白. 已提交
85 86 87 88
    toolsOptions: {
      type: Object,
      default: () => {}
    },
璃白.'s avatar
璃白. 已提交
89 90 91 92
    registerTools: {
      type: Array,
      default: () => []
    },
93
    zIndex: {
璃白.'s avatar
璃白. 已提交
94 95
      type: [String, Number],
      default: ""
96
    },
璃白.'s avatar
fix  
璃白. 已提交
97 98 99 100
    tabSize: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
101
    text: {
璃白.'s avatar
璃白. 已提交
102
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
103 104 105 106 107 108
      default: ""
    },
    selectionInfo: {
      type: Object,
      default: () => {}
    }
璃白.'s avatar
璃白. 已提交
109
  },
璃白.'s avatar
璃白. 已提交
110
  computed: {
郭维嘉 已提交
111 112 113 114 115 116 117
    previewTools() {
      const list = this.toolButtonList;
      return [
        this.dirBtn,
        ...pick(list, "help", "fullScreen", "cancelFullScreen")
      ];
    },
璃白.'s avatar
璃白. 已提交
118 119 120 121
    toolsShow() {
      const toolsList = this.toolButtonList;
      const toolsOptions = this.toolsOptions;
      if (!toolsOptions) return toolsList;
郭维嘉 已提交
122
      if (this.showPreview) return this.previewTools;
璃白.'s avatar
璃白. 已提交
123 124 125
      return toolsList.filter(item => {
        return isNotFalse(toolsOptions[item.name]);
      });
璃白.'s avatar
璃白. 已提交
126 127 128 129
    },
    isMobile() {
      const isMobile = checkBoswer();
      return isMobile;
璃白.'s avatar
璃白. 已提交
130 131
    }
  },
璃白.'s avatar
璃白. 已提交
132 133 134 135 136 137 138 139 140 141 142
  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  
璃白. 已提交
143
    },
璃白.'s avatar
璃白. 已提交
144 145 146 147 148 149 150 151 152
    selectionInfo: {
      handler: function(newVal, oldVal) {
        if (
          newVal.selectionStart === oldVal.selectionStart &&
          newVal.selectionEnd === oldVal.selectionEnd
        )
          return;
        this.copyFormat();
      }
璃白.'s avatar
璃白. 已提交
153 154 155 156 157
    },
    formatType: {
      handler: function(val) {
        this.$emit("getFormatType", val);
      }
璃白.'s avatar
璃白. 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171
    },
    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
璃白. 已提交
172 173
    }
  },
璃白.'s avatar
璃白. 已提交
174 175
  data() {
    return {
璃白.'s avatar
fix  
璃白. 已提交
176
      cursorPoint: "",
璃白.'s avatar
璃白. 已提交
177 178 179 180 181
      cancelFullScreenBtn: {
        name: "cancelFullScreen",
        icon: "quxiaoquanping_o",
        tip: "退出全屏"
      },
璃白.'s avatar
璃白. 已提交
182 183
      formatType: "", // 格式刷类型
      lock: false,
璃白.'s avatar
璃白. 已提交
184
      scrollTop: 0,
璃白.'s avatar
fix  
璃白. 已提交
185
      ulNum: 1,
璃白.'s avatar
璃白. 已提交
186 187 188 189 190
      fullScreenBtn: {
        name: "fullScreen",
        icon: "fullScreen",
        tip: "全屏模式"
      },
郭维嘉 已提交
191 192 193 194 195
      dirBtn: {
        name: "dir",
        icon: "dir",
        tip: "目录"
      },
璃白.'s avatar
璃白. 已提交
196
      toolButtonList: [
璃白.'s avatar
璃白. 已提交
197 198 199
        {
          name: "call",
          icon: "aite",
郭维嘉 已提交
200
          tip: "@关注的人",
璃白.'s avatar
璃白. 已提交
201 202 203
          startStr: "@",
          endStr: ""
        },
璃白.'s avatar
fix  
璃白. 已提交
204 205 206 207 208 209 210
        {
          name: "headline",
          icon: "biaoti",
          tip: "添加标题",
          startStr: "\n",
          endStr: "\n"
        },
璃白.'s avatar
璃白. 已提交
211 212 213 214
        {
          name: "bold",
          icon: "bold",
          tip: "粗体",
璃白.'s avatar
璃白. 已提交
215
          doc: "**内容**",
璃白.'s avatar
璃白. 已提交
216 217 218 219 220 221 222
          startStr: "**",
          endStr: "**"
        },
        {
          name: "italic",
          icon: "italic",
          tip: "斜体",
璃白.'s avatar
璃白. 已提交
223
          doc: "_内容_",
璃白.'s avatar
璃白. 已提交
224 225 226 227 228
          startStr: "_",
          endStr: "_"
        },
        {
          name: "quote",
璃白.'s avatar
璃白. 已提交
229
          icon: "yinyong",
璃白.'s avatar
璃白. 已提交
230
          tip: "插入引用",
璃白.'s avatar
feat  
璃白. 已提交
231
          doc: "> 内容",
璃白.'s avatar
璃白. 已提交
232 233 234
          startStr: "\n> ",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
235 236 237 238 239
        {
          name: "format",
          icon: "geshishua",
          tip: "格式刷"
        },
璃白.'s avatar
璃白. 已提交
240 241 242
        {
          name: "code",
          icon: "code",
243
          tip: "插入代码块",
璃白.'s avatar
璃白. 已提交
244
          startStr: "\n```\n",
245
          endStr: "\n\n\n```"
璃白.'s avatar
璃白. 已提交
246 247 248 249 250
        },
        {
          name: "link",
          icon: "lianjie",
          tip: "添加链接",
璃白.'s avatar
璃白. 已提交
251
          doc: "[标题](链接)",
璃白.'s avatar
璃白. 已提交
252
          startStr: "[](",
璃白.'s avatar
璃白. 已提交
253
          endStr: ")"
璃白.'s avatar
璃白. 已提交
254 255 256 257 258
        },
        {
          name: "ul",
          icon: "unorderedList",
          tip: "添加无序列表",
璃白.'s avatar
璃白. 已提交
259
          doc: "- 空格",
璃白.'s avatar
璃白. 已提交
260 261 262 263 264 265 266
          startStr: "\n- ",
          endStr: ""
        },
        {
          name: "ol",
          icon: "youxuliebiao",
          tip: "添加有序列表",
璃白.'s avatar
feat  
璃白. 已提交
267
          doc: "1. 内容",
璃白.'s avatar
璃白. 已提交
268 269 270
          startStr: "",
          endStr: ""
        },
271
        {
璃白.'s avatar
璃白. 已提交
272
          name: "img",
璃白.'s avatar
璃白. 已提交
273
          icon: "img",
璃白.'s avatar
璃白. 已提交
274
          tip: "上传图片",
275 276 277
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
278 279
        {
          name: "file",
璃白.'s avatar
璃白. 已提交
280
          icon: "file",
璃白.'s avatar
璃白. 已提交
281 282 283 284
          tip: "上传附件",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
285 286
        {
          name: "video",
璃白.'s avatar
璃白. 已提交
287
          icon: "video",
璃白.'s avatar
璃白. 已提交
288 289 290 291
          tip: "上传视频",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
292 293 294 295 296 297 298
        // {
        //   name: "loading",
        //   icon: "loading",
        //   tip: "上传中",
        //   startStr: "",
        //   endStr: ""
        // },
璃白.'s avatar
璃白. 已提交
299 300 301 302
        {
          name: "task",
          icon: "renwu",
          tip: "添加任务列表",
璃白.'s avatar
feat  
璃白. 已提交
303
          doc: "- [空格] 内容",
璃白.'s avatar
璃白. 已提交
304 305 306 307 308 309 310 311
          startStr: "\n- [ ] ",
          endStr: ""
        },
        {
          name: "table",
          icon: "biaoge",
          tip: "添加表格",
          startStr:
璃白.'s avatar
璃白. 已提交
312
            "\n\n| 表头 | 表头 |\n| ------ | ------ |\n| 单元格 | 单元格 |\n| 单元格 | 单元格 |\n\n",
璃白.'s avatar
璃白. 已提交
313 314
          endStr: ""
        },
郭维嘉 已提交
315 316 317 318 319
        // {
        //   name: "dir",
        //   icon: "dir",
        //   tip: "目录"
        // },
璃白.'s avatar
璃白. 已提交
320 321 322
        {
          name: "help",
          icon: "help",
璃白.'s avatar
feat  
璃白. 已提交
323
          tip: "Markdown语法"
璃白.'s avatar
璃白. 已提交
324
        },
璃白.'s avatar
璃白. 已提交
325 326 327 328 329 330 331 332
        {
          name: "fullScreen",
          icon: "fullScreen",
          tip: "全屏模式"
        }
      ]
    };
  },
璃白.'s avatar
璃白. 已提交
333
  created() {},
璃白.'s avatar
璃白. 已提交
334
  methods: {
璃白.'s avatar
璃白. 已提交
335 336
    loading(type, percent) {
      const list = this.toolButtonList;
璃白.'s avatar
璃白. 已提交
337 338 339 340
      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
璃白. 已提交
341
    },
璃白.'s avatar
fix  
璃白. 已提交
342 343 344
    resetUlNum() {
      this.ulNum = 1;
    },
璃白.'s avatar
璃白. 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
    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
璃白. 已提交
367
      this.handleUpdateText({ ...formatType, copy: true });
璃白.'s avatar
璃白. 已提交
368 369 370
      if (this.lock) return;
      this.formatType = "";
    },
璃白.'s avatar
fix  
璃白. 已提交
371
    tab() {
璃白.'s avatar
fix  
璃白. 已提交
372 373
      const textEl = document.getElementById(this.id);
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
374 375 376 377 378
      let startStr = "";
      const tabSize = parseInt(this.tabSize);
      Array.from({ length: tabSize }).forEach(() => {
        startStr += " ";
      });
璃白.'s avatar
fix  
璃白. 已提交
379 380
      const endStr = "";
      const originalText = this.text;
璃白.'s avatar
fix  
璃白. 已提交
381 382 383 384 385
      const cursorPoint = getPosition(this.id);
      const selectionInfo = {
        selectionStart: cursorPoint,
        selectionEnd: cursorPoint
      };
璃白.'s avatar
fix  
璃白. 已提交
386
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
璃白.'s avatar
fix  
璃白. 已提交
387
      const len = newText.length - originalText.length;
璃白.'s avatar
fix  
璃白. 已提交
388
      this.updateText(newText, len, scrollTop);
璃白.'s avatar
fix  
璃白. 已提交
389
    },
璃白.'s avatar
璃白. 已提交
390 391
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
璃白.'s avatar
璃白. 已提交
392 393 394 395 396 397 398 399 400 401 402 403
      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
璃白. 已提交
404
    },
璃白.'s avatar
璃白. 已提交
405

璃白.'s avatar
璃白. 已提交
406
    handleUpdateText({ startStr, endStr, type, copy }) {
璃白.'s avatar
璃白. 已提交
407 408
      const originalText = this.text;
      const selectionInfo = this.selectionInfo;
璃白.'s avatar
璃白. 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422
      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
璃白. 已提交
423 424 425 426 427
      const len =
        selectionInfo.selectionEnd -
        selectionInfo.selectionStart +
        startStr.length;
      this.updateText(newText, len);
璃白.'s avatar
璃白. 已提交
428 429
      if (startStr === "@") {
        setTimeout(() => {
郭维嘉 已提交
430
          this.$parent.$refs["md_" + this.id].createSelectUserDialog("android");
431
        }, 100);
璃白.'s avatar
璃白. 已提交
432
      }
璃白.'s avatar
fix  
璃白. 已提交
433
    },
璃白.'s avatar
fix  
璃白. 已提交
434
    updateText(val, len = 0) {
璃白.'s avatar
fix  
璃白. 已提交
435
      const textEl = document.getElementById(this.id);
436
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
437
      textEl.blur();
璃白.'s avatar
fix  
璃白. 已提交
438
      const cursorPoint = getPosition(this.id);
439
      this.$emit("update:text", removeBlankLine(val));
璃白.'s avatar
璃白. 已提交
440 441
      this.$emit("update:selectionInfo", {
        selectorId: "",
璃白.'s avatar
fix  
璃白. 已提交
442 443
        selectionStart: "",
        selectionEnd: ""
璃白.'s avatar
璃白. 已提交
444
      });
璃白.'s avatar
璃白. 已提交
445
      this.$nextTick(() => {
璃白.'s avatar
fix  
璃白. 已提交
446 447
        textEl.focus();
        textEl.setSelectionRange(cursorPoint + len, cursorPoint + len);
璃白.'s avatar
fix  
璃白. 已提交
448
        textEl.scrollTop = scrollTop;
璃白.'s avatar
璃白. 已提交
449
      });
璃白.'s avatar
璃白. 已提交
450
    }
璃白.'s avatar
璃白. 已提交
451 452 453 454 455 456 457 458 459
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
460
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
461
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
462
  // overflow-y: hidden;
璃白.'s avatar
璃白. 已提交
463
  &.active {
璃白.'s avatar
璃白. 已提交
464
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
465
  }
璃白.'s avatar
璃白. 已提交
466 467 468 469
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
璃白.'s avatar
璃白. 已提交
470
    // padding-bottom: 10px;
璃白.'s avatar
璃白. 已提交
471
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
472 473 474 475
    &.disabled {
      pointer-events: none;
      opacity: var(--md-editor-disabled-opacity);
    }
璃白.'s avatar
璃白. 已提交
476
    .tab_item {
璃白.'s avatar
璃白. 已提交
477
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
478 479
      cursor: pointer;
      position: relative;
璃白.'s avatar
璃白. 已提交
480 481
      // height: 28px;
      padding: 0 6px 8px;
璃白.'s avatar
璃白. 已提交
482 483 484 485 486
      box-sizing: border-box;
      &::after {
        display: block;
        content: "";
        position: absolute;
璃白.'s avatar
璃白. 已提交
487
        bottom: -2px;
璃白.'s avatar
璃白. 已提交
488
        width: 0;
璃白.'s avatar
璃白. 已提交
489
        height: 2px;
璃白.'s avatar
璃白. 已提交
490 491 492 493
        left: 50%;
        transform: translateX(-50%);
        background: transparent;
        transition: all 0.3s;
璃白.'s avatar
璃白. 已提交
494 495 496
        // @media screen and (max-width: 768px) {
        //   bottom: -2px;
        // }
璃白.'s avatar
璃白. 已提交
497 498 499
      }

      &:hover {
璃白.'s avatar
璃白. 已提交
500
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
501 502
        &::after {
          width: 100%;
璃白.'s avatar
fix  
璃白. 已提交
503
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
504 505 506
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
507
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
508 509 510
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
511
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
512 513 514 515 516
        }
      }
      & + .tab_item {
        margin-left: 10px;
      }
璃白.'s avatar
璃白. 已提交
517 518 519 520
      .iconfont {
        font-size: 14px;
        display: inline-block;
      }
璃白.'s avatar
璃白. 已提交
521 522 523 524 525 526
    }
  }
  .header_tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
璃白.'s avatar
璃白. 已提交
527
    // padding-bottom: 10px;
璃白.'s avatar
璃白. 已提交
528
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
529 530 531 532
    &.disabled {
      pointer-events: none;
      opacity: var(--md-editor-disabled-opacity);
    }
璃白.'s avatar
璃白. 已提交
533 534 535
  }
}
</style>