md-header.vue 4.6 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2
  <div :class="['md_header', { active: isFocus }]">
璃白.'s avatar
璃白. 已提交
3 4 5
    <div class="header_tabs">
      <div
        :class="['tab_item', { active: !showPreview }]"
璃白.'s avatar
璃白. 已提交
6
        @click="setShowPreview(false)"
璃白.'s avatar
璃白. 已提交
7 8 9 10 11
      >
        编辑
      </div>
      <div
        :class="['tab_item', { active: showPreview }]"
璃白.'s avatar
璃白. 已提交
12
        @click="setShowPreview(true)"
璃白.'s avatar
璃白. 已提交
13 14 15 16 17 18 19
      >
        预览
      </div>
    </div>
    <div class="header_tools" v-if="!showPreview">
      <tool-button
        :info="item"
璃白.'s avatar
璃白. 已提交
20 21 22
        :fullScreen="fullScreen"
        @setFullScreen="$emit('update:fullScreen', true)"
        @updateText="updateText"
璃白.'s avatar
璃白. 已提交
23 24
        v-for="(item, index) in toolButtonList"
        :key="index"
璃白.'s avatar
璃白. 已提交
25 26
        :text="text"
        :selectionInfo="selectionInfo"
璃白.'s avatar
璃白. 已提交
27 28 29 30 31 32 33 34
      />
    </div>
  </div>
</template>
<script>
import toolButton from "./tool-button";
export default {
  components: { toolButton },
璃白.'s avatar
璃白. 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  props: {
    fullScreen: {
      type: Boolean,
      default: false
    },
    isFocus: {
      type: Boolean,
      default: false
    },
    showPreview: {
      type: Boolean,
      default: false
    },
    text: {
      type: String,
      default: ""
    },
    selectionInfo: {
      type: Object,
      default: () => {}
    }
璃白.'s avatar
璃白. 已提交
56
  },
璃白.'s avatar
璃白. 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  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",
          tip: "插入代码",
          startStr: "`",
          endStr: "`"
        },
        {
          name: "link",
          icon: "lianjie",
          tip: "添加链接",
          startStr: "[",
          endStr: "](url)"
        },
        {
          name: "ul",
          icon: "unorderedList",
          tip: "添加无序列表",
          startStr: "\n- ",
          endStr: ""
        },
        {
          name: "ol",
          icon: "youxuliebiao",
          tip: "添加有序列表",
          startStr: "",
          endStr: ""
        },
        {
          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
璃白. 已提交
133
  methods: {
璃白.'s avatar
璃白. 已提交
134 135 136 137 138 139 140 141 142 143 144
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
    },
    updateText(val) {
      this.$emit("update:text", val);
      this.$emit("update:selectionInfo", {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      });
    }
璃白.'s avatar
璃白. 已提交
145 146 147 148 149 150 151 152 153
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
154
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
155
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
156
  &.active {
璃白.'s avatar
璃白. 已提交
157
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
158
  }
璃白.'s avatar
璃白. 已提交
159 160 161 162 163 164 165
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    padding-bottom: 10px;
    box-sizing: border-box;
    .tab_item {
璃白.'s avatar
璃白. 已提交
166
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
      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
璃白. 已提交
185
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
186 187
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
188
          background: var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
189 190 191
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
192
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
193 194 195
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
196
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        }
      }
      & + .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>