mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-26 16:46:51 +08:00
@ -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,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user