qown-zettelkasten/zettel.qml

196 lines
5.3 KiB
QML

import QtQml 2.0
import QOwnNotesTypes 1.0
import com.qownnotes.noteapi 1.0
QtObject {
property string idMarker;
property variant settingsVariables: [
{
"identifier": "idMarker",
"name": "Zettelkasten ID Marker",
"description": "The in-text marker denoting that a Zettel ID is following. Make sure to use something which is not ambiguous, and does not appear in any other way than to actually denote the Zettel ID. Multiple characters are fine (as in the default '§§')",
"type": "string",
"default": "§§",
}
]
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();
// Link the from the old Zettel to the new one if anything was selected
if(selected !== "") {
script.log("selected: " + 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());
if(selected == false) {
script.setCurrentNote(zettelSelectorDialog(true));
} else {
script.setCurrentNote(getNoteByZettelId(selected));
}
}
// ----------------------------
// --- Autocomplete Linking ---
// ----------------------------
function autocompletionHook() {
let uid = verifyZettelId(script.noteTextEditCurrentWord(false));
if(uid == false) {
return [];
}
let zettel = getNoteByZettelId(uid);
script.log("Autocomplete detected, Switch to " + idMarker + uid + " - " + zettel.name)
script.setCurrentNote(zettel);
return [];
}
// ----------------------------
// ---- Internal Functions ----
// ----------------------------
function zettelSelectorDialog(editableTextBox, zettelArray) {
if (zettelArray == null) {
zettelArray = [];
script.fetchNoteIdsByNoteTextPart(idMarker).forEach(function (noteId){
let note = script.fetchNoteById(noteId);
zettelArray.push(note.name + " --id:"+note.id);
});
}
let selected = script.inputDialogGetItem("Zettel", "Select a Zettel", zettelArray, 0, editableTextBox);
return script.fetchNoteById(selected.substring(selected.search(/--id:/)+5));
}
function extractZettelIdFromString(text) {
let regex = new RegExp(idMarker + '\\d{14}\\s')
let markerpos = text.search(regex) + 2;
if (markerpos == -1) {
script.log("No Zettel ID Found in text");
return;
}
return text.substring(markerpos, markerpos+14);
}
function verifyZettelId(id) {
if (id.match(new RegExp(idMarker + '^\\d{14}$')) != null) {
return id.substring(2);
} else if (id.match(/^\d{14}$/) != null) {
return id;
} else {
return false;
}
}
function getNoteByZettelId(uid) {
let noteId = script.fetchNoteIdsByNoteTextPart(idMarker + uid);
if (noteId.length == 0) {
script.log("No note found for Zettel ID: " + idMarker + uid);
return;
}
return script.fetchNoteById(noteId[0]);
}
function createLinkInPlace(uid, selected) {
script.noteTextEditWrite("["+selected+"](zettel://"+uid+")");
}
function createZettel(uid, headline) {
// Create Headline - empty if nothing selected
if (headline == "") {
headline = "Zettel"
}
let text = headline.toString() + "\n";
// Create Headline Separator
text += "===============\n";
// Create uid tag
text += idMarker + uid.toString() + "\n\n";
// Create content unit (i.e. space)
text += "\n\n\n\n\n";
// Create Related unit - FIXME add option to automatically link back to the card this was created from
text += "## Related\n\n\n\n";
// Create Source unit
text += "## Reference\n\n";
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)
}
}