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