App.vue 7.9 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"
19 20 21 22 23 24 25 26 27 28
      @upload="$refs.mdUploadFile.click()"
    />
    <input
      ref="mdUploadFile"
      class="md_upload"
      type="file"
      hidden
      name="md-upload-file"
      id="md-upload-file"
      @change="upload"
璃白.'s avatar
璃白. 已提交
29
    />
璃白.'s avatar
璃白. 已提交
30 31 32 33
    <markdownPreview
      :id="textareaId"
      :fullScreen.sync="fullScreen"
      :text="text"
璃白.'s avatar
璃白. 已提交
34
      :height="textareaHeight"
璃白.'s avatar
璃白. 已提交
35
      :html.sync="html"
璃白.'s avatar
fix  
璃白. 已提交
36 37
      :htmlMinHeight="htmlMinHeight"
      v-if="showPreview"
璃白.'s avatar
璃白. 已提交
38
    />
璃白.'s avatar
璃白. 已提交
39 40 41 42 43 44
    <markdown-editor
      :selectionInfo.sync="selectionInfo"
      :text.sync="text"
      :fileList.sync="fileList"
      :placeholder="placeholder"
      :isFocus.sync="isFocus"
45
      :throttleTime="throttle"
璃白.'s avatar
fix  
璃白. 已提交
46
      :htmlMinHeight.sync="htmlMinHeight"
璃白.'s avatar
璃白. 已提交
47
      :fullScreen.sync="fullScreen"
48 49 50
      :maxLength="maxLength"
      :textLength.sync="textLength"
      :rows="rows"
璃白.'s avatar
璃白. 已提交
51
      :height="textareaHeight"
璃白.'s avatar
fix  
璃白. 已提交
52
      :html.sync="html"
53
      :id="textareaId"
璃白.'s avatar
fix  
璃白. 已提交
54
      :ref="'md_textarea' + id"
璃白.'s avatar
fix  
璃白. 已提交
55
      @tab="$refs['md_header' + id].tab()"
璃白.'s avatar
璃白. 已提交
56
      @submit="submit"
璃白.'s avatar
fix  
璃白. 已提交
57
      @enter="handleEnter"
璃白.'s avatar
璃白. 已提交
58
      @getFilteredTags="filteredTags = $event"
璃白.'s avatar
fix  
璃白. 已提交
59
      v-else
璃白.'s avatar
璃白. 已提交
60
    />
璃白.'s avatar
璃白. 已提交
61
    <div v-if="maxLength && showWordLimit && !showPreview" class="word_limit">
62 63 64 65
      <span>{{ textLength }}</span
      >/<span>{{ maxLength }}</span>
    </div>
    <!-- <markdown-footer
璃白.'s avatar
璃白. 已提交
66 67 68
      :fileList.sync="fileList"
      :canAttachFile="canAttachFile"
      :isFocus.sync="isFocus"
璃白.'s avatar
璃白. 已提交
69 70
      :can-attach-file="canAttachFile"
      v-if="!showPreview && canAttachFile"
71
    /> -->
璃白.'s avatar
璃白. 已提交
72
    <tableSelect />
璃白.'s avatar
璃白. 已提交
73 74 75
  </div>
</template>
<script>
璃白.'s avatar
璃白. 已提交
76 77 78 79
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";
璃白.'s avatar
璃白. 已提交
80
import tableSelect from "./components/header/components/table-select";
81
import { formatText, checktUrl } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
82
export default {
璃白.'s avatar
璃白. 已提交
83 84 85 86
  components: {
    markdownHeader,
    markdownFooter,
    markdownEditor,
璃白.'s avatar
璃白. 已提交
87 88
    markdownPreview,
    tableSelect
璃白.'s avatar
璃白. 已提交
89
  },
璃白.'s avatar
璃白. 已提交
90 91 92 93 94
  props: {
    placeholder: {
      type: String,
      default: "请输入内容"
    },
95 96 97
    id: {
      type: String,
      default: ""
璃白.'s avatar
璃白. 已提交
98
    },
璃白.'s avatar
fix  
璃白. 已提交
99 100 101 102
    tabSize: {
      type: [String, Number],
      default: ""
    },
103 104 105 106 107
    // canAttachFile: {
    //   type: Boolean,
    //   default: true
    // },
    // 初始化时赋值
璃白.'s avatar
璃白. 已提交
108 109
    value: {
      type: [String, Number],
璃白.'s avatar
璃白. 已提交
110
      default: ""
璃白.'s avatar
璃白. 已提交
111
    },
112 113 114 115 116 117
    // 全屏时的z-index
    zIndex: {
      type: [String, Number],
      default: ""
    },
    // input时间节流
118 119
    throttle: {
      type: Number,
120
      default: 0
121
    },
璃白.'s avatar
fix  
璃白. 已提交
122 123 124 125 126 127
    setPreview: {
      type: Boolean,
      default: false
    },
    setFullScreen: {
      // type: Boolean,
璃白.'s avatar
fix  
璃白. 已提交
128 129
      default: ""
    },
130
    // 是否可以预览
璃白.'s avatar
璃白. 已提交
131 132 133 134
    canPreview: {
      type: Boolean,
      default: true
    },
135 136 137 138 139 140 141 142 143 144
    // 主题
    themeOptions: {
      type: Object,
      default: () => {}
    },
    focus: {
      type: Boolean,
      default: false
    },
    // 工具栏
璃白.'s avatar
璃白. 已提交
145 146 147
    toolsOptions: {
      type: Object,
      default: () => {}
148
    },
149
    // 行高度
150 151 152 153
    rows: {
      type: [Number, String],
      default: ""
    },
璃白.'s avatar
璃白. 已提交
154 155 156 157 158
    // 高度
    height: {
      type: Number,
      default: 0
    },
159
    // 最大长度
160 161 162 163
    maxLength: {
      type: [Number, String],
      default: ""
    },
164
    // 显示字数限制
165 166 167 168
    showWordLimit: {
      type: Boolean,
      default: false
    },
169 170
    // 图片路径规则
    filePathRule: {
171
      type: RegExp,
172 173 174 175 176 177
      default: () => {}
    }
  },
  computed: {
    textareaId() {
      return "textarea_" + this.id;
璃白.'s avatar
璃白. 已提交
178
    },
璃白.'s avatar
fix  
璃白. 已提交
179 180 181
    headId() {
      return "md_header" + this.id;
    },
璃白.'s avatar
璃白. 已提交
182 183 184 185
    textareaHeight() {
      const height = this.height;
      if (!height) return 0;
      return height - 83;
璃白.'s avatar
璃白. 已提交
186
    }
G
guoweijia 已提交
187
  },
璃白.'s avatar
璃白. 已提交
188 189 190 191 192 193
  data() {
    return {
      fullScreen: false,
      isFocus: false,
      showPreview: false,
      fileList: [],
璃白.'s avatar
璃白. 已提交
194
      filteredTags: [],
璃白.'s avatar
璃白. 已提交
195 196
      text: "",
      html: "",
璃白.'s avatar
fix  
璃白. 已提交
197
      ulNum: 1,
璃白.'s avatar
fix  
璃白. 已提交
198
      htmlMinHeight: 150,
199
      textLength: "",
璃白.'s avatar
璃白. 已提交
200 201 202 203 204 205 206
      selectionInfo: {
        selectorId: "",
        selectionStart: "",
        selectionEnd: ""
      }
    };
  },
207 208 209 210 211 212 213 214
  created() {
    setTimeout(() => {
      this.$emit("load", {
        text: this.text,
        html: this.html
      });
    }, 0);
  },
G
guoweijia 已提交
215
  watch: {
216 217
    focus: {
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        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;
236 237
      }
    },
璃白.'s avatar
fix  
璃白. 已提交
238
    setFullScreen: {
璃白.'s avatar
fix  
璃白. 已提交
239
      handler: function(val) {
璃白.'s avatar
fix  
璃白. 已提交
240 241 242 243 244 245
        this.fullScreen = val;
      }
    },
    fullScreen: {
      handler: function(val) {
        this.$emit("changeFullScreen", val);
璃白.'s avatar
fix  
璃白. 已提交
246 247
      }
    },
248 249 250 251 252 253
    text: {
      immediate: true,
      handler: function(val) {
        this.textLength = val.length;
      }
    },
G
guoweijia 已提交
254
    html: {
璃白.'s avatar
fix  
璃白. 已提交
255
      immediate: false,
G
guoweijia 已提交
256
      handler: function(val) {
257
        const emitContent = {
258 259
          text: this.text,
          html: this.html
260 261 262 263 264
        };
        if (this.filePathRule) {
          const checkResult = checktUrl(val, this.filePathRule);
          emitContent.invalidList = checkResult;
        }
璃白.'s avatar
璃白. 已提交
265
        emitContent.filteredTags = this.filteredTags;
266 267
        this.$emit("change", emitContent);
        this.$emit("input", emitContent);
G
guoweijia 已提交
268 269
      }
    },
璃白.'s avatar
璃白. 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282
    isFocus: {
      handler: function(val) {
        const value = {
          text: this.text,
          html: this.html
        };
        if (val) {
          this.$emit("focus", value);
        } else {
          this.$emit("blur", value);
        }
      }
    },
璃白.'s avatar
璃白. 已提交
283 284 285 286 287 288
    value: {
      immediate: true,
      handler: function(val) {
        this.text = val;
      }
    },
G
guoweijia 已提交
289 290 291 292
    fileList: {
      immediate: false,
      deep: true,
      handler: function(val) {
璃白.'s avatar
璃白. 已提交
293 294 295 296 297 298 299 300 301 302 303 304 305 306
        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,
              "\n\n![img](",
              `${url})\n`
            );
            _this.text = newText;
璃白.'s avatar
fix  
璃白. 已提交
307
            _this.$refs.mdUploadFile.value = "";
璃白.'s avatar
璃白. 已提交
308 309 310
          }
        });
        this.fileList = [];
G
guoweijia 已提交
311 312
      }
    }
璃白.'s avatar
璃白. 已提交
313 314
  },
  methods: {
璃白.'s avatar
fix  
璃白. 已提交
315 316 317
    handleEnter() {
      this.$refs[this.headId].resetUlNum();
    },
318 319 320
    upload(e) {
      this.fileList = Array.from(e.target.files);
    },
璃白.'s avatar
璃白. 已提交
321 322 323 324 325 326
    submit() {
      this.$emit("submit", {
        text: this.text,
        html: this.html
      });
    }
璃白.'s avatar
璃白. 已提交
327 328 329 330 331
  }
};
</script>
<style lang="less" scoped>
.md_container {
璃白.'s avatar
璃白. 已提交
332
  width: 100%;
璃白.'s avatar
璃白. 已提交
333
  background: var(--md-editor-frame-bg-color);
璃白.'s avatar
璃白. 已提交
334
  // margin: 200px auto;
璃白.'s avatar
璃白. 已提交
335
  border: 1px solid var(--md-editor-border-color);
璃白.'s avatar
璃白. 已提交
336 337 338
  border-radius: 4px;
  padding: 10px 16px;
  box-sizing: border-box;
璃白.'s avatar
璃白. 已提交
339
  transition: border 0.3s;
340
  position: relative;
341 342 343 344 345 346 347
  &.fullScreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    margin: 0;
璃白.'s avatar
璃白. 已提交
348
    border: 1px solid transparent !important;
349 350
    z-index: var(--md-editor-fullScrren-zIndex);
  }
璃白.'s avatar
璃白. 已提交
351
  &.active {
璃白.'s avatar
璃白. 已提交
352
    border: 1px solid var(--md-editor-border-color-active);
璃白.'s avatar
璃白. 已提交
353
  }
354 355 356 357 358 359 360
  .word_limit {
    position: absolute;
    right: 10px;
    bottom: 8px;
    font-size: 12px;
    color: var(--md-editor-text-color);
  }
璃白.'s avatar
璃白. 已提交
361 362
}
</style>