Fork自 gitcode_dev / Awesome-Markdown-Editor
体验新版 GitCode,发现更多精彩内容 >>
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) ); }