Files
biblioteca-de-contos/javascript/escrever.js

64 lines
1.5 KiB
JavaScript

function setText(textarea, text, inicio, fim, _in) {
textarea.value = text;
textarea.selectionStart = inicio + _in.length;
textarea.selectionEnd = fim + _in.length;
textarea.focus();
}
$(function () {
function editText(textarea, _in, _out) {
let inicio = textarea.selectionStart;
let fim = textarea.selectionEnd;
let valor = textarea.value;
switch (inicio === fim) {
case true:
setText(
textarea,
valor.slice(0, inicio) + _in + "texto aqui" + _out + valor.slice(fim),
inicio,
fim,
_in,
);
break;
default:
setText(
textarea,
valor.slice(0, inicio) +
_in +
valor.slice(inicio, fim) +
_out +
valor.slice(fim),
inicio,
fim,
_in,
);
break;
}
}
$("#text-write ul span").on("click", function () {
let list = [
{ start: "# ", end: "" },
{ start: "## ", end: "" },
{ start: "### ", end: "" },
{ start: "**", end: "**" },
{ start: "_", end: "_" },
{
addImg: function () {
editText($("#edit")[0], "[[img]{},'", "',size,100,100]");
},
}, // especial
{ start: "- ", end: "" },
{ start: "> ", end: "" },
];
let md = list[$(this).index()];
try {
md.addImg ? md.addImg() : editText($("#edit")[0], md.start, md.end);
} catch {
alert("vou usar essa parte para executar a previw");
}
});
});