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