main.js 2.3 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1 2
import Vue from "vue";
import App from "./App";
璃白.'s avatar
璃白. 已提交
3 4
import Vtip from "vtip";
import "vtip/lib/index.min.css";
5
import { initStyle, setzIndex, isNotEmpty } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
6 7
import "@/assets/style/global.less";

璃白.'s avatar
璃白. 已提交
8 9
Vue.use(Vtip.directive);

璃白.'s avatar
璃白. 已提交
10
function initMdEditor(obj) {
11 12 13 14 15 16 17
  const _this = this;
  this.value = {
    text: "",
    html: ""
  };
  this.vEl = "";
  let {
璃白.'s avatar
璃白. 已提交
18
    el,
19
    onLoad = () => {},
20 21 22 23 24 25
    onChange = () => {},
    onUpload = () => {},
    onFocus = () => {},
    onBlur = () => {},
    onInput = () => {},
    onSubmit = () => {},
璃白.'s avatar
璃白. 已提交
26
    placeholder,
璃白.'s avatar
璃白. 已提交
27
    value,
28
    zIndex = 2000,
29 30
    filePathRule,
    rows = 6,
31 32
    maxLength,
    showWordLimit,
33
    throttle = 1000,
璃白.'s avatar
璃白. 已提交
34
    canPreview,
璃白.'s avatar
璃白. 已提交
35
    canAttachFile,
璃白.'s avatar
璃白. 已提交
36 37
    themeOptions,
    toolsOptions
璃白.'s avatar
璃白. 已提交
38
  } = obj;
璃白.'s avatar
璃白. 已提交
39
  if (!el || !document.querySelector(el)) throw new Error("请指定容器");
璃白.'s avatar
璃白. 已提交
40
  if (isNotEmpty(themeOptions)) initStyle(themeOptions);
41
  if (isNotEmpty(zIndex)) setzIndex(zIndex);
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  const id = new Date().getTime() + "";
  const props = {
    canAttachFile,
    value,
    themeOptions,
    filePathRule,
    rows,
    id,
    throttle,
    canPreview,
    toolsOptions,
    placeholder,
    maxLength,
    zIndex,
    focus: false,
    showWordLimit
  };
  const listeners = {
    change(val) {
      onChange(val);
      _this.value = val;
    },
    input(val) {
      onInput(val);
      _this.value = val;
    },
    load(val) {
      onLoad(val);
      _this.value = val;
    },
    focus(val) {
      onFocus(val);
      _this.value = val;
    },
    blur(val) {
      onBlur(val);
      _this.value = val;
    },
    submit(val) {
      onSubmit(val);
      _this.value = val;
    },
    upload({ val, callback }) {
      onUpload(val, function(res) {
        callback(res);
      });
    }
  };
  this.vEl = new Vue({
璃白.'s avatar
璃白. 已提交
91
    render: h =>
璃白.'s avatar
璃白. 已提交
92
      h(App, {
93 94
        on: listeners,
        props: props
璃白.'s avatar
璃白. 已提交
95 96
      })
  }).$mount(el);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

  this.getValue = function(callback) {
    if (callback) {
      callback(this.value);
    }
    return this.value;
  };

  this.setValue = function(val) {
    if (!val) return;
    props.value = val + "";
    this.vEl.$forceUpdate();
  };

  this.focus = function() {
    props.focus = true;
    this.vEl.$forceUpdate();
  };

  this.blur = function() {
    props.focus = false;
    this.vEl.$forceUpdate();
  };
璃白.'s avatar
璃白. 已提交
120 121
}

璃白.'s avatar
璃白. 已提交
122
window.MdEditor = initMdEditor;