utils.js 2.7 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
  };
}

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

// 初始化样式
export function initStyle({
璃白.'s avatar
璃白. 已提交
43
  dark,
璃白.'s avatar
璃白. 已提交
44 45 46
  borderColor,
  borderColorActive,
  textColor,
璃白.'s avatar
璃白. 已提交
47 48 49 50
  textColorActive,
  frameBgColor,
  contentBgColor,
  codeBgColor
璃白.'s avatar
璃白. 已提交
51
}) {
璃白.'s avatar
璃白. 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  // 夜晚模式
  if (dark) {
    borderColor = "#b2b2b2";
    borderColorActive = "#fff";
    textColor = "#fff";
    textColorActive = "#fff";
    frameBgColor = "#343434";
    codeBgColor = "#484848";
    contentBgColor = "#484848";
  }
  if (frameBgColor) {
    document.documentElement.style.setProperty(
      "--md-editor-frame-bg-color",
      frameBgColor
    );
  }
  if (contentBgColor) {
    document.documentElement.style.setProperty(
      "--md-editor-content-bg-color",
      contentBgColor
  );
  }

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

璃白.'s avatar
璃白. 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
  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
璃白. 已提交
108
//
璃白.'s avatar
璃白. 已提交
109 110 111
export function isNotEmpty(val) {
  return val !== null && val !== undefined;
}
璃白.'s avatar
璃白. 已提交
112 113 114 115

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