md-textarea.vue 3.0 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1 2 3 4
<template>
  <div :class="['md_textarea', { fullScreen }]">
    <textarea
      :id="id"
璃白.'s avatar
璃白. 已提交
5
      @change="$emit('update:text', textContent)"
璃白.'s avatar
璃白. 已提交
6 7
      @focus="setFocus(true)"
      @blur="setFocus(false)"
G
guoweijia 已提交
8
      @paste="pasteFile"
璃白.'s avatar
璃白. 已提交
9
      v-model="textContent"
璃白.'s avatar
璃白. 已提交
10
      :placeholder="placeholder"
璃白.'s avatar
璃白. 已提交
11 12 13 14
      rows="10"
    >
    </textarea>
    <span
璃白.'s avatar
璃白. 已提交
15
      @click="$emit('update:fullScreen', false)"
璃白.'s avatar
璃白. 已提交
16 17 18 19 20 21
      v-if="fullScreen"
      class="icon iconfont icon-quxiaoquanping_o"
    ></span>
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
22
import { getSelectionInfo, getPosition } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
23
export default {
璃白.'s avatar
璃白. 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  props: {
    fullScreen: {
      type: Boolean,
      default: false
    },
    isFocus: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: false
    },
    fileList: {
      type: Array,
璃白.'s avatar
璃白. 已提交
39
      default: () => []
璃白.'s avatar
璃白. 已提交
40 41
    },
    text: {
璃白.'s avatar
璃白. 已提交
42 43
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
44 45 46
    },
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
47
      default: () => {}
璃白.'s avatar
璃白. 已提交
48 49
    }
  },
璃白.'s avatar
璃白. 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  data() {
    return {
      id: new Date().getTime(),
      textContent: ""
    };
  },
  created() {
    document.addEventListener("mouseup", this.checkSelection);
  },
  watch: {
    text: {
      immediate: true,
      handler: function(val) {
        this.textContent = val;
      }
    }
  },
  beforeDestroy() {
    document.removeEventListener("mouseup", this.checkSelection);
  },
璃白.'s avatar
璃白. 已提交
70

璃白.'s avatar
璃白. 已提交
71
  methods: {
璃白.'s avatar
璃白. 已提交
72
    setFocus(val) {
璃白.'s avatar
璃白. 已提交
73
      this.$emit("update:isFocus", val);
璃白.'s avatar
璃白. 已提交
74
    },
璃白.'s avatar
璃白. 已提交
75 76
    checkSelection() {
      const info = getSelectionInfo(this.id);
璃白.'s avatar
璃白. 已提交
77 78
      if (!info) {
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
79
        this.$emit("update:selectionInfo", {
璃白.'s avatar
璃白. 已提交
80 81 82
          selectorId: this.id,
          selectionStart: cursorPoint,
          selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
83
        });
璃白.'s avatar
璃白. 已提交
84 85
        return;
      }
璃白.'s avatar
璃白. 已提交
86
      this.$emit("update:selectionInfo", info);
G
guoweijia 已提交
87 88 89 90 91 92 93 94 95 96 97
    },
    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
璃白. 已提交
98
      this.$emit("update:fileList", fileList);
璃白.'s avatar
璃白. 已提交
99 100 101 102 103 104 105 106
    }
  }
};
</script>
<style lang="less" scoped>
.md_textarea {
  position: relative;
  padding: 10px 0;
G
guoweijia 已提交
107
  background: #fff;
璃白.'s avatar
璃白. 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  &.fullScreen {
    position: fixed;
    width: 100vw;
    height: 100vh;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 99;
    padding: 40px 60px;
    box-sizing: border-box;
    textarea {
      font-size: 20px;
    }
  }
  textarea {
    display: block;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    color: var(--md-editor-text-color);
    resize: none;
    font-family: "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas",
      "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace;
  }
  .icon {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 32px;
    cursor: pointer;
  }
}
</style>