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

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

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

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

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

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

璃白.'s avatar
璃白. 已提交
105 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
  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
璃白. 已提交
131
//
璃白.'s avatar
璃白. 已提交
132 133 134
export function isNotEmpty(val) {
  return val !== null && val !== undefined;
}
璃白.'s avatar
璃白. 已提交
135 136 137 138

export function isNotFalse(val) {
  return val !== false;
}