fix: bulleted list format error

This commit is contained in:
Lucas.Xu 2023-04-26 23:06:16 +08:00
commit 8bc0b8be99
7 changed files with 78 additions and 21 deletions

View file

@ -1,10 +1,6 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:appflowy_editor/src/core/document/attributes.dart';
import 'package:appflowy_editor/src/core/document/node.dart';
import 'package:appflowy_editor/src/core/document/path.dart';
import 'package:appflowy_editor/src/core/document/text_delta.dart';
/// [Operation] represents a change to a [Document]. /// [Operation] represents a change to a [Document].
abstract class Operation { abstract class Operation {
Operation( Operation(

View file

@ -71,11 +71,11 @@ Future<bool> _formatSymbolToBulletedList(
}, },
); );
final transaction = editorState.transaction final transaction = editorState.transaction
..deleteNode(node)
..insertNode( ..insertNode(
node.path, node.path,
bulletedListNode, bulletedListNode,
) )
..deleteNode(node)
..afterSelection = afterSelection; ..afterSelection = afterSelection;
await editorState.apply(transaction); await editorState.apply(transaction);
return true; return true;

View file

@ -1,6 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/editor/editor_component/service/renderer/block_component_service.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:appflowy_editor/src/history/undo_manager.dart'; import 'package:appflowy_editor/src/history/undo_manager.dart';
@ -160,6 +159,7 @@ class EditorState {
final completer = Completer<void>(); final completer = Completer<void>();
for (final operation in transaction.operations) { for (final operation in transaction.operations) {
Log.editor.debug('apply op: ${operation.toJson()}');
_applyOperation(operation); _applyOperation(operation);
} }

View file

@ -34,18 +34,6 @@ ShortcutEventHandler tabHandler = (editorState, event) {
end: selection.end.copyWith(path: path), end: selection.end.copyWith(path: path),
); );
// abc
// efg
// abc
// efg
// abc 0
// 1
// abc 0 -> insert([0, 0], efg)
// efg 0,0
final transaction = editorState.transaction final transaction = editorState.transaction
..deleteNode(textNode) ..deleteNode(textNode)
..insertNode(path, textNode) ..insertNode(path, textNode)

View file

@ -1,10 +1,23 @@
import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import '../../util/document_util.dart'; import '../../util/util.dart';
void main() async { void main() async {
group('bulleted_list_shortcut.dart', () { group('bulleted_list_shortcut.dart', () {
setUpAll(() {
if (kDebugMode) {
activateLog();
}
});
tearDownAll(() {
if (kDebugMode) {
deactivateLog();
}
});
group('formatAsteriskToBulletedList', () { group('formatAsteriskToBulletedList', () {
// Before // Before
// *|Welcome to AppFlowy Editor 🔥! // *|Welcome to AppFlowy Editor 🔥!
@ -58,6 +71,40 @@ void main() async {
expect(result, false); expect(result, false);
expect(before.toJson(), after.toJson()); expect(before.toJson(), after.toJson());
}); });
// Before
// Welcome to AppFlowy Editor 🔥!
// *|Welcome to AppFlowy Editor 🔥!
// After
// Welcome to AppFlowy Editor 🔥!
//[bulleted_list] Welcome to AppFlowy Editor 🔥!
test('mock inputting a ` ` in the middle of the text', () async {
const text = 'Welcome to AppFlowy Editor 🔥!';
final document = Document.blank()
.addParagraphs(
1,
builder: (index) => Delta()..insert(text),
)
.addParagraphs(
1,
builder: (index) => Delta()..insert('*$text'),
);
final editorState = EditorState(document: document);
// Welcome to AppFlowy Editor 🔥!
// *|Welcome to AppFlowy Editor 🔥!
final selection = Selection.collapsed(
Position(path: [1], offset: 1),
);
editorState.selection = selection;
final result = await formatAsteriskToBulletedList.execute(editorState);
final after = editorState.getNodeAtPath([1])!;
// the second line will be formatted as the bulleted list style
expect(result, true);
expect(after.type, 'bulleted_list');
expect(after.delta!.toPlainText(), text);
});
}); });
}); });
} }

View file

@ -1,4 +1,5 @@
import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -7,6 +8,18 @@ import '../../../util/util.dart';
// single | means the cursor // single | means the cursor
// double | means the selection // double | means the selection
void main() async { void main() async {
setUpAll(() {
if (kDebugMode) {
activateLog();
}
});
tearDownAll(() {
if (kDebugMode) {
deactivateLog();
}
});
group('backspace_command.dart', () { group('backspace_command.dart', () {
group('backspaceCommand - collapsed selection', () { group('backspaceCommand - collapsed selection', () {
const text = 'Welcome to AppFlowy Editor 🔥!'; const text = 'Welcome to AppFlowy Editor 🔥!';

View file

@ -1,9 +1,22 @@
import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import '../util/document_util.dart'; import '../util/util.dart';
void main() async { void main() async {
setUpAll(() {
if (kDebugMode) {
activateLog();
}
});
tearDownAll(() {
if (kDebugMode) {
deactivateLog();
}
});
group('selection_transform.dart', () { group('selection_transform.dart', () {
group('deleteSelection', () { group('deleteSelection', () {
test('the selection is collapsed', () async { test('the selection is collapsed', () async {