From 856a040ca6f803c66d38da154afd606d2032d57c Mon Sep 17 00:00:00 2001 From: Patrizio Bekerle Date: Sat, 5 May 2018 10:47:01 +0200 Subject: [PATCH] added new script unique-note-id --- unique-note-id/info.json | 10 +++++ unique-note-id/unique-note-id.qml | 73 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 unique-note-id/info.json create mode 100644 unique-note-id/unique-note-id.qml diff --git a/unique-note-id/info.json b/unique-note-id/info.json new file mode 100644 index 0000000..da7df1a --- /dev/null +++ b/unique-note-id/info.json @@ -0,0 +1,10 @@ +{ + "name": "Unique note id", + "identifier": "unique-note-id", + "script": "unique-note-id.qml", + "authors": ["@pbek"], + "platforms": ["linux", "macos", "windows"], + "version": "1.0.0", + "minAppVersion": "18.05.1", + "description" : "This script generates a 10 character alphanumeric id for the current note and also allows to jump to the note by it." +} diff --git a/unique-note-id/unique-note-id.qml b/unique-note-id/unique-note-id.qml new file mode 100644 index 0000000..ad9b758 --- /dev/null +++ b/unique-note-id/unique-note-id.qml @@ -0,0 +1,73 @@ +import QtQml 2.0 +import QOwnNotesTypes 1.0 +import com.qownnotes.noteapi 1.0 + +/** + * This script generates a 10 character alphanumeric id for the current note and also allows to jump to the note by it + */ +Script { + /** + * Initializes the custom actions + */ + function init() { + script.registerCustomAction("generateId", "Generate unique id for current note", + "Unique id", "xml-element-new", false, false, true); + script.registerCustomAction("jumpById", "Jump to note with unique id", + "Jump to id", "edit-find", true, false, false); + } + + /** + * This function is invoked when a custom action is triggered + * in the menu or via button + * + * @param identifier string the identifier defined in registerCustomAction + */ + function customActionInvoked(identifier) { + switch (identifier) { + case "generateId": + var uniqueId = uniqueid(); + var noteText = script.currentNote().noteText; + + // check if we already have generated an id for the note + if (noteText.search("id:\\[") != -1) { + script.log("Note already has a unique id"); + return; + } + + var noteText = script.currentNote().noteText; + noteText += "\nid:[" + uniqueId + "]"; + + script.noteTextEditSelectAll(); + script.noteTextEditWrite(noteText); + break; + case "jumpById": + var uniqueId = script.inputDialogGetText("Unique id", "Please enter the id of the note"); + var noteIds = script.fetchNoteIdsByNoteTextPart("id:[" + uniqueId + "]"); + + if (noteIds.length == 0) { + return; + } + + var note = script.fetchNoteById(noteIds[0]); + script.setCurrentNote(note); + break; + } + } + + function uniqueid() { + // always start with a letter + var idstr=String.fromCharCode(Math.floor((Math.random()*25)+65)); + + do { + // between numbers and characters (48 is 0 and 90 is Z (42-48 = 90) + var ascicode=Math.floor((Math.random()*42)+48); + + if (ascicode<58 || ascicode>64) { + // exclude all chars between : (58) and @ (64) + idstr+=String.fromCharCode(ascicode); + } + } while (idstr.length<10); + + return idstr.toLowerCase(); + } +}