editor de texto, fiz o gerador de markdown
This commit is contained in:
63
javascript/escrever.js
Normal file
63
javascript/escrever.js
Normal file
@@ -0,0 +1,63 @@
|
||||
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");
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user