md-textarea.vue 5.5 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 16
      :maxlength="maxLength"
      :rows="rows"
      :style="{ height: editorHeight, overflow: autoSize ? 'hidden' : 'auto' }"
璃白.'s avatar
璃白. 已提交
17 18 19
    >
    </textarea>
    <span
璃白.'s avatar
璃白. 已提交
20
      @click="$emit('update:fullScreen', false)"
璃白.'s avatar
璃白. 已提交
21 22 23 24 25 26
      v-if="fullScreen"
      class="icon iconfont icon-quxiaoquanping_o"
    ></span>
  </div>
</template>
<script>
27 28 29 30 31
import {
  getSelectionInfo,
  getPosition,
  throttle as throttleFn
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
32
export default {
璃白.'s avatar
璃白. 已提交
33 34 35 36 37
  props: {
    fullScreen: {
      type: Boolean,
      default: false
    },
38 39 40 41
    throttleTime: {
      type: Number,
      default: 1000
    },
璃白.'s avatar
璃白. 已提交
42 43 44 45 46 47 48 49 50 51
    isFocus: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: false
    },
    fileList: {
      type: Array,
璃白.'s avatar
璃白. 已提交
52
      default: () => []
璃白.'s avatar
璃白. 已提交
53 54
    },
    text: {
璃白.'s avatar
璃白. 已提交
55 56
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
57
    },
58 59 60 61 62 63 64 65
    maxLength: {
      type: [String, Number],
      default: ""
    },
    rows: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
66 67
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
68
      default: () => {}
璃白.'s avatar
璃白. 已提交
69 70
    }
  },
71

璃白.'s avatar
璃白. 已提交
72 73 74
  data() {
    return {
      id: new Date().getTime(),
75 76
      textContent: "",
      editorHeight: "auto"
璃白.'s avatar
璃白. 已提交
77 78 79 80 81 82 83 84 85 86 87
    };
  },
  created() {
    document.addEventListener("mouseup", this.checkSelection);
  },
  watch: {
    text: {
      immediate: true,
      handler: function(val) {
        this.textContent = val;
      }
88 89 90 91 92 93 94 95 96
    },
    textContent: {
      immediate: true,
      handler: function() {
        setTimeout(() => {
          if (!this.autoSize) return;
          this.reSizeHeight();
        }, 0);
      }
璃白.'s avatar
璃白. 已提交
97 98 99 100 101
    }
  },
  beforeDestroy() {
    document.removeEventListener("mouseup", this.checkSelection);
  },
102 103 104 105 106 107 108 109 110 111
  computed: {
    emitText() {
      return throttleFn(() => {
        this.$emit("update:text", this.textContent);
      }, this.throttleTime);
    },
    autoSize() {
      return this.rows === "auto";
    }
  },
璃白.'s avatar
璃白. 已提交
112
  methods: {
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    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;
      this.editorHeight = `${contentHeight + parseFloat(fontSize) * 1.2}px`;
      textEl.parentNode.removeChild(hideEl);
    },
璃白.'s avatar
璃白. 已提交
144 145 146
    submit() {
      this.$emit("submit");
    },
璃白.'s avatar
璃白. 已提交
147
    setFocus(val) {
璃白.'s avatar
璃白. 已提交
148
      this.$emit("update:isFocus", val);
璃白.'s avatar
璃白. 已提交
149
    },
璃白.'s avatar
璃白. 已提交
150 151
    checkSelection() {
      const info = getSelectionInfo(this.id);
璃白.'s avatar
璃白. 已提交
152 153
      if (!info) {
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
154
        this.$emit("update:selectionInfo", {
璃白.'s avatar
璃白. 已提交
155 156 157
          selectorId: this.id,
          selectionStart: cursorPoint,
          selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
158
        });
璃白.'s avatar
璃白. 已提交
159 160
        return;
      }
璃白.'s avatar
璃白. 已提交
161
      this.$emit("update:selectionInfo", info);
G
guoweijia 已提交
162 163 164 165 166 167 168 169 170 171 172
    },
    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
璃白. 已提交
173
      this.$emit("update:fileList", fileList);
璃白.'s avatar
璃白. 已提交
174 175 176 177 178 179 180
    }
  }
};
</script>
<style lang="less" scoped>
.md_textarea {
  position: relative;
181
  padding: 14px 0;
璃白.'s avatar
璃白. 已提交
182
  background: var(--md-editor-content-bg-color);
183 184
  // border-left: 1px solid var(--md-editor-border-color);
  // border-right: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
185
  transition: border 0.3s;
186
  // padding: 14px;
璃白.'s avatar
璃白. 已提交
187
  box-sizing: border-box;
188 189 190 191
  // &.isFocus {
  //   border-left: 1px solid var(--md-editor-border-color-active);
  //   border-right: 1px solid var(--md-editor-border-color-active);
  // }
璃白.'s avatar
璃白. 已提交
192 193 194 195 196 197 198
  &.fullScreen {
    position: fixed;
    width: 100vw;
    height: 100vh;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
199
    z-index: var(--md-editor-fullScrren-zIndex);
璃白.'s avatar
璃白. 已提交
200 201 202 203 204 205
    padding: 40px 60px;
    box-sizing: border-box;
    textarea {
      font-size: 20px;
    }
  }
206

璃白.'s avatar
璃白. 已提交
207 208 209 210 211
  textarea {
    display: block;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
212
    background: var(--md-editor-content-bg-color);
213 214
    color: var(--md-editor-text-color-active);
    height: var(--md-editor-height);
璃白.'s avatar
璃白. 已提交
215
    resize: none;
216

璃白.'s avatar
璃白. 已提交
217 218
    font-family: "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas",
      "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace;
219 220 221
    &::placeholder {
      color: var(--md-editor-text-color);
    }
璃白.'s avatar
璃白. 已提交
222 223 224 225 226 227 228 229 230 231
  }
  .icon {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 32px;
    cursor: pointer;
  }
}
</style>