tool-button.vue 2.6 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2
  <div
3 4 5 6 7 8 9 10
    v-if="isMobile"
    @click="handleTool(info.name, info.startStr, info.endStr)"
    class="tool_button"
  >
    <span :class="['icon iconfont', `icon-${info.icon}`]"></span>
  </div>
  <div
    v-else
璃白.'s avatar
璃白. 已提交
11
    v-tip.bottom="options"
璃白.'s avatar
璃白. 已提交
12 13 14
    @click="handleTool(info.name, info.startStr, info.endStr)"
    class="tool_button"
  >
璃白.'s avatar
璃白. 已提交
15 16 17 18
    <span :class="['icon iconfont', `icon-${info.icon}`]"></span>
  </div>
</template>
<script>
19
import { formatText, checkBoswer } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
20 21 22 23 24
export default {
  props: {
    info: {
      type: Object,
      default: () => {}
璃白.'s avatar
璃白. 已提交
25 26 27 28 29 30
    },
    fullScreen: {
      type: Boolean,
      default: false
    },
    text: {
璃白.'s avatar
璃白. 已提交
31 32
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
33
    },
34 35 36 37 38 39 40 41
    zIndex: {
      type: [String, Number],
      default: ""
    },
    themeOptions: {
      type: Object,
      default: () => {}
    },
璃白.'s avatar
璃白. 已提交
42 43
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
44
      default: () => {}
璃白.'s avatar
璃白. 已提交
45 46
    }
  },
璃白.'s avatar
璃白. 已提交
47 48 49
  data() {
    return {
      ulNum: 1
璃白.'s avatar
璃白. 已提交
50
    };
璃白.'s avatar
璃白. 已提交
51
  },
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  computed: {
    darkMode() {
      return this.themeOptions && this.themeOptions.dark;
    },
    isMobile() {
      const isMobile = checkBoswer();
      return isMobile;
    },
    options() {
      return {
        content: this.info.tip,
        zIndex: parseInt(this.zIndex) + 1,
        theme: this.darkMode ? "dark" : "light"
      };
    }
  },
璃白.'s avatar
璃白. 已提交
68
  methods: {
璃白.'s avatar
璃白. 已提交
69
    handleTool(type, startStr, endStr) {
璃白.'s avatar
璃白. 已提交
70 71 72 73 74 75 76
      switch (type) {
        case "bold":
        case "italic":
        case "quote":
        case "code":
        case "link":
        case "ul":
璃白.'s avatar
璃白. 已提交
77 78
        case "task":
        case "table":
璃白.'s avatar
璃白. 已提交
79
          this.updateText(startStr, endStr);
璃白.'s avatar
璃白. 已提交
80 81 82
          break;
        case "ol":
          let ulNum = this.ulNum;
璃白.'s avatar
璃白. 已提交
83
          this.updateText(`\n${ulNum}. `, "");
璃白.'s avatar
璃白. 已提交
84
          this.ulNum++;
璃白.'s avatar
璃白. 已提交
85
          break;
86 87 88
        case "file":
          this.$emit("upload");
          break;
璃白.'s avatar
璃白. 已提交
89
        case "fullScreen":
璃白.'s avatar
璃白. 已提交
90
          this.$emit("setFullScreen", true);
璃白.'s avatar
璃白. 已提交
91
          break;
璃白.'s avatar
璃白. 已提交
92 93 94
        case "cancelFullScreen":
          this.$emit("setFullScreen", false);
          break;
璃白.'s avatar
璃白. 已提交
95 96 97
        default:
          break;
      }
璃白.'s avatar
璃白. 已提交
98 99
    },
    updateText(startStr, endStr) {
璃白.'s avatar
璃白. 已提交
100 101 102 103 104
      const originalText = this.text;
      const selectionInfo = this.selectionInfo;
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
      this.$emit("updateText", newText);
    }
璃白.'s avatar
璃白. 已提交
105 106 107 108 109 110 111 112 113 114 115 116
  }
};
</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);
    }
璃白.'s avatar
璃白. 已提交
117 118 119 120
    &.icon-quxiaoquanping_o {
      font-size: 24px;
      margin: 0 -4px;
    }
璃白.'s avatar
璃白. 已提交
121 122 123
  }
}
</style>