Skip to content

Commit

Permalink
Fix existing tests, added tests for spoilers, and adjusted spoiler lo…
Browse files Browse the repository at this point in the history
…gic (#8)

This PR contains the following changes:
- Fixed all existing tests that were failing
- Added some spoiler tests for formatting to markdown, and widget tests
- Adjusted some of the logic for how spoilers are created. This is to
allow inline selection of content to create spoilers
- Additionally, I've adjusted to cursor positioning so that the cursor
lands at the end of the spoiler body



https://github.com/thunder-app/markdown-editor/assets/30667958/580aef1d-b95d-4c69-a318-65514b69f84b
  • Loading branch information
hjiangsu authored Apr 12, 2024
1 parent 98242e9 commit a59db6b
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 69 deletions.
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class _AppState extends State<App> {
MarkdownType.link,
MarkdownType.bold,
MarkdownType.italic,
MarkdownType.spoiler,
MarkdownType.blockquote,
MarkdownType.strikethrough,
MarkdownType.title,
Expand Down
12 changes: 10 additions & 2 deletions lib/src/format_markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,17 @@ class FormatMarkdown {
replaceCursorIndex = 3;
break;
case MarkdownType.spoiler:
changedData = '::: spoiler Spoiler\n$selectedText\n:::';
if (fromIndex == 0 && toIndex == data.length) {
// If the entire data is selected, then convert to spoiler
changedData = '::: spoiler Spoiler\n${data.substring(fromIndex, toIndex)}\n:::';
cursorIndex = 20;
} else {
// If part of the data is selected, then add new lines when necessary
changedData = '${fromIndex == 0 ? '' : '\n'}::: spoiler Spoiler\n${data.substring(fromIndex, toIndex)}\n:::${toIndex == data.length ? '' : '\n'}';
cursorIndex = fromIndex == 0 ? 20 + data.substring(fromIndex, toIndex).length : 21 + data.substring(fromIndex, toIndex).length;
}

replaceCursorIndex = 0;
cursorIndex = 20 + selectedText.length;
break;
case MarkdownType.username:
case MarkdownType.community:
Expand Down
6 changes: 5 additions & 1 deletion lib/src/markdown_text_input_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class MarkdownTextInputField extends StatefulWidget {
/// Overrides input text style
final TextStyle? textStyle;

/// Configuration for spell checking
final SpellCheckConfiguration? spellCheckConfiguration;

const MarkdownTextInputField({
super.key,
required this.controller,
Expand All @@ -47,6 +50,7 @@ class MarkdownTextInputField extends StatefulWidget {
this.minLines,
this.maxLines,
this.textStyle,
this.spellCheckConfiguration,
});

@override
Expand Down Expand Up @@ -80,7 +84,7 @@ class _MarkdownTextInputFieldState extends State<MarkdownTextInputField> {
return Column(
children: <Widget>[
TextFormField(
spellCheckConfiguration: kIsWeb ? null : const SpellCheckConfiguration(),
spellCheckConfiguration: widget.spellCheckConfiguration ?? (kIsWeb ? null : const SpellCheckConfiguration()),
minLines: widget.minLines,
focusNode: widget.focusNode,
textInputAction: TextInputAction.newline,
Expand Down
134 changes: 131 additions & 3 deletions test/format_markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -471,8 +471,8 @@ void main() {

test('successfully converts to image link (RTL)', () {
String testString = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.';
int from = 12;
int to = 17;
int from = 17;
int to = 12;

ResultMarkdown formattedText = FormatMarkdown.convertToMarkdown(
MarkdownType.image,
Expand All @@ -485,5 +485,133 @@ void main() {
expect(formattedText.cursorIndex, 15, reason: "dolor length = 5, '![](dolor)'= 10");
});

// TODO: Add tests for username, community, spoiler
test('successfully converts to spoiler (LTR)', () {
String testString = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.';
int from = 12;
int to = 17;

ResultMarkdown formattedText = FormatMarkdown.convertToMarkdown(
MarkdownType.spoiler,
testString,
from,
to,
);

expect(formattedText.data, 'Lorem ipsum \n::: spoiler Spoiler\ndolor\n:::\n sit amet consectetur adipiscing elit.');
expect(formattedText.cursorIndex, 26, reason: "dolor length = 5, '\n::: spoiler Spoiler\ndolor\n:::\n'= 31");
});

test('successfully converts to spoiler (RTL)', () {
String testString = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.';
int from = 17;
int to = 12;

ResultMarkdown formattedText = FormatMarkdown.convertToMarkdown(
MarkdownType.spoiler,
testString,
from,
to,
);

expect(formattedText.data, 'Lorem ipsum \n::: spoiler Spoiler\ndolor\n:::\n sit amet consectetur adipiscing elit.');
expect(formattedText.cursorIndex, 26, reason: "dolor length = 5, '\n::: spoiler Spoiler\ndolor\n:::\n'= 31");
});

test('successfully converts to spoiler (partial with start index at 0) (LTR)', () {
String testString = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.';
int from = 0;
int to = 5;

ResultMarkdown formattedText = FormatMarkdown.convertToMarkdown(
MarkdownType.spoiler,
testString,
from,
to,
);

expect(formattedText.data, '::: spoiler Spoiler\nLorem\n:::\n ipsum dolor sit amet consectetur adipiscing elit.');
expect(formattedText.cursorIndex, 25, reason: "::: spoiler Spoiler\nLorem = 25");
});

test('successfully converts to spoiler (partial with start index at 0) (RTL)', () {
String testString = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.';
int from = 5;
int to = 0;

ResultMarkdown formattedText = FormatMarkdown.convertToMarkdown(
MarkdownType.spoiler,
testString,
from,
to,
);

expect(formattedText.data, '::: spoiler Spoiler\nLorem\n:::\n ipsum dolor sit amet consectetur adipiscing elit.');
expect(formattedText.cursorIndex, 25, reason: "::: spoiler Spoiler\nLorem = 25");
});

test('successfully converts to spoiler (partial with start index in the middle) (LTR)', () {
String testString = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.';
int from = 6;
int to = 11;

ResultMarkdown formattedText = FormatMarkdown.convertToMarkdown(
MarkdownType.spoiler,
testString,
from,
to,
);

expect(formattedText.data, 'Lorem \n::: spoiler Spoiler\nipsum\n:::\n dolor sit amet consectetur adipiscing elit.');
expect(formattedText.cursorIndex, 26, reason: "\n::: spoiler Spoiler\nipsum = 26");
});

test('successfully converts to spoiler (partial with start index in the middle) (RTL)', () {
String testString = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.';
int from = 11;
int to = 6;

ResultMarkdown formattedText = FormatMarkdown.convertToMarkdown(
MarkdownType.spoiler,
testString,
from,
to,
);

expect(formattedText.data, 'Lorem \n::: spoiler Spoiler\nipsum\n:::\n dolor sit amet consectetur adipiscing elit.');
expect(formattedText.cursorIndex, 26, reason: "\n::: spoiler Spoiler\nipsum = 26");
});

test('successfully converts to spoiler (partial with end index at the end) (LTR)', () {
String testString = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.';
int from = 6;
int to = 55;

ResultMarkdown formattedText = FormatMarkdown.convertToMarkdown(
MarkdownType.spoiler,
testString,
from,
to,
);

expect(formattedText.data, 'Lorem \n::: spoiler Spoiler\nipsum dolor sit amet consectetur adipiscing elit.\n:::');
expect(formattedText.cursorIndex, 70, reason: "\n::: spoiler Spoiler\nipsum dolor sit amet consectetur adipiscing elit. = 70");
});

test('successfully converts to spoiler (partial with end index at the end) (RTL)', () {
String testString = 'Lorem ipsum dolor sit amet consectetur adipiscing elit.';
int from = 55;
int to = 6;

ResultMarkdown formattedText = FormatMarkdown.convertToMarkdown(
MarkdownType.spoiler,
testString,
from,
to,
);

expect(formattedText.data, 'Lorem \n::: spoiler Spoiler\nipsum dolor sit amet consectetur adipiscing elit.\n:::');
expect(formattedText.cursorIndex, 70, reason: "\n::: spoiler Spoiler\nipsum dolor sit amet consectetur adipiscing elit. = 70");
});

// TODO: Add tests for username, community
}
Loading

0 comments on commit a59db6b

Please sign in to comment.