refactor: first steps to add an editor abstraction layer

This commit is contained in:
quenousimporte 2023-03-03 09:52:11 +01:00
parent 96e247459d
commit 3298cded32
1 changed files with 23 additions and 18 deletions

41
main.js
View File

@ -740,7 +740,7 @@ function showoutline()
{ {
var outline = {}; var outline = {};
var pos = 0; var pos = 0;
getnotecontent().split("\n").forEach((line, index, lines) => geteditorcontentwithfolds().split("\n").forEach((line, index, lines) =>
{ {
pos += line.length + 1; pos += line.length + 1;
if (line.startsWith("#")) if (line.startsWith("#"))
@ -885,7 +885,7 @@ function showtemporaryinfo(data, title)
function getwords() function getwords()
{ {
return getnotecontent().split(/\s+\b/).length; return geteditorcontentwithfolds().split(/\s+\b/).length;
} }
function issplit() function issplit()
@ -961,7 +961,7 @@ function share()
{ {
navigator.share( navigator.share(
{ {
text: getnotecontent(), text: geteditorcontentwithfolds(),
title: currentnote.title title: currentnote.title
}); });
} }
@ -971,7 +971,7 @@ function sharehtml()
{ {
if (navigator.share) if (navigator.share)
{ {
var file = new File(['<html><body>' + md2html(getnotecontent()) + '</body></html>'], var file = new File(['<html><body>' + md2html(geteditorcontentwithfolds()) + '</body></html>'],
currentnote.title + ".html", currentnote.title + ".html",
{ {
type: "text/html", type: "text/html",
@ -1040,7 +1040,7 @@ function downloadnotewithsubs()
function downloadnote() function downloadnote()
{ {
download(currentnote.title + ".md", getnotecontent()); download(currentnote.title + ".md", geteditorcontentwithfolds());
} }
function remotecallfailed(error) function remotecallfailed(error)
@ -1321,7 +1321,7 @@ function getlinesrange()
function sortselection() function sortselection()
{ {
var content = getnotecontent(); var content = geteditorcontentwithfolds();
var range = {start: 0, end: content.length}; var range = {start: 0, end: content.length};
if (md.selectionStart != md.selectionEnd) if (md.selectionStart != md.selectionEnd)
{ {
@ -1369,7 +1369,7 @@ function fold()
folds.push(value); folds.push(value);
setnotecontent(content.substring(0, md.selectionStart) seteditorcontent(content.substring(0, md.selectionStart)
+ char + char
+ content.substring(md.selectionEnd)); + content.substring(md.selectionEnd));
@ -1396,7 +1396,7 @@ function unfold()
function unfoldall() function unfoldall()
{ {
md.value = getnotecontent(); md.value = geteditorcontentwithfolds();
resetfolds(); resetfolds();
setpos(0); setpos(0);
md.focus(); md.focus();
@ -1459,16 +1459,21 @@ function checkfolding()
} }
} }
function setnotecontent(content) function seteditorcontent(content)
{ {
md.value = content; md.value = content;
} }
function getnotecontent() function geteditorcontentwithfolds()
{ {
return applyfolds(md.value); return applyfolds(md.value);
} }
function geteditorcontent()
{
return md.value;
}
function ontopbarclick() function ontopbarclick()
{ {
if (title.hidden) if (title.hidden)
@ -1794,7 +1799,7 @@ function save()
return; return;
} }
var content = getnotecontent(); var content = geteditorcontentwithfolds();
if ((content == "" && backup != "") || content == "null" || content == "undefined") if ((content == "" && backup != "") || content == "null" || content == "undefined")
{ {
showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved"); showtemporaryinfo("Invalid content '" + content + "', file '" + currentnote.title + "' not saved");
@ -1822,7 +1827,7 @@ function save()
.finally(() => .finally(() =>
{ {
pending = false; pending = false;
if (content != getnotecontent()) if (content != geteditorcontentwithfolds())
{ {
console.log("but content changed: will save again"); console.log("but content changed: will save again");
datachanged(); datachanged();
@ -2000,14 +2005,14 @@ function restore()
{ {
if (confirm('restore "' + currentnote.title + '"?')) if (confirm('restore "' + currentnote.title + '"?'))
{ {
setnotecontent(backup); seteditorcontent(backup);
datachanged(); datachanged();
} }
} }
function insertheader() function insertheader()
{ {
if (!getnotecontent().startsWith("---")) if (!geteditorcontentwithfolds().startsWith("---"))
{ {
var headers = "---\ndate: " + (new Date).toISOString().substring(0, 10) + "\ntags: \n---\n\n"; var headers = "---\ndate: " + (new Date).toISOString().substring(0, 10) + "\ntags: \n---\n\n";
md.value = headers + md.value; md.value = headers + md.value;
@ -2236,7 +2241,7 @@ function insertautocomplete(selectednote)
function togglepreview() function togglepreview()
{ {
preview.innerHTML = md2html(getnotecontent()); preview.innerHTML = md2html(geteditorcontentwithfolds());
md.hidden = !md.hidden; md.hidden = !md.hidden;
preview.hidden = !preview.hidden; preview.hidden = !preview.hidden;
@ -2262,7 +2267,7 @@ function withsubs()
var tempnote = var tempnote =
{ {
title: currentnote.title + " (with subnotes)", title: currentnote.title + " (with subnotes)",
content: getnotecontent() content: geteditorcontentwithfolds()
}; };
var kids = children(tempnote); var kids = children(tempnote);
@ -2315,8 +2320,8 @@ function bindfile(note)
title.value = note.title; title.value = note.title;
setwindowtitle(); setwindowtitle();
setnotecontent(note.content || ""); seteditorcontent(note.content || "");
preview.innerHTML = md2html(getnotecontent()); preview.innerHTML = md2html(geteditorcontentwithfolds());
resetfolds(); resetfolds();