qown-zettelkasten/zettel.qml
Marty 7611441252 Add Customizable Template
Added option to set a template to be used upon creating a new Zettel.
Makes use of 2 placeholders:
{headline} and {anchor} which get inserted on creation.
2018-11-28 18:56:47 +00:00

254 lines
No EOL
7.9 KiB
QML

import QtQml 2.0
import QOwnNotesTypes 1.0
import com.qownnotes.noteapi 1.0
QtObject {
property bool anchorInFilename;
property string idMarker;
property bool wikiLinks;
property string template;
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",
},
{
"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.",
"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",
},
]
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();
// 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);
if (selected) {
setNoteToZettelIdOrElse(selected, function(uid) {script.informationMessageBox("Zettel " + uid + " does not exist.")});
} else {
script.setCurrentNote(zettelSelectorDialog(true));
}
}
// ----------------------------
// --- Autocomplete Linking ---
// ----------------------------
function autocompletionHook() {
let uid = verifyZettelId(script.noteTextEditCurrentWord(true), true);
if(uid == false) {
return [];
}
setNoteToZettelIdOrElse(uid);
return [];
}
// ----------------------------
// ---- Internal Functions ----
// ----------------------------
function zettelSelectorDialog(editableTextBox, zettelArray) {
if (zettelArray == null) {
zettelArray = [];
fetchZettelIDs().forEach(function (noteId){
let note = script.fetchNoteById(noteId);
zettelArray.push(note.name + " --id:"+note.id);
});
}
if (zettelArray.length == 0) {
script.informationMessageBox("No valid Zettel found.");
}
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
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) {
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) {
return id;
} else if (id.match(new RegExp(idMarker + '^\\d{14}$')) != null) {
return id.substring(2);
} 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;
}
function getNoteByZettelId(uid) {
let noteId = fetchZettelIDs(uid);
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);
}
function createLinkInPlace(uid, selected) {
wikiLinks ? script.noteTextEditWrite("[[" + uid + "]] " + selected) : script.noteTextEditWrite("["+selected+"](" + uid + ")");
}
function createZettel(uid, headline) {
// Create Headline - empty if nothing selected
if (headline == "") {
headline = "Zettel"
}
let text = template;
if (anchorInFilename) {
text = text.replace(/\{headline\}/, uid.toString() + " " + headline).replace(/\{anchor\}/, "");
} else {
text = text.replace(/\{headline\}/, headline).replace(/\{anchor\}/, idMarker + uid.toString());
}
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)
}
}