utils.js 3.6 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 8 9
  // const selection = window.getSelection();
  const { selectionStart = 0, selectionEnd = 0 } = selector;
  if (selectionStart === selectionEnd) return "";
  return {
璃白.'s avatar
璃白. 已提交
10
    selectorId,
璃白.'s avatar
璃白. 已提交
11 12 13 14 15
    selectionStart,
    selectionEnd
  };
}

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// 节流
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
璃白. 已提交
31 32
export const getPosition = function(selectorId) {
  const element = document.getElementById(selectorId);
33
  if (!element) return 0;
璃白.'s avatar
璃白. 已提交
34 35 36 37 38 39 40 41 42 43 44 45
  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
璃白. 已提交
46
// 工具栏格式化文本
璃白.'s avatar
璃白. 已提交
47
export function formatText(text, selectionInfo, startStr = "", endStr = "") {
璃白.'s avatar
璃白. 已提交
48 49
  if (!selectionInfo) return;
  const newText =
璃白.'s avatar
璃白. 已提交
50 51 52 53
    text.slice(0, selectionInfo.selectionStart) +
    startStr +
    text.slice(selectionInfo.selectionStart, selectionInfo.selectionEnd) +
    endStr +
璃白.'s avatar
璃白. 已提交
54 55
    text.slice(selectionInfo.selectionEnd);
  return newText;
璃白.'s avatar
璃白. 已提交
56
}
璃白.'s avatar
璃白. 已提交
57

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

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

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

璃白.'s avatar
璃白. 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  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
璃白. 已提交
132
//
璃白.'s avatar
璃白. 已提交
133 134 135
export function isNotEmpty(val) {
  return val !== null && val !== undefined;
}
璃白.'s avatar
璃白. 已提交
136 137 138 139

export function isNotFalse(val) {
  return val !== false;
}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

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;
}