1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 18:17:20 +00:00

Allow toggling wrap-lines text operations (like mono block) (#6698)

This commit is contained in:
Max Schillinger 2022-05-17 22:16:54 +02:00 committed by GitHub
parent 855b6719d6
commit 5ea315fb98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 10 deletions

View File

@ -15,6 +15,22 @@ Text editor operation to wrap the selected lines with a prefix and suffix
exports["wrap-lines"] = function(event,operation) { exports["wrap-lines"] = function(event,operation) {
var prefix = event.paramObject.prefix || "", var prefix = event.paramObject.prefix || "",
suffix = event.paramObject.suffix || ""; suffix = event.paramObject.suffix || "";
if($tw.utils.endsWith(operation.text.substring(0,operation.selStart), prefix + "\n") &&
$tw.utils.startsWith(operation.text.substring(operation.selEnd), "\n" + suffix)) {
// Selected text is already surrounded by prefix and suffix: Remove them
// Cut selected text plus prefix and suffix
operation.cutStart = operation.selStart - (prefix.length + 1);
operation.cutEnd = operation.selEnd + suffix.length + 1;
// Also cut the following newline (if there is any)
if (operation.text[operation.cutEnd] === "\n") {
operation.cutEnd++;
}
// Replace with selection
operation.replacement = operation.text.substring(operation.selStart,operation.selEnd);
// Select text that was in between prefix and suffix
operation.newSelStart = operation.cutStart;
operation.newSelEnd = operation.selEnd - (prefix.length + 1);
} else {
// Cut just past the preceding line break, or the start of the text // Cut just past the preceding line break, or the start of the text
operation.cutStart = $tw.utils.findPrecedingLineBreak(operation.text,operation.selStart); operation.cutStart = $tw.utils.findPrecedingLineBreak(operation.text,operation.selStart);
// Cut to just past the following line break, or to the end of the text // Cut to just past the following line break, or to the end of the text
@ -25,6 +41,7 @@ exports["wrap-lines"] = function(event,operation) {
suffix + "\n"; suffix + "\n";
operation.newSelStart = operation.cutStart + prefix.length + 1; operation.newSelStart = operation.cutStart + prefix.length + 1;
operation.newSelEnd = operation.newSelStart + (operation.cutEnd - operation.cutStart); operation.newSelEnd = operation.newSelStart + (operation.cutEnd - operation.cutStart);
}
}; };
})(); })();

View File

@ -95,6 +95,20 @@ exports.repeat = function(str,count) {
return result; return result;
}; };
/*
Check if a string starts with another string
*/
exports.startsWith = function(str,search) {
return str.substring(0, search.length) === search;
};
/*
Check if a string ends with another string
*/
exports.endsWith = function(str,search) {
return str.substring(str.length - search.length) === search;
};
/* /*
Trim whitespace from the start and end of a string Trim whitespace from the start and end of a string
Thanks to Steven Levithan, http://blog.stevenlevithan.com/archives/faster-trim-javascript Thanks to Steven Levithan, http://blog.stevenlevithan.com/archives/faster-trim-javascript