utils.js 663 字节
Newer Older
璃白.'s avatar
璃白. 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
  };
}

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