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

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

          if (index === 0) {
璃白.'s avatar
璃白. 已提交
266
            document
璃白.'s avatar
璃白. 已提交
267 268 269 270 271 272 273
              .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
璃白. 已提交
274 275
            // .scrollIntoView({ behavior: "smooth" });
          }
璃白.'s avatar
璃白. 已提交
276 277 278 279 280
        } else {
          this.activeUserIndex--;
          if (this.activeUserIndex < 0) {
            this.activeUserIndex = max - 1;
          }
璃白.'s avatar
璃白. 已提交
281
          const index = this.activeUserIndex;
璃白.'s avatar
璃白. 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
          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
璃白. 已提交
296
          // .scrollIntoView({ behavior: "smooth" });
璃白.'s avatar
璃白. 已提交
297
        }
璃白.'s avatar
璃白. 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311
      } else if (this.showSelectLinkType) {
        event.preventDefault();
        const max = 3;
        if (type === "down") {
          this.activeLinkTypeIndex++;
          if (this.activeLinkTypeIndex >= max) {
            this.activeLinkTypeIndex = 0;
          }
        } else {
          this.activeLinkTypeIndex--;
          if (this.activeLinkTypeIndex < 0) {
            this.activeLinkTypeIndex = max - 1;
          }
        }
璃白.'s avatar
璃白. 已提交
312 313
      }
    },
璃白.'s avatar
fix  
璃白. 已提交
314 315 316 317 318 319 320
    resetPreviewMinHeight() {
      setTimeout(() => {
        const textEl = document.getElementById(this.id);
        if (!textEl) return;
        const height = textEl.offsetHeight;
        this.$emit("update:htmlMinHeight", height);
      }, 0);
璃白.'s avatar
fix  
璃白. 已提交
321
    },
璃白.'s avatar
璃白. 已提交
322 323 324 325 326 327 328
    resetQueryInfo() {
      this.queryInfo = {
        startPosition: "",
        endPosition: "",
        keyWord: ""
      };
    },
璃白.'s avatar
璃白. 已提交
329
    input(e) {
璃白.'s avatar
璃白. 已提交
330 331
      if (isAndroid() && e.data === "@") {
        this.createSelectUserDialog("android");
璃白.'s avatar
璃白. 已提交
332
      }
璃白.'s avatar
璃白. 已提交
333
      if (this.showSelectUser) this.handleQueryUser(e);
璃白.'s avatar
璃白. 已提交
334 335 336
      if (this.showSelectLinkType) {
        this.showSelectLinkType = false;
      }
337 338 339
      this.$emit("update:textLength", this.textContent.length);
      this.emitText();
    },
璃白.'s avatar
璃白. 已提交
340
    blur() {
璃白.'s avatar
璃白. 已提交
341
      this.renderUserTags();
璃白.'s avatar
璃白. 已提交
342 343
      this.setFocus(false);
    },
璃白.'s avatar
璃白. 已提交
344
    createHideEl(type) {
345 346 347 348 349 350 351 352 353
      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
璃白. 已提交
354
      const hideElId = type + this.id;
355 356 357 358 359 360 361 362 363
      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
璃白. 已提交
364 365 366 367 368 369 370 371
      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_");
372 373
      hideEl.innerText = this.textContent;
      const contentHeight = hideEl.offsetHeight;
374 375
      this.editorHeight = this.fullScreen
        ? "calc(100% - 42px)"
璃白.'s avatar
璃白. 已提交
376 377
        : this.height
        ? this.height + "px"
378 379 380 381
        : this.autoSize
        ? `${contentHeight + parseFloat(fontSize) * 1.2}px`
        : "auto";
      this.editorOverFlow =
璃白.'s avatar
fix  
璃白. 已提交
382
        this.autoSize && !this.fullScreen && !this.height ? "hidden" : "auto";
383 384
      textEl.parentNode.removeChild(hideEl);
    },
璃白.'s avatar
璃白. 已提交
385 386 387 388 389 390
    handlePointMove() {
      if (this.showSelectLinkType) {
        this.showSelectLinkType = false;
        return;
      }
    },
璃白.'s avatar
璃白. 已提交
391
    handleEnter(e) {
璃白.'s avatar
璃白. 已提交
392
      if (this.showSelectUser) {
璃白.'s avatar
璃白. 已提交
393 394 395
        const activeUser = this.userList[this.activeUserIndex];
        this.handleSelectUser(activeUser);
        e.preventDefault();
璃白.'s avatar
璃白. 已提交
396
        return;
璃白.'s avatar
璃白. 已提交
397 398 399 400
      } else if (this.showSelectLinkType) {
        this.handleSelectLinkType(this.activeLinkTypeIndex);
        e.preventDefault();
        return;
璃白.'s avatar
璃白. 已提交
401 402 403
      }
      this.$emit("enter");
    },
璃白.'s avatar
璃白. 已提交
404 405 406
    submit() {
      this.$emit("submit");
    },
璃白.'s avatar
璃白. 已提交
407
    setFocus(val) {
璃白.'s avatar
璃白. 已提交
408
      this.$emit("update:isFocus", val);
璃白.'s avatar
璃白. 已提交
409
    },
璃白.'s avatar
璃白. 已提交
410 411
    checkSelection() {
      const info = getSelectionInfo(this.id);
璃白.'s avatar
璃白. 已提交
412 413
      if (!info) {
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
414
        this.$emit("update:selectionInfo", {
璃白.'s avatar
璃白. 已提交
415 416 417
          selectorId: this.id,
          selectionStart: cursorPoint,
          selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
418
        });
璃白.'s avatar
璃白. 已提交
419 420
        return;
      }
璃白.'s avatar
璃白. 已提交
421
      this.$emit("update:selectionInfo", info);
G
guoweijia 已提交
422 423 424 425 426 427 428 429 430
    },
    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;
        }
璃白.'s avatar
璃白. 已提交
431
        if (items[i].type.indexOf("text") !== -1) {
璃白.'s avatar
璃白. 已提交
432
          if (!this.renderLinks) return;
璃白.'s avatar
璃白. 已提交
433 434 435 436 437 438 439 440 441 442 443 444
          items[i].getAsString(str => {
            if (
              !/^((?:ftp|https?):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/.test(
                str
              )
            )
              return;
            this.createSelectLinkTypeDialog();
          });

          break;
        }
G
guoweijia 已提交
445 446
      }
      if (!fileList.length) return;
璃白.'s avatar
璃白. 已提交
447
      this.checkSelection();
璃白.'s avatar
璃白. 已提交
448
      this.$emit("update:fileList", fileList);
璃白.'s avatar
璃白. 已提交
449 450 451 452 453 454 455
    }
  }
};
</script>
<style lang="less" scoped>
.md_textarea {
  position: relative;
456
  padding: 14px 0;
璃白.'s avatar
璃白. 已提交
457
  background: var(--md-editor-content-bg-color);
458 459
  // border-left: 1px solid var(--md-editor-border-color);
  // border-right: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
460
  transition: border 0.3s;
461
  // padding: 14px;
璃白.'s avatar
璃白. 已提交
462
  box-sizing: border-box;
463 464 465 466
  // &.isFocus {
  //   border-left: 1px solid var(--md-editor-border-color-active);
  //   border-right: 1px solid var(--md-editor-border-color-active);
  // }
467

璃白.'s avatar
璃白. 已提交
468
  &.fullScreen {
469
    height: 100%;
璃白.'s avatar
璃白. 已提交
470 471
    textarea {
      font-size: 20px;
472 473
      max-height: 100%;
      overflow-y: auto;
璃白.'s avatar
璃白. 已提交
474 475
    }
  }
璃白.'s avatar
璃白. 已提交
476 477 478
  &.disabled {
    background: var(--md-editor-content-bg-color-disabled);
  }
479

璃白.'s avatar
璃白. 已提交
480 481 482 483 484
  textarea {
    display: block;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
485
    background: var(--md-editor-content-bg-color);
486 487
    color: var(--md-editor-text-color-active);
    height: var(--md-editor-height);
璃白.'s avatar
璃白. 已提交
488
    resize: none;
璃白.'s avatar
璃白. 已提交
489
    font-size: 14px;
璃白.'s avatar
璃白. 已提交
490
    line-height: 1.625;
璃白.'s avatar
璃白. 已提交
491
    word-break: break-all;
璃白.'s avatar
璃白. 已提交
492 493 494
    font-family: "Menlo", -apple-system, SF UI Text, Arial, PingFang SC,
      Hiragino Sans GB, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif, SimHei,
      SimSun;
495 496 497
    &::placeholder {
      color: var(--md-editor-text-color);
    }
璃白.'s avatar
璃白. 已提交
498 499 500
    &.disabled {
      opacity: var(--md-editor-disabled-opacity);
    }
璃白.'s avatar
璃白. 已提交
501 502 503
    // &:disabled {
    //   background: var(--md-editor-content-bg-color-disabled);
    // }
璃白.'s avatar
璃白. 已提交
504 505 506 507 508 509 510 511 512 513
  }
  .icon {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 32px;
    cursor: pointer;
  }
}
</style>