main.js 6.2 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";
璃白.'s avatar
璃白. 已提交
5 6 7 8 9 10
import {
  initStyle,
  setzIndex,
  isNotEmpty,
  getParameterName
} from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
11
import "@/assets/style/global.less";
璃白.'s avatar
璃白. 已提交
12 13
Vue.use(Vtip.directive);

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

52 53 54 55 56 57 58
  const id = new Date().getTime() + "";
  const props = {
    canAttachFile,
    value,
    themeOptions,
    filePathRule,
    rows,
璃白.'s avatar
璃白. 已提交
59
    height: parseInt(height),
60 61 62 63 64 65 66
    id,
    throttle,
    canPreview,
    toolsOptions,
    placeholder,
    maxLength,
    zIndex,
璃白.'s avatar
璃白. 已提交
67
    disabled,
璃白.'s avatar
fix  
璃白. 已提交
68
    tabSize,
璃白.'s avatar
fix  
璃白. 已提交
69 70
    setPreview: false,
    setFullScreen: false,
71 72 73 74
    focus: false,
    showWordLimit
  };
  const listeners = {
璃白.'s avatar
fix  
璃白. 已提交
75 76 77 78 79 80 81 82 83
    changeTab(val) {
      props.setPreview = val;
      props.focus = !val;
      _this.vEl.$forceUpdate();
    },
    changeFullScreen(val) {
      props.setFullScreen = val;
      _this.vEl.$forceUpdate();
    },
84 85 86
    change(val) {
      onChange(val);
      _this.value = val;
璃白.'s avatar
fix  
璃白. 已提交
87
      props.value = val.text;
璃白.'s avatar
fix  
璃白. 已提交
88
      _this.vEl.$forceUpdate();
89 90 91 92
    },
    input(val) {
      onInput(val);
      _this.value = val;
璃白.'s avatar
璃白. 已提交
93
      props.value = val.text;
94 95 96 97 98 99 100 101 102 103 104 105
    },
    load(val) {
      onLoad(val);
      _this.value = val;
    },
    focus(val) {
      onFocus(val);
      _this.value = val;
    },
    blur(val) {
      onBlur(val);
      _this.value = val;
璃白.'s avatar
璃白. 已提交
106
      props.focus = false;
璃白.'s avatar
fix  
璃白. 已提交
107
      props.value = val.text;
璃白.'s avatar
璃白. 已提交
108
      _this.vEl.$forceUpdate();
109 110 111 112
    },
    submit(val) {
      onSubmit(val);
    },
璃白.'s avatar
璃白. 已提交
113 114 115 116 117 118 119 120
    upload({ val, type, callback }) {
      const params = getParameterName(onUpload);
      if (!params.includes("type")) {
        onUpload(val, function(res) {
          callback({
            url: res,
            file: val
          });
璃白.'s avatar
璃白. 已提交
121
        });
璃白.'s avatar
璃白. 已提交
122 123 124 125 126 127 128 129
      } else {
        onUpload(val, type, function(res) {
          callback({
            url: res,
            file: val
          });
        });
      }
璃白.'s avatar
璃白. 已提交
130 131
    },
    renderLinks({ links, callback }) {
璃白.'s avatar
璃白. 已提交
132
      if (!renderLinks) return callback(links);
璃白.'s avatar
璃白. 已提交
133 134 135
      renderLinks(links, function(res) {
        callback(res);
      });
璃白.'s avatar
璃白. 已提交
136 137
    },
    queryUserList({ keyWord, callback }) {
璃白.'s avatar
璃白. 已提交
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 176 177 178 179 180 181 182 183 184 185 186 187 188
      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
璃白. 已提交
189

璃白.'s avatar
璃白. 已提交
190 191 192 193
        const list = res;
        if (!keyWord) return callback(list);
        callback(
          list.filter(item => {
璃白.'s avatar
璃白. 已提交
194
            return item.nickname.toLowerCase().includes(keyWord.toLowerCase());
璃白.'s avatar
璃白. 已提交
195 196 197
          })
        );
      });
198 199 200
    }
  };
  this.vEl = new Vue({
璃白.'s avatar
璃白. 已提交
201
    render: h =>
璃白.'s avatar
璃白. 已提交
202
      h(App, {
203 204
        on: listeners,
        props: props
璃白.'s avatar
璃白. 已提交
205 206
      })
  }).$mount(el);
207 208 209 210 211 212 213 214 215

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

  this.setValue = function(val) {
璃白.'s avatar
璃白. 已提交
216
    props.value = (val || "") + "";
217
    this.vEl.$forceUpdate();
璃白.'s avatar
璃白. 已提交
218 219 220 221 222 223 224
    if (!props.setPreview) return;
    setTimeout(() => {
      this.toggleTab();
    });
    setTimeout(() => {
      this.toggleTab();
    });
225 226 227 228 229 230 231 232 233 234 235
  };

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

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

璃白.'s avatar
璃白. 已提交
237 238 239 240 241 242 243 244 245 246
  this.disable = function() {
    props.disabled = true;
    this.vEl.$forceUpdate();
  };

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

璃白.'s avatar
fix  
璃白. 已提交
247 248
  this.toggleTab = function(setPreview) {
    if (setPreview !== "edit" && setPreview !== "preview") {
璃白.'s avatar
璃白. 已提交
249
      props.setPreview = !props.setPreview;
璃白.'s avatar
fix  
璃白. 已提交
250 251 252 253 254 255 256 257 258 259
    } 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  
璃白. 已提交
260
    } else {
璃白.'s avatar
fix  
璃白. 已提交
261
      props.setFullScreen = setFullScreen;
璃白.'s avatar
fix  
璃白. 已提交
262 263 264
    }
    this.vEl.$forceUpdate();
  };
璃白.'s avatar
璃白. 已提交
265 266
}

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