md-textarea.vue 3.5 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
璃白.'s avatar
璃白. 已提交
2
  <div :class="['md_textarea', { fullScreen, isFocus }]">
璃白.'s avatar
璃白. 已提交
3 4
    <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 10
      @keydown.meta.enter.exact="submit"
      @keydown.ctrl.enter.exact="submit"
璃白.'s avatar
璃白. 已提交
11
      v-model="textContent"
璃白.'s avatar
璃白. 已提交
12
      :placeholder="placeholder"
璃白.'s avatar
璃白. 已提交
13 14 15 16
      rows="10"
    >
    </textarea>
    <span
璃白.'s avatar
璃白. 已提交
17
      @click="$emit('update:fullScreen', false)"
璃白.'s avatar
璃白. 已提交
18 19 20 21 22 23
      v-if="fullScreen"
      class="icon iconfont icon-quxiaoquanping_o"
    ></span>
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
24
import { getSelectionInfo, getPosition } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
25
export default {
璃白.'s avatar
璃白. 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  props: {
    fullScreen: {
      type: Boolean,
      default: false
    },
    isFocus: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: false
    },
    fileList: {
      type: Array,
璃白.'s avatar
璃白. 已提交
41
      default: () => []
璃白.'s avatar
璃白. 已提交
42 43
    },
    text: {
璃白.'s avatar
璃白. 已提交
44 45
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
46 47 48
    },
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
49
      default: () => {}
璃白.'s avatar
璃白. 已提交
50 51
    }
  },
璃白.'s avatar
璃白. 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  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
璃白. 已提交
72

璃白.'s avatar
璃白. 已提交
73
  methods: {
璃白.'s avatar
璃白. 已提交
74 75 76
    submit() {
      this.$emit("submit");
    },
璃白.'s avatar
璃白. 已提交
77
    setFocus(val) {
璃白.'s avatar
璃白. 已提交
78
      this.$emit("update:isFocus", val);
璃白.'s avatar
璃白. 已提交
79
    },
璃白.'s avatar
璃白. 已提交
80 81
    checkSelection() {
      const info = getSelectionInfo(this.id);
璃白.'s avatar
璃白. 已提交
82 83
      if (!info) {
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
84
        this.$emit("update:selectionInfo", {
璃白.'s avatar
璃白. 已提交
85 86 87
          selectorId: this.id,
          selectionStart: cursorPoint,
          selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
88
        });
璃白.'s avatar
璃白. 已提交
89 90
        return;
      }
璃白.'s avatar
璃白. 已提交
91
      this.$emit("update:selectionInfo", info);
G
guoweijia 已提交
92 93 94 95 96 97 98 99 100 101 102
    },
    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
璃白. 已提交
103
      this.$emit("update:fileList", fileList);
璃白.'s avatar
璃白. 已提交
104 105 106 107 108 109 110 111
    }
  }
};
</script>
<style lang="less" scoped>
.md_textarea {
  position: relative;
  padding: 10px 0;
璃白.'s avatar
璃白. 已提交
112 113 114 115 116 117 118 119 120 121
  background: var(--md-editor-content-bg-color);
  border-left: 1px solid var(--md-editor-border-color);
  border-right: 1px solid var(--md-editor-border-color);
  transition: border 0.3s;
  padding: 14px;
  box-sizing: border-box;
  &.isFocus {
    border-left: 1px solid var(--md-editor-border-color-active);
    border-right: 1px solid var(--md-editor-border-color-active);
  }
璃白.'s avatar
璃白. 已提交
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;
璃白.'s avatar
璃白. 已提交
141
    background: var(--md-editor-content-bg-color);
璃白.'s avatar
璃白. 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155
    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>