md-header.vue 5.5 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 20
      >
        预览
      </div>
    </div>
    <div class="header_tools" v-if="!showPreview">
      <tool-button
        :info="item"
璃白.'s avatar
璃白. 已提交
21 22 23
        :fullScreen="fullScreen"
        @setFullScreen="$emit('update:fullScreen', true)"
        @updateText="updateText"
24
        @upload="$emit('upload')"
璃白.'s avatar
璃白. 已提交
25
        v-for="(item, index) in toolsShow"
璃白.'s avatar
璃白. 已提交
26
        :key="index"
璃白.'s avatar
璃白. 已提交
27
        :text="text"
28 29
        :zIndex="zIndex"
        :themeOptions="themeOptions"
璃白.'s avatar
璃白. 已提交
30
        :selectionInfo="selectionInfo"
璃白.'s avatar
璃白. 已提交
31 32 33 34 35
      />
    </div>
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
36
import { isNotFalse } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
37 38 39
import toolButton from "./tool-button";
export default {
  components: { toolButton },
璃白.'s avatar
璃白. 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52
  props: {
    fullScreen: {
      type: Boolean,
      default: false
    },
    isFocus: {
      type: Boolean,
      default: false
    },
    showPreview: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
53 54 55 56
    canPreview: {
      type: Boolean,
      default: true
    },
57 58 59 60
    themeOptions: {
      type: Object,
      default: () => {}
    },
璃白.'s avatar
璃白. 已提交
61 62 63 64
    toolsOptions: {
      type: Object,
      default: () => {}
    },
65 66 67 68
    zIndex: {
      type: [String,Number],
      default: ''
    },
璃白.'s avatar
璃白. 已提交
69
    text: {
璃白.'s avatar
璃白. 已提交
70
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
71 72 73 74 75 76
      default: ""
    },
    selectionInfo: {
      type: Object,
      default: () => {}
    }
璃白.'s avatar
璃白. 已提交
77
  },
璃白.'s avatar
璃白. 已提交
78 79 80 81 82 83 84 85 86 87
  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
璃白. 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  data() {
    return {
      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",
115 116 117
          tip: "插入代码块",
          startStr: "\n```\n",
          endStr: "\n```"
璃白.'s avatar
璃白. 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        },
        {
          name: "link",
          icon: "lianjie",
          tip: "添加链接",
          startStr: "[",
          endStr: "](url)"
        },
        {
          name: "ul",
          icon: "unorderedList",
          tip: "添加无序列表",
          startStr: "\n- ",
          endStr: ""
        },
        {
          name: "ol",
          icon: "youxuliebiao",
          tip: "添加有序列表",
          startStr: "",
          endStr: ""
        },
140 141 142 143 144 145 146
        {
          name: "file",
          icon: "tupian",
          tip: "上传文件",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
        {
          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
璃白. 已提交
171
  methods: {
璃白.'s avatar
璃白. 已提交
172 173 174 175 176 177 178 179 180 181 182
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
    },
    updateText(val) {
      this.$emit("update:text", val);
      this.$emit("update:selectionInfo", {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      });
    }
璃白.'s avatar
璃白. 已提交
183 184 185 186 187 188 189 190 191
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
192
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
193
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
194
  &.active {
璃白.'s avatar
璃白. 已提交
195
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
196
  }
璃白.'s avatar
璃白. 已提交
197 198 199 200 201 202 203
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    padding-bottom: 10px;
    box-sizing: border-box;
    .tab_item {
璃白.'s avatar
璃白. 已提交
204
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      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
璃白. 已提交
223
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
224 225
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
226
          background: var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
227 228 229
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
230
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
231 232 233
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
234
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
        }
      }
      & + .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>