1
0
Fork 0
mirror of https://github.com/marty-oehme/scripts.git synced 2024-11-16 23:08:08 +00:00
qownnotes-scripts/list-maker/list-maker.qml

158 lines
5.6 KiB
QML
Raw Normal View History

2017-10-12 18:24:19 +00:00
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 {
2017-10-14 14:06:57 +00:00
property string setLetters
property string setMarkers
2017-10-12 18:24:19 +00:00
property variant settingsVariables: [
{
2017-10-14 14:06:57 +00:00
"identifier": "setLetters",
2017-10-12 18:24:19 +00:00
"name": "Letters to use for ordered lists",
"description": "Letters/symbol and their order to use for ordered list",
"type": "string",
"default": "abcdefghijklmnopqrstuvwxyz",
},
{
2017-10-14 14:06:57 +00:00
"identifier": "setMarkers",
2017-10-12 18:24:19 +00:00
"name": "Unordered list item markers",
2017-10-14 14:06:57 +00:00
"description": "Put the symbols you want to use as marked list item markers. Spaces and commas are ignored",
2017-10-12 18:24:19 +00:00
"type": "string",
"default": "- • ‣",
2017-10-14 14:13:30 +00:00
}
2017-10-12 18:24:19 +00:00
]
property string letters
property string markers
function init() {
script.registerCustomAction("list 123", "1. 2. 3. list", "1.", "", true)
2017-10-14 14:06:57 +00:00
if (setLetters) {
letters = setLetters.replace(/[\s,;]/g, "")
2017-10-12 18:24:19 +00:00
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)
}
2017-10-14 14:06:57 +00:00
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])
2017-10-12 18:24:19 +00:00
}
script.registerCustomAction("list " + markers[0], "%1 list".arg(markers[0]), "", "", true, true)
script.registerCustomAction("list clear", "Clear list formatting", "X", "", true)
}
2017-10-14 14:06:57 +00:00
// This will return the type of a list of the first line of text
2017-10-12 18:24:19 +00:00
function getListType(text) {
2017-10-14 14:06:57 +00:00
text = text.replace(/^ */, "")
if (text.search(/\d[\.\d]*\. /) != -1)
2017-10-12 18:24:19 +00:00
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
}
2017-10-14 14:06:57 +00:00
// This will clear the text of all list formatting the script uses
2017-10-12 18:24:19 +00:00
function clearLine(text) {
var line = text
var lineType = getListType(line)
while (lineType != "none") {
if (lineType == "number")
2017-10-14 14:06:57 +00:00
line = line.replace(/\d[\.\d]*\. /, "")
2017-10-12 18:24:19 +00:00
else if (lineType == "letter")
2017-10-14 14:06:57 +00:00
line = line.replace(/[^ ]\. /, "")
2017-10-12 18:24:19 +00:00
else if (lineType == "mark")
2017-10-14 14:06:57 +00:00
line = line.replace(/[^ ] /, "")
2017-10-12 18:24:19 +00:00
lineType = getListType(line)
}
return line
}
function customActionInvoked(action) {
if (action.substring(0, 5) == "list ") {
var type = getListType(script.noteTextEditSelectedText())
var lines = script.noteTextEditSelectedText().split("\n")
var newText = []
2017-10-14 14:06:57 +00:00
lines[0] = lines[0].replace(/^ */, "")
2017-10-12 18:24:19 +00:00
2017-10-14 14:06:57 +00:00
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 {
2017-10-12 18:24:19 +00:00
var number = 0
2017-10-14 14:06:57 +00:00
var subnumber = 0
}
2017-10-12 18:24:19 +00:00
for (var n = 0; n < lines.length; n++) {
2017-10-14 14:06:57 +00:00
if (lines[n] == "" || lines[n].substring(0, 1) == "\t") {
2017-10-12 18:24:19 +00:00
newText.push(lines[n])
continue
}
var line = (clearLine(lines[n]))
if (action == "list clear") {
2017-10-14 14:06:57 +00:00
newText.push(line.replace(/^ */, ""))
2017-10-12 18:24:19 +00:00
continue
}
2017-10-14 14:06:57 +00:00
else if (action == "list 123") {
if (line.substring(0, 1) == " ") {
subnumber++
var mark = " " + number + "." + subnumber + ". "
}
else {
subnumber = 0
number++
var mark = number + ". "
}
2017-10-12 18:24:19 +00:00
}
2017-10-14 14:06:57 +00:00
else if (action == "list abc") {
2017-10-12 18:24:19 +00:00
var mark = letters[number++] + ". "
2017-10-14 14:06:57 +00:00
}
else {
2017-10-12 18:24:19 +00:00
var mark = action.substring(5, 6) + " "
2017-10-14 14:06:57 +00:00
}
2017-10-12 18:24:19 +00:00
2017-10-14 14:06:57 +00:00
newText.push(mark + line.replace(/^ */, ""))
2017-10-12 18:24:19 +00:00
}
script.noteTextEditWrite(newText.join("\n"))
}
}
}