60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
|
function select() {
|
||
|
var selectedText = '';
|
||
|
|
||
|
// window.getSelection
|
||
|
if (window.getSelection) {
|
||
|
selectedText = window.getSelection();
|
||
|
}
|
||
|
// document.getSelection
|
||
|
else if (document.getSelection) {
|
||
|
selectedText = document.getSelection();
|
||
|
}
|
||
|
// document.selection
|
||
|
else if (document.selection) {
|
||
|
selectedText =
|
||
|
document.selection.createRange().text;
|
||
|
}
|
||
|
return selectedText;
|
||
|
}
|
||
|
|
||
|
|
||
|
// let bold_button = document.getElementById('bold').addEventListener('click', function() {
|
||
|
function bold() {
|
||
|
let selection = select();
|
||
|
if (selection == "") {
|
||
|
alert("Please select text to bold.");
|
||
|
}
|
||
|
else {
|
||
|
document.getElementById('reply').innerHTML.replace(selection, `**${selection}**`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
let url_button = document.getElementById('url');
|
||
|
let url_form = document.getElementById('url-form');
|
||
|
url_button.addEventListener('click', function() {
|
||
|
url_form.style.display = "block";
|
||
|
document.getElementById("submit-url").addEventListener("click", function() {
|
||
|
let url = document.getElementById('url-input').value;
|
||
|
let description = document.getElementById('url-description').value;
|
||
|
description = description == "" ? url : description;
|
||
|
let markdown = `[${description}](${url})`;
|
||
|
document.getElementById('reply').innerHTML += markdown;
|
||
|
url_form.style.display = "none";
|
||
|
// setCaret();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
|
||
|
|
||
|
// function setCaret() {
|
||
|
// let el = document.getElementById('reply');
|
||
|
// let range = document.createRange();
|
||
|
// let sel = window.getSelection();
|
||
|
|
||
|
// range.setStar t(el, -1);
|
||
|
// range.collapse(true);
|
||
|
|
||
|
// sel.removeAllRanges();
|
||
|
// sel.addRange(range);
|
||
|
// }
|