diff --git a/lib/editors/markdown_toolbar.dart b/lib/editors/markdown_toolbar.dart index 94a3499e..ea412fc7 100644 --- a/lib/editors/markdown_toolbar.dart +++ b/lib/editors/markdown_toolbar.dart @@ -212,25 +212,34 @@ TextEditingValue modifyCurrentWord( var selection = textEditingValue.selection; var text = textEditingValue.text; - //print('Base offset: ${selection.baseOffset}'); - //print('Extent offset: ${selection.extentOffset}'); - var cursorPos = selection.baseOffset; - if (cursorPos == -1) { - cursorPos = 0; - } - //print('CursorPos: $cursorPos'); + late int wordStartPos; + late int wordEndPos; + var selectionMode = false; - var wordStartPos = - text.lastIndexOf(RegExp('\\s'), cursorPos == 0 ? 0 : cursorPos - 1); - if (wordStartPos == -1) { - wordStartPos = 0; + // Text Selected + if (selection.baseOffset != selection.extentOffset) { + wordStartPos = selection.baseOffset; + wordEndPos = selection.extentOffset; + selectionMode = true; } else { - wordStartPos += 1; - } + var cursorPos = selection.baseOffset; + if (cursorPos == -1) { + cursorPos = 0; + } + //print('CursorPos: $cursorPos'); - var wordEndPos = text.indexOf(RegExp('\\s'), cursorPos); - if (wordEndPos == -1) { - wordEndPos = text.length; + wordStartPos = + text.lastIndexOf(RegExp('\\s'), cursorPos == 0 ? 0 : cursorPos - 1); + if (wordStartPos == -1) { + wordStartPos = 0; + } else { + wordStartPos += 1; + } + + wordEndPos = text.indexOf(RegExp('\\s'), cursorPos); + if (wordEndPos == -1) { + wordEndPos = text.length; + } } //print('Word Start: $wordStartPos'); @@ -243,11 +252,16 @@ TextEditingValue modifyCurrentWord( text = text.replaceFirst(char, '', wordStartPos); wordEndPos -= char.length; + var newSelection = selectionMode + ? TextSelection( + baseOffset: wordStartPos, + extentOffset: wordEndPos - char.length, + ) + : TextSelection.collapsed(offset: wordEndPos - char.length); + return TextEditingValue( text: text.replaceFirst(char, '', wordEndPos - char.length), - selection: TextSelection.collapsed( - offset: wordEndPos - char.length, - ), + selection: newSelection, ); } @@ -255,9 +269,16 @@ TextEditingValue modifyCurrentWord( text = text.replaceRange(wordStartPos, wordStartPos, char); wordEndPos += char.length; + var newSelection = selectionMode + ? TextSelection( + baseOffset: wordStartPos, + extentOffset: wordEndPos + char.length, + ) + : TextSelection.collapsed(offset: wordEndPos); + return TextEditingValue( text: text.replaceRange(wordEndPos, wordEndPos, char), - selection: TextSelection.collapsed(offset: wordEndPos), + selection: newSelection, ); } diff --git a/test/markdown_toolbar_test.dart b/test/markdown_toolbar_test.dart index daf9c23b..484c4072 100644 --- a/test/markdown_toolbar_test.dart +++ b/test/markdown_toolbar_test.dart @@ -197,6 +197,22 @@ void main() { ); }); + test("Word selection", () { + var val = const TextEditingValue( + text: 'Hello\nHydra Person', + selection: TextSelection(baseOffset: 6, extentOffset: 11), + ); + + var newVal = modifyCurrentWord(val, '**'); + + expect(newVal.text, 'Hello\n**Hydra** Person'); + expect(newVal.selection.baseOffset, 6); + expect(newVal.selection.extentOffset, 15); + + var newVal2 = modifyCurrentWord(newVal, '**'); + expect(newVal2, val); + }); + // // Navigation // @@ -367,6 +383,4 @@ void main() { afterOffset: 1, ); }); - - // TODO: Test that if some text is selected then it should be modified }