md-textarea.vue 7.8 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)"
6
      @input="input"
璃白.'s avatar
璃白. 已提交
7 8
      @focus="setFocus(true)"
      @blur="setFocus(false)"
G
guoweijia 已提交
9
      @paste="pasteFile"
璃白.'s avatar
fix  
璃白. 已提交
10
      @keydown.enter="$emit('enter')"
璃白.'s avatar
璃白. 已提交
11 12
      @keydown.meta.enter.exact="submit"
      @keydown.ctrl.enter.exact="submit"
璃白.'s avatar
fix  
璃白. 已提交
13
      @keydown.tab.prevent="$emit('tab')"
璃白.'s avatar
璃白. 已提交
14
      v-model="textContent"
璃白.'s avatar
璃白. 已提交
15
      :placeholder="placeholder"
16 17
      :maxlength="maxLength"
      :rows="rows"
18
      :style="{ height: editorHeight, overflow: editorOverFlow }"
璃白.'s avatar
璃白. 已提交
19 20 21 22 23
    >
    </textarea>
  </div>
</template>
<script>
24 25 26
import {
  getSelectionInfo,
  getPosition,
璃白.'s avatar
璃白. 已提交
27
  getFilteredTags,
28 29
  throttle as throttleFn
} from "@/assets/js/utils";
璃白.'s avatar
fix  
璃白. 已提交
30
import marked from "marked";
璃白.'s avatar
璃白. 已提交
31
export default {
璃白.'s avatar
璃白. 已提交
32
  props: {
33 34 35 36
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
fix  
璃白. 已提交
37 38 39 40 41 42 43
    html: {
      type: String,
      default: ""
    },
    htmlMinHeight: {
      default: ""
    },
璃白.'s avatar
璃白. 已提交
44 45 46 47
    fullScreen: {
      type: Boolean,
      default: false
    },
48 49 50 51
    throttleTime: {
      type: Number,
      default: 1000
    },
璃白.'s avatar
璃白. 已提交
52 53 54 55 56 57 58 59 60 61
    isFocus: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: false
    },
    fileList: {
      type: Array,
璃白.'s avatar
璃白. 已提交
62
      default: () => []
璃白.'s avatar
璃白. 已提交
63 64
    },
    text: {
璃白.'s avatar
璃白. 已提交
65 66
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
67
    },
68 69 70 71 72 73 74 75
    maxLength: {
      type: [String, Number],
      default: ""
    },
    rows: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
76 77 78 79
    height: {
      type: Number,
      default: 0
    },
璃白.'s avatar
璃白. 已提交
80 81
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
82
      default: () => {}
璃白.'s avatar
璃白. 已提交
83 84
    }
  },
85

璃白.'s avatar
璃白. 已提交
86 87
  data() {
    return {
88
      textContent: "",
89 90
      editorHeight: "auto",
      editorOverFlow: "auto"
璃白.'s avatar
璃白. 已提交
91 92 93 94 95
    };
  },
  created() {
    document.addEventListener("mouseup", this.checkSelection);
  },
璃白.'s avatar
fix  
璃白. 已提交
96
  mounted() {
璃白.'s avatar
fix  
璃白. 已提交
97
    this.resetPreviewMinHeight();
璃白.'s avatar
fix  
璃白. 已提交
98
  },
璃白.'s avatar
璃白. 已提交
99
  watch: {
璃白.'s avatar
fix  
璃白. 已提交
100 101 102 103 104 105 106
    isFocus: {
      handler: function(val) {
        if (val) {
          this.resetPreviewMinHeight();
        }
      }
    },
璃白.'s avatar
璃白. 已提交
107 108 109
    text: {
      immediate: true,
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
110
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
111
        this.textContent = val;
璃白.'s avatar
fix  
璃白. 已提交
112
        this.transferMarkdown(val);
璃白.'s avatar
璃白. 已提交
113
      }
114
    },
115 116
    fullScreen: {
      immediate: true,
璃白.'s avatar
璃白. 已提交
117 118 119 120 121 122
      handler: function(val) {
        if (val) {
          document.body.style.overflow = "hidden";
        } else {
          document.body.style.overflow = "auto";
        }
123
        setTimeout(() => {
璃白.'s avatar
fix  
璃白. 已提交
124
          this.reSizeTextareaHeight();
125 126 127
        }, 0);
      }
    },
128 129 130 131 132
    textContent: {
      immediate: true,
      handler: function() {
        setTimeout(() => {
          if (!this.autoSize) return;
璃白.'s avatar
fix  
璃白. 已提交
133
          this.reSizeTextareaHeight();
134 135
        }, 0);
      }
璃白.'s avatar
璃白. 已提交
136 137 138 139 140
    }
  },
  beforeDestroy() {
    document.removeEventListener("mouseup", this.checkSelection);
  },
141 142
  computed: {
    emitText() {
璃白.'s avatar
fix  
璃白. 已提交
143 144
      // return throttleFn(() => {}, this.throttleTime);
      return () => {
145
        this.$emit("update:text", this.textContent);
璃白.'s avatar
fix  
璃白. 已提交
146
      };
147 148 149 150 151
    },
    autoSize() {
      return this.rows === "auto";
    }
  },
璃白.'s avatar
璃白. 已提交
152
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
153 154 155 156 157 158 159
    resetPreviewMinHeight() {
      setTimeout(() => {
        const textEl = document.getElementById(this.id);
        if (!textEl) return;
        const height = textEl.offsetHeight;
        this.$emit("update:htmlMinHeight", height);
      }, 0);
璃白.'s avatar
fix  
璃白. 已提交
160 161 162
    },
    transferMarkdown(val) {
      marked.setOptions({
163 164 165
        breaks: true,
        gfm: true,
        langPrefix: "language-",
璃白.'s avatar
fix  
璃白. 已提交
166
        highlight: function(code, lang, callback) {
璃白.'s avatar
璃白. 已提交
167
          let html = require("highlight.js").highlightAuto(code).value;
璃白.'s avatar
fix  
璃白. 已提交
168 169 170 171 172 173
          return html;
        }
      });
      const str = val + "";
      // if (!str.trim()) return;
      const html = marked(str);
174 175 176 177
      const virtualDom = document.createElement("div");
      virtualDom.innerHTML = html;
      virtualDom.querySelectorAll("code").forEach(item => {
        if (!/language-/.test(item.className)) {
璃白.'s avatar
fix  
璃白. 已提交
178
          item.className = "language-javascript";
179 180 181 182 183 184
        }
      });
      const DOMPurify = require("dompurify");
      const cleanHtml = DOMPurify.sanitize(virtualDom.innerHTML, {
        FORBID_TAGS: ["style", "script"]
      });
璃白.'s avatar
璃白. 已提交
185 186 187 188 189
      // console.log(html.length);
      // console.log(cleanHtml.length);

      const filteredTags = getFilteredTags(html, cleanHtml);
      this.$emit("getFilteredTags", filteredTags);
190
      this.$emit("update:html", cleanHtml);
璃白.'s avatar
fix  
璃白. 已提交
191
    },
192 193 194 195
    input() {
      this.$emit("update:textLength", this.textContent.length);
      this.emitText();
    },
璃白.'s avatar
fix  
璃白. 已提交
196
    reSizeTextareaHeight() {
璃白.'s avatar
璃白. 已提交
197 198
      console.log("setHeight");

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      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"
      );
      const hideElId = "hdieEl" + this.id;
      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;
      hideEl.innerText = this.textContent;
      const contentHeight = hideEl.offsetHeight;
222 223
      this.editorHeight = this.fullScreen
        ? "calc(100% - 42px)"
璃白.'s avatar
璃白. 已提交
224 225
        : this.height
        ? this.height + "px"
226 227 228 229
        : this.autoSize
        ? `${contentHeight + parseFloat(fontSize) * 1.2}px`
        : "auto";
      this.editorOverFlow =
璃白.'s avatar
fix  
璃白. 已提交
230
        this.autoSize && !this.fullScreen && !this.height ? "hidden" : "auto";
231 232
      textEl.parentNode.removeChild(hideEl);
    },
璃白.'s avatar
璃白. 已提交
233 234 235
    submit() {
      this.$emit("submit");
    },
璃白.'s avatar
璃白. 已提交
236
    setFocus(val) {
璃白.'s avatar
璃白. 已提交
237
      this.$emit("update:isFocus", val);
璃白.'s avatar
璃白. 已提交
238
    },
璃白.'s avatar
璃白. 已提交
239 240
    checkSelection() {
      const info = getSelectionInfo(this.id);
璃白.'s avatar
璃白. 已提交
241 242
      if (!info) {
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
243
        this.$emit("update:selectionInfo", {
璃白.'s avatar
璃白. 已提交
244 245 246
          selectorId: this.id,
          selectionStart: cursorPoint,
          selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
247
        });
璃白.'s avatar
璃白. 已提交
248 249
        return;
      }
璃白.'s avatar
璃白. 已提交
250
      this.$emit("update:selectionInfo", info);
G
guoweijia 已提交
251 252 253 254 255 256 257 258 259 260 261
    },
    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
璃白. 已提交
262
      this.$emit("update:fileList", fileList);
璃白.'s avatar
璃白. 已提交
263 264 265 266 267 268 269
    }
  }
};
</script>
<style lang="less" scoped>
.md_textarea {
  position: relative;
270
  padding: 14px 0;
璃白.'s avatar
璃白. 已提交
271
  background: var(--md-editor-content-bg-color);
272 273
  // border-left: 1px solid var(--md-editor-border-color);
  // border-right: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
274
  transition: border 0.3s;
275
  // padding: 14px;
璃白.'s avatar
璃白. 已提交
276
  box-sizing: border-box;
277 278 279 280
  // &.isFocus {
  //   border-left: 1px solid var(--md-editor-border-color-active);
  //   border-right: 1px solid var(--md-editor-border-color-active);
  // }
281

璃白.'s avatar
璃白. 已提交
282
  &.fullScreen {
283
    height: 100%;
璃白.'s avatar
璃白. 已提交
284 285
    textarea {
      font-size: 20px;
286 287
      max-height: 100%;
      overflow-y: auto;
璃白.'s avatar
璃白. 已提交
288 289
    }
  }
290

璃白.'s avatar
璃白. 已提交
291 292 293 294 295
  textarea {
    display: block;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
296
    background: var(--md-editor-content-bg-color);
297 298
    color: var(--md-editor-text-color-active);
    height: var(--md-editor-height);
璃白.'s avatar
璃白. 已提交
299
    resize: none;
璃白.'s avatar
璃白. 已提交
300 301 302
    font-family: "Menlo", -apple-system, SF UI Text, Arial, PingFang SC,
      Hiragino Sans GB, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif, SimHei,
      SimSun;
303 304 305
    &::placeholder {
      color: var(--md-editor-text-color);
    }
璃白.'s avatar
璃白. 已提交
306 307 308 309 310 311 312 313 314 315
  }
  .icon {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 32px;
    cursor: pointer;
  }
}
</style>