md-textarea.vue 11.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
      @focus="setFocus(true)"
      @blur="setFocus(false)"
G
guoweijia 已提交
10
      @paste="pasteFile"
璃白.'s avatar
璃白. 已提交
11 12
      @keydown.stop.up="changeActiveUserIndex($event, 'up')"
      @keydown.stop.down="changeActiveUserIndex($event, 'down')"
璃白.'s avatar
璃白. 已提交
13
      @keydown.enter="handleEnter"
璃白.'s avatar
璃白. 已提交
14 15
      @keydown.meta.enter.exact="submit"
      @keydown.ctrl.enter.exact="submit"
璃白.'s avatar
fix  
璃白. 已提交
16
      @keydown.tab.prevent="$emit('tab')"
璃白.'s avatar
璃白. 已提交
17
      v-model="textContent"
璃白.'s avatar
璃白. 已提交
18
      :placeholder="placeholder"
19 20
      :maxlength="maxLength"
      :rows="rows"
璃白.'s avatar
璃白. 已提交
21
      :disabled="disabled"
璃白.'s avatar
璃白. 已提交
22
      :class="{ 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
    },
    userList: {
璃白.'s avatar
璃白. 已提交
130 131
      type: [Boolean, Array],
      default: false
璃白.'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
璃白. 已提交
157 158 159 160 161 162
    // userList: {
    //   handler: function(val) {
    //     if (!val.length) return;
    //     this.activeUserIndex = 0;
    //   }
    // },
璃白.'s avatar
fix  
璃白. 已提交
163 164 165 166
    isFocus: {
      handler: function(val) {
        if (val) {
          this.resetPreviewMinHeight();
璃白.'s avatar
璃白. 已提交
167
        } else {
璃白.'s avatar
璃白. 已提交
168
          setTimeout(() => {
璃白.'s avatar
璃白. 已提交
169
            this.showSelectUser = false;
璃白.'s avatar
璃白. 已提交
170
          }, 200);
璃白.'s avatar
fix  
璃白. 已提交
171 172 173
        }
      }
    },
璃白.'s avatar
璃白. 已提交
174 175 176 177
    text: {
      immediate: true,
      handler: function(val) {
        this.textContent = val;
璃白.'s avatar
fix  
璃白. 已提交
178
        this.transferMarkdown(val);
璃白.'s avatar
璃白. 已提交
179
      }
180
    },
181 182
    fullScreen: {
      immediate: true,
璃白.'s avatar
璃白. 已提交
183 184 185 186 187 188
      handler: function(val) {
        if (val) {
          document.body.style.overflow = "hidden";
        } else {
          document.body.style.overflow = "auto";
        }
189
        setTimeout(() => {
璃白.'s avatar
fix  
璃白. 已提交
190
          this.reSizeTextareaHeight();
191 192 193
        }, 0);
      }
    },
194 195 196 197 198
    textContent: {
      immediate: true,
      handler: function() {
        setTimeout(() => {
          if (!this.autoSize) return;
璃白.'s avatar
fix  
璃白. 已提交
199
          this.reSizeTextareaHeight();
200 201
        }, 0);
      }
璃白.'s avatar
璃白. 已提交
202 203 204 205 206 207 208
    },
    showSelectUser: {
      handler: function(val) {
        if (!val) {
          this.resetQueryInfo();
        }
      }
璃白.'s avatar
璃白. 已提交
209 210 211 212 213
    }
  },
  beforeDestroy() {
    document.removeEventListener("mouseup", this.checkSelection);
  },
214 215
  computed: {
    emitText() {
璃白.'s avatar
fix  
璃白. 已提交
216 217
      // return throttleFn(() => {}, this.throttleTime);
      return () => {
218
        this.$emit("update:text", this.textContent);
璃白.'s avatar
fix  
璃白. 已提交
219
      };
220 221 222 223 224
    },
    autoSize() {
      return this.rows === "auto";
    }
  },
璃白.'s avatar
璃白. 已提交
225
  methods: {
璃白.'s avatar
璃白. 已提交
226
    changeActiveUserIndex(event, type) {
璃白.'s avatar
璃白. 已提交
227
      if (this.showSelectUser) {
璃白.'s avatar
璃白. 已提交
228
        event.preventDefault();
璃白.'s avatar
璃白. 已提交
229 230 231 232 233 234
        const max = this.userList.length;
        if (type === "down") {
          this.activeUserIndex++;
          if (this.activeUserIndex >= max) {
            this.activeUserIndex = 0;
          }
璃白.'s avatar
璃白. 已提交
235
          const index = this.activeUserIndex;
璃白.'s avatar
璃白. 已提交
236 237 238 239 240
          const activeItem = document.getElementById("md_user_id_" + index);
          const activeTop =
            activeItem.offsetTop - activeItem.parentNode.scrollTop;

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

璃白.'s avatar
璃白. 已提交
398
  &.fullScreen {
399
    height: 100%;
璃白.'s avatar
璃白. 已提交
400 401
    textarea {
      font-size: 20px;
402 403
      max-height: 100%;
      overflow-y: auto;
璃白.'s avatar
璃白. 已提交
404 405
    }
  }
璃白.'s avatar
璃白. 已提交
406 407 408
  &.disabled {
    background: var(--md-editor-content-bg-color-disabled);
  }
409

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