md-header.vue 7.6 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
        v-for="(item, index) in toolsShow"
璃白.'s avatar
璃白. 已提交
28
        :key="index"
璃白.'s avatar
璃白. 已提交
29
        :text="text"
30 31
        :zIndex="zIndex"
        :themeOptions="themeOptions"
璃白.'s avatar
璃白. 已提交
32
        :selectionInfo="selectionInfo"
璃白.'s avatar
璃白. 已提交
33 34 35 36 37
      />
    </div>
  </div>
</template>
<script>
38 39 40 41 42 43
import {
  isNotFalse,
  formatText,
  getPosition,
  removeBlankLine
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
44
import toolButton from "./components/tool-button";
璃白.'s avatar
璃白. 已提交
45 46
export default {
  components: { toolButton },
璃白.'s avatar
璃白. 已提交
47
  props: {
璃白.'s avatar
fix  
璃白. 已提交
48 49 50 51
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
璃白. 已提交
52 53 54 55 56 57 58 59 60 61 62 63
    fullScreen: {
      type: Boolean,
      default: false
    },
    isFocus: {
      type: Boolean,
      default: false
    },
    showPreview: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
64 65 66 67
    canPreview: {
      type: Boolean,
      default: true
    },
68 69 70 71
    themeOptions: {
      type: Object,
      default: () => {}
    },
璃白.'s avatar
璃白. 已提交
72 73 74 75
    toolsOptions: {
      type: Object,
      default: () => {}
    },
76
    zIndex: {
璃白.'s avatar
璃白. 已提交
77 78
      type: [String, Number],
      default: ""
79
    },
璃白.'s avatar
fix  
璃白. 已提交
80 81 82 83
    tabSize: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
84
    text: {
璃白.'s avatar
璃白. 已提交
85
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
86 87 88 89 90 91
      default: ""
    },
    selectionInfo: {
      type: Object,
      default: () => {}
    }
璃白.'s avatar
璃白. 已提交
92
  },
璃白.'s avatar
璃白. 已提交
93 94 95 96 97 98 99 100 101 102
  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
璃白. 已提交
103 104 105
  watch: {
    fullScreen: {
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
106
        // console.log(val);
璃白.'s avatar
璃白. 已提交
107 108 109 110 111 112 113 114
        if (val) {
          this.toolButtonList.pop();
          this.toolButtonList.push(this.cancelFullScreenBtn);
        } else {
          this.toolButtonList.pop();
          this.toolButtonList.push(this.fullScreenBtn);
        }
      }
璃白.'s avatar
fix  
璃白. 已提交
115 116 117
    },
    text: {
      handler: function(newVal, oldVal) {}
璃白.'s avatar
璃白. 已提交
118 119
    }
  },
璃白.'s avatar
璃白. 已提交
120 121
  data() {
    return {
璃白.'s avatar
fix  
璃白. 已提交
122
      cursorPoint: "",
璃白.'s avatar
璃白. 已提交
123 124 125 126 127
      cancelFullScreenBtn: {
        name: "cancelFullScreen",
        icon: "quxiaoquanping_o",
        tip: "退出全屏"
      },
璃白.'s avatar
fix  
璃白. 已提交
128
      ulNum: 1,
璃白.'s avatar
璃白. 已提交
129 130 131 132 133
      fullScreenBtn: {
        name: "fullScreen",
        icon: "fullScreen",
        tip: "全屏模式"
      },
璃白.'s avatar
璃白. 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
      toolButtonList: [
        {
          name: "bold",
          icon: "bold",
          tip: "粗体",
          startStr: "**",
          endStr: "**"
        },
        {
          name: "italic",
          icon: "italic",
          tip: "斜体",
          startStr: "_",
          endStr: "_"
        },
        {
          name: "quote",
          icon: "baojiaquotation",
          tip: "插入引用",
          startStr: "\n> ",
          endStr: ""
        },
        {
          name: "code",
          icon: "code",
159
          tip: "插入代码块",
160 161
          startStr: "\n```",
          endStr: "\n\n\n```"
璃白.'s avatar
璃白. 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        },
        {
          name: "link",
          icon: "lianjie",
          tip: "添加链接",
          startStr: "[",
          endStr: "](url)"
        },
        {
          name: "ul",
          icon: "unorderedList",
          tip: "添加无序列表",
          startStr: "\n- ",
          endStr: ""
        },
        {
          name: "ol",
          icon: "youxuliebiao",
          tip: "添加有序列表",
          startStr: "",
          endStr: ""
        },
184 185 186 187 188 189 190
        {
          name: "file",
          icon: "tupian",
          tip: "上传文件",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
191 192 193 194 195 196 197 198 199 200 201 202
        {
          name: "task",
          icon: "renwu",
          tip: "添加任务列表",
          startStr: "\n- [ ] ",
          endStr: ""
        },
        {
          name: "table",
          icon: "biaoge",
          tip: "添加表格",
          startStr:
璃白.'s avatar
璃白. 已提交
203
            "\n| header | header |\n| ------ | ------ |\n| cell | cell |\n| cell | cell |\n\n",
璃白.'s avatar
璃白. 已提交
204 205 206 207 208 209 210 211 212 213 214
          endStr: ""
        },
        {
          name: "fullScreen",
          icon: "fullScreen",
          tip: "全屏模式"
        }
      ]
    };
  },

璃白.'s avatar
璃白. 已提交
215
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
216 217 218
    resetUlNum() {
      this.ulNum = 1;
    },
璃白.'s avatar
fix  
璃白. 已提交
219
    tab() {
璃白.'s avatar
fix  
璃白. 已提交
220 221
      const textEl = document.getElementById(this.id);
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
222 223 224 225 226
      let startStr = "";
      const tabSize = parseInt(this.tabSize);
      Array.from({ length: tabSize }).forEach(() => {
        startStr += " ";
      });
璃白.'s avatar
fix  
璃白. 已提交
227 228
      const endStr = "";
      const originalText = this.text;
璃白.'s avatar
fix  
璃白. 已提交
229 230 231 232 233
      const cursorPoint = getPosition(this.id);
      const selectionInfo = {
        selectionStart: cursorPoint,
        selectionEnd: cursorPoint
      };
璃白.'s avatar
fix  
璃白. 已提交
234
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
璃白.'s avatar
fix  
璃白. 已提交
235
      const len = newText.length - originalText.length;
璃白.'s avatar
fix  
璃白. 已提交
236
      this.updateText(newText, len, scrollTop);
璃白.'s avatar
fix  
璃白. 已提交
237
    },
璃白.'s avatar
璃白. 已提交
238 239 240
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
    },
璃白.'s avatar
fix  
璃白. 已提交
241
    handleUpdateText({ val, len }) {
璃白.'s avatar
璃白. 已提交
242 243
      // const cursorPoint = getPosition(this.id);
      this.updateText(val, len);
璃白.'s avatar
fix  
璃白. 已提交
244
    },
璃白.'s avatar
fix  
璃白. 已提交
245
    updateText(val, len = 0) {
璃白.'s avatar
fix  
璃白. 已提交
246
      const textEl = document.getElementById(this.id);
247
      const scrollTop = textEl.scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
248
      textEl.blur();
璃白.'s avatar
fix  
璃白. 已提交
249
      const cursorPoint = getPosition(this.id);
250
      this.$emit("update:text", removeBlankLine(val));
璃白.'s avatar
璃白. 已提交
251 252
      this.$emit("update:selectionInfo", {
        selectorId: "",
璃白.'s avatar
fix  
璃白. 已提交
253 254
        selectionStart: "",
        selectionEnd: ""
璃白.'s avatar
璃白. 已提交
255
      });
璃白.'s avatar
fix  
璃白. 已提交
256
      setTimeout(() => {
璃白.'s avatar
fix  
璃白. 已提交
257 258
        textEl.focus();
        textEl.setSelectionRange(cursorPoint + len, cursorPoint + len);
璃白.'s avatar
fix  
璃白. 已提交
259
        textEl.scrollTop = scrollTop;
璃白.'s avatar
fix  
璃白. 已提交
260
      }, 0);
璃白.'s avatar
璃白. 已提交
261
    }
璃白.'s avatar
璃白. 已提交
262 263 264 265 266 267 268 269 270
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
271
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
272
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
273
  &.active {
璃白.'s avatar
璃白. 已提交
274
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
275
  }
璃白.'s avatar
璃白. 已提交
276 277 278 279 280 281 282
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    padding-bottom: 10px;
    box-sizing: border-box;
    .tab_item {
璃白.'s avatar
璃白. 已提交
283
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
      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
璃白. 已提交
302
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
303 304
        &::after {
          width: 100%;
璃白.'s avatar
fix  
璃白. 已提交
305
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
306 307 308
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
309
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
310 311 312
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
313
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
        }
      }
      & + .tab_item {
        margin-left: 10px;
      }
    }
  }
  .header_tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 10px;
    box-sizing: border-box;
  }
}
</style>