removed: folding

This commit is contained in:
quenousimporte 2023-05-05 12:07:03 +02:00
parent 48f1e5e352
commit 93a9ac74c7
1 changed files with 12 additions and 197 deletions

209
main.js
View File

@ -9,9 +9,7 @@ var defaultsettings =
margins: "7%", margins: "7%",
savedelay: 2000, savedelay: 2000,
foldmarkstart: 22232,
defaultpreviewinsplit: false, defaultpreviewinsplit: false,
enablefolding: false,
tagautocomplete: false, tagautocomplete: false,
titleinaccentcolor: false, titleinaccentcolor: false,
enablenetwork: false, enablenetwork: false,
@ -27,7 +25,6 @@ var codelanguages = ["xml", "js", "sql"];
var currentnote = null; var currentnote = null;
var fileindex = 0; var fileindex = 0;
var workerid = null; var workerid = null;
var folds = [];
var backup = ""; var backup = "";
var localdata = null; var localdata = null;
var saved = true; var saved = true;
@ -202,23 +199,6 @@ var commands = [
hint: "Show help", hint: "Show help",
action: showhelp action: showhelp
}, },
{
shortcut: "ctrl+shift+C",
hint: "Fold",
action: fold,
allowunsaved: true
},
{
shortcut: "ctrl+shift+O",
hint: "Unfold",
action: unfold,
allowunsaved: true
},
{
hint: "Unfold all",
action: unfoldall,
allowunsaved: true
},
{ {
hint: "Search tags", hint: "Search tags",
action: searchtags, action: searchtags,
@ -721,7 +701,7 @@ function showoutline()
{ {
var outline = {}; var outline = {};
var pos = 0; var pos = 0;
geteditorcontentwithfolds().split("\n").forEach((line, index, lines) => geteditorcontent().split("\n").forEach((line, index, lines) =>
{ {
pos += line.length + 1; pos += line.length + 1;
if (line.startsWith("#")) if (line.startsWith("#"))
@ -822,10 +802,6 @@ function clickeditor()
searchinlist(tags[tag.toLowerCase()]) searchinlist(tags[tag.toLowerCase()])
.then(loadnote); .then(loadnote);
} }
else
{
checkfolding();
}
} }
} }
@ -866,7 +842,7 @@ function showtemporaryinfo(data, title)
function getwords() function getwords()
{ {
return geteditorcontentwithfolds().split(/\s+\b/).length; return geteditorcontent().split(/\s+\b/).length;
} }
function issplit() function issplit()
@ -942,7 +918,7 @@ function share()
{ {
navigator.share( navigator.share(
{ {
text: geteditorcontentwithfolds(), text: geteditorcontent(),
title: currentnote.title title: currentnote.title
}); });
} }
@ -952,7 +928,7 @@ function sharehtml()
{ {
if (navigator.share) if (navigator.share)
{ {
var file = new File(['<html><body>' + md2html(geteditorcontentwithfolds()) + '</body></html>'], var file = new File(['<html><body>' + md2html(geteditorcontent()) + '</body></html>'],
currentnote.title + ".html", currentnote.title + ".html",
{ {
type: "text/html", type: "text/html",
@ -1020,7 +996,7 @@ function downloadnotewithsubs()
function downloadnote() function downloadnote()
{ {
download(currentnote.title + ".md", geteditorcontentwithfolds()); download(currentnote.title + ".md", geteditorcontent());
} }
function remotecallfailed(error) function remotecallfailed(error)
@ -1089,11 +1065,6 @@ function loadsettings()
applystyle(); applystyle();
if (!settings.enablefolding)
{
commands = commands.filter(c => !c.hint.toLowerCase().includes("fold"));
}
if (settings.titlebydefault) if (settings.titlebydefault)
{ {
toggletitle(); toggletitle();
@ -1383,15 +1354,6 @@ function queryremote(params)
}); });
} }
function applyfolds(content)
{
for (var i = folds.length - 1; i >= 0; i--)
{
content = content.replace(String.fromCodePoint(settings.foldmarkstart + i), folds[i]);
}
return content;
}
function getlinesrange() function getlinesrange()
{ {
var start = md.selectionStart; var start = md.selectionStart;
@ -1408,7 +1370,7 @@ function getlinesrange()
function sortselection() function sortselection()
{ {
var content = geteditorcontentwithfolds(); var content = geteditorcontent();
var range = {start: 0, end: content.length}; var range = {start: 0, end: content.length};
if (md.selectionStart != md.selectionEnd) if (md.selectionStart != md.selectionEnd)
{ {
@ -1428,134 +1390,11 @@ function selectlines()
md.selectionEnd = range.end; md.selectionEnd = range.end;
} }
function isfold(linecontent)
{
var res = linecontent.length == 1 && linecontent.codePointAt(0) >= settings.foldmarkstart;
if (res)
{
//security: if > 100, probably not a fold. Maybe an emoji. To improve.
res &= foldindex(linecontent) < 100;
}
return res;
}
function foldindex(foldmark)
{
return foldmark.codePointAt(0) - settings.foldmarkstart;
}
function fold()
{
// todo: forbid if > 100?
var start = md.selectionStart;
selectlines();
var content = md.value;
var char = String.fromCodePoint(settings.foldmarkstart + folds.length);
var value = content.substring(md.selectionStart, md.selectionEnd)
folds.push(value);
seteditorcontent(content.substring(0, md.selectionStart)
+ char
+ content.substring(md.selectionEnd));
md.focus();
setpos(start);
resize();
}
function unfold()
{
var range = getlinesrange();
var linecontent = md.value.substring(range.start, range.end);
if (isfold(linecontent))
{
var i = foldindex(linecontent);
md.value = md.value.replace(linecontent, folds[i]);
md.focus();
setpos(range.start + folds[i].length);
}
resize();
}
function unfoldall()
{
md.value = geteditorcontentwithfolds();
resetfolds();
setpos(0);
md.focus();
resize();
}
function checkfolding()
{
var range = getlinesrange();
var line = md.value.substring(range.start, range.end);
var sectionmark = sectionmarks.find(m => line.startsWith(m));
if (sectionmark)
{
event.preventDefault();
// move to next line
setpos(range.end + 1);
range = getlinesrange();
var nextline = md.value.substring(range.start, range.end);
if (isfold(nextline))
{
unfold();
}
else
{
// find next occurence. If not found take all the remaining file.
if (md.value.includes("\n" + sectionmark, range.end))
{
sectionend = md.value.indexOf("\n" + sectionmark, range.end);
}
else
{
sectionend = md.value.length;
}
// keep last empty line if any
if (md.value[sectionend] == "\n")
{
sectionend--;
}
md.setSelectionRange(range.start, sectionend);
if (settings.enablefolding)
{
fold();
}
else
{
createsubnote(currentnote.title + " -" + line.replace(/#/g, ""));
}
}
}
else if (isfold(line))
{
event.preventDefault();
unfold();
}
}
function seteditorcontent(content) function seteditorcontent(content)
{ {
md.value = content; md.value = content;
} }
function geteditorcontentwithfolds()
{
return applyfolds(md.value);
}
function geteditorcontent() function geteditorcontent()
{ {
return md.value; return md.value;
@ -1569,23 +1408,6 @@ function ontopbarclick()
} }
} }
/*function checkfoldmismatch()
{
start = settings.foldmarkstart.toString(16);
end = (settings.foldmarkstart + 100).toString(16);
var match = md.value.match(new RegExp("[\\u" + start + "-\\u" + end + "]", "g"));
var markcount = 0;
if (match)
{
markcount = match.length;
}
var diff = folds.length - markcount;
if (diff)
{
console.warn(diff + " fold(s) missing.");
}
}*/
function md2html(content) function md2html(content)
{ {
// dumb fallback for offline mode // dumb fallback for offline mode
@ -1893,7 +1715,7 @@ function save()
return; return;
} }
var content = geteditorcontentwithfolds(); var content = geteditorcontent();
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");
@ -1921,7 +1743,7 @@ function save()
.finally(() => .finally(() =>
{ {
pending = false; pending = false;
if (content != geteditorcontentwithfolds()) if (content != geteditorcontent())
{ {
console.log("but content changed: will save again"); console.log("but content changed: will save again");
datachanged(); datachanged();
@ -2111,7 +1933,7 @@ function restore()
function insertheader() function insertheader()
{ {
if (!geteditorcontentwithfolds().startsWith("---")) if (!geteditorcontent().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;
@ -2342,7 +2164,7 @@ function insertautocomplete(selectednote)
function togglepreview() function togglepreview()
{ {
preview.innerHTML = md2html(geteditorcontentwithfolds()); preview.innerHTML = md2html(geteditorcontent());
md.hidden = !md.hidden; md.hidden = !md.hidden;
preview.hidden = !preview.hidden; preview.hidden = !preview.hidden;
@ -2368,7 +2190,7 @@ function withsubs()
var tempnote = var tempnote =
{ {
title: currentnote.title + " (with subnotes)", title: currentnote.title + " (with subnotes)",
content: geteditorcontentwithfolds() content: geteditorcontent()
}; };
var kids = children(tempnote); var kids = children(tempnote);
@ -2407,11 +2229,6 @@ function togglepreviewwithsubs()
} }
} }
function resetfolds()
{
folds = [];
}
function bindfile(note) function bindfile(note)
{ {
var changed = currentnote != note; var changed = currentnote != note;
@ -2422,9 +2239,7 @@ function bindfile(note)
setwindowtitle(); setwindowtitle();
seteditorcontent(note.content || ""); seteditorcontent(note.content || "");
preview.innerHTML = md2html(geteditorcontentwithfolds()); preview.innerHTML = md2html(geteditorcontent());
resetfolds();
if (changed) if (changed)
{ {