md-header.vue 9.1 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 8 9 10
      >
        编辑
      </div>
      <div
璃白.'s avatar
璃白. 已提交
11
        v-if="canPreview"
璃白.'s avatar
璃白. 已提交
12
        :class="['tab_item', { active: showPreview }]"
璃白.'s avatar
璃白. 已提交
13
        @click="setShowPreview(true)"
璃白.'s avatar
璃白. 已提交
14 15 16 17 18 19
      >
        预览
      </div>
    </div>
    <div class="header_tools" v-if="!showPreview">
      <tool-button
璃白.'s avatar
fix  
璃白. 已提交
20 21
        :ref="item.name"
        :ulNum.sync="ulNum"
璃白.'s avatar
璃白. 已提交
22
        :info="item"
璃白.'s avatar
璃白. 已提交
23
        :fullScreen="fullScreen"
璃白.'s avatar
璃白. 已提交
24
        @setFullScreen="$emit('update:fullScreen', $event)"
璃白.'s avatar
fix  
璃白. 已提交
25
        @updateText="handleUpdateText"
26
        @upload="$emit('upload')"
璃白.'s avatar
璃白. 已提交
27 28
        @setFormatType="setFormatType"
        :class="{ active: item.name === 'format' && formatType }"
璃白.'s avatar
璃白. 已提交
29
        v-for="(item, index) in toolsShow"
璃白.'s avatar
璃白. 已提交
30
        :key="index"
璃白.'s avatar
璃白. 已提交
31
        :text="text"
璃白.'s avatar
璃白. 已提交
32
        :formatType.sync="formatType"
33 34
        :zIndex="zIndex"
        :themeOptions="themeOptions"
璃白.'s avatar
璃白. 已提交
35
        :selectionInfo="selectionInfo"
璃白.'s avatar
璃白. 已提交
36 37 38 39 40
      />
    </div>
  </div>
</template>
<script>
41 42 43 44
import {
  isNotFalse,
  formatText,
  getPosition,
璃白.'s avatar
璃白. 已提交
45 46
  removeBlankLine,
  copyFormatRules
47
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
48
import toolButton from "./components/tool-button";
璃白.'s avatar
璃白. 已提交
49 50
export default {
  components: { toolButton },
璃白.'s avatar
璃白. 已提交
51
  props: {
璃白.'s avatar
fix  
璃白. 已提交
52 53 54 55
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
璃白. 已提交
56 57 58 59 60 61 62 63 64 65 66 67
    fullScreen: {
      type: Boolean,
      default: false
    },
    isFocus: {
      type: Boolean,
      default: false
    },
    showPreview: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
68 69 70 71
    canPreview: {
      type: Boolean,
      default: true
    },
72 73 74 75
    themeOptions: {
      type: Object,
      default: () => {}
    },
璃白.'s avatar
璃白. 已提交
76 77 78 79
    toolsOptions: {
      type: Object,
      default: () => {}
    },
80
    zIndex: {
璃白.'s avatar
璃白. 已提交
81 82
      type: [String, Number],
      default: ""
83
    },
璃白.'s avatar
fix  
璃白. 已提交
84 85 86 87
    tabSize: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
88
    text: {
璃白.'s avatar
璃白. 已提交
89
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
90 91 92 93 94 95
      default: ""
    },
    selectionInfo: {
      type: Object,
      default: () => {}
    }
璃白.'s avatar
璃白. 已提交
96
  },
璃白.'s avatar
璃白. 已提交
97 98 99 100 101 102 103 104 105 106
  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
璃白. 已提交
107 108 109
  watch: {
    fullScreen: {
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
110
        // console.log(val);
璃白.'s avatar
璃白. 已提交
111 112 113 114 115 116 117 118
        if (val) {
          this.toolButtonList.pop();
          this.toolButtonList.push(this.cancelFullScreenBtn);
        } else {
          this.toolButtonList.pop();
          this.toolButtonList.push(this.fullScreenBtn);
        }
      }
璃白.'s avatar
fix  
璃白. 已提交
119
    },
璃白.'s avatar
璃白. 已提交
120 121 122 123 124 125 126 127 128
    selectionInfo: {
      handler: function(newVal, oldVal) {
        if (
          newVal.selectionStart === oldVal.selectionStart &&
          newVal.selectionEnd === oldVal.selectionEnd
        )
          return;
        this.copyFormat();
      }
璃白.'s avatar
璃白. 已提交
129 130
    }
  },
璃白.'s avatar
璃白. 已提交
131 132
  data() {
    return {
璃白.'s avatar
fix  
璃白. 已提交
133
      cursorPoint: "",
璃白.'s avatar
璃白. 已提交
134 135 136 137 138
      cancelFullScreenBtn: {
        name: "cancelFullScreen",
        icon: "quxiaoquanping_o",
        tip: "退出全屏"
      },
璃白.'s avatar
璃白. 已提交
139 140
      formatType: "", // 格式刷类型
      lock: false,
璃白.'s avatar
fix  
璃白. 已提交
141
      ulNum: 1,
璃白.'s avatar
璃白. 已提交
142 143 144 145 146
      fullScreenBtn: {
        name: "fullScreen",
        icon: "fullScreen",
        tip: "全屏模式"
      },
璃白.'s avatar
璃白. 已提交
147
      toolButtonList: [
璃白.'s avatar
璃白. 已提交
148 149 150 151 152
        {
          name: "format",
          icon: "geshishua",
          tip: "格式刷"
        },
璃白.'s avatar
璃白. 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        {
          name: "bold",
          icon: "bold",
          tip: "粗体",
          startStr: "**",
          endStr: "**"
        },
        {
          name: "italic",
          icon: "italic",
          tip: "斜体",
          startStr: "_",
          endStr: "_"
        },
        {
          name: "quote",
璃白.'s avatar
璃白. 已提交
169
          icon: "yinyong",
璃白.'s avatar
璃白. 已提交
170 171 172 173 174 175 176
          tip: "插入引用",
          startStr: "\n> ",
          endStr: ""
        },
        {
          name: "code",
          icon: "code",
177
          tip: "插入代码块",
璃白.'s avatar
璃白. 已提交
178
          startStr: "\n```\n",
179
          endStr: "\n\n\n```"
璃白.'s avatar
璃白. 已提交
180 181 182 183 184
        },
        {
          name: "link",
          icon: "lianjie",
          tip: "添加链接",
璃白.'s avatar
璃白. 已提交
185 186
          startStr: "[链接](",
          endStr: ")"
璃白.'s avatar
璃白. 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        },
        {
          name: "ul",
          icon: "unorderedList",
          tip: "添加无序列表",
          startStr: "\n- ",
          endStr: ""
        },
        {
          name: "ol",
          icon: "youxuliebiao",
          tip: "添加有序列表",
          startStr: "",
          endStr: ""
        },
202 203 204 205 206 207 208
        {
          name: "file",
          icon: "tupian",
          tip: "上传文件",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
209 210 211 212 213 214 215 216 217 218 219 220
        {
          name: "task",
          icon: "renwu",
          tip: "添加任务列表",
          startStr: "\n- [ ] ",
          endStr: ""
        },
        {
          name: "table",
          icon: "biaoge",
          tip: "添加表格",
          startStr:
璃白.'s avatar
璃白. 已提交
221
            "\n\n| 表头 | 表头 |\n| ------ | ------ |\n| 单元格 | 单元格 |\n| 单元格 | 单元格 |\n\n",
璃白.'s avatar
璃白. 已提交
222 223 224 225 226 227 228 229 230 231
          endStr: ""
        },
        {
          name: "fullScreen",
          icon: "fullScreen",
          tip: "全屏模式"
        }
      ]
    };
  },
璃白.'s avatar
璃白. 已提交
232
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
233 234 235
    resetUlNum() {
      this.ulNum = 1;
    },
璃白.'s avatar
璃白. 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
    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;
      this.handleUpdateText(formatType);
      if (this.lock) return;
      this.formatType = "";
    },
璃白.'s avatar
fix  
璃白. 已提交
262
    tab() {
璃白.'s avatar
fix  
璃白. 已提交
263 264
      const textEl = document.getElementById(this.id);
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
265 266 267 268 269
      let startStr = "";
      const tabSize = parseInt(this.tabSize);
      Array.from({ length: tabSize }).forEach(() => {
        startStr += " ";
      });
璃白.'s avatar
fix  
璃白. 已提交
270 271
      const endStr = "";
      const originalText = this.text;
璃白.'s avatar
fix  
璃白. 已提交
272 273 274 275 276
      const cursorPoint = getPosition(this.id);
      const selectionInfo = {
        selectionStart: cursorPoint,
        selectionEnd: cursorPoint
      };
璃白.'s avatar
fix  
璃白. 已提交
277
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
璃白.'s avatar
fix  
璃白. 已提交
278
      const len = newText.length - originalText.length;
璃白.'s avatar
fix  
璃白. 已提交
279
      this.updateText(newText, len, scrollTop);
璃白.'s avatar
fix  
璃白. 已提交
280
    },
璃白.'s avatar
璃白. 已提交
281 282 283
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
    },
璃白.'s avatar
璃白. 已提交
284 285 286 287 288 289 290 291 292 293

    handleUpdateText({ startStr, endStr }) {
      const originalText = this.text;
      const selectionInfo = this.selectionInfo;
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
      const len =
        selectionInfo.selectionEnd -
        selectionInfo.selectionStart +
        startStr.length;
      this.updateText(newText, len);
璃白.'s avatar
fix  
璃白. 已提交
294
    },
璃白.'s avatar
fix  
璃白. 已提交
295
    updateText(val, len = 0) {
璃白.'s avatar
fix  
璃白. 已提交
296
      const textEl = document.getElementById(this.id);
297
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
298
      textEl.blur();
璃白.'s avatar
fix  
璃白. 已提交
299
      const cursorPoint = getPosition(this.id);
300
      this.$emit("update:text", removeBlankLine(val));
璃白.'s avatar
璃白. 已提交
301 302
      this.$emit("update:selectionInfo", {
        selectorId: "",
璃白.'s avatar
fix  
璃白. 已提交
303 304
        selectionStart: "",
        selectionEnd: ""
璃白.'s avatar
璃白. 已提交
305
      });
璃白.'s avatar
fix  
璃白. 已提交
306
      setTimeout(() => {
璃白.'s avatar
fix  
璃白. 已提交
307 308
        textEl.focus();
        textEl.setSelectionRange(cursorPoint + len, cursorPoint + len);
璃白.'s avatar
fix  
璃白. 已提交
309
        textEl.scrollTop = scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
310
      }, 0);
璃白.'s avatar
璃白. 已提交
311
    }
璃白.'s avatar
璃白. 已提交
312 313 314 315 316 317 318 319 320
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
321
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
322
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
323
  &.active {
璃白.'s avatar
璃白. 已提交
324
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
325
  }
璃白.'s avatar
璃白. 已提交
326 327 328 329 330 331 332
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    padding-bottom: 10px;
    box-sizing: border-box;
    .tab_item {
璃白.'s avatar
璃白. 已提交
333
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
      cursor: pointer;
      position: relative;
      padding: 0 6px;
      box-sizing: border-box;
      &::after {
        display: block;
        content: "";
        position: absolute;
        bottom: -12px;
        width: 0;
        height: 3px;
        left: 50%;
        transform: translateX(-50%);
        background: transparent;
        transition: all 0.3s;
      }

      &:hover {
璃白.'s avatar
璃白. 已提交
352
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
353 354
        &::after {
          width: 100%;
璃白.'s avatar
fix  
璃白. 已提交
355
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
356 357 358
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
359
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
360 361 362
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
363
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
        }
      }
      & + .tab_item {
        margin-left: 10px;
      }
    }
  }
  .header_tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 10px;
    box-sizing: border-box;
  }
}
</style>