tool-button.vue 3.0 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
fix  
璃白. 已提交
45 46 47 48
    },
    ulNum: {
      type: Number,
      default: 1
璃白.'s avatar
璃白. 已提交
49 50
    }
  },
璃白.'s avatar
璃白. 已提交
51 52
  data() {
    return {
璃白.'s avatar
fix  
璃白. 已提交
53
      // ulNum: 1
璃白.'s avatar
璃白. 已提交
54
    };
璃白.'s avatar
璃白. 已提交
55
  },
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  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
璃白. 已提交
72
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
73 74 75
    // resetUlNum() {
    //   this.ulNum = 1;
    // },
璃白.'s avatar
璃白. 已提交
76
    handleTool(type, startStr, endStr) {
璃白.'s avatar
璃白. 已提交
77 78 79 80 81 82 83
      switch (type) {
        case "bold":
        case "italic":
        case "quote":
        case "code":
        case "link":
        case "ul":
璃白.'s avatar
璃白. 已提交
84 85
        case "task":
        case "table":
璃白.'s avatar
璃白. 已提交
86
          this.updateText(startStr, endStr);
璃白.'s avatar
璃白. 已提交
87 88 89
          break;
        case "ol":
          let ulNum = this.ulNum;
璃白.'s avatar
fix  
璃白. 已提交
90 91
          this.updateText(`\n${ulNum++}. `, "");
          this.$emit("update:ulNum", ulNum);
璃白.'s avatar
璃白. 已提交
92
          break;
93 94 95
        case "file":
          this.$emit("upload");
          break;
璃白.'s avatar
璃白. 已提交
96
        case "fullScreen":
璃白.'s avatar
璃白. 已提交
97
          this.$emit("setFullScreen", true);
璃白.'s avatar
璃白. 已提交
98
          break;
璃白.'s avatar
璃白. 已提交
99 100 101
        case "cancelFullScreen":
          this.$emit("setFullScreen", false);
          break;
璃白.'s avatar
璃白. 已提交
102 103 104
        default:
          break;
      }
璃白.'s avatar
璃白. 已提交
105 106
    },
    updateText(startStr, endStr) {
璃白.'s avatar
璃白. 已提交
107 108 109
      const originalText = this.text;
      const selectionInfo = this.selectionInfo;
      const newText = formatText(originalText, selectionInfo, startStr, endStr);
璃白.'s avatar
fix  
璃白. 已提交
110 111 112
      const len =
        selectionInfo.selectionEnd -
        selectionInfo.selectionStart +
113
        startStr.length;
璃白.'s avatar
璃白. 已提交
114

璃白.'s avatar
fix  
璃白. 已提交
115
      this.$emit("updateText", { val: newText, len });
璃白.'s avatar
璃白. 已提交
116
    }
璃白.'s avatar
璃白. 已提交
117 118 119 120 121
  }
};
</script>
<style lang="less" scoped>
.tool_button {
122 123 124
  padding: 4px 8px;
  box-sizing: border-box;
  cursor: pointer;
璃白.'s avatar
璃白. 已提交
125 126 127
  .icon {
    font-size: 18px;
    color: var(--md-editor-text-color);
璃白.'s avatar
fix  
璃白. 已提交
128 129 130 131
    @media (any-hover: hover) {
      &:hover {
        color: var(--md-editor-text-color-active);
      }
璃白.'s avatar
璃白. 已提交
132
    }
璃白.'s avatar
璃白. 已提交
133 134 135 136
    &.icon-quxiaoquanping_o {
      font-size: 24px;
      margin: 0 -4px;
    }
璃白.'s avatar
璃白. 已提交
137 138 139
  }
}
</style>