fix: resolve data loss issue during block type conversion (#726)

* fix: resolve data loss issue during block type conversion

* fix: flutter test

* test: add more tests
This commit is contained in:
Lucas.Xu 2024-02-26 08:44:43 +07:00 • committed by GitHub
commit 5e5b908e78
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 162 additions and 52 deletions

View file

@ -21,7 +21,7 @@ void main() async {
(result, before, after, editorState) {
expect(result, true);
expect(after.delta!.toPlainText(), text);
expect(after.type, 'bulleted_list');
expect(after.type, BulletedListBlockKeys.type);
},
text: text,
);

View file

@ -116,5 +116,32 @@ void main() async {
expect(after.type, 'heading');
expect(after.delta!.toPlainText(), text);
});
test('convert bulleted_list to heading', () async {
const syntax = '#';
const text = 'Welcome to AppFlowy Editor 🔥!';
testFormatCharacterShortcut(
formatSignToHeading,
syntax,
syntax.length,
(result, before, after, editorState) {
expect(result, true);
expect(after.delta!.toPlainText(), text);
expect(after.type, HeadingBlockKeys.type);
expect(after.attributes[HeadingBlockKeys.level], 1);
expect(after.children.isEmpty, true);
expect(after.next!.delta!.toPlainText(), '1 $text');
expect(after.next!.next!.delta!.toPlainText(), '2 $text');
},
text: text,
node: bulletedListNode(
text: '$syntax$text',
children: [
bulletedListNode(text: '1 $text'),
bulletedListNode(text: '2 $text'),
],
),
);
});
});
}

View file

@ -172,5 +172,31 @@ void main() async {
expect(after.type, ParagraphBlockKeys.type);
expect(after.delta!.toPlainText(), '3.');
});
test('convert todo_list to bulleted_list', () async {
const syntax = '1.';
const text = 'Welcome to AppFlowy Editor 🔥!';
testFormatCharacterShortcut(
formatNumberToNumberedList,
syntax,
syntax.length,
(result, before, after, editorState) {
expect(result, true);
expect(after.delta!.toPlainText(), text);
expect(after.type, NumberedListBlockKeys.type);
expect(after.children[0].delta!.toPlainText(), '1 $text');
expect(after.children[1].delta!.toPlainText(), '2 $text');
},
text: text,
node: todoListNode(
text: '$syntax$text',
checked: false,
children: [
todoListNode(text: '1 $text', checked: false),
todoListNode(text: '2 $text', checked: false),
],
),
);
});
});
}

View file

@ -10,10 +10,16 @@ Future<void> testFormatCharacterShortcut(
void Function(bool result, Node before, Node after, EditorState editorState)
test, {
String text = 'Welcome to AppFlowy Editor 🔥!',
Node? node,
}) async {
final document = Document.blank().addParagraph(
builder: (index) => Delta()..insert('$prefix$text'),
);
final document = Document.blank();
if (node != null) {
document.insert([0], [node]);
} else {
document.addParagraph(
builder: (index) => Delta()..insert('$prefix$text'),
);
}
final editorState = EditorState(document: document);
final selection = Selection.collapsed(

View file

@ -86,6 +86,32 @@ void main() async {
text: text,
);
});
test('convert numbered_list to todo_list', () async {
const syntax = '-[x]';
const text = 'Welcome to AppFlowy Editor 🔥!';
testFormatCharacterShortcut(
formatHyphenFilledBracketsToCheckedBox,
syntax,
syntax.length,
(result, before, after, editorState) {
expect(result, true);
expect(after.delta!.toPlainText(), text);
expect(after.type, TodoListBlockKeys.type);
expect(after.attributes[TodoListBlockKeys.checked], true);
expect(after.children[0].delta!.toPlainText(), '1 $text');
expect(after.children[1].delta!.toPlainText(), '2 $text');
},
text: text,
node: bulletedListNode(
text: '$syntax$text',
children: [
bulletedListNode(text: '1 $text'),
bulletedListNode(text: '2 $text'),
],
),
);
});
},
);
}