md-textarea.vue 5.8 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2
  <div :class="['md_textarea', { fullScreen, isFocus }]">
璃白.'s avatar
璃白. 已提交
3 4
    <textarea
      :id="id"
璃白.'s avatar
璃白. 已提交
5
      @change="$emit('update:text', textContent)"
6
      @input="input"
璃白.'s avatar
璃白. 已提交
7 8
      @focus="setFocus(true)"
      @blur="setFocus(false)"
G
guoweijia 已提交
9
      @paste="pasteFile"
璃白.'s avatar
璃白. 已提交
10 11
      @keydown.meta.enter.exact="submit"
      @keydown.ctrl.enter.exact="submit"
璃白.'s avatar
璃白. 已提交
12
      v-model="textContent"
璃白.'s avatar
璃白. 已提交
13
      :placeholder="placeholder"
14 15
      :maxlength="maxLength"
      :rows="rows"
16
      :style="{ height: editorHeight, overflow: editorOverFlow }"
璃白.'s avatar
璃白. 已提交
17 18 19 20 21
    >
    </textarea>
  </div>
</template>
<script>
22 23 24 25 26
import {
  getSelectionInfo,
  getPosition,
  throttle as throttleFn
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
27
export default {
璃白.'s avatar
璃白. 已提交
28
  props: {
29 30 31 32
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
璃白. 已提交
33 34 35 36
    fullScreen: {
      type: Boolean,
      default: false
    },
37 38 39 40
    throttleTime: {
      type: Number,
      default: 1000
    },
璃白.'s avatar
璃白. 已提交
41 42 43 44 45 46 47 48 49 50
    isFocus: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: false
    },
    fileList: {
      type: Array,
璃白.'s avatar
璃白. 已提交
51
      default: () => []
璃白.'s avatar
璃白. 已提交
52 53
    },
    text: {
璃白.'s avatar
璃白. 已提交
54 55
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
56
    },
57 58 59 60 61 62 63 64
    maxLength: {
      type: [String, Number],
      default: ""
    },
    rows: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
65 66
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
67
      default: () => {}
璃白.'s avatar
璃白. 已提交
68 69
    }
  },
70

璃白.'s avatar
璃白. 已提交
71 72
  data() {
    return {
73
      textContent: "",
74 75
      editorHeight: "auto",
      editorOverFlow: "auto"
璃白.'s avatar
璃白. 已提交
76 77 78 79 80 81 82 83 84 85 86
    };
  },
  created() {
    document.addEventListener("mouseup", this.checkSelection);
  },
  watch: {
    text: {
      immediate: true,
      handler: function(val) {
        this.textContent = val;
      }
87
    },
88 89
    fullScreen: {
      immediate: true,
璃白.'s avatar
璃白. 已提交
90 91 92 93 94 95
      handler: function(val) {
        if (val) {
          document.body.style.overflow = "hidden";
        } else {
          document.body.style.overflow = "auto";
        }
96 97 98 99 100
        setTimeout(() => {
          this.reSizeHeight();
        }, 0);
      }
    },
101 102 103 104 105 106 107 108
    textContent: {
      immediate: true,
      handler: function() {
        setTimeout(() => {
          if (!this.autoSize) return;
          this.reSizeHeight();
        }, 0);
      }
璃白.'s avatar
璃白. 已提交
109 110 111 112 113
    }
  },
  beforeDestroy() {
    document.removeEventListener("mouseup", this.checkSelection);
  },
114 115 116 117 118 119 120 121 122 123
  computed: {
    emitText() {
      return throttleFn(() => {
        this.$emit("update:text", this.textContent);
      }, this.throttleTime);
    },
    autoSize() {
      return this.rows === "auto";
    }
  },
璃白.'s avatar
璃白. 已提交
124
  methods: {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    input() {
      this.$emit("update:textLength", this.textContent.length);
      this.emitText();
    },
    reSizeHeight() {
      const textEl = document.getElementById(this.id);
      if (!textEl) return;
      const fontSize = getComputedStyle(textEl).getPropertyValue("font-size");
      const lineHeight = getComputedStyle(textEl).getPropertyValue(
        "line-height"
      );

      const fontFamily = getComputedStyle(textEl).getPropertyValue(
        "font-family"
      );
      const hideElId = "hdieEl" + this.id;
      let hideEl = document.getElementById(hideElId);

      if (!hideEl) {
        hideEl = document.createElement("div");
        textEl.parentNode.appendChild(hideEl);
      }
      hideEl.id = hideElId;
      hideEl.style.fontSize = fontSize;
      hideEl.style.lineHeight = lineHeight;
      hideEl.style.fontFamily = fontFamily;
      hideEl.innerText = this.textContent;
      const contentHeight = hideEl.offsetHeight;
153 154 155 156 157 158 159
      this.editorHeight = this.fullScreen
        ? "calc(100% - 42px)"
        : this.autoSize
        ? `${contentHeight + parseFloat(fontSize) * 1.2}px`
        : "auto";
      this.editorOverFlow =
        this.autoSize && !this.fullScreen ? "hidden" : "auto";
160 161
      textEl.parentNode.removeChild(hideEl);
    },
璃白.'s avatar
璃白. 已提交
162 163 164
    submit() {
      this.$emit("submit");
    },
璃白.'s avatar
璃白. 已提交
165
    setFocus(val) {
璃白.'s avatar
璃白. 已提交
166
      this.$emit("update:isFocus", val);
璃白.'s avatar
璃白. 已提交
167
    },
璃白.'s avatar
璃白. 已提交
168 169
    checkSelection() {
      const info = getSelectionInfo(this.id);
璃白.'s avatar
璃白. 已提交
170 171
      if (!info) {
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
172
        this.$emit("update:selectionInfo", {
璃白.'s avatar
璃白. 已提交
173 174 175
          selectorId: this.id,
          selectionStart: cursorPoint,
          selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
176
        });
璃白.'s avatar
璃白. 已提交
177 178
        return;
      }
璃白.'s avatar
璃白. 已提交
179
      this.$emit("update:selectionInfo", info);
G
guoweijia 已提交
180 181 182 183 184 185 186 187 188 189 190
    },
    pasteFile(event) {
      let fileList = [];
      const items = (event.clipboardData || window.clipboardData).items;
      for (let i = 0; i < items.length; i++) {
        if (items[i].type.indexOf("image") !== -1) {
          fileList.push(items[i].getAsFile());
          break;
        }
      }
      if (!fileList.length) return;
璃白.'s avatar
璃白. 已提交
191
      this.$emit("update:fileList", fileList);
璃白.'s avatar
璃白. 已提交
192 193 194 195 196 197 198
    }
  }
};
</script>
<style lang="less" scoped>
.md_textarea {
  position: relative;
199
  padding: 14px 0;
璃白.'s avatar
璃白. 已提交
200
  background: var(--md-editor-content-bg-color);
201 202
  // border-left: 1px solid var(--md-editor-border-color);
  // border-right: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
203
  transition: border 0.3s;
204
  // padding: 14px;
璃白.'s avatar
璃白. 已提交
205
  box-sizing: border-box;
206 207 208 209
  // &.isFocus {
  //   border-left: 1px solid var(--md-editor-border-color-active);
  //   border-right: 1px solid var(--md-editor-border-color-active);
  // }
210

璃白.'s avatar
璃白. 已提交
211
  &.fullScreen {
212
    height: 100%;
璃白.'s avatar
璃白. 已提交
213 214
    textarea {
      font-size: 20px;
215 216
      max-height: 100%;
      overflow-y: auto;
璃白.'s avatar
璃白. 已提交
217 218
    }
  }
219

璃白.'s avatar
璃白. 已提交
220 221 222 223 224
  textarea {
    display: block;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
225
    background: var(--md-editor-content-bg-color);
226 227
    color: var(--md-editor-text-color-active);
    height: var(--md-editor-height);
璃白.'s avatar
璃白. 已提交
228
    resize: none;
229

璃白.'s avatar
璃白. 已提交
230 231
    font-family: "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas",
      "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace;
232 233 234
    &::placeholder {
      color: var(--md-editor-text-color);
    }
璃白.'s avatar
璃白. 已提交
235 236 237 238 239 240 241 242 243 244
  }
  .icon {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 32px;
    cursor: pointer;
  }
}
</style>