utils.js 1.4 KB
Newer Older
璃白.'s avatar
璃白. 已提交
1
// 获取选中文本信息
璃白.'s avatar
璃白. 已提交
2 3 4 5 6 7 8 9 10 11 12
export function getSelectionInfo(selectorId) {
  const selector = document.getElementById(selectorId);
  // const selection = window.getSelection();
  const { selectionStart = 0, selectionEnd = 0 } = selector;
  if (selectionStart === selectionEnd) return "";
  return {
    selectionStart,
    selectionEnd
  };
}

璃白.'s avatar
璃白. 已提交
13
// 工具栏格式化文本
璃白.'s avatar
璃白. 已提交
14 15 16 17 18 19 20 21 22 23
export function formatText(text, selectionInfo, startStr = "", endStr = "") {
  if (!selectionInfo) return text + startStr + endStr;
  return (
    text.slice(0, selectionInfo.selectionStart) +
    startStr +
    text.slice(selectionInfo.selectionStart, selectionInfo.selectionEnd) +
    endStr +
    text.slice(selectionInfo.selectionEnd)
  );
}
璃白.'s avatar
璃白. 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

// 初始化样式
export function initStyle({
  borderColor,
  borderColorActive,
  textColor,
  textColorActive
}) {
  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
    );
  }
}

// 
export function isNotEmpty(val) {
  return val !== null && val !== undefined;
}