added: experimental newtwork feature

added: preview with subnotes
This commit is contained in:
quenousimporte 2023-02-05 10:04:07 +01:00
parent cba4848da0
commit 276e4a0eff
3 changed files with 151 additions and 28 deletions

View File

@ -16,6 +16,12 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js" integrity="sha512-LhccdVNGe2QMEfI3x4DVV3ckMRe36TfydKss6mJpdHjNFiV07dFpS2xzeZedptKZrwxfICJpez09iNioiSZ3hA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/2.1.0/showdown.min.js" integrity="sha512-LhccdVNGe2QMEfI3x4DVV3ckMRe36TfydKss6mJpdHjNFiV07dFpS2xzeZedptKZrwxfICJpez09iNioiSZ3hA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="main.js"></script> <script src="main.js"></script>
<script type="text/javascript" src="https://unpkg.com/vis-network/standalone/umd/vis-network.min.js"></script>
<div id="networkpage" hidden>
<div id="network"></div>
</div>
<div id="authentpage" hidden> <div id="authentpage" hidden>
<div id="bigtitle">notes</div> <div id="bigtitle">notes</div>
<div> <div>

163
main.js
View File

@ -13,7 +13,8 @@ var defaultsettings =
defaultpreviewinsplit: false, defaultpreviewinsplit: false,
enablefolding: false, enablefolding: false,
tagautocomplete: false, tagautocomplete: false,
titleinaccentcolor: false titleinaccentcolor: false,
enablenetwork: false
}; };
//builtin //builtin
@ -186,6 +187,12 @@ var commands = [
action: togglepreview, action: togglepreview,
allowunsaved: true allowunsaved: true
}, },
{
shortcut: "ctrl+shift+M",
hint: "Toggle preview with merged subnotes",
action: togglepreviewwithsubs,
allowunsaved: true
},
{ {
shortcut: "ctrl+d", shortcut: "ctrl+d",
hint: "Delete note", hint: "Delete note",
@ -638,8 +645,82 @@ function connected(note)
function shownotelinks() function shownotelinks()
{ {
searchinlist(connected(currentnote).map(n => n.title)) if (settings.enablenetwork)
.then(loadnote); {
networkpage.hidden = false;
function id(note)
{
return localdata.indexOf(note);
}
var nodes = [];
var edges = [];
var list = [currentnote];
while (list.length)
{
var current = list.shift();
if (!nodes.find(n => n.id == id(current)))
{
nodes.push(
{
id: id(current),
label: current.title
});
var buddies = children(current).concat(parents(current));
list = list.concat(buddies);
buddies.
forEach(buddy => {
if (!edges.find(edge => (edge.to == id(current) && edge.from == id(buddy)) || (edge.to == id(buddy) && edge.from == id(current))))
{
edges.push({
from: id(current),
to: id(buddy)
});
}
});
}
}
var data = {
nodes: nodes,
edges: edges
};
var options =
{
nodes:
{
color:
{
background: settings.bgcolor,
border: settings.fontcolor,
},
font:
{
color: settings.fontcolor,
size: 16,
face: settings.fontfamily
}
}
};
var graph = new vis.Network(network, data, options);
graph.on("click", function(event)
{
loadnote(nodes.find(n => n.id == event.nodes[0]).label);
networkpage.hidden = true;
});
}
else
{
searchinlist(connected(currentnote).map(n => n.title))
.then(loadnote);
}
} }
@ -933,32 +1014,11 @@ function downloadvault()
function downloadnotewithsubs() function downloadnotewithsubs()
{ {
try var note = withsubs();
if (note)
{ {
descendants(currentnote); download(note.title + ".md", note.content);
} }
catch (err)
{
showtemporaryinfo("Loop detected");
return;
}
var tempnote =
{
title: currentnote.title + " (with subnotes)",
content: getnotecontent()
};
var kids = children(tempnote);
while (kids.length)
{
kids.forEach(t =>
{
tempnote.content = tempnote.content.replaceAll("[[" + t + "]]", getnote(t).content);
});
kids = children(tempnote);
}
download(tempnote.title + ".md", tempnote.content);
} }
function downloadnote() function downloadnote()
@ -1807,6 +1867,7 @@ function showhelp()
help.push("## Libs"); help.push("## Libs");
help.push("[Showdown](https://showdownjs.com/)"); help.push("[Showdown](https://showdownjs.com/)");
help.push("[vis-network](https://visjs.org/)");
help.push("## Fonts"); help.push("## Fonts");
help.push("[Inconsolata](https://levien.com/type/myfonts/inconsolata.html)"); help.push("[Inconsolata](https://levien.com/type/myfonts/inconsolata.html)");
@ -2154,6 +2215,54 @@ function togglepreview()
} }
} }
function withsubs()
{
try
{
descendants(currentnote);
}
catch (err)
{
showtemporaryinfo(err);
return null;
}
var tempnote =
{
title: currentnote.title + " (with subnotes)",
content: getnotecontent()
};
var kids = children(tempnote);
while (kids.length)
{
kids.forEach(kid =>
{
tempnote.content = tempnote.content.replaceAll("[[" + kid.title + "]]", kid.content);
});
kids = children(tempnote);
}
return tempnote;
}
function togglepreviewwithsubs()
{
var note = withsubs();
if (note)
{
preview.innerHTML = md2html(note.content);
md.hidden = !md.hidden;
preview.hidden = !preview.hidden;
if (preview.hidden)
{
resize();
md.focus();
}
}
}
function resetfolds() function resetfolds()
{ {
folds = []; folds = [];

View File

@ -53,6 +53,14 @@ body::-webkit-scrollbar-thumb {
background-color: inherit; background-color: inherit;
} }
#network {
position: absolute;
top: 0;
width: 100%;
height: 600px;
opacity: 1;
}
/* top bar content */ /* top bar content */
#topbar { #topbar {