1
0
Fork 0
mirror of https://github.com/marty-oehme/scripts.git synced 2024-11-14 13:58:07 +00:00
qownnotes-scripts/sort-lines/sort-lines.qml
2018-05-07 15:46:13 +02:00

46 lines
1.3 KiB
QML

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