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

      // this.$refs.tool_button.tab();
    },
璃白.'s avatar
璃白. 已提交
207 208 209 210 211 212 213 214 215 216 217
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
    },
    updateText(val) {
      this.$emit("update:text", val);
      this.$emit("update:selectionInfo", {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      });
    }
璃白.'s avatar
璃白. 已提交
218 219 220 221 222 223 224 225 226
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
227
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
228
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
229
  &.active {
璃白.'s avatar
璃白. 已提交
230
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
231
  }
璃白.'s avatar
璃白. 已提交
232 233 234 235 236 237 238
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    padding-bottom: 10px;
    box-sizing: border-box;
    .tab_item {
璃白.'s avatar
璃白. 已提交
239
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
      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
璃白. 已提交
258
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
259 260
        &::after {
          width: 100%;
璃白.'s avatar
fix  
璃白. 已提交
261
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
262 263 264
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
265
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
266 267 268
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
269
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        }
      }
      & + .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>