main.js 5.6 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
    },
    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 119 120 121
    },
    renderLinks({ links, callback }) {
      renderLinks(links, function(res) {
        callback(res);
      });
璃白.'s avatar
璃白. 已提交
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
    },
    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: "王一博",
璃白.'s avatar
璃白. 已提交
147
          url: "https://weibo.com/u/5492443184",
璃白.'s avatar
璃白. 已提交
148 149 150 151 152 153
          avatar:
            "https://img2.baidu.com/it/u=298051053,3773223854&fm=26&fmt=auto&gp=0.jpg"
        },
        {
          id: 5,
          name: "王俊凯",
璃白.'s avatar
璃白. 已提交
154
          url: "https://weibo.com/tfwangjunkai",
璃白.'s avatar
璃白. 已提交
155 156 157 158 159 160
          avatar:
            "https://img1.baidu.com/it/u=2378425879,2273515018&fm=26&fmt=auto&gp=0.jpg"
        },
        {
          id: 6,
          name: "易烊千玺",
璃白.'s avatar
璃白. 已提交
161
          url: "https://weibo.com/tfyiyangqianxi",
璃白.'s avatar
璃白. 已提交
162 163
          avatar:
            "https://img0.baidu.com/it/u=2227200088,1939721201&fm=26&fmt=auto&gp=0.jpg"
璃白.'s avatar
璃白. 已提交
164 165 166 167 168 169 170
        },
        {
          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"
璃白.'s avatar
璃白. 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183
        }
      ];

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

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

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

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

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

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

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

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

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