1
1
import QtQml 2.0
2
2
import QOwnNotesTypes 1.0
3
3
4
-
5
4
/**
6
5
* This script inserts and updates the decimal outline numbering in a document
7
6
**/
@@ -16,86 +15,92 @@ Script {
16
15
}
17
16
18
17
/**
19
- * this function finds all the headings and overwrites them with decimal outline numbering
18
+ * This function finds all the headings and overwrites them with decimal outline numbering
20
19
*
21
20
* @param lines an array of strings containing the lines of the document
22
21
* @return lines an array of strings with the heading lines updated
23
22
**/
24
23
function addOlNumbers (lines ) {
25
- // set the current nums to their start
26
- var curNums = [0 ,0 , 0 , 0 , 0 , 0 ];
24
+ // Set the current numbers to their start
25
+ var curNums = [0 , 0 , 0 , 0 , 0 , 0 ];
27
26
var depth = 0 ;
28
27
var last_depth = 0 ;
29
28
30
- // go through all the lines
29
+ // Go through all the lines
31
30
for (var n = 0 ; n < lines .length ; n++ ) {
32
- // if we found a heading
31
+ // If we found a heading
33
32
var match = lines[n].match (/ ^ (#+ )\s * ([0-9 \. ] * )\s + (. * )$ / );
34
33
if (match) {
35
-
36
- // get the depth - the heading number
34
+ // Get the depth - the heading number
37
35
depth = match[1 ].length - 1 ;
38
36
39
- // if the current depth is at a higher level than the last, reset all the lower level vals
37
+ // If the current depth is at a higher level than the last, reset all the lower level values
40
38
if (depth < last_depth) {
41
- for (var j = depth; n < curNums .length ; n ++ ) {
42
- curNums[j] == 0 ;
39
+ for (var j = depth + 1 ; j < curNums .length ; j ++ ) {
40
+ curNums[j] = 0 ; // Reset lower-level numbering
43
41
}
44
42
}
45
-
46
- // up the val for the current depth and save this depth as the last one
43
+
44
+ // Increment the value for the current depth and save this depth as the last one
47
45
curNums[depth] += 1 ;
48
46
last_depth = depth;
49
-
50
- // rewrite the currentt line with the number
47
+
48
+ // Rewrite the current line with the number
51
49
lines[n] = match[1 ] + " " + getOlNumber (curNums, depth) + " " + match[3 ];
52
- };
50
+ }
53
51
}
54
52
return lines;
55
53
}
56
-
54
+
57
55
/**
58
- * based on the current depth and the current digits, return the outline number string
59
- * which is the first depth number of elements in the nums array joined by a "."
60
- *
61
- * @param nums a 6 element array containing the current oultline numbering values
56
+ * Based on the current depth and the current digits, return the outline number string
57
+ * which is the first depth number of elements in the nums array joined by "."
58
+ *
59
+ * @param nums a 6- element array containing the current outline numbering values
62
60
* @param depth the current depth that we want a number for
63
- * @return string containing #depth numbers seperated by "."s
61
+ * @return string containing #depth numbers separated by "."s
64
62
*
65
- * example : getOlNumber([1,2,3,4,5,6], 4) returns "1.2.3.4"
63
+ * Example : getOlNumber([1,2,3,4,5,6], 4) returns "1.2.3.4"
66
64
**/
67
65
function getOlNumber (nums , depth ) {
66
+ var num = " " ;
67
+ for (var n = 0 ; n < depth + 1 ; n++ ) {
68
+ num += nums[n];
68
69
69
- var num = " " ;
70
- for (var n= 0 ; n< depth+ 1 ; n++ ) {
71
- num += nums[n];
72
-
73
- // only add the delim if there are more numbers to get
74
- if (n < depth) { num += " ." ;};
70
+ // Only add the delimiter if there are more numbers to get
71
+ if (n < depth) {
72
+ num += " ." ;
75
73
}
76
- return num;
77
74
}
78
75
76
+ // Ensure that the number always ends with a full stop
77
+ num += " ." ;
78
+
79
+ return num;
80
+ }
81
+
82
+
79
83
/**
80
- * this function is invoked when a custom action is triggered
84
+ * This function is invoked when a custom action is triggered
81
85
*
82
- * @param action string identifier defined in registerCumstomAction
86
+ * @param action string identifier defined in registerCustomAction
83
87
**/
84
88
function customActionInvoked (action ) {
85
- if (action == " outlineNumbering" )
86
- // get the document and update the lines
89
+ if (action == " outlineNumbering" ) {
90
+ // Get the document and update the lines
87
91
var lines = script .currentNote ().noteText .split (" \n " );
88
92
var updated_lines = addOlNumbers (lines);
89
-
90
- // save the current cursor position
93
+
94
+ // Save the current cursor position
91
95
var cursorPositionStart = script .noteTextEditSelectionStart ();
92
96
var cursorPositionEnd = script .noteTextEditSelectionEnd ();
93
97
94
- // select all and overwrite with the new text
98
+ // Select all and overwrite with the new text
95
99
script .noteTextEditSelectAll ();
96
100
script .noteTextEditWrite (updated_lines .join (" \n " ));
97
101
98
- // restore the cursor position
102
+ // Restore the cursor position
99
103
script .noteTextEditSetSelection (cursorPositionStart, cursorPositionEnd);
100
104
}
101
105
}
106
+ }
0 commit comments