-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathlist-maker.qml
158 lines (138 loc) · 5.86 KB
/
list-maker.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import QtQml 2.2
import QOwnNotesTypes 1.0
/* This script adds toolbar buttons and context menu items to change selected text formatting:
* - make it an ordered list with numbers or letters;
* - make it an unordered list with markers of your choice;
* - clear any list formatting.
*/
Script {
property string letters
property string markers
property string setLetters
property string setMarkers
property variant settingsVariables: [
{
"identifier": "setLetters",
"name": "Letters to use for ordered lists",
"description": "Letters/symbol and their order to use for ordered list",
"type": "string",
"default": "abcdefghijklmnopqrstuvwxyz"
},
{
"identifier": "setMarkers",
"name": "Unordered list item markers",
"description": "Put the symbols you want to use as marked list item markers. Spaces and commas are ignored",
"type": "string",
"default": "- • ‣"
}
]
function addCheckboxBrackets(text) {
// Remove normal list characters
var newText = text.replace(/^(\s*)[+\*\-] (.*)$/gm, "$1$2");
// Add checkbox list items
return newText.replace(/^(\s*)(.+)$/gm, "$1- [ ] $2");
}
// This will clear the text of all list formatting the script uses
function clearLine(text) {
var line = text;
var lineType = getListType(line);
while (lineType != "none") {
if (lineType == "number")
line = line.replace(/\d[\.\d]*\. /, "");
else if (lineType == "letter")
line = line.replace(/[^ ]\. /, "");
else if (lineType == "mark")
line = line.replace(/[^ ] /, "");
lineType = getListType(line);
}
return line;
}
function customActionInvoked(action) {
if (action === 'list checkbox') {
var lines = script.noteTextEditSelectedText();
lines = addCheckboxBrackets(lines);
script.noteTextEditWrite(lines);
return;
}
if (action.substring(0, 5) == "list ") {
var type = getListType(script.noteTextEditSelectedText());
var lines = script.noteTextEditSelectedText().split("\n");
var newText = [];
lines[0] = lines[0].replace(/^ */, "");
if (action == "list 123" && type == "number") {
// Continue the list for nested and flat numbered list
if (lines[0].search(/\d+\.\d+\. /) != -1) {
var number = lines[0].match(/^\d*/);
lines[0] = lines[0].replace(number + ".", "");
var subnumber = lines[0].match(/^\d*/) - 1;
lines[0] = " " + lines[0];
} else {
var number = lines[0].match(/\d*/) - 1;
var subnumber = 0;
}
} else if (action == "list abc" && type == "letter") {
var number = letters.indexOf(lines[0].substring(0, 1));
var subnumber = 0;
} else {
var number = 0;
var subnumber = 0;
}
for (var n = 0; n < lines.length; n++) {
if (lines[n] == "" || lines[n].substring(0, 1) == "\t") {
newText.push(lines[n]);
continue;
}
var line = (clearLine(lines[n]));
if (action == "list clear") {
newText.push(line.replace(/^ */, ""));
continue;
} else if (action == "list 123") {
if (line.substring(0, 1) == " ") {
subnumber++;
var mark = " " + number + "." + subnumber + ". ";
} else {
subnumber = 0;
number++;
var mark = number + ". ";
}
} else if (action == "list abc") {
var mark = letters[number++] + ". ";
} else {
var mark = action.substring(5, 6) + " ";
}
newText.push(mark + line.replace(/^ */, ""));
}
script.noteTextEditWrite(newText.join("\n"));
}
}
// This will return the type of a list of the first line of text
function getListType(text) {
text = text.replace(/^ */, "");
if (text.search(/\d[\.\d]*\. /) != -1)
var type = "number";
else if (letters.indexOf(text.substring(0, 1)) != -1 && text.substring(1, 3) == ". ")
var type = "letter";
else if (markers.indexOf(text.substring(0, 1)) != -1 && text.substring(1, 2) == " ")
var type = "mark";
else
var type = "none";
return type;
}
function init() {
script.registerCustomAction("list 123", "1. 2. 3. list", "1.", "", true);
if (setLetters) {
letters = setLetters.replace(/[\s,;]/g, "");
var orderedLettersListIcon = letters[0] + ".";
var orderedLettersListName = "%1. %2. %3. list".arg(letters[0]).arg(letters[1]).arg(letters[2]);
script.registerCustomAction("list abc", orderedLettersListName, orderedLettersListIcon, "", true);
}
if (setMarkers) {
markers = setMarkers.replace(/[\s,;]/g, "");
for (var n = 0; n < markers.length; n++)
script.registerCustomAction("list " + markers[n], markers[n] + " list", markers[n]);
}
script.registerCustomAction("list " + markers[0], "%1 list".arg(markers[0]), "", "", true, true);
script.registerCustomAction("list checkbox", "Create checkbox list", "", "", true, true);
script.registerCustomAction("list clear", "Clear list formatting", "X", "", true);
}
}