MarkdownToolbar: Make it work for selected text

Fixes #522
This commit is contained in:
Vishesh Handa
2021-08-02 10:40:27 +02:00
parent 60ca4a6a9d
commit 1ac0627f13
2 changed files with 57 additions and 22 deletions

View File

@ -212,25 +212,34 @@ TextEditingValue modifyCurrentWord(
var selection = textEditingValue.selection; var selection = textEditingValue.selection;
var text = textEditingValue.text; var text = textEditingValue.text;
//print('Base offset: ${selection.baseOffset}'); late int wordStartPos;
//print('Extent offset: ${selection.extentOffset}'); late int wordEndPos;
var cursorPos = selection.baseOffset; var selectionMode = false;
if (cursorPos == -1) {
cursorPos = 0;
}
//print('CursorPos: $cursorPos');
var wordStartPos = // Text Selected
text.lastIndexOf(RegExp('\\s'), cursorPos == 0 ? 0 : cursorPos - 1); if (selection.baseOffset != selection.extentOffset) {
if (wordStartPos == -1) { wordStartPos = selection.baseOffset;
wordStartPos = 0; wordEndPos = selection.extentOffset;
selectionMode = true;
} else { } else {
wordStartPos += 1; var cursorPos = selection.baseOffset;
} if (cursorPos == -1) {
cursorPos = 0;
}
//print('CursorPos: $cursorPos');
var wordEndPos = text.indexOf(RegExp('\\s'), cursorPos); wordStartPos =
if (wordEndPos == -1) { text.lastIndexOf(RegExp('\\s'), cursorPos == 0 ? 0 : cursorPos - 1);
wordEndPos = text.length; if (wordStartPos == -1) {
wordStartPos = 0;
} else {
wordStartPos += 1;
}
wordEndPos = text.indexOf(RegExp('\\s'), cursorPos);
if (wordEndPos == -1) {
wordEndPos = text.length;
}
} }
//print('Word Start: $wordStartPos'); //print('Word Start: $wordStartPos');
@ -243,11 +252,16 @@ TextEditingValue modifyCurrentWord(
text = text.replaceFirst(char, '', wordStartPos); text = text.replaceFirst(char, '', wordStartPos);
wordEndPos -= char.length; wordEndPos -= char.length;
var newSelection = selectionMode
? TextSelection(
baseOffset: wordStartPos,
extentOffset: wordEndPos - char.length,
)
: TextSelection.collapsed(offset: wordEndPos - char.length);
return TextEditingValue( return TextEditingValue(
text: text.replaceFirst(char, '', wordEndPos - char.length), text: text.replaceFirst(char, '', wordEndPos - char.length),
selection: TextSelection.collapsed( selection: newSelection,
offset: wordEndPos - char.length,
),
); );
} }
@ -255,9 +269,16 @@ TextEditingValue modifyCurrentWord(
text = text.replaceRange(wordStartPos, wordStartPos, char); text = text.replaceRange(wordStartPos, wordStartPos, char);
wordEndPos += char.length; wordEndPos += char.length;
var newSelection = selectionMode
? TextSelection(
baseOffset: wordStartPos,
extentOffset: wordEndPos + char.length,
)
: TextSelection.collapsed(offset: wordEndPos);
return TextEditingValue( return TextEditingValue(
text: text.replaceRange(wordEndPos, wordEndPos, char), text: text.replaceRange(wordEndPos, wordEndPos, char),
selection: TextSelection.collapsed(offset: wordEndPos), selection: newSelection,
); );
} }

View File

@ -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 // Navigation
// //
@ -367,6 +383,4 @@ void main() {
afterOffset: 1, afterOffset: 1,
); );
}); });
// TODO: Test that if some text is selected then it should be modified
} }