main.js 5.1 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
    renderLinks = () => {},
璃白.'s avatar
璃白. 已提交
26
    queryUserList = () => {},
璃白.'s avatar
璃白. 已提交
27
    placeholder,
璃白.'s avatar
璃白. 已提交
28
    value,
璃白.'s avatar
璃白. 已提交
29
    disabled,
30
    zIndex = 2000,
31 32
    filePathRule,
    rows = 6,
璃白.'s avatar
璃白. 已提交
33
    height,
璃白.'s avatar
fix  
璃白. 已提交
34
    tabSize = 2,
35 36
    maxLength,
    showWordLimit,
璃白.'s avatar
fix  
璃白. 已提交
37
    throttle = 600,
璃白.'s avatar
璃白. 已提交
38
    canPreview,
璃白.'s avatar
璃白. 已提交
39
    canAttachFile,
璃白.'s avatar
璃白. 已提交
40 41
    themeOptions,
    toolsOptions
璃白.'s avatar
璃白. 已提交
42
  } = obj;
璃白.'s avatar
璃白. 已提交
43
  if (!el || !document.querySelector(el)) throw new Error("请指定容器");
璃白.'s avatar
璃白. 已提交
44
  if (isNotEmpty(themeOptions)) initStyle(themeOptions);
45
  if (isNotEmpty(zIndex)) setzIndex(zIndex);
璃白.'s avatar
fix  
璃白. 已提交
46

47 48 49 50 51 52 53
  const id = new Date().getTime() + "";
  const props = {
    canAttachFile,
    value,
    themeOptions,
    filePathRule,
    rows,
璃白.'s avatar
璃白. 已提交
54
    height: parseInt(height),
55 56 57 58 59 60 61
    id,
    throttle,
    canPreview,
    toolsOptions,
    placeholder,
    maxLength,
    zIndex,
璃白.'s avatar
璃白. 已提交
62
    disabled,
璃白.'s avatar
fix  
璃白. 已提交
63
    tabSize,
璃白.'s avatar
fix  
璃白. 已提交
64 65
    setPreview: false,
    setFullScreen: false,
66 67 68 69
    focus: false,
    showWordLimit
  };
  const listeners = {
璃白.'s avatar
fix  
璃白. 已提交
70 71 72 73 74 75 76 77 78
    changeTab(val) {
      props.setPreview = val;
      props.focus = !val;
      _this.vEl.$forceUpdate();
    },
    changeFullScreen(val) {
      props.setFullScreen = val;
      _this.vEl.$forceUpdate();
    },
79 80 81
    change(val) {
      onChange(val);
      _this.value = val;
璃白.'s avatar
fix  
璃白. 已提交
82
      props.value = val.text;
璃白.'s avatar
fix  
璃白. 已提交
83
      _this.vEl.$forceUpdate();
84 85 86 87
    },
    input(val) {
      onInput(val);
      _this.value = val;
璃白.'s avatar
璃白. 已提交
88
      props.value = val.text;
89 90 91 92 93 94 95 96 97 98 99 100
    },
    load(val) {
      onLoad(val);
      _this.value = val;
    },
    focus(val) {
      onFocus(val);
      _this.value = val;
    },
    blur(val) {
      onBlur(val);
      _this.value = val;
璃白.'s avatar
璃白. 已提交
101
      props.focus = false;
璃白.'s avatar
fix  
璃白. 已提交
102
      props.value = val.text;
璃白.'s avatar
璃白. 已提交
103
      _this.vEl.$forceUpdate();
104 105 106 107 108 109 110 111
    },
    submit(val) {
      onSubmit(val);
    },
    upload({ val, callback }) {
      onUpload(val, function(res) {
        callback(res);
      });
璃白.'s avatar
璃白. 已提交
112 113 114 115 116
    },
    renderLinks({ links, callback }) {
      renderLinks(links, function(res) {
        callback(res);
      });
璃白.'s avatar
璃白. 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    },
    queryUserList({ keyWord, callback }) {
      // queryUserList(keyWord, function(res) {
      const list = [
        {
          id: 1,
          name: "藤原拓海",
          avatar:
            "https://img2.baidu.com/it/u=2380211986,3979961921&fm=26&fmt=auto&gp=0.jpg"
        },
        {
          id: 2,
          name: "高桥凉介",
          avatar:
            "https://img0.baidu.com/it/u=777620324,2343967729&fm=26&fmt=auto&gp=0.jpg"
        },
        {
          id: 3,
          name: "马奎斯",
          avatar:
            "https://img2.baidu.com/it/u=1297316011,1869565258&fm=26&fmt=auto&gp=0.jpg"
        },
        {
          id: 4,
          name: "王一博",
          avatar:
            "https://img2.baidu.com/it/u=298051053,3773223854&fm=26&fmt=auto&gp=0.jpg"
        },
        {
          id: 5,
          name: "王俊凯",
          avatar:
            "https://img1.baidu.com/it/u=2378425879,2273515018&fm=26&fmt=auto&gp=0.jpg"
        },
        {
          id: 6,
          name: "易烊千玺",
          avatar:
            "https://img0.baidu.com/it/u=2227200088,1939721201&fm=26&fmt=auto&gp=0.jpg"
        }
      ];

      if (!keyWord) {
        callback(list);
        return;
      }
      callback(
        list.filter(item => {
          return item.name.includes(keyWord);
        })
      );
      // });
169 170 171
    }
  };
  this.vEl = new Vue({
璃白.'s avatar
璃白. 已提交
172
    render: h =>
璃白.'s avatar
璃白. 已提交
173
      h(App, {
174 175
        on: listeners,
        props: props
璃白.'s avatar
璃白. 已提交
176 177
      })
  }).$mount(el);
178 179 180 181 182 183 184 185 186

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

  this.setValue = function(val) {
璃白.'s avatar
璃白. 已提交
187
    props.value = (val || "") + "";
188
    this.vEl.$forceUpdate();
璃白.'s avatar
璃白. 已提交
189 190 191 192 193 194 195
    if (!props.setPreview) return;
    setTimeout(() => {
      this.toggleTab();
    });
    setTimeout(() => {
      this.toggleTab();
    });
196 197 198 199 200 201 202 203 204 205 206
  };

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

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

璃白.'s avatar
璃白. 已提交
208 209 210 211 212 213 214 215 216 217
  this.disable = function() {
    props.disabled = true;
    this.vEl.$forceUpdate();
  };

  this.enable = function() {
    props.disabled = false;
    this.vEl.$forceUpdate();
  };

璃白.'s avatar
fix  
璃白. 已提交
218 219
  this.toggleTab = function(setPreview) {
    if (setPreview !== "edit" && setPreview !== "preview") {
璃白.'s avatar
璃白. 已提交
220
      props.setPreview = !props.setPreview;
璃白.'s avatar
fix  
璃白. 已提交
221 222 223 224 225 226 227 228 229 230
    } else {
      props.setPreview = setPreview === "preview";
    }
    props.focus = !props.setPreview;
    this.vEl.$forceUpdate();
  };

  this.toggleFullScreen = function(setFullScreen) {
    if (setFullScreen === undefined) {
      props.setFullScreen = !props.setFullScreen;
璃白.'s avatar
fix  
璃白. 已提交
231
    } else {
璃白.'s avatar
fix  
璃白. 已提交
232
      props.setFullScreen = setFullScreen;
璃白.'s avatar
fix  
璃白. 已提交
233 234 235
    }
    this.vEl.$forceUpdate();
  };
璃白.'s avatar
璃白. 已提交
236 237
}

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