From ff8129188401df929be735228ee9161742e76ba5 Mon Sep 17 00:00:00 2001 From: Marty Date: Wed, 21 Nov 2018 16:07:57 +0000 Subject: [PATCH] initial commit --- zettel.qml | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100644 zettel.qml diff --git a/zettel.qml b/zettel.qml new file mode 100644 index 0000000..711b757 --- /dev/null +++ b/zettel.qml @@ -0,0 +1,183 @@ +import QtQml 2.0 +import QOwnNotesTypes 1.0 +import com.qownnotes.noteapi 1.0 + +QtObject { + 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 §§" + uid + " - " + zettel.name) + + script.setCurrentNote(zettel); + + return []; + } + + +// ---------------------------- +// ---- Internal Functions ---- +// ---------------------------- + + function zettelSelectorDialog(editableTextBox, zettelArray) { + if (zettelArray == null) { + zettelArray = []; + script.fetchNoteIdsByNoteTextPart("§§").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 markerpos = text.search(/§§\d{14}\s/) + 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(/^§§\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("§§" + uid); + if (noteId.length == 0) { + script.log("No note found for Zettel ID: §§" + 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 += "§§" + 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) + } +}