md-textarea.vue 13.4 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

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

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

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

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

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