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,16 +15,33 @@ 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 || "";
// Cut just past the preceding line break, or the start of the text if($tw.utils.endsWith(operation.text.substring(0,operation.selStart), prefix + "\n") &&
operation.cutStart = $tw.utils.findPrecedingLineBreak(operation.text,operation.selStart); $tw.utils.startsWith(operation.text.substring(operation.selEnd), "\n" + suffix)) {
// Cut to just past the following line break, or to the end of the text // Selected text is already surrounded by prefix and suffix: Remove them
operation.cutEnd = $tw.utils.findFollowingLineBreak(operation.text,operation.selEnd); // Cut selected text plus prefix and suffix
// Add the prefix and suffix operation.cutStart = operation.selStart - (prefix.length + 1);
operation.replacement = prefix + "\n" + operation.cutEnd = operation.selEnd + suffix.length + 1;
operation.text.substring(operation.cutStart,operation.cutEnd) + "\n" + // Also cut the following newline (if there is any)
suffix + "\n"; if (operation.text[operation.cutEnd] === "\n") {
operation.newSelStart = operation.cutStart + prefix.length + 1; operation.cutEnd++;
operation.newSelEnd = operation.newSelStart + (operation.cutEnd - operation.cutStart); }
// 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
operation.cutStart = $tw.utils.findPrecedingLineBreak(operation.text,operation.selStart);
// Cut to just past the following line break, or to the end of the text
operation.cutEnd = $tw.utils.findFollowingLineBreak(operation.text,operation.selEnd);
// Add the prefix and suffix
operation.replacement = prefix + "\n" +
operation.text.substring(operation.cutStart,operation.cutEnd) + "\n" +
suffix + "\n";
operation.newSelStart = operation.cutStart + prefix.length + 1;
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