From b69f64966378c73cc3dcdf000922f93ba2bb8d4a Mon Sep 17 00:00:00 2001 From: pmario Date: Tue, 23 Jan 2024 15:31:03 +0100 Subject: [PATCH 01/12] add trimSelection - both, start, end, no --- .../editor/operations/text/wrap-selection.js | 132 ++++++++++++++---- 1 file changed, 108 insertions(+), 24 deletions(-) diff --git a/core/modules/editor/operations/text/wrap-selection.js b/core/modules/editor/operations/text/wrap-selection.js index 6800cbe5b9c..ffd51ab9eea 100644 --- a/core/modules/editor/operations/text/wrap-selection.js +++ b/core/modules/editor/operations/text/wrap-selection.js @@ -13,37 +13,121 @@ Text editor operation to wrap the selection with the specified prefix and suffix "use strict"; exports["wrap-selection"] = function(event,operation) { - if(operation.selStart === operation.selEnd) { - // No selection; check if we're within the prefix/suffix - if(operation.text.substring(operation.selStart - event.paramObject.prefix.length,operation.selStart + event.paramObject.suffix.length) === event.paramObject.prefix + event.paramObject.suffix) { + var o = operation, + prefix = event.paramObject.prefix, + prefixLength = prefix.length, + suffix = event.paramObject.suffix, + suffixLength = suffix.length, + trimSeletion = event.paramObject.trimSeletion || "no", + selLength = o.selEnd - o.selStart; + + var trailingSpaceAt = function(sel) { + // returns "both", "start", "end", "no" + var _start, + _end, + result; + // this evaluation takes the user configuration into account! + switch (trimSeletion) { + case "end": + result = (sel.trimEnd().length !== selLength) ? "end" : "no"; + break; + case "both": + _start = sel.trimStart().length !== selLength; + _end = sel.trimEnd().length !== selLength; + result = (_start && _end) ? "both" : (_start) ? "start" : (_end) ? "end" : "no"; + break; + case "start": + result = (sel.trimStart().length !== selLength) ? "start" : "no"; + break; + default: + result = "no"; + break; + } + return result; + } + + function togglePrefixSuffix() { + // this was the only behavour till TW v5.3.3 + if(o.text.substring(o.selStart - prefixLength, o.selStart + suffixLength) === prefix + suffix) { // Remove the prefix and suffix - operation.cutStart = operation.selStart - event.paramObject.prefix.length; - operation.cutEnd = operation.selEnd + event.paramObject.suffix.length; - operation.replacement = ""; - operation.newSelStart = operation.cutStart; - operation.newSelEnd = operation.newSelStart; + o.cutStart = o.selStart - prefixLength; + o.cutEnd = o.selEnd + suffixLength; + o.replacement = ""; + o.newSelStart = o.cutStart; + o.newSelEnd = o.newSelStart; } else { // Wrap the cursor instead - operation.cutStart = operation.selStart; - operation.cutEnd = operation.selEnd; - operation.replacement = event.paramObject.prefix + event.paramObject.suffix; - operation.newSelStart = operation.selStart + event.paramObject.prefix.length; - operation.newSelEnd = operation.newSelStart; + o.cutStart = o.selStart; + o.cutEnd = o.selEnd; + o.replacement = prefix + suffix; + o.newSelStart = o.selStart + prefixLength; + 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: prefixLen, suffixLen + function removePrefixSuffix(options) { + options = options || {}; + var prefixLen = options.prefixLen || 0; + var suffixLen = options.suffixLen || 0; + + o.cutStart = o.selStart - prefixLen; + o.cutEnd = o.selEnd + suffixLen; + o.replacement = (prefixLen || suffixLen) ? o.selection : o.selection.substring(prefixLength, o.selection.length - suffixLength); + 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 "both": + 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": + 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": + 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 between them: ""|"" + togglePrefixSuffix(); + } else if( o.text.substring(o.selStart, o.selStart + prefixLength) === prefix && + o.text.substring(o.selEnd - suffixLength,o.selEnd) === suffix) { // Prefix and suffix are already present, so remove them - operation.cutStart = operation.selStart; - operation.cutEnd = operation.selEnd; - operation.replacement = operation.selection.substring(event.paramObject.prefix.length,operation.selection.length - event.paramObject.suffix.length); - operation.newSelStart = operation.selStart; - operation.newSelEnd = operation.selStart + operation.replacement.length; + removePrefixSuffix(); + } else if( o.text.substring(o.selStart - prefixLength, o.selStart) === prefix && + o.text.substring(o.selEnd, o.selEnd + suffixLength) === suffix) { + // Prefix and suffix are present BUT not selected -> remove them + removePrefixSuffix({"prefixLen": prefixLength, "suffixLen": suffixLength}); } else { // Add the prefix and suffix - operation.cutStart = operation.selStart; - 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; + addPrefixSuffix(); } }; From 1b3a66b7a4cedee37b7e400afee0820c4cbed118 Mon Sep 17 00:00:00 2001 From: pmario Date: Tue, 23 Jan 2024 15:34:22 +0100 Subject: [PATCH 02/12] add documentation for trimSelection to tm-edit-text-operation tiddler --- .../WidgetMessage_ tm-edit-text-operation.tid | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid index a34fe3fc0a5..de8656f2ac8 100644 --- a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid @@ -1,6 +1,6 @@ caption: tm-edit-text-operation created: 20160424211339792 -modified: 20230723214636245 +modified: 20240123112124856 tags: Messages title: WidgetMessage: tm-edit-text-operation type: text/vnd.tiddlywiki @@ -11,9 +11,9 @@ type: text/vnd.tiddlywiki Excises the currently selected text into a new tiddler and replaces it with a link, a macro or a transclude of the new tiddler. Parameters include: |!Name |!Description | -|title |Title of the new tiddler the selected content is excised to| -|type |Type of the replacement to be inserted: Can be one of <<.value "transclude">>, <<.value "link">> or <<.value "macro">>| -|macro |In case //type=<<.value "macro">>//, specifies the name of the macro to be inserted. The title of the new tiddler is provided as the first parameter to the macro. Defaults to the [[translink macro|translink Macro]]| +|title |Title of the new tiddler the selected content is excised to | +|type |Type of the replacement to be inserted: Can be one of <<.value "transclude">>, <<.value "link">> or <<.value "macro">> | +|macro |In case //type=<<.value "macro">>//, specifies the name of the macro to be inserted. The title of the new tiddler is provided as the first parameter to the macro. Defaults to the [[translink macro|translink Macro]] | |tagnew |If '<<.value "yes">>', will tag the new tiddler with the title of the tiddler currently being edited | @@ -25,7 +25,7 @@ Excises the currently selected text into a new tiddler and replaces it with a li Replaces ''all'' contents of the editor with the provided text. |!Name |!Description | -|text |Text to be inserted| +|text |Text to be inserted | \end @@ -36,7 +36,7 @@ Replaces ''all'' contents of the editor with the provided text. Replaces the current selection with the provided text. |!Name |!Description | -|text |Text to be inserted| +|text |Text to be inserted | \end @@ -47,8 +47,8 @@ Replaces the current selection with the provided text. Prefixes the currently selected line//(s)// with the provided character. If a line is already prefixed by the provided prefix, the prefix is removed instead. |!Name |!Description | -|character |Prefix character| -|count |Number of characters that make up the prefix| +|character |Prefix character | +|count |Number of characters that make up the prefix | ''Example'' Setting //character="<<.value "!">>"// and //count="<<.value "3">>"// would insert the prefix "<<.value "!!!" >>", which will resolve to a subheading when parsed as WikiText. @@ -61,8 +61,8 @@ Prefixes the currently selected line//(s)// with the provided character. If a li Surrounds the selected //lines// with the provided <<.param "prefix">> and <<.param "suffix">>. |!Name |!Description | -|prefix |String to be prefixed to the selected lines| -|suffix |Suffix to be inserted after the selected lines| +|prefix |String to be prefixed to the selected lines | +|suffix |Suffix to be inserted after the selected lines | @@ -75,8 +75,9 @@ Surrounds the selected //lines// with the provided <<.param "prefix">> and <<.pa Surrounds the current //selection// with the provided <<.param "prefix">> and <<.param "suffix">>. |!Name |!Description | -|prefix |String to be prefixed to the selection| -|suffix |Suffix to be inserted after the selection| +|prefix |String to be prefixed to the selection | +|suffix |Suffix to be inserted after the selection | +|trimSelection |<<.from-version 5.3.4>> Trim leading and trailing white-space from the selection and move it to the surrounding text | \end @@ -138,11 +139,11 @@ At this point the following text operations have been implemented: |<<.def "wrap-selection">>|<> | |<<.def "save-selection">>|<> | |<<.def "make-link">>|<> | -|<<.def "insert-text">>|<>| +|<<.def "insert-text">>|<> | |<<.def "focus-editor">>|<<.from-version 5.2.0>> <> | -!Example +! Example An example can be seen in [[$:/core/ui/EditorToolbar/bold]]: @@ -152,6 +153,7 @@ An example can be seen in [[$:/core/ui/EditorToolbar/bold]]: $param="wrap-selection" prefix="''" suffix="''" + trimSeletion="both" /> ``` From 6abf4c888b6ed494bcec032875af755c4f2e7104 Mon Sep 17 00:00:00 2001 From: pmario Date: Tue, 23 Jan 2024 15:35:09 +0100 Subject: [PATCH 03/12] add trimSelection both to all buttions where it makes sense --- core/ui/EditorToolbar/bold.tid | 1 + core/ui/EditorToolbar/italic.tid | 1 + core/ui/EditorToolbar/linkify.tid | 1 + core/ui/EditorToolbar/mono-line.tid | 1 + core/ui/EditorToolbar/stamp-dropdown-item-template.tid | 2 +- core/ui/EditorToolbar/strikethrough.tid | 1 + core/ui/EditorToolbar/subscript.tid | 1 + core/ui/EditorToolbar/superscript.tid | 1 + core/ui/EditorToolbar/transcludify.tid | 1 + core/ui/EditorToolbar/underline.tid | 1 + 10 files changed, 10 insertions(+), 1 deletion(-) diff --git a/core/ui/EditorToolbar/bold.tid b/core/ui/EditorToolbar/bold.tid index ffe84de6145..ab448a2cc0b 100644 --- a/core/ui/EditorToolbar/bold.tid +++ b/core/ui/EditorToolbar/bold.tid @@ -11,4 +11,5 @@ shortcuts: ((bold)) $param="wrap-selection" prefix="''" suffix="''" + trimSeletion="both" /> diff --git a/core/ui/EditorToolbar/italic.tid b/core/ui/EditorToolbar/italic.tid index 518f3d0c238..aa21feeb447 100644 --- a/core/ui/EditorToolbar/italic.tid +++ b/core/ui/EditorToolbar/italic.tid @@ -11,4 +11,5 @@ shortcuts: ((italic)) $param="wrap-selection" prefix="//" suffix="//" + trimSeletion="both" /> diff --git a/core/ui/EditorToolbar/linkify.tid b/core/ui/EditorToolbar/linkify.tid index 68a45857e18..ff7f512da03 100644 --- a/core/ui/EditorToolbar/linkify.tid +++ b/core/ui/EditorToolbar/linkify.tid @@ -12,4 +12,5 @@ tags: $:/tags/EditorToolbar $param="wrap-selection" prefix="[[" suffix="]]" + trimSeletion="both" /> diff --git a/core/ui/EditorToolbar/mono-line.tid b/core/ui/EditorToolbar/mono-line.tid index 15ca77de1e5..a713964161b 100644 --- a/core/ui/EditorToolbar/mono-line.tid +++ b/core/ui/EditorToolbar/mono-line.tid @@ -11,4 +11,5 @@ shortcuts: ((mono-line)) $param="wrap-selection" prefix="`" suffix="`" + trimSeletion="both" /> diff --git a/core/ui/EditorToolbar/stamp-dropdown-item-template.tid b/core/ui/EditorToolbar/stamp-dropdown-item-template.tid index 5e5acb16238..cef3de8b079 100644 --- a/core/ui/EditorToolbar/stamp-dropdown-item-template.tid +++ b/core/ui/EditorToolbar/stamp-dropdown-item-template.tid @@ -21,7 +21,7 @@ title: $:/core/ui/EditorToolbar/StampDropdown/ItemTemplate $message="tm-edit-text-operation" $param="wrap-selection" prefix={{{ [addsuffix[/prefix]get[text]] }}} - suffix={{{ [addsuffix[/suffix]get[text]] }}} + suffix={{{ [addsuffix[/suffix]get[text]] }}} /> diff --git a/core/ui/EditorToolbar/strikethrough.tid b/core/ui/EditorToolbar/strikethrough.tid index 53808cac368..d5d7debb598 100644 --- a/core/ui/EditorToolbar/strikethrough.tid +++ b/core/ui/EditorToolbar/strikethrough.tid @@ -11,4 +11,5 @@ shortcuts: ((strikethrough)) $param="wrap-selection" prefix="~~" suffix="~~" + trimSeletion="both" /> diff --git a/core/ui/EditorToolbar/subscript.tid b/core/ui/EditorToolbar/subscript.tid index e2c83abbd82..bdd079b6a0a 100644 --- a/core/ui/EditorToolbar/subscript.tid +++ b/core/ui/EditorToolbar/subscript.tid @@ -11,4 +11,5 @@ shortcuts: ((subscript)) $param="wrap-selection" prefix=",," suffix=",," + trimSeletion="both" /> diff --git a/core/ui/EditorToolbar/superscript.tid b/core/ui/EditorToolbar/superscript.tid index c1a40efea3b..b301b6861e4 100644 --- a/core/ui/EditorToolbar/superscript.tid +++ b/core/ui/EditorToolbar/superscript.tid @@ -11,4 +11,5 @@ shortcuts: ((superscript)) $param="wrap-selection" prefix="^^" suffix="^^" + trimSeletion="both" /> diff --git a/core/ui/EditorToolbar/transcludify.tid b/core/ui/EditorToolbar/transcludify.tid index dbc008d56a7..57db9c34096 100644 --- a/core/ui/EditorToolbar/transcludify.tid +++ b/core/ui/EditorToolbar/transcludify.tid @@ -12,4 +12,5 @@ tags: $:/tags/EditorToolbar $param="wrap-selection" prefix="{{" suffix="}}" + trimSeletion="both" /> diff --git a/core/ui/EditorToolbar/underline.tid b/core/ui/EditorToolbar/underline.tid index ec6d63319a0..e5f7c80ea2f 100644 --- a/core/ui/EditorToolbar/underline.tid +++ b/core/ui/EditorToolbar/underline.tid @@ -11,4 +11,5 @@ shortcuts: ((underline)) $param="wrap-selection" prefix="__" suffix="__" + trimSeletion="both" /> From 756dd142d581659b0750829d219c5dc0a2d44108 Mon Sep 17 00:00:00 2001 From: pmario Date: Sun, 28 Jan 2024 15:47:46 +0100 Subject: [PATCH 04/12] change "both" to "yes" as a default --- core/modules/editor/operations/text/wrap-selection.js | 8 ++++---- core/ui/EditorToolbar/bold.tid | 2 +- core/ui/EditorToolbar/italic.tid | 2 +- core/ui/EditorToolbar/linkify.tid | 2 +- core/ui/EditorToolbar/mono-line.tid | 2 +- core/ui/EditorToolbar/strikethrough.tid | 2 +- core/ui/EditorToolbar/subscript.tid | 2 +- core/ui/EditorToolbar/superscript.tid | 2 +- core/ui/EditorToolbar/transcludify.tid | 2 +- core/ui/EditorToolbar/underline.tid | 2 +- .../messages/WidgetMessage_ tm-edit-text-operation.tid | 4 ++-- 11 files changed, 15 insertions(+), 15 deletions(-) diff --git a/core/modules/editor/operations/text/wrap-selection.js b/core/modules/editor/operations/text/wrap-selection.js index ffd51ab9eea..a5bf6f01f42 100644 --- a/core/modules/editor/operations/text/wrap-selection.js +++ b/core/modules/editor/operations/text/wrap-selection.js @@ -22,7 +22,7 @@ exports["wrap-selection"] = function(event,operation) { selLength = o.selEnd - o.selStart; var trailingSpaceAt = function(sel) { - // returns "both", "start", "end", "no" + // returns "yes", "start", "end", "no" var _start, _end, result; @@ -31,10 +31,10 @@ exports["wrap-selection"] = function(event,operation) { case "end": result = (sel.trimEnd().length !== selLength) ? "end" : "no"; break; - case "both": + case "yes": _start = sel.trimStart().length !== selLength; _end = sel.trimEnd().length !== selLength; - result = (_start && _end) ? "both" : (_start) ? "start" : (_end) ? "end" : "no"; + result = (_start && _end) ? "yes" : (_start) ? "start" : (_end) ? "end" : "no"; break; case "start": result = (sel.trimStart().length !== selLength) ? "start" : "no"; @@ -89,7 +89,7 @@ exports["wrap-selection"] = function(event,operation) { o.newSelStart = o.selStart; o.newSelEnd = o.selStart + o.replacement.length; break; - case "both": + case "yes": o.cutStart = o.selEnd - (o.selection.trimStart().length); o.cutEnd = o.selection.trimEnd().length + o.selStart; o.replacement = prefix + o.selection.trim() + suffix; diff --git a/core/ui/EditorToolbar/bold.tid b/core/ui/EditorToolbar/bold.tid index ab448a2cc0b..115ea63dd00 100644 --- a/core/ui/EditorToolbar/bold.tid +++ b/core/ui/EditorToolbar/bold.tid @@ -11,5 +11,5 @@ shortcuts: ((bold)) $param="wrap-selection" prefix="''" suffix="''" - trimSeletion="both" + trimSeletion="yes" /> diff --git a/core/ui/EditorToolbar/italic.tid b/core/ui/EditorToolbar/italic.tid index aa21feeb447..27aa0d66e16 100644 --- a/core/ui/EditorToolbar/italic.tid +++ b/core/ui/EditorToolbar/italic.tid @@ -11,5 +11,5 @@ shortcuts: ((italic)) $param="wrap-selection" prefix="//" suffix="//" - trimSeletion="both" + trimSeletion="yes" /> diff --git a/core/ui/EditorToolbar/linkify.tid b/core/ui/EditorToolbar/linkify.tid index ff7f512da03..6c51d1512e8 100644 --- a/core/ui/EditorToolbar/linkify.tid +++ b/core/ui/EditorToolbar/linkify.tid @@ -12,5 +12,5 @@ tags: $:/tags/EditorToolbar $param="wrap-selection" prefix="[[" suffix="]]" - trimSeletion="both" + trimSeletion="yes" /> diff --git a/core/ui/EditorToolbar/mono-line.tid b/core/ui/EditorToolbar/mono-line.tid index a713964161b..58f2a8d072a 100644 --- a/core/ui/EditorToolbar/mono-line.tid +++ b/core/ui/EditorToolbar/mono-line.tid @@ -11,5 +11,5 @@ shortcuts: ((mono-line)) $param="wrap-selection" prefix="`" suffix="`" - trimSeletion="both" + trimSeletion="yes" /> diff --git a/core/ui/EditorToolbar/strikethrough.tid b/core/ui/EditorToolbar/strikethrough.tid index d5d7debb598..6a288849b64 100644 --- a/core/ui/EditorToolbar/strikethrough.tid +++ b/core/ui/EditorToolbar/strikethrough.tid @@ -11,5 +11,5 @@ shortcuts: ((strikethrough)) $param="wrap-selection" prefix="~~" suffix="~~" - trimSeletion="both" + trimSeletion="yes" /> diff --git a/core/ui/EditorToolbar/subscript.tid b/core/ui/EditorToolbar/subscript.tid index bdd079b6a0a..e1e6ba6f799 100644 --- a/core/ui/EditorToolbar/subscript.tid +++ b/core/ui/EditorToolbar/subscript.tid @@ -11,5 +11,5 @@ shortcuts: ((subscript)) $param="wrap-selection" prefix=",," suffix=",," - trimSeletion="both" + trimSeletion="yes" /> diff --git a/core/ui/EditorToolbar/superscript.tid b/core/ui/EditorToolbar/superscript.tid index b301b6861e4..a62403f6475 100644 --- a/core/ui/EditorToolbar/superscript.tid +++ b/core/ui/EditorToolbar/superscript.tid @@ -11,5 +11,5 @@ shortcuts: ((superscript)) $param="wrap-selection" prefix="^^" suffix="^^" - trimSeletion="both" + trimSeletion="yes" /> diff --git a/core/ui/EditorToolbar/transcludify.tid b/core/ui/EditorToolbar/transcludify.tid index 57db9c34096..852d02fed38 100644 --- a/core/ui/EditorToolbar/transcludify.tid +++ b/core/ui/EditorToolbar/transcludify.tid @@ -12,5 +12,5 @@ tags: $:/tags/EditorToolbar $param="wrap-selection" prefix="{{" suffix="}}" - trimSeletion="both" + trimSeletion="yes" /> diff --git a/core/ui/EditorToolbar/underline.tid b/core/ui/EditorToolbar/underline.tid index e5f7c80ea2f..3a0b85802dc 100644 --- a/core/ui/EditorToolbar/underline.tid +++ b/core/ui/EditorToolbar/underline.tid @@ -11,5 +11,5 @@ shortcuts: ((underline)) $param="wrap-selection" prefix="__" suffix="__" - trimSeletion="both" + trimSeletion="yes" /> diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid index de8656f2ac8..a57864a9e88 100644 --- a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid @@ -77,7 +77,7 @@ Surrounds the current //selection// with the provided <<.param "prefix">> and << |!Name |!Description | |prefix |String to be prefixed to the selection | |suffix |Suffix to be inserted after the selection | -|trimSelection |<<.from-version 5.3.4>> Trim leading and trailing white-space from the selection and move it to the surrounding text | +|trimSelection |<<.from-version 5.3.4>> Trim leading and trailing white-space from the selection and move it to the surrounding text. Possible values are: `yes`, `start` and `end` | \end @@ -153,7 +153,7 @@ An example can be seen in [[$:/core/ui/EditorToolbar/bold]]: $param="wrap-selection" prefix="''" suffix="''" - trimSeletion="both" + trimSeletion="yes" /> ``` From acccf67fd47d705511f7b34c0dd5b54e543dc66d Mon Sep 17 00:00:00 2001 From: pmario Date: Sun, 28 Jan 2024 17:21:40 +0100 Subject: [PATCH 05/12] fix trimSelection typo --- core/ui/EditorToolbar/bold.tid | 2 +- core/ui/EditorToolbar/italic.tid | 2 +- core/ui/EditorToolbar/linkify.tid | 2 +- core/ui/EditorToolbar/mono-line.tid | 2 +- core/ui/EditorToolbar/strikethrough.tid | 2 +- core/ui/EditorToolbar/subscript.tid | 2 +- core/ui/EditorToolbar/superscript.tid | 2 +- core/ui/EditorToolbar/transcludify.tid | 2 +- core/ui/EditorToolbar/underline.tid | 2 +- .../tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core/ui/EditorToolbar/bold.tid b/core/ui/EditorToolbar/bold.tid index 115ea63dd00..44676d9bc19 100644 --- a/core/ui/EditorToolbar/bold.tid +++ b/core/ui/EditorToolbar/bold.tid @@ -11,5 +11,5 @@ shortcuts: ((bold)) $param="wrap-selection" prefix="''" suffix="''" - trimSeletion="yes" + trimSelection="yes" /> diff --git a/core/ui/EditorToolbar/italic.tid b/core/ui/EditorToolbar/italic.tid index 27aa0d66e16..c9f6b8d8799 100644 --- a/core/ui/EditorToolbar/italic.tid +++ b/core/ui/EditorToolbar/italic.tid @@ -11,5 +11,5 @@ shortcuts: ((italic)) $param="wrap-selection" prefix="//" suffix="//" - trimSeletion="yes" + trimSelection="yes" /> diff --git a/core/ui/EditorToolbar/linkify.tid b/core/ui/EditorToolbar/linkify.tid index 6c51d1512e8..40a82655ce0 100644 --- a/core/ui/EditorToolbar/linkify.tid +++ b/core/ui/EditorToolbar/linkify.tid @@ -12,5 +12,5 @@ tags: $:/tags/EditorToolbar $param="wrap-selection" prefix="[[" suffix="]]" - trimSeletion="yes" + trimSelection="yes" /> diff --git a/core/ui/EditorToolbar/mono-line.tid b/core/ui/EditorToolbar/mono-line.tid index 58f2a8d072a..583d6a0a257 100644 --- a/core/ui/EditorToolbar/mono-line.tid +++ b/core/ui/EditorToolbar/mono-line.tid @@ -11,5 +11,5 @@ shortcuts: ((mono-line)) $param="wrap-selection" prefix="`" suffix="`" - trimSeletion="yes" + trimSelection="yes" /> diff --git a/core/ui/EditorToolbar/strikethrough.tid b/core/ui/EditorToolbar/strikethrough.tid index 6a288849b64..9435fad544d 100644 --- a/core/ui/EditorToolbar/strikethrough.tid +++ b/core/ui/EditorToolbar/strikethrough.tid @@ -11,5 +11,5 @@ shortcuts: ((strikethrough)) $param="wrap-selection" prefix="~~" suffix="~~" - trimSeletion="yes" + trimSelection="yes" /> diff --git a/core/ui/EditorToolbar/subscript.tid b/core/ui/EditorToolbar/subscript.tid index e1e6ba6f799..ac85581ca76 100644 --- a/core/ui/EditorToolbar/subscript.tid +++ b/core/ui/EditorToolbar/subscript.tid @@ -11,5 +11,5 @@ shortcuts: ((subscript)) $param="wrap-selection" prefix=",," suffix=",," - trimSeletion="yes" + trimSelection="yes" /> diff --git a/core/ui/EditorToolbar/superscript.tid b/core/ui/EditorToolbar/superscript.tid index a62403f6475..a3c9d646f34 100644 --- a/core/ui/EditorToolbar/superscript.tid +++ b/core/ui/EditorToolbar/superscript.tid @@ -11,5 +11,5 @@ shortcuts: ((superscript)) $param="wrap-selection" prefix="^^" suffix="^^" - trimSeletion="yes" + trimSelection="yes" /> diff --git a/core/ui/EditorToolbar/transcludify.tid b/core/ui/EditorToolbar/transcludify.tid index 852d02fed38..852fa8905d8 100644 --- a/core/ui/EditorToolbar/transcludify.tid +++ b/core/ui/EditorToolbar/transcludify.tid @@ -12,5 +12,5 @@ tags: $:/tags/EditorToolbar $param="wrap-selection" prefix="{{" suffix="}}" - trimSeletion="yes" + trimSelection="yes" /> diff --git a/core/ui/EditorToolbar/underline.tid b/core/ui/EditorToolbar/underline.tid index 3a0b85802dc..d377cebb8fe 100644 --- a/core/ui/EditorToolbar/underline.tid +++ b/core/ui/EditorToolbar/underline.tid @@ -11,5 +11,5 @@ shortcuts: ((underline)) $param="wrap-selection" prefix="__" suffix="__" - trimSeletion="yes" + trimSelection="yes" /> diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid index a57864a9e88..222204bbc75 100644 --- a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid @@ -153,7 +153,7 @@ An example can be seen in [[$:/core/ui/EditorToolbar/bold]]: $param="wrap-selection" prefix="''" suffix="''" - trimSeletion="yes" + trimSelection="yes" /> ``` From 2236591913bc700d7c70654a05fd74bc122b9da6 Mon Sep 17 00:00:00 2001 From: pmario Date: Sun, 28 Jan 2024 17:23:23 +0100 Subject: [PATCH 06/12] fix trimSelection typo --- core/modules/editor/operations/text/wrap-selection.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/modules/editor/operations/text/wrap-selection.js b/core/modules/editor/operations/text/wrap-selection.js index a5bf6f01f42..df4f37e7974 100644 --- a/core/modules/editor/operations/text/wrap-selection.js +++ b/core/modules/editor/operations/text/wrap-selection.js @@ -18,7 +18,7 @@ exports["wrap-selection"] = function(event,operation) { prefixLength = prefix.length, suffix = event.paramObject.suffix, suffixLength = suffix.length, - trimSeletion = event.paramObject.trimSeletion || "no", + trimSelection = event.paramObject.trimSelection || "no", selLength = o.selEnd - o.selStart; var trailingSpaceAt = function(sel) { @@ -27,7 +27,7 @@ exports["wrap-selection"] = function(event,operation) { _end, result; // this evaluation takes the user configuration into account! - switch (trimSeletion) { + switch (trimSelection) { case "end": result = (sel.trimEnd().length !== selLength) ? "end" : "no"; break; From d005efa34ff838f086919d96cdee01ab704d6b09 Mon Sep 17 00:00:00 2001 From: pmario Date: Sun, 28 Jan 2024 17:27:22 +0100 Subject: [PATCH 07/12] docs trimSelection add "no" as possible value - it's the default --- .../tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid index 222204bbc75..69da9f8cb1c 100644 --- a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid @@ -77,7 +77,7 @@ Surrounds the current //selection// with the provided <<.param "prefix">> and << |!Name |!Description | |prefix |String to be prefixed to the selection | |suffix |Suffix to be inserted after the selection | -|trimSelection |<<.from-version 5.3.4>> Trim leading and trailing white-space from the selection and move it to the surrounding text. Possible values are: `yes`, `start` and `end` | +|trimSelection |<<.from-version 5.3.4>> 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` | \end From 586905121c2031c17e6b09709e1c92816429af0b Mon Sep 17 00:00:00 2001 From: pmario Date: Sat, 30 Mar 2024 08:28:19 +0100 Subject: [PATCH 08/12] remove some redundant variable declarations --- .../editor/operations/text/wrap-selection.js | 62 ++++++++++--------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/core/modules/editor/operations/text/wrap-selection.js b/core/modules/editor/operations/text/wrap-selection.js index df4f37e7974..e48cdf2869b 100644 --- a/core/modules/editor/operations/text/wrap-selection.js +++ b/core/modules/editor/operations/text/wrap-selection.js @@ -15,43 +15,45 @@ Text editor operation to wrap the selection with the specified prefix and suffix exports["wrap-selection"] = function(event,operation) { var o = operation, prefix = event.paramObject.prefix, - prefixLength = prefix.length, suffix = event.paramObject.suffix, - suffixLength = suffix.length, 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 start + // end .. there are trailing spaces at the end + // no .. no trailing spaces are taken into account var trailingSpaceAt = function(sel) { - // returns "yes", "start", "end", "no" var _start, _end, result; - // this evaluation takes the user configuration into account! + // trimSelection is a user parameter, which this evaluations take into account switch (trimSelection) { case "end": result = (sel.trimEnd().length !== selLength) ? "end" : "no"; - break; + break; case "yes": _start = sel.trimStart().length !== selLength; _end = sel.trimEnd().length !== selLength; result = (_start && _end) ? "yes" : (_start) ? "start" : (_end) ? "end" : "no"; - break; + break; case "start": result = (sel.trimStart().length !== selLength) ? "start" : "no"; - break; + break; default: result = "no"; - break; + break; } return result; } function togglePrefixSuffix() { - // this was the only behavour till TW v5.3.3 - if(o.text.substring(o.selStart - prefixLength, o.selStart + suffixLength) === prefix + suffix) { + if(o.text.substring(o.selStart - prefix.length, o.selStart + suffix.length) === prefix + suffix) { // Remove the prefix and suffix - o.cutStart = o.selStart - prefixLength; - o.cutEnd = o.selEnd + suffixLength; + o.cutStart = o.selStart - prefix.length; + o.cutEnd = o.selEnd + suffix.length; o.replacement = ""; o.newSelStart = o.cutStart; o.newSelEnd = o.newSelStart; @@ -60,20 +62,20 @@ exports["wrap-selection"] = function(event,operation) { o.cutStart = o.selStart; o.cutEnd = o.selEnd; o.replacement = prefix + suffix; - o.newSelStart = o.selStart + prefixLength; + o.newSelStart = o.selStart + prefix.length; o.newSelEnd = o.newSelStart; } } - // options: prefixLen, suffixLen + // options: lenPrefix, lenSuffix function removePrefixSuffix(options) { options = options || {}; - var prefixLen = options.prefixLen || 0; - var suffixLen = options.suffixLen || 0; + var lenPrefix = options.lenPrefix || 0; + var lenSuffix = options.lenSuffix || 0; - o.cutStart = o.selStart - prefixLen; - o.cutEnd = o.selEnd + suffixLen; - o.replacement = (prefixLen || suffixLen) ? o.selection : o.selection.substring(prefixLength, o.selection.length - suffixLength); + 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; } @@ -88,43 +90,45 @@ exports["wrap-selection"] = function(event,operation) { o.replacement = prefix + o.selection + suffix; o.newSelStart = o.selStart; o.newSelEnd = o.selStart + o.replacement.length; - break; + 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; + 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; + 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; + break; } } - if(o.selStart === o.selEnd) { // No selection; Create prefix and suffix. Set cursor between them: ""|"" togglePrefixSuffix(); - } else if( o.text.substring(o.selStart, o.selStart + prefixLength) === prefix && - o.text.substring(o.selEnd - suffixLength,o.selEnd) === suffix) { + } 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 removePrefixSuffix(); - } else if( o.text.substring(o.selStart - prefixLength, o.selStart) === prefix && - o.text.substring(o.selEnd, o.selEnd + suffixLength) === suffix) { + } else if( o.text.substring(o.selStart - prefix.length, o.selStart) === prefix && + o.text.substring(o.selEnd, o.selEnd + suffix.length) === suffix) { // Prefix and suffix are present BUT not selected -> remove them - removePrefixSuffix({"prefixLen": prefixLength, "suffixLen": suffixLength}); + removePrefixSuffix({"lenPrefix": prefix.length, "lenSuffix": suffix.length}); } else { // Add the prefix and suffix addPrefixSuffix(); From 8d9a26eec4d1b7161355db1405095276b06cdb1a Mon Sep 17 00:00:00 2001 From: pmario Date: Mon, 9 Sep 2024 10:36:31 +0200 Subject: [PATCH 09/12] update from-version --- .../messages/WidgetMessage_ tm-edit-text-operation.tid | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid index 69da9f8cb1c..10dfcf16334 100644 --- a/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid +++ b/editions/tw5.com/tiddlers/messages/WidgetMessage_ tm-edit-text-operation.tid @@ -1,6 +1,6 @@ caption: tm-edit-text-operation created: 20160424211339792 -modified: 20240123112124856 +modified: 20240909083525060 tags: Messages title: WidgetMessage: tm-edit-text-operation type: text/vnd.tiddlywiki @@ -77,7 +77,7 @@ Surrounds the current //selection// with the provided <<.param "prefix">> and << |!Name |!Description | |prefix |String to be prefixed to the selection | |suffix |Suffix to be inserted after the selection | -|trimSelection |<<.from-version 5.3.4>> 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` | +|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` | \end From 6ad0c9f9735330b0cfcc3d58f62133b49dab449b Mon Sep 17 00:00:00 2001 From: pmario Date: Mon, 9 Sep 2024 11:56:22 +0200 Subject: [PATCH 10/12] fix comment typos --- .../editor/operations/text/wrap-selection.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/modules/editor/operations/text/wrap-selection.js b/core/modules/editor/operations/text/wrap-selection.js index e48cdf2869b..6f053457036 100644 --- a/core/modules/editor/operations/text/wrap-selection.js +++ b/core/modules/editor/operations/text/wrap-selection.js @@ -19,17 +19,17 @@ exports["wrap-selection"] = function(event,operation) { 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 + // 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 start - // end .. there are trailing spaces at the end - // no .. no trailing spaces are taken into account + // 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 take into account + // trimSelection is a user parameter, which this evaluations takes into account switch (trimSelection) { case "end": result = (sel.trimEnd().length !== selLength) ? "end" : "no"; @@ -119,7 +119,7 @@ exports["wrap-selection"] = function(event,operation) { } if(o.selStart === o.selEnd) { - // No selection; Create prefix and suffix. Set cursor between them: ""|"" + // 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) { From 95bd306a98a4aa2884a6432f3be1aa812fa4bf62 Mon Sep 17 00:00:00 2001 From: pmario Date: Mon, 9 Sep 2024 11:57:22 +0200 Subject: [PATCH 11/12] fix whitespace --- core/modules/editor/operations/text/wrap-selection.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/modules/editor/operations/text/wrap-selection.js b/core/modules/editor/operations/text/wrap-selection.js index 6f053457036..dd297f07729 100644 --- a/core/modules/editor/operations/text/wrap-selection.js +++ b/core/modules/editor/operations/text/wrap-selection.js @@ -30,7 +30,7 @@ exports["wrap-selection"] = function(event,operation) { _end, result; // trimSelection is a user parameter, which this evaluations takes into account - switch (trimSelection) { + switch(trimSelection) { case "end": result = (sel.trimEnd().length !== selLength) ? "end" : "no"; break; @@ -82,7 +82,7 @@ exports["wrap-selection"] = function(event,operation) { function addPrefixSuffix() { // remove trailing space if requested - switch (trailingSpaceAt(o.selection)) { + switch(trailingSpaceAt(o.selection)) { case "no": // has no trailing spaces o.cutStart = o.selStart; @@ -121,11 +121,11 @@ exports["wrap-selection"] = function(event,operation) { 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 && + } 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 removePrefixSuffix(); - } else if( o.text.substring(o.selStart - prefix.length, o.selStart) === prefix && + } else if(o.text.substring(o.selStart - prefix.length, o.selStart) === prefix && o.text.substring(o.selEnd, o.selEnd + suffix.length) === suffix) { // Prefix and suffix are present BUT not selected -> remove them removePrefixSuffix({"lenPrefix": prefix.length, "lenSuffix": suffix.length}); From 94a1fd7636e547dcaefce6205f0f82b5a15c4ce5 Mon Sep 17 00:00:00 2001 From: pmario Date: Mon, 9 Sep 2024 11:57:49 +0200 Subject: [PATCH 12/12] make local variables more visible --- core/modules/editor/operations/text/wrap-selection.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/modules/editor/operations/text/wrap-selection.js b/core/modules/editor/operations/text/wrap-selection.js index dd297f07729..665d72eb45d 100644 --- a/core/modules/editor/operations/text/wrap-selection.js +++ b/core/modules/editor/operations/text/wrap-selection.js @@ -70,12 +70,12 @@ exports["wrap-selection"] = function(event,operation) { // options: lenPrefix, lenSuffix function removePrefixSuffix(options) { options = options || {}; - var lenPrefix = options.lenPrefix || 0; - var lenSuffix = options.lenSuffix || 0; + 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.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; }