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