tool-button.vue 1.8 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2 3 4 5 6
  <div
    v-tip.top="info.tip"
    @click="handleTool(info.name, info.startStr, info.endStr)"
    class="tool_button"
  >
璃白.'s avatar
璃白. 已提交
7 8 9 10
    <span :class="['icon iconfont', `icon-${info.icon}`]"></span>
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
11
import { formatText } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
12 13 14 15 16
export default {
  props: {
    info: {
      type: Object,
      default: () => {}
璃白.'s avatar
璃白. 已提交
17 18 19 20 21 22
    },
    fullScreen: {
      type: Boolean,
      default: false
    },
    text: {
璃白.'s avatar
璃白. 已提交
23 24
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
25 26 27
    },
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
28
      default: () => {}
璃白.'s avatar
璃白. 已提交
29 30
    }
  },
璃白.'s avatar
璃白. 已提交
31 32 33
  data() {
    return {
      ulNum: 1
璃白.'s avatar
璃白. 已提交
34
    };
璃白.'s avatar
璃白. 已提交
35 36
  },
  methods: {
璃白.'s avatar
璃白. 已提交
37
    handleTool(type, startStr, endStr) {
璃白.'s avatar
璃白. 已提交
38 39 40 41 42 43 44
      switch (type) {
        case "bold":
        case "italic":
        case "quote":
        case "code":
        case "link":
        case "ul":
璃白.'s avatar
璃白. 已提交
45 46
        case "task":
        case "table":
璃白.'s avatar
璃白. 已提交
47
          this.updateText(startStr, endStr);
璃白.'s avatar
璃白. 已提交
48 49 50
          break;
        case "ol":
          let ulNum = this.ulNum;
璃白.'s avatar
璃白. 已提交
51
          this.updateText(`\n${ulNum}. `, "");
璃白.'s avatar
璃白. 已提交
52
          this.ulNum++;
璃白.'s avatar
璃白. 已提交
53
          break;
54 55 56
        case "file":
          this.$emit("upload");
          break;
璃白.'s avatar
璃白. 已提交
57
        case "fullScreen":
璃白.'s avatar
璃白. 已提交
58
          this.$emit("setFullScreen", true);
璃白.'s avatar
璃白. 已提交
59 60 61 62
          break;
        default:
          break;
      }
璃白.'s avatar
璃白. 已提交
63 64
    },
    updateText(startStr, endStr) {
璃白.'s avatar
璃白. 已提交
65 66 67 68 69
      const originalText = this.text;
      const selectionInfo = this.selectionInfo;
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
      this.$emit("updateText", newText);
    }
璃白.'s avatar
璃白. 已提交
70 71 72 73 74 75 76 77 78 79 80 81
  }
};
</script>
<style lang="less" scoped>
.tool_button {
  .icon {
    font-size: 18px;
    color: var(--md-editor-text-color);
    cursor: pointer;
    &:hover {
      color: var(--md-editor-text-color-active);
    }
82 83 84
    &.icon-tupian {
      font-size: 24px;
    }
璃白.'s avatar
璃白. 已提交
85 86 87
  }
}
</style>