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

璃白.'s avatar
璃白. 已提交
139 140
  data() {
    return {
141
      textContent: "",
142
      editorHeight: "auto",
璃白.'s avatar
璃白. 已提交
143 144
      editorOverFlow: "auto",
      showSelectUser: false,
璃白.'s avatar
璃白. 已提交
145 146 147 148 149
      queryInfo: {
        startPosition: "",
        endPosition: "",
        keyWord: ""
      },
璃白.'s avatar
璃白. 已提交
150
      waiting: false,
璃白.'s avatar
璃白. 已提交
151
      activeUserIndex: 0,
璃白.'s avatar
璃白. 已提交
152
      selectUserPosition: { left: 0, top: 0 }
璃白.'s avatar
璃白. 已提交
153 154 155 156 157
    };
  },
  created() {
    document.addEventListener("mouseup", this.checkSelection);
  },
璃白.'s avatar
fix  
璃白. 已提交
158
  mounted() {
璃白.'s avatar
fix  
璃白. 已提交
159
    this.resetPreviewMinHeight();
璃白.'s avatar
fix  
璃白. 已提交
160
  },
璃白.'s avatar
璃白. 已提交
161
  watch: {
璃白.'s avatar
璃白. 已提交
162 163 164 165 166 167
    // userList: {
    //   handler: function(val) {
    //     if (!val.length) return;
    //     this.activeUserIndex = 0;
    //   }
    // },
璃白.'s avatar
fix  
璃白. 已提交
168 169 170 171
    isFocus: {
      handler: function(val) {
        if (val) {
          this.resetPreviewMinHeight();
璃白.'s avatar
璃白. 已提交
172
        } else {
璃白.'s avatar
璃白. 已提交
173
          setTimeout(() => {
璃白.'s avatar
璃白. 已提交
174
            this.showSelectUser = false;
璃白.'s avatar
璃白. 已提交
175
          }, 200);
璃白.'s avatar
fix  
璃白. 已提交
176 177 178
        }
      }
    },
璃白.'s avatar
璃白. 已提交
179 180 181 182
    text: {
      immediate: true,
      handler: function(val) {
        this.textContent = val;
璃白.'s avatar
fix  
璃白. 已提交
183
        this.transferMarkdown(val);
璃白.'s avatar
璃白. 已提交
184
      }
185
    },
186 187
    fullScreen: {
      immediate: true,
璃白.'s avatar
璃白. 已提交
188 189 190 191 192 193
      handler: function(val) {
        if (val) {
          document.body.style.overflow = "hidden";
        } else {
          document.body.style.overflow = "auto";
        }
194
        setTimeout(() => {
璃白.'s avatar
fix  
璃白. 已提交
195
          this.reSizeTextareaHeight();
196 197 198
        }, 0);
      }
    },
199 200 201 202 203
    textContent: {
      immediate: true,
      handler: function() {
        setTimeout(() => {
          if (!this.autoSize) return;
璃白.'s avatar
fix  
璃白. 已提交
204
          this.reSizeTextareaHeight();
205 206
        }, 0);
      }
璃白.'s avatar
璃白. 已提交
207 208 209 210 211 212 213
    },
    showSelectUser: {
      handler: function(val) {
        if (!val) {
          this.resetQueryInfo();
        }
      }
璃白.'s avatar
璃白. 已提交
214 215 216 217 218
    }
  },
  beforeDestroy() {
    document.removeEventListener("mouseup", this.checkSelection);
  },
219 220
  computed: {
    emitText() {
璃白.'s avatar
fix  
璃白. 已提交
221 222
      // return throttleFn(() => {}, this.throttleTime);
      return () => {
223
        this.$emit("update:text", this.textContent);
璃白.'s avatar
fix  
璃白. 已提交
224
      };
225 226 227 228 229
    },
    autoSize() {
      return this.rows === "auto";
    }
  },
璃白.'s avatar
璃白. 已提交
230
  methods: {
璃白.'s avatar
璃白. 已提交
231
    changeActiveUserIndex(event, type) {
璃白.'s avatar
璃白. 已提交
232
      if (this.showSelectUser) {
璃白.'s avatar
璃白. 已提交
233
        event.preventDefault();
璃白.'s avatar
璃白. 已提交
234 235 236 237 238 239
        const max = this.userList.length;
        if (type === "down") {
          this.activeUserIndex++;
          if (this.activeUserIndex >= max) {
            this.activeUserIndex = 0;
          }
璃白.'s avatar
璃白. 已提交
240
          const index = this.activeUserIndex;
璃白.'s avatar
璃白. 已提交
241 242 243 244 245
          const activeItem = document.getElementById("md_user_id_" + index);
          const activeTop =
            activeItem.offsetTop - activeItem.parentNode.scrollTop;

          if (index === 0) {
璃白.'s avatar
璃白. 已提交
246
            document
璃白.'s avatar
璃白. 已提交
247 248 249 250 251 252 253
              .getElementById("md_user_id_" + index)
              .parentNode.scrollTo(0, 0);
          }
          if (index > 3 && activeTop > 126) {
            document
              .getElementById("md_user_id_" + index)
              .parentNode.scrollTo(0, 40 * (index - 3));
璃白.'s avatar
璃白. 已提交
254 255
            // .scrollIntoView({ behavior: "smooth" });
          }
璃白.'s avatar
璃白. 已提交
256 257 258 259 260
        } else {
          this.activeUserIndex--;
          if (this.activeUserIndex < 0) {
            this.activeUserIndex = max - 1;
          }
璃白.'s avatar
璃白. 已提交
261
          const index = this.activeUserIndex;
璃白.'s avatar
璃白. 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275
          const activeItem = document.getElementById("md_user_id_" + index);
          const activeTop =
            activeItem.offsetTop - activeItem.parentNode.scrollTop;
          if (index === max - 1) {
            activeItem.parentNode.scrollTo(
              0,
              activeItem.parentNode.scrollHeight
            );
          }
          if (activeTop < 46) {
            document
              .getElementById("md_user_id_" + this.activeUserIndex)
              .parentNode.scrollTo(0, 40 * index);
          }
璃白.'s avatar
璃白. 已提交
276
          // .scrollIntoView({ behavior: "smooth" });
璃白.'s avatar
璃白. 已提交
277 278 279
        }
      }
    },
璃白.'s avatar
fix  
璃白. 已提交
280 281 282 283 284 285 286
    resetPreviewMinHeight() {
      setTimeout(() => {
        const textEl = document.getElementById(this.id);
        if (!textEl) return;
        const height = textEl.offsetHeight;
        this.$emit("update:htmlMinHeight", height);
      }, 0);
璃白.'s avatar
fix  
璃白. 已提交
287
    },
璃白.'s avatar
璃白. 已提交
288 289 290 291 292 293 294
    resetQueryInfo() {
      this.queryInfo = {
        startPosition: "",
        endPosition: "",
        keyWord: ""
      };
    },
璃白.'s avatar
璃白. 已提交
295
    input(e) {
璃白.'s avatar
璃白. 已提交
296 297
      if (isAndroid() && e.data === "@") {
        this.createSelectUserDialog("android");
璃白.'s avatar
璃白. 已提交
298
      }
璃白.'s avatar
璃白. 已提交
299
      if (this.showSelectUser) this.handleQueryUser(e);
300 301 302
      this.$emit("update:textLength", this.textContent.length);
      this.emitText();
    },
璃白.'s avatar
璃白. 已提交
303
    blur() {
璃白.'s avatar
璃白. 已提交
304
      this.renderUserTags();
璃白.'s avatar
璃白. 已提交
305 306
      this.setFocus(false);
    },
璃白.'s avatar
璃白. 已提交
307
    createHideEl(type) {
308 309 310 311 312 313 314 315 316
      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
璃白. 已提交
317
      const hideElId = type + this.id;
318 319 320 321 322 323 324 325 326
      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
璃白. 已提交
327 328 329 330 331 332 333 334
      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_");
335 336
      hideEl.innerText = this.textContent;
      const contentHeight = hideEl.offsetHeight;
337 338
      this.editorHeight = this.fullScreen
        ? "calc(100% - 42px)"
璃白.'s avatar
璃白. 已提交
339 340
        : this.height
        ? this.height + "px"
341 342 343 344
        : this.autoSize
        ? `${contentHeight + parseFloat(fontSize) * 1.2}px`
        : "auto";
      this.editorOverFlow =
璃白.'s avatar
fix  
璃白. 已提交
345
        this.autoSize && !this.fullScreen && !this.height ? "hidden" : "auto";
346 347
      textEl.parentNode.removeChild(hideEl);
    },
璃白.'s avatar
璃白. 已提交
348
    handleEnter(e) {
璃白.'s avatar
璃白. 已提交
349
      if (this.showSelectUser) {
璃白.'s avatar
璃白. 已提交
350 351 352
        const activeUser = this.userList[this.activeUserIndex];
        this.handleSelectUser(activeUser);
        e.preventDefault();
璃白.'s avatar
璃白. 已提交
353 354 355 356
        return;
      }
      this.$emit("enter");
    },
璃白.'s avatar
璃白. 已提交
357 358 359
    submit() {
      this.$emit("submit");
    },
璃白.'s avatar
璃白. 已提交
360
    setFocus(val) {
璃白.'s avatar
璃白. 已提交
361
      this.$emit("update:isFocus", val);
璃白.'s avatar
璃白. 已提交
362
    },
璃白.'s avatar
璃白. 已提交
363 364
    checkSelection() {
      const info = getSelectionInfo(this.id);
璃白.'s avatar
璃白. 已提交
365 366
      if (!info) {
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
367
        this.$emit("update:selectionInfo", {
璃白.'s avatar
璃白. 已提交
368 369 370
          selectorId: this.id,
          selectionStart: cursorPoint,
          selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
371
        });
璃白.'s avatar
璃白. 已提交
372 373
        return;
      }
璃白.'s avatar
璃白. 已提交
374
      this.$emit("update:selectionInfo", info);
G
guoweijia 已提交
375 376 377 378 379 380 381 382 383 384 385
    },
    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
璃白. 已提交
386
      this.checkSelection();
璃白.'s avatar
璃白. 已提交
387
      this.$emit("update:fileList", fileList);
璃白.'s avatar
璃白. 已提交
388 389 390 391 392 393 394
    }
  }
};
</script>
<style lang="less" scoped>
.md_textarea {
  position: relative;
395
  padding: 14px 0;
璃白.'s avatar
璃白. 已提交
396
  background: var(--md-editor-content-bg-color);
397 398
  // border-left: 1px solid var(--md-editor-border-color);
  // border-right: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
399
  transition: border 0.3s;
400
  // padding: 14px;
璃白.'s avatar
璃白. 已提交
401
  box-sizing: border-box;
402 403 404 405
  // &.isFocus {
  //   border-left: 1px solid var(--md-editor-border-color-active);
  //   border-right: 1px solid var(--md-editor-border-color-active);
  // }
406

璃白.'s avatar
璃白. 已提交
407
  &.fullScreen {
408
    height: 100%;
璃白.'s avatar
璃白. 已提交
409 410
    textarea {
      font-size: 20px;
411 412
      max-height: 100%;
      overflow-y: auto;
璃白.'s avatar
璃白. 已提交
413 414
    }
  }
璃白.'s avatar
璃白. 已提交
415 416 417
  &.disabled {
    background: var(--md-editor-content-bg-color-disabled);
  }
418

璃白.'s avatar
璃白. 已提交
419 420 421 422 423
  textarea {
    display: block;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
424
    background: var(--md-editor-content-bg-color);
425 426
    color: var(--md-editor-text-color-active);
    height: var(--md-editor-height);
璃白.'s avatar
璃白. 已提交
427
    resize: none;
璃白.'s avatar
璃白. 已提交
428
    font-size: 14px;
璃白.'s avatar
璃白. 已提交
429
    line-height: 1.625;
璃白.'s avatar
璃白. 已提交
430
    word-break: break-all;
璃白.'s avatar
璃白. 已提交
431 432 433
    font-family: "Menlo", -apple-system, SF UI Text, Arial, PingFang SC,
      Hiragino Sans GB, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif, SimHei,
      SimSun;
434 435 436
    &::placeholder {
      color: var(--md-editor-text-color);
    }
璃白.'s avatar
璃白. 已提交
437 438 439
    &.disabled {
      opacity: var(--md-editor-disabled-opacity);
    }
璃白.'s avatar
璃白. 已提交
440 441 442
    // &:disabled {
    //   background: var(--md-editor-content-bg-color-disabled);
    // }
璃白.'s avatar
璃白. 已提交
443 444 445 446 447 448 449 450 451 452
  }
  .icon {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 32px;
    cursor: pointer;
  }
}
</style>