From c6d5ebc0b261b3ac556be785e616150206ba964c Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 14 Aug 2020 21:17:29 +0200 Subject: [PATCH] MarkdownToolbar: Add tests for surrounding the current word with ** --- lib/widgets/markdown_toolbar.dart | 99 +++++++++++++++++-------------- test/markdown_toolbar_test.dart | 43 ++++++++++++++ 2 files changed, 97 insertions(+), 45 deletions(-) diff --git a/lib/widgets/markdown_toolbar.dart b/lib/widgets/markdown_toolbar.dart index cca4002d..c78a6f83 100644 --- a/lib/widgets/markdown_toolbar.dart +++ b/lib/widgets/markdown_toolbar.dart @@ -32,51 +32,7 @@ class MarkdownToolBar extends StatelessWidget { } void _modifyCurrentWord(String char) { - var selection = textController.value.selection; - var text = textController.value.text; - - print('Base offset: ${selection.baseOffset}'); - print('Extent offset: ${selection.extentOffset}'); - var cursorPos = selection.baseOffset; - if (cursorPos == -1) { - cursorPos = 0; - } - print('CursorPos: $cursorPos'); - - var wordStartPos = - text.lastIndexOf(' ', cursorPos == 0 ? 0 : cursorPos - 1); - if (wordStartPos == -1) { - wordStartPos = 0; - } - - var wordEndPos = text.indexOf(' ', cursorPos); - if (wordEndPos == -1) { - wordEndPos = text.length; - } - - print('Word Start: $wordStartPos'); - print('Word End: $wordEndPos'); - print('Word: ${text.substring(wordStartPos, wordEndPos)}'); - - // Check if already present - if (text.startsWith(char, wordStartPos)) { - print('Removing `$char`'); - textController.text = text.replaceFirst(char, '', wordStartPos); - textController.selection = - TextSelection.collapsed(offset: cursorPos - (char.length * 2)); - return; - } - - print('Adding `$char`'); - textController.text = text.replaceRange(wordStartPos, wordStartPos, char); - wordEndPos += char.length; - - textController.text = - text.replaceRange(wordEndPos - 1, wordEndPos - 1, char); - textController.selection = - TextSelection.collapsed(offset: cursorPos + (char.length * 2)); - - print('$char'); + textController.value = modifyCurrentWord(textController.value, char); } } @@ -135,3 +91,56 @@ TextEditingValue modifyCurrentLine( selection: TextSelection.collapsed(offset: cursorPos + char.length), ); } + +TextEditingValue modifyCurrentWord( + TextEditingValue textEditingValue, + String char, +) { + 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'); + + var wordStartPos = text.lastIndexOf(' ', cursorPos == 0 ? 0 : cursorPos - 1); + if (wordStartPos == -1) { + wordStartPos = 0; + } + + var wordEndPos = text.indexOf(' ', cursorPos); + if (wordEndPos == -1) { + wordEndPos = text.length; + } + + print('Word Start: $wordStartPos'); + print('Word End: $wordEndPos'); + print('Word: ${text.substring(wordStartPos, wordEndPos)}'); + + // Check if already present + if (text.startsWith(char, wordStartPos) && + text.startsWith(char, wordEndPos - char.length)) { + text = text.replaceFirst(char, '', wordStartPos); + wordEndPos -= char.length; + + return TextEditingValue( + text: text.replaceFirst(char, '', wordEndPos - char.length), + selection: TextSelection.collapsed( + offset: wordEndPos - char.length, + ), + ); + } + + print('Adding `$char`'); + text = text.replaceRange(wordStartPos, wordStartPos, char); + wordEndPos += char.length; + + return TextEditingValue( + text: text.replaceRange(wordEndPos, wordEndPos, char), + selection: TextSelection.collapsed(offset: wordEndPos), + ); +} diff --git a/test/markdown_toolbar_test.dart b/test/markdown_toolbar_test.dart index abb202bb..bd0ca2d8 100644 --- a/test/markdown_toolbar_test.dart +++ b/test/markdown_toolbar_test.dart @@ -103,4 +103,47 @@ void main() { char: '# ', ); }); + + // + // Word based + // + void _testWord({ + @required String before, + @required int beforeOffset, + @required String after, + @required int afterOffset, + @required String char, + }) { + var val = TextEditingValue( + text: before, + selection: TextSelection.collapsed(offset: beforeOffset), + ); + + var expectedVal = TextEditingValue( + text: after, + selection: TextSelection.collapsed(offset: afterOffset), + ); + + expect(modifyCurrentWord(val, char), expectedVal); + } + + test("Surrounds the first word", () { + _testWord( + before: 'Hello', + beforeOffset: 3, + after: '**Hello**', + afterOffset: 7, + char: '**', + ); + }); + + test("Removing from the first word", () { + _testWord( + before: '**Hello**', + beforeOffset: 3, + after: 'Hello', + afterOffset: 5, + char: '**', + ); + }); }