mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 03:19:11 +08:00
MarkdownToolbar: Add tests for surrounding the current word with **
This commit is contained in:
@ -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),
|
||||
);
|
||||
}
|
||||
|
@ -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: '**',
|
||||
);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user