Merge pull request #31 from wiktor2200/master

sort-lines script added
This commit is contained in:
Patrizio Bekerle 2018-05-07 17:12:01 +02:00 committed by GitHub
commit 4c786fbd8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

10
sort-lines/info.json Normal file
View File

@ -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."
}

46
sort-lines/sort-lines.qml Normal file
View File

@ -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);
}
}