Critic Markup approval
Shared by @EvanWillnow
Script
var cont = true;
var text = editor.getText();
var origIndex = editor.getSelectedRange()[1];
if (origIndex >= text.length) {var origIndex = 0}
var pattern = "{[-#~+>][-#~+>].+?[-#~+<][-#~+<]}";
if (pattern) {
var index = text.substring(origIndex || 0).search(pattern) + origIndex;
var found = text.substring(origIndex || 0).match(pattern);
if (index >= origIndex) {
foundLen = found[0].length
editor.setSelectedRange(index, index + found[0].length);
if (found[0].search("{>>.+?<<}") > -1) {
ui.alert(found[0],"Keep note?", "Cancel", "Remove", "Keep", function (res) {
switch(res) {
case 0:
ui.hudError ('Cancel');
var cont = false;
break;
case 1:
ui.hudError ('Remove');
editor.replaceSelection('');
break;
case 2:
ui.hudError ('Keep');
break;
}
}
);
}
else if (found[0].search("{##.+?##}") > -1) {
ui.alert(found[0],"Keep highlight?", "Cancel", "Remove", "Remove & edit", "Keep",
function (res) {
switch(res) {
case 0:
ui.hudError ('Cancel');
var cont = false;
break;
case 1:
ui.hudError ('Remove');
editor.replaceSelection(found[0].substring(3,foundLen - 3));
break;
case 2:
ui.hudError ('Remove & edit');
editor.replaceSelection(found[0].substring(3,foundLen - 3));
editor.setSelectedRange(index);
var cont = false;
break;
case 3:
ui.hudError ('Keep');
break;
}
}
);
}
else if (found[0].search("{~~.+?~>.+?~~}") > -1) {
divider = found[0].search("~>")
ui.alert(found[0],"Approve substitution?", "Cancel", "Decline", "Approve",
function (res) {
switch(res) {
case 0:
ui.hudError ('Cancel');
var cont = false;
break;
case 1:
ui.hudError ('Decline');
editor.replaceSelection(found[0].substring(3,divider));
break;
case 2:
ui.hudError ('Approve');
editor.replaceSelection(found[0].substring(divider+2,foundLen - 3));
break;
}
}
);
}
else if (found[0].search("{--.+?--}") > -1) {
ui.alert(found[0],"Approve deletion?", "Cancel", "Decline", "Approve",
function (res) {
switch(res) {
case 0:
ui.hudError ('Cancel');
var cont = false;
break;
case 1:
ui.hudError ('Decline');
editor.replaceSelection(found[0].substring(3,foundLen - 3));
break;
case 2:
ui.hudError ('Approve');
editor.replaceSelection('');
break;
}
}
);
}
else if (found[0].search("{[+][+].*?[+][+]}") > -1) {
ui.alert(found[0],"Approve addition?", "Cancel", "Decline", "Approve",
function (res) {
switch (res) {
case 0:
ui.hudError ('Cancel');
var cont = false;
break;
case 1:
ui.hudError ('Decline');
editor.replaceSelection('');
break;
case 2:
ui.hudError ('Approve');
editor.replaceSelection(found[0].substring(3,foundLen - 3));
break;
}
}
);
}
}
else {
ui.hudError('No CriticMarkup found');
var cont = false;
}
}