main.js 2.4 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
import "@/assets/style/global.less";
璃白.'s avatar
璃白. 已提交
7 8
Vue.use(Vtip.directive);

璃白.'s avatar
璃白. 已提交
9
function initMdEditor(obj) {
10 11 12 13 14 15 16
  const _this = this;
  this.value = {
    text: "",
    html: ""
  };
  this.vEl = "";
  let {
璃白.'s avatar
璃白. 已提交
17
    el,
18
    onLoad = () => {},
19 20 21 22 23 24
    onChange = () => {},
    onUpload = () => {},
    onFocus = () => {},
    onBlur = () => {},
    onInput = () => {},
    onSubmit = () => {},
璃白.'s avatar
璃白. 已提交
25
    placeholder,
璃白.'s avatar
璃白. 已提交
26
    value,
27
    zIndex = 2000,
28 29
    filePathRule,
    rows = 6,
30 31
    maxLength,
    showWordLimit,
32
    throttle = 1000,
璃白.'s avatar
璃白. 已提交
33
    canPreview,
璃白.'s avatar
璃白. 已提交
34
    canAttachFile,
璃白.'s avatar
璃白. 已提交
35 36
    themeOptions,
    toolsOptions
璃白.'s avatar
璃白. 已提交
37
  } = obj;
璃白.'s avatar
璃白. 已提交
38
  if (!el || !document.querySelector(el)) throw new Error("请指定容器");
璃白.'s avatar
璃白. 已提交
39
  if (isNotEmpty(themeOptions)) initStyle(themeOptions);
40
  if (isNotEmpty(zIndex)) setzIndex(zIndex);
璃白.'s avatar
fix  
璃白. 已提交
41

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
  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;
璃白.'s avatar
璃白. 已提交
67
      props.value = val.text;
68 69 70 71 72 73 74 75 76 77 78 79
    },
    load(val) {
      onLoad(val);
      _this.value = val;
    },
    focus(val) {
      onFocus(val);
      _this.value = val;
    },
    blur(val) {
      onBlur(val);
      _this.value = val;
璃白.'s avatar
璃白. 已提交
80 81
      props.focus = false;
      _this.vEl.$forceUpdate();
82 83 84 85 86 87 88 89 90 91 92 93
    },
    submit(val) {
      onSubmit(val);
      _this.value = val;
    },
    upload({ val, callback }) {
      onUpload(val, function(res) {
        callback(res);
      });
    }
  };
  this.vEl = new Vue({
璃白.'s avatar
璃白. 已提交
94
    render: h =>
璃白.'s avatar
璃白. 已提交
95
      h(App, {
96 97
        on: listeners,
        props: props
璃白.'s avatar
璃白. 已提交
98 99
      })
  }).$mount(el);
100 101 102 103 104 105 106 107 108

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

  this.setValue = function(val) {
璃白.'s avatar
璃白. 已提交
109
    props.value = (val || "") + "";
110 111 112 113 114 115 116 117 118 119 120 121
    this.vEl.$forceUpdate();
  };

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

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

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