utils.js 6.8 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
// 获取选中文本信息
G
guoweijia 已提交
2

璃白.'s avatar
璃白. 已提交
3 4
export function getSelectionInfo(selectorId) {
  const selector = document.getElementById(selectorId);
5
  if (!selector) return;
璃白.'s avatar
璃白. 已提交
6 7
  const { selectionStart = 0, selectionEnd = 0 } = selector;
  if (selectionStart === selectionEnd) return "";
璃白.'s avatar
璃白. 已提交
8
  const selection = window.getSelection().toString();
璃白.'s avatar
璃白. 已提交
9
  return {
璃白.'s avatar
璃白. 已提交
10
    selectorId,
璃白.'s avatar
璃白. 已提交
11
    selection,
璃白.'s avatar
璃白. 已提交
12 13 14 15 16
    selectionStart,
    selectionEnd
  };
}

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// 节流
export const throttle = function(fn, wait) {
  var timer = null;
  return function() {
    var context = this;
    var args = arguments;
    if (!timer) {
      timer = setTimeout(function() {
        fn.apply(context, args);
        timer = null;
      }, wait);
    }
  };
};

璃白.'s avatar
璃白. 已提交
32 33
export const getPosition = function(selectorId) {
  const element = document.getElementById(selectorId);
34
  if (!element) return 0;
璃白.'s avatar
璃白. 已提交
35 36 37 38 39 40 41 42 43 44 45 46
  let cursorPos = 0;
  if (document.selection) {
    //IE
    var selectRange = document.selection.createRange();
    selectRange.moveStart("character", -element.value.length);
    cursorPos = selectRange.text.length;
  } else if (element.selectionStart || element.selectionStart == "0") {
    cursorPos = element.selectionStart;
  }
  return cursorPos;
};

璃白.'s avatar
璃白. 已提交
47
// 工具栏格式化文本
璃白.'s avatar
璃白. 已提交
48
export function formatText(text, selectionInfo, startStr = "", endStr = "") {
璃白.'s avatar
璃白. 已提交
49 50
  if (!selectionInfo) return;
  const newText =
璃白.'s avatar
璃白. 已提交
51 52 53 54
    text.slice(0, selectionInfo.selectionStart) +
    startStr +
    text.slice(selectionInfo.selectionStart, selectionInfo.selectionEnd) +
    endStr +
璃白.'s avatar
璃白. 已提交
55 56
    text.slice(selectionInfo.selectionEnd);
  return newText;
璃白.'s avatar
璃白. 已提交
57
}
璃白.'s avatar
璃白. 已提交
58

59 60 61 62 63 64 65
export function setzIndex(index) {
  document.documentElement.style.setProperty(
    "--md-editor-fullScrren-zIndex",
    index
  );
}

璃白.'s avatar
璃白. 已提交
66 67
// 初始化样式
export function initStyle({
璃白.'s avatar
璃白. 已提交
68
  dark,
璃白.'s avatar
璃白. 已提交
69 70 71
  borderColor,
  borderColorActive,
  textColor,
璃白.'s avatar
璃白. 已提交
72
  textColorActive,
璃白.'s avatar
fix  
璃白. 已提交
73
  placeholderColor,
璃白.'s avatar
璃白. 已提交
74 75
  frameBgColor,
  contentBgColor,
璃白.'s avatar
fix  
璃白. 已提交
76 77
  codeBgColor,
  codeTheme
璃白.'s avatar
璃白. 已提交
78
}) {
璃白.'s avatar
璃白. 已提交
79 80
  // 夜晚模式
  if (dark) {
81 82 83
    borderColor = "#44444F";
    borderColorActive = "#2998F2";
    textColor = "#777888";
璃白.'s avatar
fix  
璃白. 已提交
84
    placeholderColor = "#777888";
85 86 87 88
    textColorActive = "#CCCCD8";
    frameBgColor = "#222226";
    codeBgColor = "#777888";
    contentBgColor = "#222226";
璃白.'s avatar
璃白. 已提交
89 90 91 92 93 94 95 96 97 98 99
  }
  if (frameBgColor) {
    document.documentElement.style.setProperty(
      "--md-editor-frame-bg-color",
      frameBgColor
    );
  }
  if (contentBgColor) {
    document.documentElement.style.setProperty(
      "--md-editor-content-bg-color",
      contentBgColor
100
    );
璃白.'s avatar
璃白. 已提交
101 102 103 104 105 106 107 108 109
  }

  if (codeBgColor) {
    document.documentElement.style.setProperty(
      "--md-editor-code-bg-color",
      codeBgColor
    );
  }

璃白.'s avatar
璃白. 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  if (borderColor) {
    document.documentElement.style.setProperty(
      "--md-editor-border-color",
      borderColor
    );
  }
  if (borderColorActive) {
    document.documentElement.style.setProperty(
      "--md-editor-border-color-active",
      borderColorActive
    );
  }
  if (textColor) {
    document.documentElement.style.setProperty(
      "--md-editor-text-color",
      textColor
    );
  }
  if (textColorActive) {
    document.documentElement.style.setProperty(
      "--md-editor-text-color-active",
      textColorActive
    );
  }
璃白.'s avatar
fix  
璃白. 已提交
134 135 136
  if (codeTheme) {
    switch (codeTheme) {
      case "dark":
璃白.'s avatar
fix  
璃白. 已提交
137
        import("@/assets/style/code/dark.css");
璃白.'s avatar
fix  
璃白. 已提交
138 139
        break;
      case "light":
璃白.'s avatar
fix  
璃白. 已提交
140
        import("@/assets/style/code/lightfair.css");
璃白.'s avatar
fix  
璃白. 已提交
141
        break;
璃白.'s avatar
fix  
璃白. 已提交
142 143
      case "atom-one-dark":
        import("@/assets/style/code/atom-one-dark.css");
璃白.'s avatar
fix  
璃白. 已提交
144 145 146 147
        break;
      default:
        break;
    }
璃白.'s avatar
fix  
璃白. 已提交
148 149
  } else {
    import("@/assets/style/code/lightfair.css");
璃白.'s avatar
fix  
璃白. 已提交
150
  }
璃白.'s avatar
璃白. 已提交
151 152
}

璃白.'s avatar
璃白. 已提交
153
//
璃白.'s avatar
璃白. 已提交
154 155 156
export function isNotEmpty(val) {
  return val !== null && val !== undefined;
}
璃白.'s avatar
璃白. 已提交
157 158 159 160

export function isNotFalse(val) {
  return val !== false;
}
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

export function checktUrl(val, rule) {
  if (!val || !rule) return;
  const hideEl = document.createElement("div");
  hideEl.style.display = "none";
  hideEl.innerHTML = val;
  const imgList = Array.from(hideEl.getElementsByTagName("img"));
  return imgList.filter(item => !rule.test(item.src)).map(item => item.src);
}

export function checkBoswer() {
  const agent = navigator.userAgent.match(
    /(iPhone|iPod|Android|ios|iOS|iPad|Backerry|WebOS|Symbian|Windows Phone|Phone)/i
  );
  return agent !== null;
}
璃白.'s avatar
璃白. 已提交
177
// 去除头部空格行
178 179 180 181
export function removeBlankLine(val) {
  if (!val) return "";
  return val.replace(/^\n/, "");
}
璃白.'s avatar
璃白. 已提交
182 183 184 185 186 187 188 189 190 191

// 获取被过滤掉的标签
export function getFilteredTags(oldStr, newStr) {
  if (oldStr.length - newStr.length === 0) return [];
  const filteredStr = oldStr.replace(newStr.trim(), "");
  const virtualDom = document.createElement("div");
  virtualDom.innerHTML = filteredStr;
  const filteredTags = Array.from(virtualDom.getElementsByTagName("*"));
  return filteredTags;
}
璃白.'s avatar
璃白. 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
// 格式刷的规则
export function copyFormatRules(selection) {
  if (!selection) return "";
  const first1 = selection.slice(0, 1);
  const first2 = selection.slice(0, 2);
  const first3 = selection.slice(0, 3);
  const first4 = selection.slice(0, 4);
  const first5 = selection.slice(0, 5);
  const first6 = selection.slice(0, 6);
  const first7 = selection.slice(0, 7);
  const end1 = selection.slice(-1);
  const end2 = selection.slice(-2);
  const end3 = selection.slice(-3);
  let formatType = "";
  switch (true) {
    // 斜体
    case first1 === "_" && end1 === "_":
      formatType = {
        startStr: "_",
        endStr: "_"
      };
      break;
    // 加粗
    case first2 === "**" && end2 === "**":
      formatType = {
        startStr: "**",
        endStr: "**"
      };
      break;
    // 代码
    case first3 === "```" && end3 === "```":
      formatType = {
        startStr: "\n```\n",
璃白.'s avatar
璃白. 已提交
225 226
        endStr: "\n\n```",
        type: "code"
璃白.'s avatar
璃白. 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
      };
      break;
    // 引用
    case first2 === "> ":
      formatType = {
        startStr: "\n> ",
        endStr: ""
      };
      break;
    // 待办
    case first6 === "- [ ] ":
      formatType = {
        startStr: "\n- [ ] ",
        endStr: ""
      };
      break;
    // 无序列表
    case first2 === "- ":
      formatType = {
        startStr: "\n- ",
        endStr: ""
      };
      break;
    // H6
    case first7 === "###### ":
      formatType = {
        startStr: "\n###### ",
        endStr: ""
      };
      break;
    // H5
    case first6 === "##### ":
      formatType = {
        startStr: "\n##### ",
        endStr: ""
      };
      break;
    // H4
    case first5 === "#### ":
      formatType = {
        startStr: "\n#### ",
        endStr: ""
      };
      break;
    // H3
    case first4 === "### ":
      formatType = {
        startStr: "\n### ",
        endStr: ""
      };
      break;
    // H2
    case first3 === "## ":
      formatType = {
        startStr: "\n## ",
        endStr: ""
      };
      break;
    // H1
    case first2 === "# ":
      formatType = {
        startStr: "\n# ",
        endStr: ""
      };
      break;
    default:
      break;
  }

  return formatType;
}