md-textarea.vue 9.8 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2
  <div :class="['md_textarea', { fullScreen, isFocus }]">
璃白.'s avatar
璃白. 已提交
3
    <textarea
璃白.'s avatar
璃白. 已提交
4
      spellcheck="false"
璃白.'s avatar
璃白. 已提交
5
      :id="id"
璃白.'s avatar
璃白. 已提交
6
      @change="$emit('update:text', textContent)"
7
      @input="input"
璃白.'s avatar
璃白. 已提交
8 9
      @focus="setFocus(true)"
      @blur="setFocus(false)"
G
guoweijia 已提交
10
      @paste="pasteFile"
璃白.'s avatar
璃白. 已提交
11 12 13
      @keydown.stop.50="handleCallUser"
      @keydown.stop.up.prevent="changeActiveUserIndex('up')"
      @keydown.stop.down.prevent="changeActiveUserIndex('down')"
璃白.'s avatar
璃白. 已提交
14
      @keydown.enter="handleEnter"
璃白.'s avatar
璃白. 已提交
15 16
      @keydown.meta.enter.exact="submit"
      @keydown.ctrl.enter.exact="submit"
璃白.'s avatar
fix  
璃白. 已提交
17
      @keydown.tab.prevent="$emit('tab')"
璃白.'s avatar
璃白. 已提交
18
      v-model="textContent"
璃白.'s avatar
璃白. 已提交
19
      :placeholder="placeholder"
20 21
      :maxlength="maxLength"
      :rows="rows"
璃白.'s avatar
璃白. 已提交
22
      :disabled="disabled"
璃白.'s avatar
璃白. 已提交
23 24 25
      :style="{
        height: editorHeight,
        overflow: editorOverFlow,
璃白.'s avatar
璃白. 已提交
26 27 28
        cursor: disabled
          ? 'not-allowed'
          : formatType
璃白.'s avatar
璃白. 已提交
29 30 31
          ? `url(https://codechina.csdn.net/codechina/operation-work/uploads/a1b7c2a995b2320dca911e2f2ecb9b88/format.png),text`
          : 'text'
      }"
璃白.'s avatar
璃白. 已提交
32 33
    >
    </textarea>
璃白.'s avatar
璃白. 已提交
34 35 36 37 38 39 40
    <transition name="slide-fade">
      <helpDoc
        v-if="showHelp"
        @updateShowHelp="$emit('updateShowHelp', $event)"
        :showHelp.sync="showHelp"
      />
    </transition>
璃白.'s avatar
璃白. 已提交
41
    <transition name="slideup-fade">
璃白.'s avatar
璃白. 已提交
42 43
      <selectUser
        :userList="userList"
璃白.'s avatar
璃白. 已提交
44
        :activeUserIndex.sync="activeUserIndex"
璃白.'s avatar
璃白. 已提交
45 46
        v-show="showSelectUser"
        :position="selectUserPosition"
璃白.'s avatar
璃白. 已提交
47
        @selectUser="handleSelectUser"
璃白.'s avatar
璃白. 已提交
48
      />
璃白.'s avatar
璃白. 已提交
49
    </transition>
璃白.'s avatar
璃白. 已提交
50 51 52
  </div>
</template>
<script>
53 54 55
import {
  getSelectionInfo,
  getPosition,
璃白.'s avatar
璃白. 已提交
56
  preventDefault,
57 58
  throttle as throttleFn
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
59
import selectUser from "./components/user-select";
璃白.'s avatar
璃白. 已提交
60
import helpDoc from "./components/help-doc";
璃白.'s avatar
璃白. 已提交
61 62
import renderMix from "./mixins/render-mixins";
import selectUserMix from "./mixins/select-user-mixins";
璃白.'s avatar
璃白. 已提交
63
export default {
璃白.'s avatar
璃白. 已提交
64
  components: { helpDoc, selectUser },
璃白.'s avatar
璃白. 已提交
65
  mixins: [renderMix, selectUserMix],
璃白.'s avatar
璃白. 已提交
66
  props: {
67 68 69 70
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
fix  
璃白. 已提交
71 72 73 74 75 76 77
    html: {
      type: String,
      default: ""
    },
    htmlMinHeight: {
      default: ""
    },
璃白.'s avatar
璃白. 已提交
78 79 80 81
    fullScreen: {
      type: Boolean,
      default: false
    },
82 83 84 85
    throttleTime: {
      type: Number,
      default: 1000
    },
璃白.'s avatar
璃白. 已提交
86 87 88 89
    isFocus: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
90 91 92 93
    disabled: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
94 95 96 97 98 99
    placeholder: {
      type: String,
      default: false
    },
    fileList: {
      type: Array,
璃白.'s avatar
璃白. 已提交
100
      default: () => []
璃白.'s avatar
璃白. 已提交
101 102
    },
    text: {
璃白.'s avatar
璃白. 已提交
103 104
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
105
    },
106 107 108 109 110 111 112 113
    maxLength: {
      type: [String, Number],
      default: ""
    },
    rows: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
114 115 116 117
    height: {
      type: Number,
      default: 0
    },
璃白.'s avatar
璃白. 已提交
118 119
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
120
      default: () => {}
璃白.'s avatar
璃白. 已提交
121 122 123 124 125 126 127
    },
    formatType: {
      default: ""
    },
    showHelp: {
      type: Boolean,
      default: false
璃白.'s avatar
璃白. 已提交
128 129 130 131
    },
    userList: {
      type: Array,
      default: () => []
璃白.'s avatar
璃白. 已提交
132 133
    }
  },
134

璃白.'s avatar
璃白. 已提交
135 136
  data() {
    return {
137
      textContent: "",
138
      editorHeight: "auto",
璃白.'s avatar
璃白. 已提交
139 140
      editorOverFlow: "auto",
      showSelectUser: false,
璃白.'s avatar
璃白. 已提交
141 142 143 144 145
      queryInfo: {
        startPosition: "",
        endPosition: "",
        keyWord: ""
      },
璃白.'s avatar
璃白. 已提交
146
      activeUserIndex: 0,
璃白.'s avatar
璃白. 已提交
147
      selectUserPosition: { left: 0, top: 0 }
璃白.'s avatar
璃白. 已提交
148 149 150 151 152
    };
  },
  created() {
    document.addEventListener("mouseup", this.checkSelection);
  },
璃白.'s avatar
fix  
璃白. 已提交
153
  mounted() {
璃白.'s avatar
fix  
璃白. 已提交
154
    this.resetPreviewMinHeight();
璃白.'s avatar
fix  
璃白. 已提交
155
  },
璃白.'s avatar
璃白. 已提交
156
  watch: {
璃白.'s avatar
fix  
璃白. 已提交
157 158 159 160
    isFocus: {
      handler: function(val) {
        if (val) {
          this.resetPreviewMinHeight();
璃白.'s avatar
璃白. 已提交
161
        } else {
璃白.'s avatar
璃白. 已提交
162 163 164
          setTimeout(() => {
            // this.showSelectUser = false;
          }, 200);
璃白.'s avatar
fix  
璃白. 已提交
165 166 167
        }
      }
    },
璃白.'s avatar
璃白. 已提交
168 169 170 171
    text: {
      immediate: true,
      handler: function(val) {
        this.textContent = val;
璃白.'s avatar
fix  
璃白. 已提交
172
        this.transferMarkdown(val);
璃白.'s avatar
璃白. 已提交
173
      }
174
    },
175 176
    fullScreen: {
      immediate: true,
璃白.'s avatar
璃白. 已提交
177 178 179 180 181 182
      handler: function(val) {
        if (val) {
          document.body.style.overflow = "hidden";
        } else {
          document.body.style.overflow = "auto";
        }
183
        setTimeout(() => {
璃白.'s avatar
fix  
璃白. 已提交
184
          this.reSizeTextareaHeight();
185 186 187
        }, 0);
      }
    },
188 189 190 191 192
    textContent: {
      immediate: true,
      handler: function() {
        setTimeout(() => {
          if (!this.autoSize) return;
璃白.'s avatar
fix  
璃白. 已提交
193
          this.reSizeTextareaHeight();
194 195
        }, 0);
      }
璃白.'s avatar
璃白. 已提交
196 197 198 199 200 201 202
    },
    showSelectUser: {
      handler: function(val) {
        if (!val) {
          this.resetQueryInfo();
        }
      }
璃白.'s avatar
璃白. 已提交
203 204 205 206 207
    }
  },
  beforeDestroy() {
    document.removeEventListener("mouseup", this.checkSelection);
  },
208 209
  computed: {
    emitText() {
璃白.'s avatar
fix  
璃白. 已提交
210 211
      // return throttleFn(() => {}, this.throttleTime);
      return () => {
212
        this.$emit("update:text", this.textContent);
璃白.'s avatar
fix  
璃白. 已提交
213
      };
214 215 216 217 218
    },
    autoSize() {
      return this.rows === "auto";
    }
  },
璃白.'s avatar
璃白. 已提交
219
  methods: {
璃白.'s avatar
璃白. 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    changeActiveUserIndex(type) {
      if (this.showSelectUser) {
        const max = this.userList.length;
        if (type === "down") {
          this.activeUserIndex++;
          if (this.activeUserIndex >= max) {
            this.activeUserIndex = 0;
          }
        } else {
          this.activeUserIndex--;
          if (this.activeUserIndex < 0) {
            this.activeUserIndex = max - 1;
          }
        }
      }
    },
璃白.'s avatar
fix  
璃白. 已提交
236 237 238 239 240 241 242
    resetPreviewMinHeight() {
      setTimeout(() => {
        const textEl = document.getElementById(this.id);
        if (!textEl) return;
        const height = textEl.offsetHeight;
        this.$emit("update:htmlMinHeight", height);
      }, 0);
璃白.'s avatar
fix  
璃白. 已提交
243
    },
璃白.'s avatar
璃白. 已提交
244 245 246 247 248 249 250
    resetQueryInfo() {
      this.queryInfo = {
        startPosition: "",
        endPosition: "",
        keyWord: ""
      };
    },
251
    input() {
璃白.'s avatar
璃白. 已提交
252
      if (this.showSelectUser) this.handleQueryUser();
253 254 255
      this.$emit("update:textLength", this.textContent.length);
      this.emitText();
    },
璃白.'s avatar
璃白. 已提交
256
    createHideEl(type) {
257 258 259 260 261 262 263 264 265
      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"
      );
璃白.'s avatar
璃白. 已提交
266
      const hideElId = type + this.id;
267 268 269 270 271 272 273 274 275
      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;
璃白.'s avatar
璃白. 已提交
276 277 278 279 280 281 282 283
      return hideEl;
    },
    reSizeTextareaHeight() {
      const textEl = document.getElementById(this.id);
      if (!textEl) return;
      const fontSize = getComputedStyle(textEl).getPropertyValue("font-size");

      const hideEl = this.createHideEl("clac_height_El_");
284 285
      hideEl.innerText = this.textContent;
      const contentHeight = hideEl.offsetHeight;
286 287
      this.editorHeight = this.fullScreen
        ? "calc(100% - 42px)"
璃白.'s avatar
璃白. 已提交
288 289
        : this.height
        ? this.height + "px"
290 291 292 293
        : this.autoSize
        ? `${contentHeight + parseFloat(fontSize) * 1.2}px`
        : "auto";
      this.editorOverFlow =
璃白.'s avatar
fix  
璃白. 已提交
294
        this.autoSize && !this.fullScreen && !this.height ? "hidden" : "auto";
295 296
      textEl.parentNode.removeChild(hideEl);
    },
璃白.'s avatar
璃白. 已提交
297
    handleEnter(e) {
璃白.'s avatar
璃白. 已提交
298
      if (this.showSelectUser) {
璃白.'s avatar
璃白. 已提交
299 300 301
        const activeUser = this.userList[this.activeUserIndex];
        this.handleSelectUser(activeUser);
        e.preventDefault();
璃白.'s avatar
璃白. 已提交
302 303 304 305
        return;
      }
      this.$emit("enter");
    },
璃白.'s avatar
璃白. 已提交
306 307 308
    submit() {
      this.$emit("submit");
    },
璃白.'s avatar
璃白. 已提交
309
    setFocus(val) {
璃白.'s avatar
璃白. 已提交
310
      this.$emit("update:isFocus", val);
璃白.'s avatar
璃白. 已提交
311
    },
璃白.'s avatar
璃白. 已提交
312 313
    checkSelection() {
      const info = getSelectionInfo(this.id);
璃白.'s avatar
璃白. 已提交
314 315
      if (!info) {
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
316
        this.$emit("update:selectionInfo", {
璃白.'s avatar
璃白. 已提交
317 318 319
          selectorId: this.id,
          selectionStart: cursorPoint,
          selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
320
        });
璃白.'s avatar
璃白. 已提交
321 322
        return;
      }
璃白.'s avatar
璃白. 已提交
323
      this.$emit("update:selectionInfo", info);
G
guoweijia 已提交
324 325 326 327 328 329 330 331 332 333 334
    },
    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
璃白. 已提交
335
      this.checkSelection();
璃白.'s avatar
璃白. 已提交
336
      this.$emit("update:fileList", fileList);
璃白.'s avatar
璃白. 已提交
337 338 339 340 341 342 343
    }
  }
};
</script>
<style lang="less" scoped>
.md_textarea {
  position: relative;
344
  padding: 14px 0;
璃白.'s avatar
璃白. 已提交
345
  background: var(--md-editor-content-bg-color);
346 347
  // border-left: 1px solid var(--md-editor-border-color);
  // border-right: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
348
  transition: border 0.3s;
349
  // padding: 14px;
璃白.'s avatar
璃白. 已提交
350
  box-sizing: border-box;
351 352 353 354
  // &.isFocus {
  //   border-left: 1px solid var(--md-editor-border-color-active);
  //   border-right: 1px solid var(--md-editor-border-color-active);
  // }
355

璃白.'s avatar
璃白. 已提交
356
  &.fullScreen {
357
    height: 100%;
璃白.'s avatar
璃白. 已提交
358 359
    textarea {
      font-size: 20px;
360 361
      max-height: 100%;
      overflow-y: auto;
璃白.'s avatar
璃白. 已提交
362 363
    }
  }
璃白.'s avatar
璃白. 已提交
364 365 366
  &.disabled {
    background: var(--md-editor-content-bg-color-disabled);
  }
367

璃白.'s avatar
璃白. 已提交
368 369 370 371 372
  textarea {
    display: block;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
373
    background: var(--md-editor-content-bg-color);
374 375
    color: var(--md-editor-text-color-active);
    height: var(--md-editor-height);
璃白.'s avatar
璃白. 已提交
376
    resize: none;
璃白.'s avatar
璃白. 已提交
377
    font-size: 14px;
璃白.'s avatar
璃白. 已提交
378
    line-height: 1.625;
璃白.'s avatar
璃白. 已提交
379
    word-break: break-all;
璃白.'s avatar
璃白. 已提交
380 381 382
    font-family: "Menlo", -apple-system, SF UI Text, Arial, PingFang SC,
      Hiragino Sans GB, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif, SimHei,
      SimSun;
383 384 385
    &::placeholder {
      color: var(--md-editor-text-color);
    }
璃白.'s avatar
璃白. 已提交
386 387 388
    // &:disabled {
    //   background: var(--md-editor-content-bg-color-disabled);
    // }
璃白.'s avatar
璃白. 已提交
389 390 391 392 393 394 395 396 397 398
  }
  .icon {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 32px;
    cursor: pointer;
  }
}
</style>