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

璃白.'s avatar
璃白. 已提交
335
    handleUpdateText({ startStr, endStr, type, copy }) {
璃白.'s avatar
璃白. 已提交
336 337
      const originalText = this.text;
      const selectionInfo = this.selectionInfo;
璃白.'s avatar
璃白. 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351
      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
璃白. 已提交
352 353 354 355 356
      const len =
        selectionInfo.selectionEnd -
        selectionInfo.selectionStart +
        startStr.length;
      this.updateText(newText, len);
璃白.'s avatar
fix  
璃白. 已提交
357
    },
璃白.'s avatar
fix  
璃白. 已提交
358
    updateText(val, len = 0) {
璃白.'s avatar
fix  
璃白. 已提交
359
      const textEl = document.getElementById(this.id);
360
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
361
      textEl.blur();
璃白.'s avatar
fix  
璃白. 已提交
362
      const cursorPoint = getPosition(this.id);
363
      this.$emit("update:text", removeBlankLine(val));
璃白.'s avatar
璃白. 已提交
364 365
      this.$emit("update:selectionInfo", {
        selectorId: "",
璃白.'s avatar
fix  
璃白. 已提交
366 367
        selectionStart: "",
        selectionEnd: ""
璃白.'s avatar
璃白. 已提交
368
      });
璃白.'s avatar
璃白. 已提交
369
      this.$nextTick(() => {
璃白.'s avatar
fix  
璃白. 已提交
370 371
        textEl.focus();
        textEl.setSelectionRange(cursorPoint + len, cursorPoint + len);
璃白.'s avatar
fix  
璃白. 已提交
372
        textEl.scrollTop = scrollTop;
璃白.'s avatar
璃白. 已提交
373
      });
璃白.'s avatar
璃白. 已提交
374
    }
璃白.'s avatar
璃白. 已提交
375 376 377 378 379 380 381 382 383
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
384
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
385
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
386
  // overflow-y: hidden;
璃白.'s avatar
璃白. 已提交
387
  &.active {
璃白.'s avatar
璃白. 已提交
388
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
389
  }
璃白.'s avatar
璃白. 已提交
390 391 392 393
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
璃白.'s avatar
璃白. 已提交
394
    // padding-bottom: 10px;
璃白.'s avatar
璃白. 已提交
395 396
    box-sizing: border-box;
    .tab_item {
璃白.'s avatar
璃白. 已提交
397
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
398 399
      cursor: pointer;
      position: relative;
璃白.'s avatar
璃白. 已提交
400 401
      // height: 28px;
      padding: 0 6px 8px;
璃白.'s avatar
璃白. 已提交
402 403 404 405 406
      box-sizing: border-box;
      &::after {
        display: block;
        content: "";
        position: absolute;
璃白.'s avatar
璃白. 已提交
407
        bottom: -2px;
璃白.'s avatar
璃白. 已提交
408
        width: 0;
璃白.'s avatar
璃白. 已提交
409
        height: 2px;
璃白.'s avatar
璃白. 已提交
410 411 412 413
        left: 50%;
        transform: translateX(-50%);
        background: transparent;
        transition: all 0.3s;
璃白.'s avatar
璃白. 已提交
414 415 416
        // @media screen and (max-width: 768px) {
        //   bottom: -2px;
        // }
璃白.'s avatar
璃白. 已提交
417 418 419
      }

      &:hover {
璃白.'s avatar
璃白. 已提交
420
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
421 422
        &::after {
          width: 100%;
璃白.'s avatar
fix  
璃白. 已提交
423
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
424 425 426
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
427
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
428 429 430
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
431
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
432 433 434 435 436
        }
      }
      & + .tab_item {
        margin-left: 10px;
      }
璃白.'s avatar
璃白. 已提交
437 438 439 440
      .iconfont {
        font-size: 14px;
        display: inline-block;
      }
璃白.'s avatar
璃白. 已提交
441 442 443 444 445 446
    }
  }
  .header_tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
璃白.'s avatar
璃白. 已提交
447
    // padding-bottom: 10px;
璃白.'s avatar
璃白. 已提交
448 449 450 451
    box-sizing: border-box;
  }
}
</style>