mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 10:17:16 +08:00
Add a test for MarkdownToolbar's replace current line
The logic is non trivial
This commit is contained in:
@ -28,45 +28,7 @@ class MarkdownToolBar extends StatelessWidget {
|
||||
}
|
||||
|
||||
void _modifyCurrentLine(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 lineStartPos =
|
||||
text.lastIndexOf('\n', cursorPos == 0 ? 0 : cursorPos - 1);
|
||||
if (lineStartPos == -1) {
|
||||
lineStartPos = 0;
|
||||
}
|
||||
|
||||
var lineEndPos = text.indexOf('\n', cursorPos);
|
||||
if (lineEndPos == -1) {
|
||||
lineEndPos = text.length;
|
||||
}
|
||||
|
||||
print('Line Start: $lineStartPos');
|
||||
print('Line End: $lineEndPos');
|
||||
print('Line: ${text.substring(lineStartPos, lineEndPos)}');
|
||||
|
||||
// Check if already present
|
||||
if (text.startsWith(char, lineStartPos)) {
|
||||
print('Removing `$char`');
|
||||
textController.text = text.replaceFirst(char, '', lineStartPos);
|
||||
textController.selection =
|
||||
TextSelection.collapsed(offset: cursorPos - char.length);
|
||||
return;
|
||||
}
|
||||
|
||||
print('Adding `$char`');
|
||||
textController.text = text.replaceRange(lineStartPos, lineStartPos, char);
|
||||
textController.selection =
|
||||
TextSelection.collapsed(offset: cursorPos + char.length);
|
||||
textController.value = modifyCurrentLine(textController.value, char);
|
||||
}
|
||||
|
||||
void _modifyCurrentWord(String char) {
|
||||
@ -117,3 +79,47 @@ class MarkdownToolBar extends StatelessWidget {
|
||||
print('$char');
|
||||
}
|
||||
}
|
||||
|
||||
TextEditingValue modifyCurrentLine(
|
||||
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 lineStartPos = text.lastIndexOf('\n', cursorPos == 0 ? 0 : cursorPos - 1);
|
||||
if (lineStartPos == -1) {
|
||||
lineStartPos = 0;
|
||||
}
|
||||
|
||||
var lineEndPos = text.indexOf('\n', cursorPos);
|
||||
if (lineEndPos == -1) {
|
||||
lineEndPos = text.length;
|
||||
}
|
||||
|
||||
//print('Line Start: $lineStartPos');
|
||||
//print('Line End: $lineEndPos');
|
||||
//print('Line: ${text.substring(lineStartPos, lineEndPos)}');
|
||||
|
||||
// Check if already present
|
||||
if (text.startsWith(char, lineStartPos)) {
|
||||
//print('Removing `$char`');
|
||||
return TextEditingValue(
|
||||
text: text.replaceFirst(char, '', lineStartPos),
|
||||
selection: TextSelection.collapsed(offset: cursorPos - char.length),
|
||||
);
|
||||
}
|
||||
|
||||
return TextEditingValue(
|
||||
text: text.replaceRange(lineStartPos, lineStartPos, char),
|
||||
selection: TextSelection.collapsed(offset: cursorPos + char.length),
|
||||
);
|
||||
}
|
||||
|
20
test/markdown_toolbar_test.dart
Normal file
20
test/markdown_toolbar_test.dart
Normal file
@ -0,0 +1,20 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'package:gitjournal/widgets/markdown_toolbar.dart';
|
||||
|
||||
void main() {
|
||||
test('Adds a header to the first line correctly', () {
|
||||
var val = const TextEditingValue(
|
||||
text: 'Hello',
|
||||
selection: TextSelection.collapsed(offset: 5),
|
||||
);
|
||||
|
||||
var expectedVal = const TextEditingValue(
|
||||
text: '# Hello',
|
||||
selection: TextSelection.collapsed(offset: 7),
|
||||
);
|
||||
|
||||
expect(modifyCurrentLine(val, '# '), expectedVal);
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user