App.vue 8.4 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
<template>
2 3 4 5
  <div
    :class="['md_container', { active: isFocus, fullScreen }]"
    :id="'md_' + id"
  >
璃白.'s avatar
璃白. 已提交
6
    <markdown-header
璃白.'s avatar
fix  
璃白. 已提交
7
      :id="textareaId"
璃白.'s avatar
fix  
璃白. 已提交
8
      :ref="'md_header' + id"
璃白.'s avatar
璃白. 已提交
9 10 11 12
      :text.sync="text"
      :selectionInfo.sync="selectionInfo"
      :showPreview.sync="showPreview"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
13 14
      :canPreview="canPreview"
      :toolsOptions="toolsOptions"
15
      :zIndex="zIndex"
璃白.'s avatar
fix  
璃白. 已提交
16
      :tabSize="tabSize"
璃白.'s avatar
璃白. 已提交
17
      :fullScreen.sync="fullScreen"
18
      :themeOptions="themeOptions"
璃白.'s avatar
璃白. 已提交
19 20 21
      @upload="handleUpload"
      @getFormatType="formatType = $event"
      @updateShowHelp="showHelp = $event"
22 23 24 25 26 27 28 29 30
    />
    <input
      ref="mdUploadFile"
      class="md_upload"
      type="file"
      hidden
      name="md-upload-file"
      id="md-upload-file"
      @change="upload"
璃白.'s avatar
璃白. 已提交
31
    />
璃白.'s avatar
璃白. 已提交
32 33 34 35
    <markdownPreview
      :id="textareaId"
      :fullScreen.sync="fullScreen"
      :text="text"
璃白.'s avatar
璃白. 已提交
36
      :height="textareaHeight"
璃白.'s avatar
璃白. 已提交
37
      :html.sync="html"
璃白.'s avatar
fix  
璃白. 已提交
38 39
      :htmlMinHeight="htmlMinHeight"
      v-if="showPreview"
璃白.'s avatar
璃白. 已提交
40
    />
璃白.'s avatar
璃白. 已提交
41 42 43 44 45 46
    <markdown-editor
      :selectionInfo.sync="selectionInfo"
      :text.sync="text"
      :fileList.sync="fileList"
      :placeholder="placeholder"
      :isFocus.sync="isFocus"
47
      :throttleTime="throttle"
璃白.'s avatar
fix  
璃白. 已提交
48
      :htmlMinHeight.sync="htmlMinHeight"
璃白.'s avatar
璃白. 已提交
49
      :fullScreen.sync="fullScreen"
50 51 52
      :maxLength="maxLength"
      :textLength.sync="textLength"
      :rows="rows"
璃白.'s avatar
璃白. 已提交
53
      :height="textareaHeight"
璃白.'s avatar
fix  
璃白. 已提交
54
      :html.sync="html"
55
      :id="textareaId"
璃白.'s avatar
璃白. 已提交
56 57
      :show-help="showHelp"
      :formatType="formatType"
璃白.'s avatar
fix  
璃白. 已提交
58
      :ref="'md_textarea' + id"
璃白.'s avatar
fix  
璃白. 已提交
59
      @tab="$refs['md_header' + id].tab()"
璃白.'s avatar
璃白. 已提交
60
      @submit="submit"
璃白.'s avatar
fix  
璃白. 已提交
61
      @enter="handleEnter"
璃白.'s avatar
璃白. 已提交
62
      @getFilteredTags="filteredTags = $event"
璃白.'s avatar
璃白. 已提交
63
      @updateShowHelp="showHelp = $event"
璃白.'s avatar
fix  
璃白. 已提交
64
      v-else
璃白.'s avatar
璃白. 已提交
65
    />
璃白.'s avatar
璃白. 已提交
66
    <div v-if="maxLength && showWordLimit && !showPreview" class="word_limit">
67 68 69 70
      <span>{{ textLength }}</span
      >/<span>{{ maxLength }}</span>
    </div>
    <!-- <markdown-footer
璃白.'s avatar
璃白. 已提交
71 72 73
      :fileList.sync="fileList"
      :canAttachFile="canAttachFile"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
74 75
      :can-attach-file="canAttachFile"
      v-if="!showPreview && canAttachFile"
76
    /> -->
璃白.'s avatar
璃白. 已提交
77 78 79
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
80 81 82 83
import markdownHeader from "./components/header/md-header";
import markdownFooter from "./components/footer/md-footer";
import markdownEditor from "./components/content/md-textarea";
import markdownPreview from "./components/content/md-preview";
84
import { formatText, checktUrl } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
85
export default {
璃白.'s avatar
璃白. 已提交
86 87 88 89
  components: {
    markdownHeader,
    markdownFooter,
    markdownEditor,
璃白.'s avatar
璃白. 已提交
90
    markdownPreview
璃白.'s avatar
璃白. 已提交
91
  },
璃白.'s avatar
璃白. 已提交
92 93 94 95 96
  props: {
    placeholder: {
      type: String,
      default: "请输入内容"
    },
97 98 99
    id: {
      type: String,
      default: ""
璃白.'s avatar
璃白. 已提交
100
    },
璃白.'s avatar
fix  
璃白. 已提交
101 102 103 104
    tabSize: {
      type: [String, Number],
      default: ""
    },
105 106 107 108 109
    // canAttachFile: {
    //   type: Boolean,
    //   default: true
    // },
    // 初始化时赋值
璃白.'s avatar
璃白. 已提交
110 111
    value: {
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
112
      default: ""
璃白.'s avatar
璃白. 已提交
113
    },
114 115 116 117 118 119
    // 全屏时的z-index
    zIndex: {
      type: [String, Number],
      default: ""
    },
    // input时间节流
120 121
    throttle: {
      type: Number,
122
      default: 0
123
    },
璃白.'s avatar
fix  
璃白. 已提交
124 125 126 127 128 129
    setPreview: {
      type: Boolean,
      default: false
    },
    setFullScreen: {
      // type: Boolean,
璃白.'s avatar
fix  
璃白. 已提交
130 131
      default: ""
    },
132
    // 是否可以预览
璃白.'s avatar
璃白. 已提交
133 134 135 136
    canPreview: {
      type: Boolean,
      default: true
    },
137 138 139 140 141 142 143 144 145 146
    // 主题
    themeOptions: {
      type: Object,
      default: () => {}
    },
    focus: {
      type: Boolean,
      default: false
    },
    // 工具栏
璃白.'s avatar
璃白. 已提交
147 148 149
    toolsOptions: {
      type: Object,
      default: () => {}
150
    },
151
    // 行高度
152 153 154 155
    rows: {
      type: [Number, String],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
156 157 158 159 160
    // 高度
    height: {
      type: Number,
      default: 0
    },
161
    // 最大长度
162 163 164 165
    maxLength: {
      type: [Number, String],
      default: ""
    },
166
    // 显示字数限制
167 168 169 170
    showWordLimit: {
      type: Boolean,
      default: false
    },
171 172
    // 图片路径规则
    filePathRule: {
173
      type: RegExp,
174 175 176 177
      default: () => {}
    }
  },
  computed: {
璃白.'s avatar
璃白. 已提交
178 179 180 181
    // isMobile() {
    //   const isMobile = checkBoswer();
    //   return isMobile;
    // },
182 183
    textareaId() {
      return "textarea_" + this.id;
璃白.'s avatar
璃白. 已提交
184
    },
璃白.'s avatar
fix  
璃白. 已提交
185 186 187
    headId() {
      return "md_header" + this.id;
    },
璃白.'s avatar
璃白. 已提交
188 189 190 191
    textareaHeight() {
      const height = this.height;
      if (!height) return 0;
      return height - 83;
璃白.'s avatar
璃白. 已提交
192
    }
G
guoweijia 已提交
193
  },
璃白.'s avatar
璃白. 已提交
194 195 196 197 198 199
  data() {
    return {
      fullScreen: false,
      isFocus: false,
      showPreview: false,
      fileList: [],
璃白.'s avatar
璃白. 已提交
200
      filteredTags: [],
璃白.'s avatar
璃白. 已提交
201 202
      text: "",
      html: "",
璃白.'s avatar
fix  
璃白. 已提交
203
      ulNum: 1,
璃白.'s avatar
璃白. 已提交
204
      formatType: "",
璃白.'s avatar
fix  
璃白. 已提交
205
      htmlMinHeight: 150,
璃白.'s avatar
璃白. 已提交
206
      showHelp: false,
207
      textLength: "",
璃白.'s avatar
璃白. 已提交
208 209 210 211 212 213 214
      selectionInfo: {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      }
    };
  },
215 216 217 218 219 220 221
  created() {
    setTimeout(() => {
      this.$emit("load", {
        text: this.text,
        html: this.html
      });
    }, 0);
璃白.'s avatar
璃白. 已提交
222 223 224 225

    // if(this.isMobile) {

    // }
226
  },
G
guoweijia 已提交
227
  watch: {
228 229
    focus: {
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        setTimeout(() => {
          const textEl = document.getElementById(this.textareaId);
          if (val) {
            textEl.focus();
          } else {
            textEl.blur();
          }
        });
      }
    },
    showPreview: {
      handler: function(val) {
        this.$emit("changeTab", val);
      }
    },
    setPreview: {
      handler: function(val) {
        this.showPreview = val;
248 249
      }
    },
璃白.'s avatar
fix  
璃白. 已提交
250
    setFullScreen: {
璃白.'s avatar
fix  
璃白. 已提交
251
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
252 253 254 255 256 257
        this.fullScreen = val;
      }
    },
    fullScreen: {
      handler: function(val) {
        this.$emit("changeFullScreen", val);
璃白.'s avatar
fix  
璃白. 已提交
258 259
      }
    },
260 261 262 263 264 265
    text: {
      immediate: true,
      handler: function(val) {
        this.textLength = val.length;
      }
    },
G
guoweijia 已提交
266
    html: {
璃白.'s avatar
fix  
璃白. 已提交
267
      immediate: false,
G
guoweijia 已提交
268
      handler: function(val) {
269
        const emitContent = {
270 271
          text: this.text,
          html: this.html
272
        };
璃白.'s avatar
璃白. 已提交
273

274 275 276
        if (this.filePathRule) {
          const checkResult = checktUrl(val, this.filePathRule);
          emitContent.invalidList = checkResult;
璃白.'s avatar
璃白. 已提交
277
          console.log(checkResult);
278
        }
璃白.'s avatar
璃白. 已提交
279
        emitContent.filteredTags = this.filteredTags;
280 281
        this.$emit("change", emitContent);
        this.$emit("input", emitContent);
G
guoweijia 已提交
282 283
      }
    },
璃白.'s avatar
璃白. 已提交
284 285 286 287 288 289 290
    isFocus: {
      handler: function(val) {
        const value = {
          text: this.text,
          html: this.html
        };
        if (val) {
璃白.'s avatar
璃白. 已提交
291
          this.showHelp = false;
璃白.'s avatar
璃白. 已提交
292 293 294 295 296 297
          this.$emit("focus", value);
        } else {
          this.$emit("blur", value);
        }
      }
    },
璃白.'s avatar
璃白. 已提交
298 299 300 301 302 303
    value: {
      immediate: true,
      handler: function(val) {
        this.text = val;
      }
    },
G
guoweijia 已提交
304 305 306 307
    fileList: {
      immediate: false,
      deep: true,
      handler: function(val) {
璃白.'s avatar
璃白. 已提交
308 309 310 311 312 313 314 315 316 317
        const _this = this;
        if (!val.length) return;
        this.$emit("upload", {
          val: val[0],
          callback: function(url) {
            const originalText = _this.text;
            const selectionInfo = _this.selectionInfo;
            const newText = formatText(
              originalText,
              selectionInfo,
璃白.'s avatar
feat  
璃白. 已提交
318
              "\n![img](",
璃白.'s avatar
璃白. 已提交
319 320 321
              `${url})\n`
            );
            _this.text = newText;
璃白.'s avatar
fix  
璃白. 已提交
322
            _this.$refs.mdUploadFile.value = "";
璃白.'s avatar
璃白. 已提交
323 324 325
          }
        });
        this.fileList = [];
G
guoweijia 已提交
326 327
      }
    }
璃白.'s avatar
璃白. 已提交
328 329
  },
  methods: {
璃白.'s avatar
璃白. 已提交
330 331 332 333
    handleUpload() {
      this.$refs.mdUploadFile.click();
      document.getElementById(this.textareaId).focus();
    },
璃白.'s avatar
fix  
璃白. 已提交
334 335 336
    handleEnter() {
      this.$refs[this.headId].resetUlNum();
    },
337 338 339
    upload(e) {
      this.fileList = Array.from(e.target.files);
    },
璃白.'s avatar
璃白. 已提交
340 341 342 343 344 345
    submit() {
      this.$emit("submit", {
        text: this.text,
        html: this.html
      });
    }
璃白.'s avatar
璃白. 已提交
346 347 348 349 350
  }
};
</script>
<style lang="less" scoped>
.md_container {
璃白.'s avatar
璃白. 已提交
351
  // width: 100%;
璃白.'s avatar
璃白. 已提交
352
  background: var(--md-editor-frame-bg-color);
璃白.'s avatar
璃白. 已提交
353
  // margin: 200px auto;
璃白.'s avatar
璃白. 已提交
354
  border: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
355 356 357
  border-radius: 4px;
  padding: 10px 16px;
  box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
358
  transition: border 0.3s;
359
  position: relative;
璃白.'s avatar
璃白. 已提交
360
  overflow: hidden;
361 362 363 364 365 366 367
  &.fullScreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    margin: 0;
璃白.'s avatar
璃白. 已提交
368
    border: 1px solid transparent !important;
369 370
    z-index: var(--md-editor-fullScrren-zIndex);
  }
璃白.'s avatar
璃白. 已提交
371
  &.active {
璃白.'s avatar
璃白. 已提交
372
    border: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
373
  }
374 375 376 377 378 379 380
  .word_limit {
    position: absolute;
    right: 10px;
    bottom: 8px;
    font-size: 12px;
    color: var(--md-editor-text-color);
  }
璃白.'s avatar
璃白. 已提交
381 382
}
</style>