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 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,
);
}

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