md-header.vue 12.7 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
璃白. 已提交
183 184 185
        {
          name: "call",
          icon: "aite",
璃白.'s avatar
璃白. 已提交
186
          tip: "@好友",
璃白.'s avatar
璃白. 已提交
187 188 189
          startStr: "@",
          endStr: ""
        },
璃白.'s avatar
fix  
璃白. 已提交
190 191 192 193 194 195 196
        {
          name: "headline",
          icon: "biaoti",
          tip: "添加标题",
          startStr: "\n",
          endStr: "\n"
        },
璃白.'s avatar
璃白. 已提交
197 198 199 200
        {
          name: "bold",
          icon: "bold",
          tip: "粗体",
璃白.'s avatar
璃白. 已提交
201
          doc: "**内容**",
璃白.'s avatar
璃白. 已提交
202 203 204 205 206 207 208
          startStr: "**",
          endStr: "**"
        },
        {
          name: "italic",
          icon: "italic",
          tip: "斜体",
璃白.'s avatar
璃白. 已提交
209
          doc: "_内容_",
璃白.'s avatar
璃白. 已提交
210 211 212 213 214
          startStr: "_",
          endStr: "_"
        },
        {
          name: "quote",
璃白.'s avatar
璃白. 已提交
215
          icon: "yinyong",
璃白.'s avatar
璃白. 已提交
216
          tip: "插入引用",
璃白.'s avatar
feat  
璃白. 已提交
217
          doc: "> 内容",
璃白.'s avatar
璃白. 已提交
218 219 220
          startStr: "\n> ",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
221 222 223 224 225
        {
          name: "format",
          icon: "geshishua",
          tip: "格式刷"
        },
璃白.'s avatar
璃白. 已提交
226 227 228
        {
          name: "code",
          icon: "code",
229
          tip: "插入代码块",
璃白.'s avatar
璃白. 已提交
230
          startStr: "\n```\n",
231
          endStr: "\n\n\n```"
璃白.'s avatar
璃白. 已提交
232 233 234 235 236
        },
        {
          name: "link",
          icon: "lianjie",
          tip: "添加链接",
璃白.'s avatar
璃白. 已提交
237
          doc: "[标题](链接)",
璃白.'s avatar
璃白. 已提交
238
          startStr: "[](",
璃白.'s avatar
璃白. 已提交
239
          endStr: ")"
璃白.'s avatar
璃白. 已提交
240 241 242 243 244
        },
        {
          name: "ul",
          icon: "unorderedList",
          tip: "添加无序列表",
璃白.'s avatar
璃白. 已提交
245
          doc: "- 空格",
璃白.'s avatar
璃白. 已提交
246 247 248 249 250 251 252
          startStr: "\n- ",
          endStr: ""
        },
        {
          name: "ol",
          icon: "youxuliebiao",
          tip: "添加有序列表",
璃白.'s avatar
feat  
璃白. 已提交
253
          doc: "1. 内容",
璃白.'s avatar
璃白. 已提交
254 255 256
          startStr: "",
          endStr: ""
        },
257
        {
璃白.'s avatar
璃白. 已提交
258
          name: "img",
璃白.'s avatar
璃白. 已提交
259
          icon: "img",
璃白.'s avatar
璃白. 已提交
260
          tip: "上传图片",
261 262 263
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
264 265
        {
          name: "file",
璃白.'s avatar
璃白. 已提交
266
          icon: "file",
璃白.'s avatar
璃白. 已提交
267 268 269 270
          tip: "上传附件",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
271 272
        {
          name: "video",
璃白.'s avatar
璃白. 已提交
273
          icon: "video",
璃白.'s avatar
璃白. 已提交
274 275 276 277
          tip: "上传视频",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
278 279 280 281 282 283 284
        // {
        //   name: "loading",
        //   icon: "loading",
        //   tip: "上传中",
        //   startStr: "",
        //   endStr: ""
        // },
璃白.'s avatar
璃白. 已提交
285 286 287 288
        {
          name: "task",
          icon: "renwu",
          tip: "添加任务列表",
璃白.'s avatar
feat  
璃白. 已提交
289
          doc: "- [空格] 内容",
璃白.'s avatar
璃白. 已提交
290 291 292 293 294 295 296 297
          startStr: "\n- [ ] ",
          endStr: ""
        },
        {
          name: "table",
          icon: "biaoge",
          tip: "添加表格",
          startStr:
璃白.'s avatar
璃白. 已提交
298
            "\n\n| 表头 | 表头 |\n| ------ | ------ |\n| 单元格 | 单元格 |\n| 单元格 | 单元格 |\n\n",
璃白.'s avatar
璃白. 已提交
299 300
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
301 302 303
        {
          name: "help",
          icon: "help",
璃白.'s avatar
feat  
璃白. 已提交
304
          tip: "Markdown语法"
璃白.'s avatar
璃白. 已提交
305
        },
璃白.'s avatar
璃白. 已提交
306 307 308 309 310 311 312 313
        {
          name: "fullScreen",
          icon: "fullScreen",
          tip: "全屏模式"
        }
      ]
    };
  },
璃白.'s avatar
璃白. 已提交
314
  created() {},
璃白.'s avatar
璃白. 已提交
315
  methods: {
璃白.'s avatar
璃白. 已提交
316 317
    loading(type, percent) {
      const list = this.toolButtonList;
璃白.'s avatar
璃白. 已提交
318 319 320 321
      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
璃白. 已提交
322
    },
璃白.'s avatar
fix  
璃白. 已提交
323 324 325
    resetUlNum() {
      this.ulNum = 1;
    },
璃白.'s avatar
璃白. 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    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
璃白. 已提交
348
      this.handleUpdateText({ ...formatType, copy: true });
璃白.'s avatar
璃白. 已提交
349 350 351
      if (this.lock) return;
      this.formatType = "";
    },
璃白.'s avatar
fix  
璃白. 已提交
352
    tab() {
璃白.'s avatar
fix  
璃白. 已提交
353 354
      const textEl = document.getElementById(this.id);
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
355 356 357 358 359
      let startStr = "";
      const tabSize = parseInt(this.tabSize);
      Array.from({ length: tabSize }).forEach(() => {
        startStr += " ";
      });
璃白.'s avatar
fix  
璃白. 已提交
360 361
      const endStr = "";
      const originalText = this.text;
璃白.'s avatar
fix  
璃白. 已提交
362 363 364 365 366
      const cursorPoint = getPosition(this.id);
      const selectionInfo = {
        selectionStart: cursorPoint,
        selectionEnd: cursorPoint
      };
璃白.'s avatar
fix  
璃白. 已提交
367
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
璃白.'s avatar
fix  
璃白. 已提交
368
      const len = newText.length - originalText.length;
璃白.'s avatar
fix  
璃白. 已提交
369
      this.updateText(newText, len, scrollTop);
璃白.'s avatar
fix  
璃白. 已提交
370
    },
璃白.'s avatar
璃白. 已提交
371 372
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
璃白.'s avatar
璃白. 已提交
373 374 375 376 377 378 379 380 381 382 383 384
      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
璃白. 已提交
385
    },
璃白.'s avatar
璃白. 已提交
386

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

      &:hover {
璃白.'s avatar
璃白. 已提交
481
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
482 483
        &::after {
          width: 100%;
璃白.'s avatar
fix  
璃白. 已提交
484
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
485 486 487
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
488
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
489 490 491
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
492
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
493 494 495 496 497
        }
      }
      & + .tab_item {
        margin-left: 10px;
      }
璃白.'s avatar
璃白. 已提交
498 499 500 501
      .iconfont {
        font-size: 14px;
        display: inline-block;
      }
璃白.'s avatar
璃白. 已提交
502 503 504 505 506 507
    }
  }
  .header_tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
璃白.'s avatar
璃白. 已提交
508
    // padding-bottom: 10px;
璃白.'s avatar
璃白. 已提交
509
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
510 511 512 513
    &.disabled {
      pointer-events: none;
      opacity: var(--md-editor-disabled-opacity);
    }
璃白.'s avatar
璃白. 已提交
514 515 516
  }
}
</style>