mirror of
				https://github.com/Jermolene/TiddlyWiki5
				synced 2025-10-29 22:53:00 +00:00 
			
		
		
		
	Add trimSelection parameter to text editor (#7944)
* add trimSelection - both, start, end, no * add documentation for trimSelection to tm-edit-text-operation tiddler * add trimSelection both to all buttions where it makes sense * change "both" to "yes" as a default * fix trimSelection typo * fix trimSelection typo * docs trimSelection add "no" as possible value - it's the default * remove some redundant variable declarations * update from-version * fix comment typos * fix whitespace * make local variables more visible
This commit is contained in:
		| @@ -13,37 +13,125 @@ Text editor operation to wrap the selection with the specified prefix and suffix | |||||||
| "use strict"; | "use strict"; | ||||||
|  |  | ||||||
| exports["wrap-selection"] = function(event,operation) { | exports["wrap-selection"] = function(event,operation) { | ||||||
| 	if(operation.selStart === operation.selEnd) { | 	var o = operation, | ||||||
| 		// No selection; check if we're within the prefix/suffix | 		prefix = event.paramObject.prefix, | ||||||
| 		if(operation.text.substring(operation.selStart - event.paramObject.prefix.length,operation.selStart + event.paramObject.suffix.length) === event.paramObject.prefix + event.paramObject.suffix) { | 		suffix = event.paramObject.suffix, | ||||||
|  | 		trimSelection = event.paramObject.trimSelection || "no", | ||||||
|  | 		selLength = o.selEnd - o.selStart; | ||||||
|  |  | ||||||
|  | 	// This function detects, if trailing spaces are part of the selection __and__ if the user wants to handle them | ||||||
|  | 	// Returns "yes", "start", "end", "no" (default) | ||||||
|  | 	//	yes .. there are trailing spaces at both ends | ||||||
|  | 	//	start .. there are trailing spaces at the start | ||||||
|  | 	//	end .. there are trailing spaces at the end | ||||||
|  | 	//	no .. no trailing spaces are taken into account | ||||||
|  | 	var trailingSpaceAt = function(sel) { | ||||||
|  | 		var _start, | ||||||
|  | 			_end, | ||||||
|  | 			result; | ||||||
|  | 		// trimSelection is a user parameter, which this evaluations takes into account | ||||||
|  | 		switch(trimSelection) { | ||||||
|  | 			case "end": | ||||||
|  | 				result = (sel.trimEnd().length !== selLength) ? "end" : "no"; | ||||||
|  | 				break; | ||||||
|  | 			case "yes": | ||||||
|  | 				_start = sel.trimStart().length !== selLength; | ||||||
|  | 				_end = sel.trimEnd().length !== selLength; | ||||||
|  | 				result = (_start && _end) ? "yes" : (_start) ? "start" : (_end) ? "end" : "no"; | ||||||
|  | 				break; | ||||||
|  | 			case "start": | ||||||
|  | 				result = (sel.trimStart().length !== selLength) ? "start" : "no"; | ||||||
|  | 				break; | ||||||
|  | 			default: | ||||||
|  | 				result = "no"; | ||||||
|  | 				break; | ||||||
|  | 		} | ||||||
|  | 		return result; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	function togglePrefixSuffix() { | ||||||
|  | 		if(o.text.substring(o.selStart - prefix.length, o.selStart + suffix.length) === prefix + suffix) { | ||||||
| 			// Remove the prefix and suffix | 			// Remove the prefix and suffix | ||||||
| 			operation.cutStart = operation.selStart - event.paramObject.prefix.length; | 			o.cutStart = o.selStart - prefix.length; | ||||||
| 			operation.cutEnd = operation.selEnd + event.paramObject.suffix.length; | 			o.cutEnd = o.selEnd + suffix.length; | ||||||
| 			operation.replacement = ""; | 			o.replacement = ""; | ||||||
| 			operation.newSelStart = operation.cutStart; | 			o.newSelStart = o.cutStart; | ||||||
| 			operation.newSelEnd = operation.newSelStart; | 			o.newSelEnd = o.newSelStart; | ||||||
| 		} else { | 		} else { | ||||||
| 			// Wrap the cursor instead | 			// Wrap the cursor instead | ||||||
| 			operation.cutStart = operation.selStart; | 			o.cutStart = o.selStart; | ||||||
| 			operation.cutEnd = operation.selEnd; | 			o.cutEnd = o.selEnd; | ||||||
| 			operation.replacement = event.paramObject.prefix + event.paramObject.suffix; | 			o.replacement = prefix + suffix; | ||||||
| 			operation.newSelStart = operation.selStart + event.paramObject.prefix.length; | 			o.newSelStart = o.selStart + prefix.length; | ||||||
| 			operation.newSelEnd = operation.newSelStart; | 			o.newSelEnd = o.newSelStart; | ||||||
| 		} | 		} | ||||||
| 	} else if(operation.text.substring(operation.selStart,operation.selStart + event.paramObject.prefix.length) === event.paramObject.prefix && operation.text.substring(operation.selEnd - event.paramObject.suffix.length,operation.selEnd) === event.paramObject.suffix) { | 	} | ||||||
|  |  | ||||||
|  | 	// options: lenPrefix, lenSuffix | ||||||
|  | 	function removePrefixSuffix(options) { | ||||||
|  | 		options = options || {}; | ||||||
|  | 		var _lenPrefix = options.lenPrefix || 0; | ||||||
|  | 		var _lenSuffix = options.lenSuffix || 0; | ||||||
|  |  | ||||||
|  | 		o.cutStart = o.selStart - _lenPrefix; | ||||||
|  | 		o.cutEnd = o.selEnd + _lenSuffix; | ||||||
|  | 		o.replacement = (_lenPrefix || _lenSuffix) ? o.selection : o.selection.substring(prefix.length, o.selection.length - suffix.length); | ||||||
|  | 		o.newSelStart = o.cutStart; | ||||||
|  | 		o.newSelEnd = o.cutStart + o.replacement.length; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	function addPrefixSuffix() { | ||||||
|  | 		// remove trailing space if requested | ||||||
|  | 		switch(trailingSpaceAt(o.selection)) { | ||||||
|  | 			case "no": | ||||||
|  | 				// has no trailing spaces | ||||||
|  | 				o.cutStart = o.selStart; | ||||||
|  | 				o.cutEnd = o.selEnd; | ||||||
|  | 				o.replacement = prefix + o.selection + suffix; | ||||||
|  | 				o.newSelStart = o.selStart; | ||||||
|  | 				o.newSelEnd = o.selStart + o.replacement.length; | ||||||
|  | 				break; | ||||||
|  | 			case "yes": | ||||||
|  | 				// handle both ends | ||||||
|  | 				o.cutStart = o.selEnd - (o.selection.trimStart().length); | ||||||
|  | 				o.cutEnd = o.selection.trimEnd().length + o.selStart; | ||||||
|  | 				o.replacement = prefix + o.selection.trim() + suffix; | ||||||
|  | 				o.newSelStart = o.cutStart; | ||||||
|  | 				o.newSelEnd = o.cutStart + o.replacement.length; | ||||||
|  | 				break; | ||||||
|  | 			case "start": | ||||||
|  | 				// handle leading | ||||||
|  | 				o.cutStart = o.selEnd - (o.selection.trimStart().length); | ||||||
|  | 				o.cutEnd = o.selEnd; | ||||||
|  | 				o.replacement = prefix + o.selection.trimStart() + suffix; | ||||||
|  | 				o.newSelStart = o.cutStart; | ||||||
|  | 				o.newSelEnd = o.cutStart + o.replacement.length; | ||||||
|  | 				break; | ||||||
|  | 			case "end": | ||||||
|  | 				// handle trailing | ||||||
|  | 				o.cutStart = o.selStart; | ||||||
|  | 				o.cutEnd = o.selection.trimEnd().length + o.selStart; | ||||||
|  | 				o.replacement = prefix + o.selection.trimEnd() + suffix; | ||||||
|  | 				o.newSelStart = o.selStart; | ||||||
|  | 				o.newSelEnd = o.selStart + o.replacement.length; | ||||||
|  | 				break; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if(o.selStart === o.selEnd) { | ||||||
|  | 		// No selection; Create prefix and suffix. Set cursor in between them: ""|"" | ||||||
|  | 		togglePrefixSuffix(); | ||||||
|  | 	} else if(o.text.substring(o.selStart, o.selStart + prefix.length) === prefix && | ||||||
|  | 				o.text.substring(o.selEnd - suffix.length,o.selEnd) === suffix) { | ||||||
| 		// Prefix and suffix are already present, so remove them | 		// Prefix and suffix are already present, so remove them | ||||||
| 		operation.cutStart = operation.selStart; | 		removePrefixSuffix(); | ||||||
| 		operation.cutEnd = operation.selEnd; | 	} else if(o.text.substring(o.selStart - prefix.length, o.selStart) === prefix && | ||||||
| 		operation.replacement = operation.selection.substring(event.paramObject.prefix.length,operation.selection.length - event.paramObject.suffix.length); | 				o.text.substring(o.selEnd, o.selEnd + suffix.length) === suffix) { | ||||||
| 		operation.newSelStart = operation.selStart; | 		// Prefix and suffix are present BUT not selected -> remove them | ||||||
| 		operation.newSelEnd = operation.selStart + operation.replacement.length; | 		removePrefixSuffix({"lenPrefix": prefix.length, "lenSuffix": suffix.length}); | ||||||
| 	} else { | 	} else { | ||||||
| 		// Add the prefix and suffix | 		// Add the prefix and suffix | ||||||
| 		operation.cutStart = operation.selStart; | 		addPrefixSuffix(); | ||||||
| 		operation.cutEnd = operation.selEnd; |  | ||||||
| 		operation.replacement = event.paramObject.prefix + operation.selection + event.paramObject.suffix; |  | ||||||
| 		operation.newSelStart = operation.selStart; |  | ||||||
| 		operation.newSelEnd = operation.selStart + operation.replacement.length; |  | ||||||
| 	} | 	} | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -11,4 +11,5 @@ shortcuts: ((bold)) | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix="''" | 	prefix="''" | ||||||
| 	suffix="''" | 	suffix="''" | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
|   | |||||||
| @@ -11,4 +11,5 @@ shortcuts: ((italic)) | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix="//" | 	prefix="//" | ||||||
| 	suffix="//" | 	suffix="//" | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
|   | |||||||
| @@ -12,4 +12,5 @@ tags: $:/tags/EditorToolbar | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix="[[" | 	prefix="[[" | ||||||
| 	suffix="]]" | 	suffix="]]" | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
|   | |||||||
| @@ -11,4 +11,5 @@ shortcuts: ((mono-line)) | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix="`" | 	prefix="`" | ||||||
| 	suffix="`" | 	suffix="`" | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
|   | |||||||
| @@ -11,4 +11,5 @@ shortcuts: ((strikethrough)) | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix="~~" | 	prefix="~~" | ||||||
| 	suffix="~~" | 	suffix="~~" | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
|   | |||||||
| @@ -11,4 +11,5 @@ shortcuts: ((subscript)) | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix=",," | 	prefix=",," | ||||||
| 	suffix=",," | 	suffix=",," | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
|   | |||||||
| @@ -11,4 +11,5 @@ shortcuts: ((superscript)) | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix="^^" | 	prefix="^^" | ||||||
| 	suffix="^^" | 	suffix="^^" | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
|   | |||||||
| @@ -12,4 +12,5 @@ tags: $:/tags/EditorToolbar | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix="{{" | 	prefix="{{" | ||||||
| 	suffix="}}" | 	suffix="}}" | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
|   | |||||||
| @@ -11,4 +11,5 @@ shortcuts: ((underline)) | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix="__" | 	prefix="__" | ||||||
| 	suffix="__" | 	suffix="__" | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| caption: tm-edit-text-operation | caption: tm-edit-text-operation | ||||||
| created: 20160424211339792 | created: 20160424211339792 | ||||||
| modified: 20230803045746596 | modified: 20240909083525060 | ||||||
| tags: Messages | tags: Messages | ||||||
| title: WidgetMessage: tm-edit-text-operation | title: WidgetMessage: tm-edit-text-operation | ||||||
| type: text/vnd.tiddlywiki | type: text/vnd.tiddlywiki | ||||||
| @@ -77,6 +77,7 @@ Surrounds the current //selection// with the provided <<.param "prefix">> and << | |||||||
| |!Name |!Description | | |!Name |!Description | | ||||||
| |prefix |String to be prefixed to the selection | | |prefix |String to be prefixed to the selection | | ||||||
| |suffix |Suffix to be inserted after the selection | | |suffix |Suffix to be inserted after the selection | | ||||||
|  | |trimSelection |<<.from-version 5.3.6>> Trim leading and trailing white-space from the selection and move it to the surrounding text. Possible values are: `yes`, `no` (default), `start` and `end` | | ||||||
|  |  | ||||||
| </div> | </div> | ||||||
| \end | \end | ||||||
| @@ -152,6 +153,7 @@ An example can be seen in [[$:/core/ui/EditorToolbar/bold]]: | |||||||
| 	$param="wrap-selection" | 	$param="wrap-selection" | ||||||
| 	prefix="''" | 	prefix="''" | ||||||
| 	suffix="''" | 	suffix="''" | ||||||
|  | 	trimSelection="yes" | ||||||
| /> | /> | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Mario Pietsch
					Mario Pietsch