md-textarea.vue 8.4 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"
璃白.'s avatar
璃白. 已提交
18 19 20 21 22 23 24
      :style="{
        height: editorHeight,
        overflow: editorOverFlow,
        cursor: formatType
          ? `url(https://codechina.csdn.net/codechina/operation-work/uploads/a1b7c2a995b2320dca911e2f2ecb9b88/format.png),text`
          : 'text'
      }"
璃白.'s avatar
璃白. 已提交
25 26
    >
    </textarea>
璃白.'s avatar
璃白. 已提交
27 28 29 30 31 32 33
    <transition name="slide-fade">
      <helpDoc
        v-if="showHelp"
        @updateShowHelp="$emit('updateShowHelp', $event)"
        :showHelp.sync="showHelp"
      />
    </transition>
璃白.'s avatar
璃白. 已提交
34 35 36
  </div>
</template>
<script>
37 38 39
import {
  getSelectionInfo,
  getPosition,
璃白.'s avatar
璃白. 已提交
40
  getFilteredTags,
41 42
  throttle as throttleFn
} from "@/assets/js/utils";
璃白.'s avatar
fix  
璃白. 已提交
43
import marked from "marked";
璃白.'s avatar
璃白. 已提交
44
import helpDoc from "./help-doc";
璃白.'s avatar
璃白. 已提交
45
export default {
璃白.'s avatar
璃白. 已提交
46
  components: { helpDoc },
璃白.'s avatar
璃白. 已提交
47
  props: {
48 49 50 51
    id: {
      type: String,
      default: ""
    },
璃白.'s avatar
fix  
璃白. 已提交
52 53 54 55 56 57 58
    html: {
      type: String,
      default: ""
    },
    htmlMinHeight: {
      default: ""
    },
璃白.'s avatar
璃白. 已提交
59 60 61 62
    fullScreen: {
      type: Boolean,
      default: false
    },
63 64 65 66
    throttleTime: {
      type: Number,
      default: 1000
    },
璃白.'s avatar
璃白. 已提交
67 68 69 70 71 72 73 74 75 76
    isFocus: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: false
    },
    fileList: {
      type: Array,
璃白.'s avatar
璃白. 已提交
77
      default: () => []
璃白.'s avatar
璃白. 已提交
78 79
    },
    text: {
璃白.'s avatar
璃白. 已提交
80 81
      type: [String, Number],
      default: ""
璃白.'s avatar
璃白. 已提交
82
    },
83 84 85 86 87 88 89 90
    maxLength: {
      type: [String, Number],
      default: ""
    },
    rows: {
      type: [String, Number],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
91 92 93 94
    height: {
      type: Number,
      default: 0
    },
璃白.'s avatar
璃白. 已提交
95 96
    selectionInfo: {
      type: Object,
璃白.'s avatar
璃白. 已提交
97
      default: () => {}
璃白.'s avatar
璃白. 已提交
98 99 100 101 102 103 104
    },
    formatType: {
      default: ""
    },
    showHelp: {
      type: Boolean,
      default: false
璃白.'s avatar
璃白. 已提交
105 106
    }
  },
107

璃白.'s avatar
璃白. 已提交
108 109
  data() {
    return {
110
      textContent: "",
111 112
      editorHeight: "auto",
      editorOverFlow: "auto"
璃白.'s avatar
璃白. 已提交
113 114 115 116 117
    };
  },
  created() {
    document.addEventListener("mouseup", this.checkSelection);
  },
璃白.'s avatar
fix  
璃白. 已提交
118
  mounted() {
璃白.'s avatar
fix  
璃白. 已提交
119
    this.resetPreviewMinHeight();
璃白.'s avatar
fix  
璃白. 已提交
120
  },
璃白.'s avatar
璃白. 已提交
121
  watch: {
璃白.'s avatar
fix  
璃白. 已提交
122 123 124 125 126 127 128
    isFocus: {
      handler: function(val) {
        if (val) {
          this.resetPreviewMinHeight();
        }
      }
    },
璃白.'s avatar
璃白. 已提交
129 130 131
    text: {
      immediate: true,
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
132
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
133
        this.textContent = val;
璃白.'s avatar
fix  
璃白. 已提交
134
        this.transferMarkdown(val);
璃白.'s avatar
璃白. 已提交
135
      }
136
    },
137 138
    fullScreen: {
      immediate: true,
璃白.'s avatar
璃白. 已提交
139 140 141 142 143 144
      handler: function(val) {
        if (val) {
          document.body.style.overflow = "hidden";
        } else {
          document.body.style.overflow = "auto";
        }
145
        setTimeout(() => {
璃白.'s avatar
fix  
璃白. 已提交
146
          this.reSizeTextareaHeight();
147 148 149
        }, 0);
      }
    },
150 151 152 153 154
    textContent: {
      immediate: true,
      handler: function() {
        setTimeout(() => {
          if (!this.autoSize) return;
璃白.'s avatar
fix  
璃白. 已提交
155
          this.reSizeTextareaHeight();
156 157
        }, 0);
      }
璃白.'s avatar
璃白. 已提交
158 159 160 161 162
    }
  },
  beforeDestroy() {
    document.removeEventListener("mouseup", this.checkSelection);
  },
163
  computed: {
璃白.'s avatar
璃白. 已提交
164 165 166 167
    formatIcon() {
      return import("@/assets/img/icon-format.png");
      return require("@/assets/img/icon-format.png");
    },
168
    emitText() {
璃白.'s avatar
fix  
璃白. 已提交
169 170
      // return throttleFn(() => {}, this.throttleTime);
      return () => {
171
        this.$emit("update:text", this.textContent);
璃白.'s avatar
fix  
璃白. 已提交
172
      };
173 174 175 176 177
    },
    autoSize() {
      return this.rows === "auto";
    }
  },
璃白.'s avatar
璃白. 已提交
178
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
179 180 181 182 183 184 185
    resetPreviewMinHeight() {
      setTimeout(() => {
        const textEl = document.getElementById(this.id);
        if (!textEl) return;
        const height = textEl.offsetHeight;
        this.$emit("update:htmlMinHeight", height);
      }, 0);
璃白.'s avatar
fix  
璃白. 已提交
186 187 188
    },
    transferMarkdown(val) {
      marked.setOptions({
189 190 191
        breaks: true,
        gfm: true,
        langPrefix: "language-",
璃白.'s avatar
fix  
璃白. 已提交
192
        highlight: function(code, lang, callback) {
璃白.'s avatar
璃白. 已提交
193
          let html = require("highlight.js").highlightAuto(code).value;
璃白.'s avatar
fix  
璃白. 已提交
194 195 196 197 198 199
          return html;
        }
      });
      const str = val + "";
      // if (!str.trim()) return;
      const html = marked(str);
200 201 202 203
      const virtualDom = document.createElement("div");
      virtualDom.innerHTML = html;
      virtualDom.querySelectorAll("code").forEach(item => {
        if (!/language-/.test(item.className)) {
璃白.'s avatar
fix  
璃白. 已提交
204
          item.className = "language-javascript";
205 206 207 208 209 210
        }
      });
      const DOMPurify = require("dompurify");
      const cleanHtml = DOMPurify.sanitize(virtualDom.innerHTML, {
        FORBID_TAGS: ["style", "script"]
      });
璃白.'s avatar
璃白. 已提交
211 212 213 214 215
      // console.log(html.length);
      // console.log(cleanHtml.length);

      const filteredTags = getFilteredTags(html, cleanHtml);
      this.$emit("getFilteredTags", filteredTags);
216
      this.$emit("update:html", cleanHtml);
璃白.'s avatar
fix  
璃白. 已提交
217
    },
218 219 220 221
    input() {
      this.$emit("update:textLength", this.textContent.length);
      this.emitText();
    },
璃白.'s avatar
fix  
璃白. 已提交
222
    reSizeTextareaHeight() {
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
      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;
246 247
      this.editorHeight = this.fullScreen
        ? "calc(100% - 42px)"
璃白.'s avatar
璃白. 已提交
248 249
        : this.height
        ? this.height + "px"
250 251 252 253
        : this.autoSize
        ? `${contentHeight + parseFloat(fontSize) * 1.2}px`
        : "auto";
      this.editorOverFlow =
璃白.'s avatar
fix  
璃白. 已提交
254
        this.autoSize && !this.fullScreen && !this.height ? "hidden" : "auto";
255 256
      textEl.parentNode.removeChild(hideEl);
    },
璃白.'s avatar
璃白. 已提交
257 258 259
    submit() {
      this.$emit("submit");
    },
璃白.'s avatar
璃白. 已提交
260
    setFocus(val) {
璃白.'s avatar
璃白. 已提交
261
      this.$emit("update:isFocus", val);
璃白.'s avatar
璃白. 已提交
262
    },
璃白.'s avatar
璃白. 已提交
263 264
    checkSelection() {
      const info = getSelectionInfo(this.id);
璃白.'s avatar
璃白. 已提交
265 266
      if (!info) {
        const cursorPoint = getPosition(this.id);
璃白.'s avatar
璃白. 已提交
267
        this.$emit("update:selectionInfo", {
璃白.'s avatar
璃白. 已提交
268 269 270
          selectorId: this.id,
          selectionStart: cursorPoint,
          selectionEnd: cursorPoint
璃白.'s avatar
璃白. 已提交
271
        });
璃白.'s avatar
璃白. 已提交
272 273
        return;
      }
璃白.'s avatar
璃白. 已提交
274
      this.$emit("update:selectionInfo", info);
G
guoweijia 已提交
275 276 277 278 279 280 281 282 283 284 285
    },
    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
璃白. 已提交
286
      this.$emit("update:fileList", fileList);
璃白.'s avatar
璃白. 已提交
287 288 289 290 291 292 293
    }
  }
};
</script>
<style lang="less" scoped>
.md_textarea {
  position: relative;
294
  padding: 14px 0;
璃白.'s avatar
璃白. 已提交
295
  background: var(--md-editor-content-bg-color);
296 297
  // border-left: 1px solid var(--md-editor-border-color);
  // border-right: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
298
  transition: border 0.3s;
299
  // padding: 14px;
璃白.'s avatar
璃白. 已提交
300
  box-sizing: border-box;
301 302 303 304
  // &.isFocus {
  //   border-left: 1px solid var(--md-editor-border-color-active);
  //   border-right: 1px solid var(--md-editor-border-color-active);
  // }
305

璃白.'s avatar
璃白. 已提交
306
  &.fullScreen {
307
    height: 100%;
璃白.'s avatar
璃白. 已提交
308 309
    textarea {
      font-size: 20px;
310 311
      max-height: 100%;
      overflow-y: auto;
璃白.'s avatar
璃白. 已提交
312 313
    }
  }
314

璃白.'s avatar
璃白. 已提交
315 316 317 318 319
  textarea {
    display: block;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
320
    background: var(--md-editor-content-bg-color);
321 322
    color: var(--md-editor-text-color-active);
    height: var(--md-editor-height);
璃白.'s avatar
璃白. 已提交
323
    resize: none;
璃白.'s avatar
璃白. 已提交
324 325 326
    font-family: "Menlo", -apple-system, SF UI Text, Arial, PingFang SC,
      Hiragino Sans GB, Microsoft YaHei, WenQuanYi Micro Hei, sans-serif, SimHei,
      SimSun;
327 328 329
    &::placeholder {
      color: var(--md-editor-text-color);
    }
璃白.'s avatar
璃白. 已提交
330 331 332 333 334 335 336 337 338 339
  }
  .icon {
    position: absolute;
    top: 20px;
    right: 20px;
    font-size: 32px;
    cursor: pointer;
  }
}
</style>