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