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 30
        cursor: disabled
          ? 'not-allowed'
          : formatType
璃白.'s avatar
璃白. 已提交
31 32 33
          ? `url(https://codechina.csdn.net/codechina/operation-work/uploads/a1b7c2a995b2320dca911e2f2ecb9b88/format.png),text`
          : 'text'
      }"
璃白.'s avatar
璃白. 已提交
34 35
    >
    </textarea>
璃白.'s avatar
璃白. 已提交
36 37 38 39 40 41 42
    <transition name="slide-fade">
      <helpDoc
        v-if="showHelp"
        @updateShowHelp="$emit('updateShowHelp', $event)"
        :showHelp.sync="showHelp"
      />
    </transition>
璃白.'s avatar
璃白. 已提交
43
    <transition name="slideup-fade">
璃白.'s avatar
璃白. 已提交
44 45
      <selectUser
        :userList="userList"
璃白.'s avatar
璃白. 已提交
46
        :activeUserIndex.sync="activeUserIndex"
璃白.'s avatar
璃白. 已提交
47 48
        v-show="showSelectUser"
        :position="selectUserPosition"
璃白.'s avatar
璃白. 已提交
49
        @selectUser="handleSelectUser"
璃白.'s avatar
璃白. 已提交
50
      />
璃白.'s avatar
璃白. 已提交
51
    </transition>
璃白.'s avatar
璃白. 已提交
52 53 54
  </div>
</template>
<script>
55 56 57
import {
  getSelectionInfo,
  getPosition,
璃白.'s avatar
璃白. 已提交
58
  isAndroid,
59 60
  throttle as throttleFn
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
61
import selectUser from "./components/user-select";
璃白.'s avatar
璃白. 已提交
62
import helpDoc from "./components/help-doc";
璃白.'s avatar
璃白. 已提交
63 64
import renderMix from "./mixins/render-mixins";
import selectUserMix from "./mixins/select-user-mixins";
璃白.'s avatar
璃白. 已提交
65
export default {
璃白.'s avatar
璃白. 已提交
66
  components: { helpDoc, selectUser },
璃白.'s avatar
璃白. 已提交
67
  mixins: [renderMix, selectUserMix],
璃白.'s avatar
璃白. 已提交
68
  props: {
69 70 71 72
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
fix  
璃白. 已提交
73
    html: {
璃白.'s avatar
璃白. 已提交
74
      type: [String, Promise],
璃白.'s avatar
fix  
璃白. 已提交
75 76 77 78 79
      default: ""
    },
    htmlMinHeight: {
      default: ""
    },
璃白.'s avatar
璃白. 已提交
80 81 82 83
    fullScreen: {
      type: Boolean,
      default: false
    },
84 85 86 87
    throttleTime: {
      type: Number,
      default: 1000
    },
璃白.'s avatar
璃白. 已提交
88 89 90 91
    isFocus: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
92 93 94 95
    disabled: {
      type: Boolean,
      default: false
    },
璃白.'s avatar
璃白. 已提交
96 97 98 99 100 101
    placeholder: {
      type: String,
      default: false
    },
    fileList: {
      type: Array,
璃白.'s avatar
璃白. 已提交
102
      default: () => []
璃白.'s avatar
璃白. 已提交
103 104
    },
    text: {
璃白.'s avatar
璃白. 已提交
105 106
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
107
    },
108 109 110 111 112 113 114 115
    maxLength: {
      type: [String, Number],
      default: ""
    },
    rows: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
116 117 118 119
    height: {
      type: Number,
      default: 0
    },
璃白.'s avatar
璃白. 已提交
120 121
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
122
      default: () => {}
璃白.'s avatar
璃白. 已提交
123 124 125 126 127 128 129
    },
    formatType: {
      default: ""
    },
    showHelp: {
      type: Boolean,
      default: false
璃白.'s avatar
璃白. 已提交
130 131
    },
    userList: {
璃白.'s avatar
璃白. 已提交
132 133
      type: [Boolean, Array],
      default: false
璃白.'s avatar
璃白. 已提交
134 135
    }
  },
136

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

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

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

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