misuzu/assets/misuzu.js/utility.js

30 lines
1,011 B
JavaScript
Raw Normal View History

2024-01-24 21:53:26 +00:00
const $insertTags = function(target, tagOpen, tagClose) {
tagOpen = tagOpen || '';
tagClose = tagClose || '';
if(document.selection) {
target.focus();
2024-01-24 21:53:26 +00:00
const selected = document.selection.createRange();
selected.text = tagOpen + selected.text + tagClose;
target.focus();
} else if(target.selectionStart || target.selectionStart === 0) {
2024-01-24 21:53:26 +00:00
const startPos = target.selectionStart,
endPos = target.selectionEnd,
scrollTop = target.scrollTop;
target.value = target.value.substring(0, startPos)
+ tagOpen
+ target.value.substring(startPos, endPos)
+ tagClose
+ target.value.substring(endPos, target.value.length);
target.focus();
target.selectionStart = startPos + tagOpen.length;
target.selectionEnd = endPos + tagOpen.length;
2024-01-24 21:53:26 +00:00
target.scrollTop = scrollTop;
} else {
target.value += tagOpen + tagClose;
target.focus();
}
};