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

璃白.'s avatar
璃白. 已提交
203
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
204 205 206 207 208 209
    tab() {
      const startStr = " ";
      const endStr = "";
      const originalText = this.text;
      const selectionInfo = this.selectionInfo;
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
璃白.'s avatar
fix  
璃白. 已提交
210 211 212
      const len = newText.length - originalText.length;
      // const len = 0;
      this.updateText(newText, len);
璃白.'s avatar
fix  
璃白. 已提交
213
    },
璃白.'s avatar
璃白. 已提交
214 215 216
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
    },
璃白.'s avatar
fix  
璃白. 已提交
217 218
    updateText(val, len = 0) {
      console.log(val);
璃白.'s avatar
璃白. 已提交
219
      this.$emit("update:text", val);
璃白.'s avatar
fix  
璃白. 已提交
220 221 222
      const cursorPoint = getPosition(this.id);
      console.log(cursorPoint);

璃白.'s avatar
璃白. 已提交
223 224
      this.$emit("update:selectionInfo", {
        selectorId: "",
璃白.'s avatar
fix  
璃白. 已提交
225 226
        selectionStart: cursorPoint,
        selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
227
      });
璃白.'s avatar
fix  
璃白. 已提交
228 229 230 231 232 233 234 235
      console.log(cursorPoint);

      setTimeout(() => {
        // document.getElementById(this.id).setSelectionRange(0, 0);
        document
          .getElementById(this.id)
          .setSelectionRange(cursorPoint + len, cursorPoint + len);
      }, 0);
璃白.'s avatar
璃白. 已提交
236
    }
璃白.'s avatar
璃白. 已提交
237 238 239 240 241 242 243 244 245
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
246
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
247
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
248
  &.active {
璃白.'s avatar
璃白. 已提交
249
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
250
  }
璃白.'s avatar
璃白. 已提交
251 252 253 254 255 256 257
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    padding-bottom: 10px;
    box-sizing: border-box;
    .tab_item {
璃白.'s avatar
璃白. 已提交
258
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
      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
璃白. 已提交
277
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
278 279
        &::after {
          width: 100%;
璃白.'s avatar
fix  
璃白. 已提交
280
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
281 282 283
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
284
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
285 286 287
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
288
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
        }
      }
      & + .tab_item {
        margin-left: 10px;
      }
    }
  }
  .header_tools {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 10px;
    box-sizing: border-box;
    /deep/.tool_button {
      margin: 0 8px;
    }
  }
}
</style>