md-header.vue 10.5 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2
  <div :class="['md_header', { active: isFocus }]">
璃白.'s avatar
璃白. 已提交
3 4
    <div class="header_tabs">
      <div
璃白.'s avatar
璃白. 已提交
5
        :class="['tab_item', { active: canPreview && !showPreview }]"
璃白.'s avatar
璃白. 已提交
6
        @click="setShowPreview(false)"
璃白.'s avatar
璃白. 已提交
7
      >
璃白.'s avatar
璃白. 已提交
8 9
        <span :class="['icon iconfont', `icon-bianji`]"></span>
        <span v-if="!isMobile">编辑</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
璃白. 已提交
16 17
        <span :class="['icon iconfont', `icon-yulan`]"></span>
        <span v-if="!isMobile">预览</span>
璃白.'s avatar
璃白. 已提交
18 19 20 21
      </div>
    </div>
    <div class="header_tools" v-if="!showPreview">
      <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"
28
        @upload="$emit('upload')"
璃白.'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
    },
76 77 78 79
    themeOptions: {
      type: Object,
      default: () => {}
    },
璃白.'s avatar
璃白. 已提交
80 81 82 83
    toolsOptions: {
      type: Object,
      default: () => {}
    },
84
    zIndex: {
璃白.'s avatar
璃白. 已提交
85 86
      type: [String, Number],
      default: ""
87
    },
璃白.'s avatar
fix  
璃白. 已提交
88 89 90 91
    tabSize: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
92
    text: {
璃白.'s avatar
璃白. 已提交
93
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
94 95 96 97 98 99
      default: ""
    },
    selectionInfo: {
      type: Object,
      default: () => {}
    }
璃白.'s avatar
璃白. 已提交
100
  },
璃白.'s avatar
璃白. 已提交
101 102 103 104 105 106 107 108
  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
璃白. 已提交
109 110 111 112
    },
    isMobile() {
      const isMobile = checkBoswer();
      return isMobile;
璃白.'s avatar
璃白. 已提交
113 114
    }
  },
璃白.'s avatar
璃白. 已提交
115 116 117
  watch: {
    fullScreen: {
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
118
        // console.log(val);
璃白.'s avatar
璃白. 已提交
119 120 121 122 123 124 125 126
        if (val) {
          this.toolButtonList.pop();
          this.toolButtonList.push(this.cancelFullScreenBtn);
        } else {
          this.toolButtonList.pop();
          this.toolButtonList.push(this.fullScreenBtn);
        }
      }
璃白.'s avatar
fix  
璃白. 已提交
127
    },
璃白.'s avatar
璃白. 已提交
128 129 130 131 132 133 134 135 136
    selectionInfo: {
      handler: function(newVal, oldVal) {
        if (
          newVal.selectionStart === oldVal.selectionStart &&
          newVal.selectionEnd === oldVal.selectionEnd
        )
          return;
        this.copyFormat();
      }
璃白.'s avatar
璃白. 已提交
137 138 139 140 141
    },
    formatType: {
      handler: function(val) {
        this.$emit("getFormatType", val);
      }
璃白.'s avatar
璃白. 已提交
142 143
    }
  },
璃白.'s avatar
璃白. 已提交
144 145
  data() {
    return {
璃白.'s avatar
fix  
璃白. 已提交
146
      cursorPoint: "",
璃白.'s avatar
璃白. 已提交
147 148 149 150 151
      cancelFullScreenBtn: {
        name: "cancelFullScreen",
        icon: "quxiaoquanping_o",
        tip: "退出全屏"
      },
璃白.'s avatar
璃白. 已提交
152 153
      formatType: "", // 格式刷类型
      lock: false,
璃白.'s avatar
fix  
璃白. 已提交
154
      ulNum: 1,
璃白.'s avatar
璃白. 已提交
155 156 157 158 159
      fullScreenBtn: {
        name: "fullScreen",
        icon: "fullScreen",
        tip: "全屏模式"
      },
璃白.'s avatar
璃白. 已提交
160 161 162 163 164
      toolButtonList: [
        {
          name: "bold",
          icon: "bold",
          tip: "粗体",
璃白.'s avatar
璃白. 已提交
165
          doc: "**内容**",
璃白.'s avatar
璃白. 已提交
166 167 168 169 170 171 172
          startStr: "**",
          endStr: "**"
        },
        {
          name: "italic",
          icon: "italic",
          tip: "斜体",
璃白.'s avatar
璃白. 已提交
173
          doc: "_内容_",
璃白.'s avatar
璃白. 已提交
174 175 176 177 178
          startStr: "_",
          endStr: "_"
        },
        {
          name: "quote",
璃白.'s avatar
璃白. 已提交
179
          icon: "yinyong",
璃白.'s avatar
璃白. 已提交
180
          tip: "插入引用",
璃白.'s avatar
璃白. 已提交
181
          doc: "> 空格",
璃白.'s avatar
璃白. 已提交
182 183 184
          startStr: "\n> ",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
185 186 187 188 189
        {
          name: "format",
          icon: "geshishua",
          tip: "格式刷"
        },
璃白.'s avatar
璃白. 已提交
190 191 192
        {
          name: "code",
          icon: "code",
193
          tip: "插入代码块",
璃白.'s avatar
璃白. 已提交
194
          startStr: "\n```\n",
195
          endStr: "\n\n\n```"
璃白.'s avatar
璃白. 已提交
196 197 198 199 200
        },
        {
          name: "link",
          icon: "lianjie",
          tip: "添加链接",
璃白.'s avatar
璃白. 已提交
201 202
          doc: "[标题](链接)",
          startStr: "[链接标题](",
璃白.'s avatar
璃白. 已提交
203
          endStr: ")"
璃白.'s avatar
璃白. 已提交
204 205 206 207 208
        },
        {
          name: "ul",
          icon: "unorderedList",
          tip: "添加无序列表",
璃白.'s avatar
璃白. 已提交
209
          doc: "- 空格",
璃白.'s avatar
璃白. 已提交
210 211 212 213 214 215 216
          startStr: "\n- ",
          endStr: ""
        },
        {
          name: "ol",
          icon: "youxuliebiao",
          tip: "添加有序列表",
璃白.'s avatar
璃白. 已提交
217
          doc: "1. 空格",
璃白.'s avatar
璃白. 已提交
218 219 220
          startStr: "",
          endStr: ""
        },
221 222 223
        {
          name: "file",
          icon: "tupian",
璃白.'s avatar
璃白. 已提交
224
          tip: "上传图片",
225 226 227
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
228 229 230 231
        {
          name: "task",
          icon: "renwu",
          tip: "添加任务列表",
璃白.'s avatar
璃白. 已提交
232
          doc: "- [空格]",
璃白.'s avatar
璃白. 已提交
233 234 235 236 237 238 239 240
          startStr: "\n- [ ] ",
          endStr: ""
        },
        {
          name: "table",
          icon: "biaoge",
          tip: "添加表格",
          startStr:
璃白.'s avatar
璃白. 已提交
241
            "\n\n| 表头 | 表头 |\n| ------ | ------ |\n| 单元格 | 单元格 |\n| 单元格 | 单元格 |\n\n",
璃白.'s avatar
璃白. 已提交
242 243
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
244 245 246 247 248
        {
          name: "help",
          icon: "help",
          tip: "帮助"
        },
璃白.'s avatar
璃白. 已提交
249 250 251 252 253 254 255 256
        {
          name: "fullScreen",
          icon: "fullScreen",
          tip: "全屏模式"
        }
      ]
    };
  },
璃白.'s avatar
璃白. 已提交
257
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
258 259 260
    resetUlNum() {
      this.ulNum = 1;
    },
璃白.'s avatar
璃白. 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    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
璃白. 已提交
283
      this.handleUpdateText({ ...formatType, copy: true });
璃白.'s avatar
璃白. 已提交
284 285 286
      if (this.lock) return;
      this.formatType = "";
    },
璃白.'s avatar
fix  
璃白. 已提交
287
    tab() {
璃白.'s avatar
fix  
璃白. 已提交
288 289
      const textEl = document.getElementById(this.id);
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
290 291 292 293 294
      let startStr = "";
      const tabSize = parseInt(this.tabSize);
      Array.from({ length: tabSize }).forEach(() => {
        startStr += " ";
      });
璃白.'s avatar
fix  
璃白. 已提交
295 296
      const endStr = "";
      const originalText = this.text;
璃白.'s avatar
fix  
璃白. 已提交
297 298 299 300 301
      const cursorPoint = getPosition(this.id);
      const selectionInfo = {
        selectionStart: cursorPoint,
        selectionEnd: cursorPoint
      };
璃白.'s avatar
fix  
璃白. 已提交
302
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
璃白.'s avatar
fix  
璃白. 已提交
303
      const len = newText.length - originalText.length;
璃白.'s avatar
fix  
璃白. 已提交
304
      this.updateText(newText, len, scrollTop);
璃白.'s avatar
fix  
璃白. 已提交
305
    },
璃白.'s avatar
璃白. 已提交
306 307 308
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
    },
璃白.'s avatar
璃白. 已提交
309

璃白.'s avatar
璃白. 已提交
310
    handleUpdateText({ startStr, endStr, type, copy }) {
璃白.'s avatar
璃白. 已提交
311 312
      const originalText = this.text;
      const selectionInfo = this.selectionInfo;
璃白.'s avatar
璃白. 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326
      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
璃白. 已提交
327 328 329 330 331
      const len =
        selectionInfo.selectionEnd -
        selectionInfo.selectionStart +
        startStr.length;
      this.updateText(newText, len);
璃白.'s avatar
fix  
璃白. 已提交
332
    },
璃白.'s avatar
fix  
璃白. 已提交
333
    updateText(val, len = 0) {
璃白.'s avatar
fix  
璃白. 已提交
334
      const textEl = document.getElementById(this.id);
335
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
336
      textEl.blur();
璃白.'s avatar
fix  
璃白. 已提交
337
      const cursorPoint = getPosition(this.id);
338
      this.$emit("update:text", removeBlankLine(val));
璃白.'s avatar
璃白. 已提交
339 340
      this.$emit("update:selectionInfo", {
        selectorId: "",
璃白.'s avatar
fix  
璃白. 已提交
341 342
        selectionStart: "",
        selectionEnd: ""
璃白.'s avatar
璃白. 已提交
343
      });
璃白.'s avatar
璃白. 已提交
344 345
      this.$nextTick(() => {
        // setTimeout(() => {
璃白.'s avatar
fix  
璃白. 已提交
346 347
        textEl.focus();
        textEl.setSelectionRange(cursorPoint + len, cursorPoint + len);
璃白.'s avatar
fix  
璃白. 已提交
348
        textEl.scrollTop = scrollTop;
璃白.'s avatar
璃白. 已提交
349 350
      });
      // }, 0);
璃白.'s avatar
璃白. 已提交
351
    }
璃白.'s avatar
璃白. 已提交
352 353 354 355 356 357 358 359 360
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
361
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
362
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
363
  // overflow-y: hidden;
璃白.'s avatar
璃白. 已提交
364
  &.active {
璃白.'s avatar
璃白. 已提交
365
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
366
  }
璃白.'s avatar
璃白. 已提交
367 368 369 370
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
璃白.'s avatar
璃白. 已提交
371
    // padding-bottom: 10px;
璃白.'s avatar
璃白. 已提交
372 373
    box-sizing: border-box;
    .tab_item {
璃白.'s avatar
璃白. 已提交
374
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
375 376
      cursor: pointer;
      position: relative;
璃白.'s avatar
璃白. 已提交
377 378
      // height: 28px;
      padding: 0 6px 8px;
璃白.'s avatar
璃白. 已提交
379 380 381 382 383
      box-sizing: border-box;
      &::after {
        display: block;
        content: "";
        position: absolute;
璃白.'s avatar
璃白. 已提交
384
        bottom: -2px;
璃白.'s avatar
璃白. 已提交
385
        width: 0;
璃白.'s avatar
璃白. 已提交
386
        height: 2px;
璃白.'s avatar
璃白. 已提交
387 388 389 390
        left: 50%;
        transform: translateX(-50%);
        background: transparent;
        transition: all 0.3s;
璃白.'s avatar
璃白. 已提交
391 392 393
        // @media screen and (max-width: 768px) {
        //   bottom: -2px;
        // }
璃白.'s avatar
璃白. 已提交
394 395 396
      }

      &:hover {
璃白.'s avatar
璃白. 已提交
397
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
398 399
        &::after {
          width: 100%;
璃白.'s avatar
fix  
璃白. 已提交
400
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
401 402 403
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
404
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
405 406 407
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
408
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
409 410 411 412 413
        }
      }
      & + .tab_item {
        margin-left: 10px;
      }
璃白.'s avatar
璃白. 已提交
414 415 416 417
      .iconfont {
        font-size: 14px;
        display: inline-block;
      }
璃白.'s avatar
璃白. 已提交
418 419 420 421 422 423
    }
  }
  .header_tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
璃白.'s avatar
璃白. 已提交
424
    // padding-bottom: 10px;
璃白.'s avatar
璃白. 已提交
425 426 427 428
    box-sizing: border-box;
  }
}
</style>