deadjournal/js/main.js
2025-01-08 22:46:44 +05:00

31 lines
1.1 KiB
JavaScript

function addTag(tag) {
const textarea = document.getElementById('postContent');
if (textarea) {
switch (tag) {
case 'read_more':
textarea.value += "\n<!--more-->\n";
break;
case 'img':
textarea.value += "\n<img src=\"\" />\n";
break;
case 'list':
textarea.value += "\n<ul>\n<li></li>\n</ul>\n";
break;
}
}
}
function wrapSelectedTextWithTag(tag) {
const textarea = document.getElementById('postContent');
const selectionStart = textarea.selectionStart;
const selectionEnd = textarea.selectionEnd;
const before = textarea.value.substring(0, selectionStart);
const selectedText = textarea.value.substring(selectionStart, selectionEnd);
const after = textarea.value.substring(selectionEnd);
const newValue = before + `<${tag}>` + selectedText + `</${tag}>` + after;
textarea.value = newValue;
const newPos = selectionStart + tag.length + 2 + selectedText.length + tag.length + 3;
textarea.focus();
textarea.setSelectionRange(selectionStart, newPos);
}