tool-button.vue 2.0 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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
<template>
  <div @click="handleTool(info.name)" class="tool_button">
    <span :class="['icon iconfont', `icon-${info.icon}`]"></span>
  </div>
</template>
<script>
import { mapMutations, mapState } from "vuex";
import { formatText } from "@/assets/js/utils";
export default {
  props: {
    info: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    ...mapState(["selectionInfo", "text", "ulNum"])
  },
  methods: {
    ...mapMutations(["setFullScreen", "setText", "setUlNum"]),
    handleTool(type) {
      switch (type) {
        case "bold":
          this.updateText("**", "**");
          break;
        case "italic":
          this.updateText("_", "_");
          break;
        case "quote":
          this.updateText("\n> ", "");
          break;
        case "code":
          this.updateText("`", "`");
          break;
        case "link":
          this.updateText("[", "](url)");
          break;
        case "ul":
          this.updateText("\n- ", "");
          break;
        case "ol":
          let ulNum = this.ulNum;
          this.updateText(`\n${ulNum++}. `, "");
          this.setUlNum(ulNum);
          break;
        case "task":
          this.updateText("\n- [ ] ", "");
          break;
        case "table":
          this.updateText(
            `\n\n| header | header |\n| ------ | ------ |\n| cell | cell |\n| cell | cell |\n\n`,
            ""
          );
          break;
        case "fullScreen":
          this.setFullScreen(true);
          break;

        default:
          break;
      }
    },
    updateText(startStr, endStr) {
      const selectionInfo = this.selectionInfo;
      const originalText = this.text;
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
      if (!newText) return;
      this.setText(newText);
    }
  }
};
</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);
    }
  }
}
</style>