chiro-canto/public/forum/topics/scripts/editor.js

81 lines
2.3 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}**`);
// }
if (window.getSelection) {
var sel = window.getSelection();
if (sel.rangeCount) {
var range = sel.getRangeAt(0).cloneRange();
range.surroundContents(document.createTextNode('**'));
sel.removeAllRanges();
sel.addRange(range);
}
}
}
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);
// }
function surroundSelection(element) {
if (window.getSelection) {
var sel = window.getSelection();
if (sel.rangeCount) {
var range = sel.getRangeAt(0).cloneRange();
range.surroundContents(element);
sel.removeAllRanges();
sel.addRange(range);
}
}
}