MarkdownToolbar: Remove old block tag

If a text starts with '# ' and we click on the '- ' button, it should
remove the old tag '#' and replace it with '- '. Before this it would
have done something like this '- #'
This commit is contained in:
Vishesh Handa
2020-09-02 00:00:53 +02:00
parent fefe354e83
commit 9ec33842bc
2 changed files with 83 additions and 20 deletions

View File

@ -85,6 +85,22 @@ class MarkdownToolBar extends StatelessWidget {
} }
} }
final _allowedBlockTags = [
'# ',
'## ',
'### ',
'#### ',
'##### ',
'###### ',
'- ',
'* ',
];
final _allowedBlockRegExps = [
RegExp('- \[[xX ]\] '),
RegExp('\d+\. '),
];
TextEditingValue modifyCurrentLine( TextEditingValue modifyCurrentLine(
TextEditingValue textEditingValue, TextEditingValue textEditingValue,
String char, String char,
@ -112,13 +128,46 @@ TextEditingValue modifyCurrentLine(
lineEndPos = text.length; lineEndPos = text.length;
} }
// Check if line already has a block tag
for (var blockTag in _allowedBlockTags) {
if (text.startsWith(blockTag, lineStartPos)) {
var newVal = _removeFromLine(text, cursorPos, lineStartPos, blockTag);
if (blockTag == char) {
return newVal;
}
return modifyCurrentLine(newVal, char);
}
}
for (var blockTagRegExp in _allowedBlockRegExps) {
var match = blockTagRegExp.matchAsPrefix(text, lineStartPos);
if (match != null) {
var blockTag = match.group(0);
var newVal = _removeFromLine(text, cursorPos, lineStartPos, blockTag);
if (blockTag == char) {
return newVal;
}
return modifyCurrentLine(newVal, char);
}
}
//print('Line Start: $lineStartPos'); //print('Line Start: $lineStartPos');
//print('Line End: $lineEndPos'); //print('Line End: $lineEndPos');
//print('Line: ${text.substring(lineStartPos, lineEndPos)}'); //print('Line: ${text.substring(lineStartPos, lineEndPos)}');
// Check if already present return TextEditingValue(
if (text.startsWith(char, lineStartPos)) { text: text.replaceRange(lineStartPos, lineStartPos, char),
//print('Removing `$char`'); selection: TextSelection.collapsed(offset: cursorPos + char.length),
);
}
TextEditingValue _removeFromLine(
String text,
int cursorPos,
int lineStartPos,
String char,
) {
//print('Removing `$char`');
var endOffset = cursorPos; var endOffset = cursorPos;
//print("End Offset: $endOffset"); //print("End Offset: $endOffset");
if (endOffset > lineStartPos) { if (endOffset > lineStartPos) {
@ -133,12 +182,6 @@ TextEditingValue modifyCurrentLine(
text: text.replaceFirst(char, '', lineStartPos), text: text.replaceFirst(char, '', lineStartPos),
selection: TextSelection.collapsed(offset: endOffset), selection: TextSelection.collapsed(offset: endOffset),
); );
}
return TextEditingValue(
text: text.replaceRange(lineStartPos, lineStartPos, char),
selection: TextSelection.collapsed(offset: cursorPos + char.length),
);
} }
TextEditingValue modifyCurrentWord( TextEditingValue modifyCurrentWord(

View File

@ -104,6 +104,26 @@ void main() {
); );
}); });
test("Replaces h1 with h2", () {
_testLine(
before: 'Hi\n# Hello Darkness\nFire',
beforeOffset: 4,
after: 'Hi\n## Hello Darkness\nFire',
afterOffset: 6,
char: '## ',
);
});
test("Replaces h2 with list", () {
_testLine(
before: 'Hi\n## Hello Darkness\nFire',
beforeOffset: 5,
after: 'Hi\n- Hello Darkness\nFire',
afterOffset: 5,
char: '- ',
);
});
// //
// Word based // Word based
// //
@ -252,5 +272,5 @@ void main() {
_testPrevWord(text, 0, 0); _testPrevWord(text, 0, 0);
}); });
// Test that clicking on h2 removes h1 and then adds h2 // Test that if some text is selected then it should be modified
} }