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 textEditingValue,
String char,
@ -112,35 +128,62 @@ TextEditingValue modifyCurrentLine(
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 End: $lineEndPos');
//print('Line: ${text.substring(lineStartPos, lineEndPos)}');
// Check if already present
if (text.startsWith(char, lineStartPos)) {
//print('Removing `$char`');
var endOffset = cursorPos;
//print("End Offset: $endOffset");
if (endOffset > lineStartPos) {
endOffset -= char.length;
//print("End Offset min char: $endOffset");
}
if (endOffset < lineStartPos) {
endOffset = lineStartPos;
//print("End Offset equal LineStartPos: $endOffset");
}
return TextEditingValue(
text: text.replaceFirst(char, '', lineStartPos),
selection: TextSelection.collapsed(offset: endOffset),
);
}
return TextEditingValue(
text: text.replaceRange(lineStartPos, lineStartPos, char),
selection: TextSelection.collapsed(offset: cursorPos + char.length),
);
}
TextEditingValue _removeFromLine(
String text,
int cursorPos,
int lineStartPos,
String char,
) {
//print('Removing `$char`');
var endOffset = cursorPos;
//print("End Offset: $endOffset");
if (endOffset > lineStartPos) {
endOffset -= char.length;
//print("End Offset min char: $endOffset");
}
if (endOffset < lineStartPos) {
endOffset = lineStartPos;
//print("End Offset equal LineStartPos: $endOffset");
}
return TextEditingValue(
text: text.replaceFirst(char, '', lineStartPos),
selection: TextSelection.collapsed(offset: endOffset),
);
}
TextEditingValue modifyCurrentWord(
TextEditingValue textEditingValue,
String char,

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
//
@ -252,5 +272,5 @@ void main() {
_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
}