mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-25 00:00:26 +08:00
@ -212,15 +212,23 @@ TextEditingValue modifyCurrentWord(
|
||||
var selection = textEditingValue.selection;
|
||||
var text = textEditingValue.text;
|
||||
|
||||
//print('Base offset: ${selection.baseOffset}');
|
||||
//print('Extent offset: ${selection.extentOffset}');
|
||||
late int wordStartPos;
|
||||
late int wordEndPos;
|
||||
var selectionMode = false;
|
||||
|
||||
// Text Selected
|
||||
if (selection.baseOffset != selection.extentOffset) {
|
||||
wordStartPos = selection.baseOffset;
|
||||
wordEndPos = selection.extentOffset;
|
||||
selectionMode = true;
|
||||
} else {
|
||||
var cursorPos = selection.baseOffset;
|
||||
if (cursorPos == -1) {
|
||||
cursorPos = 0;
|
||||
}
|
||||
//print('CursorPos: $cursorPos');
|
||||
|
||||
var wordStartPos =
|
||||
wordStartPos =
|
||||
text.lastIndexOf(RegExp('\\s'), cursorPos == 0 ? 0 : cursorPos - 1);
|
||||
if (wordStartPos == -1) {
|
||||
wordStartPos = 0;
|
||||
@ -228,10 +236,11 @@ TextEditingValue modifyCurrentWord(
|
||||
wordStartPos += 1;
|
||||
}
|
||||
|
||||
var wordEndPos = text.indexOf(RegExp('\\s'), cursorPos);
|
||||
wordEndPos = text.indexOf(RegExp('\\s'), cursorPos);
|
||||
if (wordEndPos == -1) {
|
||||
wordEndPos = text.length;
|
||||
}
|
||||
}
|
||||
|
||||
//print('Word Start: $wordStartPos');
|
||||
//print('Word End: $wordEndPos');
|
||||
@ -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,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user