md-header.vue 5.3 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 28
        :text="text"
        :selectionInfo="selectionInfo"
璃白.'s avatar
璃白. 已提交
29 30 31 32 33
      />
    </div>
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
34
import { isNotFalse } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
35 36 37
import toolButton from "./tool-button";
export default {
  components: { toolButton },
璃白.'s avatar
璃白. 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50
  props: {
    fullScreen: {
      type: Boolean,
      default: false
    },
    isFocus: {
      type: Boolean,
      default: false
    },
    showPreview: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
51 52 53 54 55 56 57 58
    canPreview: {
      type: Boolean,
      default: true
    },
    toolsOptions: {
      type: Object,
      default: () => {}
    },
59

璃白.'s avatar
璃白. 已提交
60
    text: {
璃白.'s avatar
璃白. 已提交
61
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
62 63 64 65 66 67
      default: ""
    },
    selectionInfo: {
      type: Object,
      default: () => {}
    }
璃白.'s avatar
璃白. 已提交
68
  },
璃白.'s avatar
璃白. 已提交
69 70 71 72 73 74 75 76 77 78
  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
璃白. 已提交
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
  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",
106 107 108
          tip: "插入代码块",
          startStr: "\n```\n",
          endStr: "\n```"
璃白.'s avatar
璃白. 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        },
        {
          name: "link",
          icon: "lianjie",
          tip: "添加链接",
          startStr: "[",
          endStr: "](url)"
        },
        {
          name: "ul",
          icon: "unorderedList",
          tip: "添加无序列表",
          startStr: "\n- ",
          endStr: ""
        },
        {
          name: "ol",
          icon: "youxuliebiao",
          tip: "添加有序列表",
          startStr: "",
          endStr: ""
        },
131 132 133 134 135 136 137
        {
          name: "file",
          icon: "tupian",
          tip: "上传文件",
          startStr: "",
          endStr: ""
        },
璃白.'s avatar
璃白. 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        {
          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
璃白. 已提交
162
  methods: {
璃白.'s avatar
璃白. 已提交
163 164 165 166 167 168 169 170 171 172 173
    setShowPreview(val) {
      this.$emit("update:showPreview", val);
    },
    updateText(val) {
      this.$emit("update:text", val);
      this.$emit("update:selectionInfo", {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      });
    }
璃白.'s avatar
璃白. 已提交
174 175 176 177 178 179 180 181 182
  }
};
</script>
<style lang="less" scoped>
.md_header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 32px;
璃白.'s avatar
璃白. 已提交
183
  transition: border-bottom 0.3s;
璃白.'s avatar
璃白. 已提交
184
  border-bottom: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
185
  &.active {
璃白.'s avatar
璃白. 已提交
186
    border-bottom: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
187
  }
璃白.'s avatar
璃白. 已提交
188 189 190 191 192 193 194
  .header_tabs {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    padding-bottom: 10px;
    box-sizing: border-box;
    .tab_item {
璃白.'s avatar
璃白. 已提交
195
      color: var(--md-editor-text-color);
璃白.'s avatar
璃白. 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
      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
璃白. 已提交
214
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
215 216
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
217
          background: var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
218 219 220
        }
      }
      &.active {
璃白.'s avatar
璃白. 已提交
221
        color: var(--md-editor-text-color-active);
璃白.'s avatar
璃白. 已提交
222 223 224
        font-weight: 700;
        &::after {
          width: 100%;
璃白.'s avatar
璃白. 已提交
225
          background: var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        }
      }
      & + .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>