fix: update colors each time editor content changes

This commit is contained in:
quenousimporte 2023-09-24 22:00:20 +02:00
parent 8c224d49d3
commit 763de412a5
1 changed files with 30 additions and 26 deletions

40
main.js
View File

@ -405,6 +405,12 @@ var snippets = [
cursor: -4 cursor: -4
}]; }];
function seteditorcontent(content)
{
md.value = content;
applycolors();
}
function encryptstring(str) function encryptstring(str)
{ {
console.log("encrypting..."); console.log("encrypting...");
@ -509,9 +515,9 @@ function createsubnote(suggestedtitle)
content: content content: content
} }
localdata.unshift(newnote); localdata.unshift(newnote);
md.value = md.value.substring(0, range.start) seteditorcontent(md.value.substring(0, range.start)
+ "[[" + title + "]]" + "[[" + title + "]]"
+ md.value.substring(range.end); + md.value.substring(range.end));
datachanged(); datachanged();
} }
}); });
@ -519,11 +525,11 @@ function createsubnote(suggestedtitle)
function comment() function comment()
{ {
md.value = md.value.substring(0, md.selectionStart) seteditorcontent(md.value.substring(0, md.selectionStart)
+ "<!-- " + "<!-- "
+ md.value.substring(md.selectionStart, md.selectionEnd) + md.value.substring(md.selectionStart, md.selectionEnd)
+ " -->" + " -->"
+ md.value.substring(md.selectionEnd); + md.value.substring(md.selectionEnd));
} }
function includesub() function includesub()
@ -535,10 +541,9 @@ function includesub()
if (confirm("Replace [[" + title + "]] by its content?")) if (confirm("Replace [[" + title + "]] by its content?"))
{ {
var subnote = getnote(title); var subnote = getnote(title);
md.value = seteditorcontent(md.value.substring(0, range.start)
md.value.substring(0, range.start)
+ subnote.content + subnote.content
+ md.value.substring(range.end); + md.value.substring(range.end));
if (confirm("Delete '" + title + "'?")) if (confirm("Delete '" + title + "'?"))
{ {
@ -982,7 +987,7 @@ function decryptnote()
decryptstring(md.value) decryptstring(md.value)
.then(decrypted => .then(decrypted =>
{ {
md.value = decrypted; seteditorcontent(decrypted);
resize(); resize();
}); });
} }
@ -1653,7 +1658,7 @@ function sortselection()
var selection = content.substring(range.start, range.end); var selection = content.substring(range.start, range.end);
var sorted = selection.split("\n").sort().join("\n"); var sorted = selection.split("\n").sort().join("\n");
md.value = content.substring(0, range.start) + sorted + content.substring(range.end); seteditorcontent(content.substring(0, range.start) + sorted + content.substring(range.end));
datachanged(); datachanged();
} }
@ -1853,10 +1858,10 @@ function insert(text, cursoroffset = 0, nbtodelete = 0)
{ {
var pos = md.selectionStart; var pos = md.selectionStart;
var content = md.value; var content = md.value;
md.value = seteditorcontent(
content.substring(0, pos - nbtodelete) content.substring(0, pos - nbtodelete)
+ text + text
+ content.substring(pos); + content.substring(pos));
setpos(pos - nbtodelete + text.length + cursoroffset); setpos(pos - nbtodelete + text.length + cursoroffset);
datachanged(); datachanged();
} }
@ -2307,7 +2312,7 @@ function restore()
{ {
if (confirm('restore "' + currentnote.title + '"?')) if (confirm('restore "' + currentnote.title + '"?'))
{ {
md.value = backup; seteditorcontent(backup);
datachanged(); datachanged();
} }
} }
@ -2317,7 +2322,7 @@ function insertheader()
if (preview.hidden && !md.value.startsWith("---\n")) if (preview.hidden && !md.value.startsWith("---\n"))
{ {
var headers = defaultheaders(currentnote.title); var headers = defaultheaders(currentnote.title);
md.value = headers + md.value; seteditorcontent(headers + md.value);
setpos(27); setpos(27);
} }
resize(); resize();
@ -2468,7 +2473,7 @@ function backspace(nb)
{ {
var pos = getpos(); var pos = getpos();
var c = md.value; var c = md.value;
md.value = c.substring(0, pos - nb) + c.substring(pos); seteditorcontent(c.substring(0, pos - nb) + c.substring(pos));
setpos(pos - nb); setpos(pos - nb);
datachanged(); datachanged();
} }
@ -2513,9 +2518,9 @@ function editorkeydown()
{ {
newtext = selection.replaceAll("\n", "\n "); newtext = selection.replaceAll("\n", "\n ");
} }
md.value = md.value.substring(0, range.start) seteditorcontent(md.value.substring(0, range.start)
+ newtext + newtext
+ md.value.substring(range.end); + md.value.substring(range.end));
var shift = 0; var shift = 0;
if (newtext.length < selection.length) if (newtext.length < selection.length)
@ -2641,7 +2646,7 @@ function bindfile(note)
title.value = note.title; title.value = note.title;
setwindowtitle(); setwindowtitle();
md.value = note.content || ""; seteditorcontent(note.content || "");
preview.innerHTML = md2html(md.value); preview.innerHTML = md2html(md.value);
if (changed) if (changed)
@ -2657,7 +2662,6 @@ function bindfile(note)
} }
setpos(note.pos || 0); setpos(note.pos || 0);
applycolors();
} }
function defaultheaders(title, tags = "") function defaultheaders(title, tags = "")