diff --git a/sort-lines/info.json b/sort-lines/info.json new file mode 100644 index 0000000..3ad0b77 --- /dev/null +++ b/sort-lines/info.json @@ -0,0 +1,10 @@ +{ + "name": "Sort lines", + "identifier": "sort-lines-script", + "script": "sort-lines.qml", + "authors": ["@wiktor2200"], + "platforms": ["linux", "macos", "windows"], + "version": "0.0.1", + "minAppVersion": "17.06.2", + "description" : "This script sort selected lines in note text edit. \n\nIt creates an array from selected lines, sorts array and joins it again to output string." +} diff --git a/sort-lines/sort-lines.qml b/sort-lines/sort-lines.qml new file mode 100644 index 0000000..d031c63 --- /dev/null +++ b/sort-lines/sort-lines.qml @@ -0,0 +1,46 @@ +import QtQml 2.0 +import QOwnNotesTypes 1.0 + +/** + * This script sort selected lines in note text edit. It creates an array from selected lines, + * sorts array and joins it again to output string. + */ + +Script { + /** + * Initializes the custom actions + */ + function init() { + script.registerCustomAction("sortLinesAsc", "Sort lines ascending", "SortAsc", "view-sort-ascending", true, true); + script.registerCustomAction("sortLinesDesc", "Sort lines descending", "SortDesc", "view-sort-descending", true, true); + } + + function customActionInvoked(identifier) { + + // getting selected text from the note text edit + var text = script.noteTextEditSelectedText(); + + switch (identifier) { + // sort lines ascending + case "sortLinesAsc": + text = text.split("\n").sort(function(a, b) { + return a.localeCompare(b, { + 'sensitivity': 'base' + }); + }).join("\n"); + break; + + // sort lines descending + case "sortLinesDesc": + text = text.split("\n").sort(function(a, b) { + return a.localeCompare(b, { + 'sensitivity': 'base' + }); + }).reverse().join("\n"); + break; + } + + // put the result to the current cursor position in the note text edit + script.noteTextEditWrite(text); + } +}