main.js 4.8 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
import { initStyle, setzIndex, isNotEmpty } from "@/assets/js/utils";
璃白.'s avatar
璃白. 已提交
6
import "@/assets/style/global.less";
郭维嘉 已提交
7 8
// import "@/assets/jupyter-render/dist/index.js";
// import "@/assets/jupyter-render/dist/assets/index.css";
璃白.'s avatar
璃白. 已提交
9 10
Vue.use(Vtip.directive);

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

51 52 53 54 55 56 57
  const id = new Date().getTime() + "";
  const props = {
    canAttachFile,
    value,
    themeOptions,
    filePathRule,
    rows,
璃白.'s avatar
璃白. 已提交
58
    height: parseInt(height),
59 60 61 62 63 64 65
    id,
    throttle,
    canPreview,
    toolsOptions,
    placeholder,
    maxLength,
    zIndex,
璃白.'s avatar
璃白. 已提交
66
    disabled,
璃白.'s avatar
fix  
璃白. 已提交
67
    tabSize,
璃白.'s avatar
璃白. 已提交
68
    renderLinks: Boolean(renderLinks),
璃白.'s avatar
璃白. 已提交
69
    registerTools,
璃白.'s avatar
fix  
璃白. 已提交
70 71
    setPreview: false,
    setFullScreen: false,
72 73 74 75
    focus: false,
    showWordLimit
  };
  const listeners = {
璃白.'s avatar
fix  
璃白. 已提交
76 77 78 79 80 81 82 83 84
    changeTab(val) {
      props.setPreview = val;
      props.focus = !val;
      _this.vEl.$forceUpdate();
    },
    changeFullScreen(val) {
      props.setFullScreen = val;
      _this.vEl.$forceUpdate();
    },
85 86 87
    change(val) {
      onChange(val);
      _this.value = val;
璃白.'s avatar
fix  
璃白. 已提交
88
      props.value = val.text;
璃白.'s avatar
fix  
璃白. 已提交
89
      _this.vEl.$forceUpdate();
90 91 92 93
    },
    input(val) {
      onInput(val);
      _this.value = val;
璃白.'s avatar
璃白. 已提交
94
      props.value = val.text;
95 96 97 98 99 100 101 102 103 104 105 106
    },
    load(val) {
      onLoad(val);
      _this.value = val;
    },
    focus(val) {
      onFocus(val);
      _this.value = val;
    },
    blur(val) {
      onBlur(val);
      _this.value = val;
璃白.'s avatar
璃白. 已提交
107
      props.focus = false;
璃白.'s avatar
fix  
璃白. 已提交
108
      props.value = val.text;
璃白.'s avatar
璃白. 已提交
109
      _this.vEl.$forceUpdate();
110 111 112 113
    },
    submit(val) {
      onSubmit(val);
    },
璃白.'s avatar
璃白. 已提交
114
    upload({ val, type, callback }) {
璃白.'s avatar
璃白. 已提交
115 116 117 118
      onUpload(val, type, function(res) {
        callback({
          url: res,
          file: val
璃白.'s avatar
璃白. 已提交
119
        });
璃白.'s avatar
璃白. 已提交
120
      });
璃白.'s avatar
璃白. 已提交
121 122
    },
    renderLinks({ links, callback }) {
璃白.'s avatar
璃白. 已提交
123
      if (!renderLinks) return callback(links);
璃白.'s avatar
璃白. 已提交
124 125 126
      renderLinks(links, function(res) {
        callback(res);
      });
璃白.'s avatar
璃白. 已提交
127 128
    },
    queryUserList({ keyWord, callback }) {
璃白.'s avatar
璃白. 已提交
129 130 131 132 133 134
      if (!queryUserList) return callback(false); // 返回false则不触发@弹窗
      queryUserList(keyWord, function(res) {
        const list = res;
        if (!keyWord) return callback(list);
        callback(
          list.filter(item => {
璃白.'s avatar
璃白. 已提交
135
            return item.nickname.toLowerCase().includes(keyWord.toLowerCase());
璃白.'s avatar
璃白. 已提交
136 137 138
          })
        );
      });
139 140 141
    }
  };
  this.vEl = new Vue({
璃白.'s avatar
璃白. 已提交
142
    render: h =>
璃白.'s avatar
璃白. 已提交
143
      h(App, {
144 145
        on: listeners,
        props: props
璃白.'s avatar
璃白. 已提交
146 147
      })
  }).$mount(el);
148

璃白.'s avatar
璃白. 已提交
149
  this.getVideoList = function(callback) {
璃白.'s avatar
fix  
璃白. 已提交
150
    return this.vEl.$children[0].getVideoList(callback);
璃白.'s avatar
璃白. 已提交
151 152
  };

153 154 155 156 157 158 159 160
  this.getValue = function(callback) {
    if (callback) {
      callback(this.value);
    }
    return this.value;
  };

  this.setValue = function(val) {
璃白.'s avatar
璃白. 已提交
161
    props.value = (val || "") + "";
162
    this.vEl.$forceUpdate();
璃白.'s avatar
璃白. 已提交
163 164 165 166 167 168 169
    if (!props.setPreview) return;
    setTimeout(() => {
      this.toggleTab();
    });
    setTimeout(() => {
      this.toggleTab();
    });
170 171 172 173 174 175 176 177 178 179 180
  };

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

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

璃白.'s avatar
璃白. 已提交
182 183 184 185 186 187 188 189 190 191
  this.disable = function() {
    props.disabled = true;
    this.vEl.$forceUpdate();
  };

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

璃白.'s avatar
璃白. 已提交
192 193 194 195 196 197 198 199 200 201
  this.registerTools = function(option) {
    props.registerTools = [];
    if (Array.isArray(option)) {
      props.registerTools.push(...option);
    } else {
      props.registerTools.push(option);
    }
    this.vEl.$forceUpdate();
  };

璃白.'s avatar
fix  
璃白. 已提交
202 203
  this.toggleTab = function(setPreview) {
    if (setPreview !== "edit" && setPreview !== "preview") {
璃白.'s avatar
璃白. 已提交
204
      props.setPreview = !props.setPreview;
璃白.'s avatar
fix  
璃白. 已提交
205 206 207 208 209 210 211 212 213 214
    } 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  
璃白. 已提交
215
    } else {
璃白.'s avatar
fix  
璃白. 已提交
216
      props.setFullScreen = setFullScreen;
璃白.'s avatar
fix  
璃白. 已提交
217 218 219
    }
    this.vEl.$forceUpdate();
  };
璃白.'s avatar
璃白. 已提交
220 221
}

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