qown-zettelkasten/zettel.qml

254 lines
7.9 KiB
QML
Raw Normal View History

2018-11-21 16:07:57 +00:00
import QtQml 2.0
import QOwnNotesTypes 1.0
import com.qownnotes.noteapi 1.0
QtObject {
property bool anchorInFilename;
2018-11-21 17:47:21 +00:00
property string idMarker;
property bool wikiLinks;
property string template;
2018-11-21 17:47:21 +00:00
property variant settingsVariables: [
{
"identifier": "anchorInFilename",
"name": "Display Zettel anchors in note filenames",
"description": "By default, the anchor of a Zettel will appear in its body, prepended with its ID Marker selected below.\nIf enabled, this will instead put the anchor in the filename itself and disregard any anchors in Zettel bodies.",
"type": "boolean",
"default": "false",
},
2018-11-21 17:47:21 +00:00
{
"identifier": "idMarker",
"name": "Zettelkasten anchor marker",
"description": "The in-text marker denoting that a Zettel ID is following.\nMake sure to use something which is not ambiguous, and does not appear in any other way than to actually denote the Zettel ID.\nMultiple characters are fine (as in the default '§§')\nIf 'In-Title Anchors' are selected above this option has no effect.",
2018-11-21 17:47:21 +00:00
"type": "string",
"default": "§§",
},
{
"identifier": "wikiLinks",
"name": "Default to [[WikiLinks]]",
"description": "By default, links will take the form of standard markdown links.\nThis will make automatically created links between Zettel take the form of wiki-links instead.\nThe 'go to link' function understands both types, regardless of this choice.",
"type": "boolean",
"default": "false",
},
{
"identifier": "template",
"name": "Zettel Template",
"description": "What gets created when letting the script create a new Zettel.\nUse {headline} and {anchor} as placeholders to be put there on Zettel creation.",
"type": "text",
"default": "{headline}\n===============\n{anchor}\n\n\n\n\n\n\n## Related\n\n\n\n## Reference\n\n",
},
2018-11-21 17:47:21 +00:00
]
2018-11-21 16:07:57 +00:00
function init() {
script.registerCustomAction("zettelCreate", "Create Zettelkasten Zettel", "Zettel", "address-book-new");
script.registerCustomAction("manualLink", "Insert Zettelkasten Link", "InsertLink", "insert-link");
script.registerCustomAction("jumpToId", "Follow Zettelkasten Link", "Link", "go-next");
}
function customActionInvoked(identifier) {
switch(identifier) {
case "zettelCreate":
onZettelCreateClicked();
break;
case "jumpToId":
onJumpToZettelClicked();
break;
case "manualLink":
onInsertLinkClicked();
break;
}
}
// ----------------------------
// ----- BUTTON FUNCTIONS -----
// ----------------------------
function onZettelCreateClicked() {
let uid = exactDate();
let selected = script.noteTextEditSelectedText().trim();
2018-11-21 16:07:57 +00:00
// Link the from the old Zettel to the new one if anything was selected
if(selected !== "") {
createLinkInPlace(uid, selected);
}
// Create a Zettel
createZettel(uid, selected);
}
function onInsertLinkClicked() {
let selected = script.noteTextEditSelectedText();
let clipboard = script.clipboard();
let uid = null
if (verifyZettelId(clipboard)) {
uid = verifyZettelId(clipboard);
} else {
let otherNote = zettelSelectorDialog(true);
uid = extractZettelIdFromString(otherNote.noteText);
}
if (uid == null) {
return;
}
createLinkInPlace(uid, selected)
}
function onJumpToZettelClicked() {
let selected = verifyZettelId(script.noteTextEditSelectedText(), true) ? verifyZettelId(script.noteTextEditSelectedText(), true) : verifyZettelId(script.noteTextEditCurrentWord(true), true);
2018-11-21 16:07:57 +00:00
if (selected) {
setNoteToZettelIdOrElse(selected, function(uid) {script.informationMessageBox("Zettel " + uid + " does not exist.")});
2018-11-21 16:07:57 +00:00
} else {
script.setCurrentNote(zettelSelectorDialog(true));
2018-11-21 16:07:57 +00:00
}
}
// ----------------------------
// --- Autocomplete Linking ---
// ----------------------------
function autocompletionHook() {
let uid = verifyZettelId(script.noteTextEditCurrentWord(true), true);
2018-11-21 16:07:57 +00:00
if(uid == false) {
return [];
}
setNoteToZettelIdOrElse(uid);
2018-11-21 16:07:57 +00:00
return [];
}
// ----------------------------
// ---- Internal Functions ----
// ----------------------------
function zettelSelectorDialog(editableTextBox, zettelArray) {
if (zettelArray == null) {
zettelArray = [];
fetchZettelIDs().forEach(function (noteId){
2018-11-21 16:07:57 +00:00
let note = script.fetchNoteById(noteId);
zettelArray.push(note.name + " --id:"+note.id);
});
}
if (zettelArray.length == 0) {
script.informationMessageBox("No valid Zettel found.");
}
2018-11-21 16:07:57 +00:00
let selected = script.inputDialogGetItem("Zettel", "Select a Zettel", zettelArray, 0, editableTextBox);
// FIXME dont display id in names - rather, go getNotebyName(name) -> (get its id) -> set note to it
2018-11-21 16:07:57 +00:00
return script.fetchNoteById(selected.substring(selected.search(/--id:/)+5));
}
function extractZettelIdFromString(text) {
2018-11-21 17:47:21 +00:00
let regex = new RegExp(idMarker + '\\d{14}\\s')
let markerpos = text.search(regex) + 2;
2018-11-21 16:07:57 +00:00
if (markerpos == -1) {
return;
}
return text.substring(markerpos, markerpos+14);
}
function stripZettelId(link) {
return link.replace(/\D*/g, "");
}
function verifyZettelId(id, shouldStrip) {
if (shouldStrip) id = stripZettelId(id);
if (id.match(/^\d{14}$/) != null) {
2018-11-21 16:07:57 +00:00
return id;
} else if (id.match(new RegExp(idMarker + '^\\d{14}$')) != null) {
return id.substring(2);
2018-11-21 16:07:57 +00:00
} else {
return false;
}
}
// Returns List of Zettel ids with the specified uID.
// If no uID provided returns all Zettel found in the current directory.
function fetchZettelIDs(uid) {
let uid = uid ? uid : "";
let noteIds = anchorInFilename ? _getAllZettelFromTitle(uid) : _getAllZettelFromAnchor(uid);
return noteIds;
}
// Looks for the combination of idMarker and uID (optionally) in all note texts.
function _getAllZettelFromAnchor(optionaluId) {
return script.fetchNoteIdsByNoteTextPart(idMarker + optionaluId);
}
// Looks for the uID (optionally) in all note texts.
function _getAllZettelFromTitle(optionaluID) {
let allnotes = script.fetchNoteIdsByNoteTextPart("");
let noteIds = [];
if (optionaluID != "") {
allnotes.forEach(function(noteId) {
if (script.fetchNoteById(noteId).name.startsWith(optionaluID)) noteIds.push(noteId);
});
return noteIds;
}
allnotes.forEach(function(noteId) {
if (script.fetchNoteById(noteId).name.search(/^\d{14}/) != -1) noteIds.push(noteId);
});
return noteIds;
}
2018-11-21 16:07:57 +00:00
function getNoteByZettelId(uid) {
let noteId = fetchZettelIDs(uid);
2018-11-21 16:07:57 +00:00
if (noteId.length == 0) {
return;
}
return script.fetchNoteById(noteId[0]);
}
function setNoteToZettelIdOrElse(uid, orElse) {
let zettel = getNoteByZettelId(uid);
if (zettel == null) {
if(orElse != null) orElse(uid);
return;
}
script.setCurrentNote(zettel);
}
2018-11-21 16:07:57 +00:00
function createLinkInPlace(uid, selected) {
wikiLinks ? script.noteTextEditWrite("[[" + uid + "]] " + selected) : script.noteTextEditWrite("["+selected+"](" + uid + ")");
2018-11-21 16:07:57 +00:00
}
function createZettel(uid, headline) {
// Create Headline - empty if nothing selected
if (headline == "") {
headline = "Zettel"
}
let text = template;
2018-11-21 16:07:57 +00:00
if (anchorInFilename) {
text = text.replace(/\{headline\}/, uid.toString() + " " + headline).replace(/\{anchor\}/, "");
} else {
text = text.replace(/\{headline\}/, headline).replace(/\{anchor\}/, idMarker + uid.toString());
}
2018-11-21 16:07:57 +00:00
script.createNote(text);
return script.currentNote();
}
function exactDate() {
let date = new Date();
return (""
+ date.getUTCFullYear().toString()
+ padToLen2((date.getUTCMonth()+1).toString())
+ padToLen2( date.getUTCDate().toString())
+ padToLen2( date.getUTCHours().toString())
+ padToLen2( date.getUTCMinutes().toString())
+ padToLen2( date.getUTCSeconds().toString())
);
}
function padToLen2(text) {
return ("00" + text).slice(-2)
}
}