1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-06-25 23:03:15 +00:00

Refine behaviour of heading formatting toolbar buttons

Previous behaviour was that the H1 button on `!!! x` gave us `!! x`
This commit is contained in:
Jermolene 2016-05-05 07:54:30 +01:00
parent 048d421c06
commit 854a9a3b75

View File

@ -17,28 +17,23 @@ exports["prefix-lines"] = function(event,operation) {
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
operation.cutEnd = $tw.utils.findFollowingLineBreak(operation.text,operation.selEnd); operation.cutEnd = $tw.utils.findFollowingLineBreak(operation.text,operation.selEnd);
// Compose the required prefix
var prefix = $tw.utils.repeat(event.paramObject.character,event.paramObject.count);
// Process each line // Process each line
var lines = operation.text.substring(operation.cutStart,operation.cutEnd).split(/\r?\n/mg); var lines = operation.text.substring(operation.cutStart,operation.cutEnd).split(/\r?\n/mg);
$tw.utils.each(lines,function(line,index) { $tw.utils.each(lines,function(line,index) {
// Compose the required prefix // Remove and count any existing prefix characters
var prefix = $tw.utils.repeat(event.paramObject.character,event.paramObject.count); var count = 0;
// Check if we already have the required prefix while(line.charAt(0) === event.paramObject.character) {
if(line.substring(0,prefix.length) === prefix) { line = line.substring(1);
// If so, remove the prefix count++;
line = line.substring(prefix.length); }
// Remove any whitespace // Remove any whitespace
while(line.charAt(0) === " ") { while(line.charAt(0) === " ") {
line = line.substring(1); line = line.substring(1);
} }
} else { // We're done if we removed the exact required prefix, otherwise add it
// If we didn't have the prefix, remove any existing prefix characters if(count !== event.paramObject.count) {
while(line.charAt(0) === event.paramObject.character) {
line = line.substring(1);
}
// Remove any whitespace
while(line.charAt(0) === " ") {
line = line.substring(1);
}
// Apply the prefix // Apply the prefix
line = prefix + " " + line; line = prefix + " " + line;
} }