Skip to content

Commit 04bb786

Browse files
committed
feat: trim CR by default
1 parent d612282 commit 04bb786

1 file changed

Lines changed: 22 additions & 9 deletions

File tree

lib/src/editor_controller.dart

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,26 +80,36 @@ extension BBCodeExt on BBCodeEditorController {
8080
bool get isNotEmpty => !isEmpty;
8181

8282
/// Convert current document to bbcode.
83-
String toBBCode() {
83+
String toBBCode({bool trimCR = true}) {
8484
final converter = DeltaToBBCode();
85-
final ret = converter.convert(document.toDelta());
85+
var ret = converter.convert(document.toDelta());
86+
if (trimCR) {
87+
ret = ret.replaceAll('\r', '');
88+
}
8689
if (ret.endsWith('\n')) {
8790
return ret.substring(0, ret.length - 1);
8891
}
8992
return ret;
9093
}
9194

9295
/// Convert current document to quill delta.
93-
String toQuillDelta() => jsonEncode(document.toDelta().toJson());
96+
String toQuillDelta({bool trimCR = true}) {
97+
final data = jsonEncode(document.toDelta().toJson());
98+
return trimCR ? data.replaceAll('\r', '') : data;
99+
}
94100

95101
/// Insert [text] into current cursor position and format with [attr].
96-
void insertFormattedText(String text, Attribute<dynamic> attr) {
102+
void insertFormattedText(String text, Attribute<dynamic> attr, {bool trimCR = true}) {
103+
final data = switch (trimCR) {
104+
true => text.replaceAll('\r', ''),
105+
false => text,
106+
};
97107
final position = selection.baseOffset;
98108
final length = selection.extentOffset - position;
99109
this
100-
..replaceText(position, length, text, null)
101-
..formatText(position, text.length, attr)
102-
..moveCursorToPosition(position + text.length);
110+
..replaceText(position, length, data, null)
111+
..formatText(position, data.length, attr)
112+
..moveCursorToPosition(position + data.length);
103113
}
104114

105115
/// Insert raw bbcode that has a [head] and [tail] and move cursor to the
@@ -136,8 +146,11 @@ extension BBCodeExt on BBCodeEditorController {
136146
}
137147

138148
/// Insert bbcode text in the cursor position.
139-
void insertBBCode(String bbcode) {
140-
final code = bbcode.replaceAll('\r', '');
149+
void insertBBCode(String bbcode, {bool trimCR = true}) {
150+
final code = switch (trimCR) {
151+
true => bbcode.replaceAll('\r', ''),
152+
false => bbcode,
153+
};
141154
final position = selection.baseOffset;
142155
final length = selection.extentOffset - position;
143156
var delta = parseBBCodeTextToDelta(code);

0 commit comments

Comments
 (0)