main.js 6.0 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 26
    renderLinks,
    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
    },
    submit(val) {
      onSubmit(val);
    },
    upload({ val, callback }) {
璃白.'s avatar
璃白. 已提交
109 110
      console.log(val);

111
      onUpload(val, function(res) {
璃白.'s avatar
璃白. 已提交
112 113 114 115
        callback({
          url: res,
          file: val
        });
116
      });
璃白.'s avatar
璃白. 已提交
117 118
    },
    renderLinks({ links, callback }) {
璃白.'s avatar
璃白. 已提交
119
      if (!renderLinks) return callback(links);
璃白.'s avatar
璃白. 已提交
120 121 122
      renderLinks(links, function(res) {
        callback(res);
      });
璃白.'s avatar
璃白. 已提交
123 124
    },
    queryUserList({ keyWord, callback }) {
璃白.'s avatar
璃白. 已提交
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 169 170 171 172 173 174 175
      if (!queryUserList) return callback(false); // 返回false则不触发@弹窗
      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: "王一博",
        //     url: "https://weibo.com/u/5492443184",
        //     avatar:
        //       "https://img2.baidu.com/it/u=298051053,3773223854&fm=26&fmt=auto&gp=0.jpg"
        //   },
        //   {
        //     id: 5,
        //     name: "王俊凯",
        //     url: "https://weibo.com/tfwangjunkai",
        //     avatar:
        //       "https://img1.baidu.com/it/u=2378425879,2273515018&fm=26&fmt=auto&gp=0.jpg"
        //   },
        //   {
        //     id: 6,
        //     name: "易烊千玺",
        //     url: "https://weibo.com/tfyiyangqianxi",
        //     avatar:
        //       "https://img0.baidu.com/it/u=2227200088,1939721201&fm=26&fmt=auto&gp=0.jpg"
        //   },
        //   {
        //     id: 7,
        //     name: "白敬亭",
        //     url: "https://weibo.com/u/2112496475",
        //     avatar:
        //       "https://img1.baidu.com/it/u=3265411836,2089649447&fm=11&fmt=auto&gp=0.jpg"
        //   }
        // ];
        // callback(res);
璃白.'s avatar
璃白. 已提交
176

璃白.'s avatar
璃白. 已提交
177 178 179 180
        const list = res;
        if (!keyWord) return callback(list);
        callback(
          list.filter(item => {
璃白.'s avatar
璃白. 已提交
181
            return item.nickname.toLowerCase().includes(keyWord.toLowerCase());
璃白.'s avatar
璃白. 已提交
182 183 184
          })
        );
      });
185 186 187
    }
  };
  this.vEl = new Vue({
璃白.'s avatar
璃白. 已提交
188
    render: h =>
璃白.'s avatar
璃白. 已提交
189
      h(App, {
190 191
        on: listeners,
        props: props
璃白.'s avatar
璃白. 已提交
192 193
      })
  }).$mount(el);
194 195 196 197 198 199 200 201 202

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

  this.setValue = function(val) {
璃白.'s avatar
璃白. 已提交
203
    props.value = (val || "") + "";
204
    this.vEl.$forceUpdate();
璃白.'s avatar
璃白. 已提交
205 206 207 208 209 210 211
    if (!props.setPreview) return;
    setTimeout(() => {
      this.toggleTab();
    });
    setTimeout(() => {
      this.toggleTab();
    });
212 213 214 215 216 217 218 219 220 221 222
  };

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

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

璃白.'s avatar
璃白. 已提交
224 225 226 227 228 229 230 231 232 233
  this.disable = function() {
    props.disabled = true;
    this.vEl.$forceUpdate();
  };

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

璃白.'s avatar
fix  
璃白. 已提交
234 235
  this.toggleTab = function(setPreview) {
    if (setPreview !== "edit" && setPreview !== "preview") {
璃白.'s avatar
璃白. 已提交
236
      props.setPreview = !props.setPreview;
璃白.'s avatar
fix  
璃白. 已提交
237 238 239 240 241 242 243 244 245 246
    } 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  
璃白. 已提交
247
    } else {
璃白.'s avatar
fix  
璃白. 已提交
248
      props.setFullScreen = setFullScreen;
璃白.'s avatar
fix  
璃白. 已提交
249 250 251
    }
    this.vEl.$forceUpdate();
  };
璃白.'s avatar
璃白. 已提交
252 253
}

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