chore: migrate to the latest api

This commit is contained in:
Lucas.Xu 2023-08-17 14:50:48 +08:00
parent e226eaad45
commit de4f47b2a1
9 changed files with 71 additions and 45 deletions

View File

@ -93,9 +93,8 @@ void main() {
// Press the enter key
await tester.editor.updateSelection(
Selection.collapse(
[0],
'Hello '.length,
Selection.collapsed(
Position(path: [0], offset: 'Hello '.length),
),
);
await tester.ime.insertCharacter('\n');
@ -129,9 +128,8 @@ void main() {
// Press the enter key
await tester.editor.updateSelection(
Selection.collapse(
[0],
'Hello '.length,
Selection.collapsed(
Position(path: [0], offset: 'Hello '.length),
),
);
await tester.ime.insertCharacter('\n');
@ -170,9 +168,8 @@ void main() {
await tester.tapButton(toggleListIcon);
await tester.editor.updateSelection(
Selection.collapse(
[0],
0,
Selection.collapsed(
Position(path: [0], offset: 0),
),
);
await tester.ime.insertCharacter('\n');
@ -202,9 +199,8 @@ void main() {
expectToggleListOpened();
await tester.editor.updateSelection(
Selection.collapse(
[0],
0,
Selection.collapsed(
Position(path: [0], offset: 0),
),
);
await tester.simulateKeyEvent(

View File

@ -1,22 +1,30 @@
import 'dart:async';
import 'package:appflowy/plugins/document/application/doc_service.dart';
import 'package:appflowy/plugins/document/application/document_data_pb_extension.dart';
import 'package:appflowy/plugins/document/application/editor_transaction_adapter.dart';
import 'package:appflowy/plugins/trash/application/trash_service.dart';
import 'package:appflowy/user/application/user_service.dart';
import 'package:appflowy/util/json_print.dart';
import 'package:appflowy/workspace/application/view/view_listener.dart';
import 'package:appflowy/workspace/application/doc/doc_listener.dart';
import 'package:appflowy/plugins/document/application/doc_service.dart';
import 'package:appflowy/workspace/application/view/view_listener.dart';
import 'package:appflowy_backend/protobuf/flowy-document2/protobuf.dart';
import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pbserver.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
show EditorState, LogLevel, TransactionTime, Selection, paragraphNode;
import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
import 'package:appflowy_backend/protobuf/flowy-user/user_profile.pbserver.dart';
import 'package:appflowy_editor/appflowy_editor.dart'
show
EditorState,
LogLevel,
TransactionTime,
Selection,
Position,
paragraphNode;
import 'package:dartz/dartz.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:dartz/dartz.dart';
import 'dart:async';
part 'doc_bloc.freezed.dart';
class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
@ -198,7 +206,9 @@ class DocumentBloc extends Bloc<DocumentEvent, DocumentState> {
if (document.root.children.isEmpty) {
final transaction = editorState.transaction;
transaction.insertNode([0], paragraphNode());
transaction.afterSelection = Selection.collapse([0], 0);
transaction.afterSelection = Selection.collapsed(
Position(path: [0], offset: 0),
);
await editorState.apply(transaction);
}
}

View File

@ -386,14 +386,24 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
(bool, Selection?) _computeAutoFocusParameters() {
if (widget.editorState.document.isEmpty) {
return (true, Selection.collapse([0], 0));
return (
true,
Selection.collapsed(
Position(path: [0], offset: 0),
),
);
}
final nodes = widget.editorState.document.root.children
.where((element) => element.delta != null);
final isAllEmpty =
nodes.isNotEmpty && nodes.every((element) => element.delta!.isEmpty);
if (isAllEmpty) {
return (true, Selection.collapse(nodes.first.path, 0));
return (
true,
Selection.collapsed(
Position(path: nodes.first.path, offset: 0),
)
);
}
return const (false, null);
}

View File

@ -58,9 +58,13 @@ class BlockAddButton extends StatelessWidget {
final path = isAltPressed ? node.path : node.path.next;
transaction.insertNode(path, paragraphNode());
transaction.afterSelection = Selection.collapse(path, 0);
transaction.afterSelection = Selection.collapsed(
Position(path: path, offset: 0),
);
} else {
transaction.afterSelection = Selection.collapse(node.path, 0);
transaction.afterSelection = Selection.collapsed(
Position(path: node.path, offset: 0),
);
}
// show the slash menu.

View File

@ -231,9 +231,8 @@ class _CalloutBlockComponentWidgetState
..updateNode(node, {
CalloutBlockKeys.icon: emoji,
})
..afterSelection = Selection.collapse(
node.path,
node.delta?.length ?? 0,
..afterSelection = Selection.collapsed(
Position(path: node.path, offset: node.delta?.length ?? 0),
);
await editorState.apply(transaction);
}

View File

@ -286,9 +286,8 @@ class _CodeBlockComponentWidgetState extends State<CodeBlockComponentWidget>
..updateNode(node, {
CodeBlockKeys.language: language == 'auto' ? null : language,
})
..afterSelection = Selection.collapse(
node.path,
node.delta?.length ?? 0,
..afterSelection = Selection.collapsed(
Position(path: node.path, offset: node.delta?.length ?? 0),
);
await editorState.apply(transaction);
}

View File

@ -161,9 +161,8 @@ CommandShortcutEventHandler _insertNewParagraphNextToCodeBlockCommandHandler =
},
),
)
..afterSelection = Selection.collapse(
selection.end.path.next,
0,
..afterSelection = Selection.collapsed(
Position(path: selection.end.path.next, offset: 0),
);
editorState.apply(transaction);
return KeyEventResult.handled;
@ -192,9 +191,11 @@ CommandShortcutEventHandler _tabToInsertSpacesInCodeBlockCommandHandler =
index,
spaces, // two spaces
)
..afterSelection = Selection.collapse(
selection.end.path,
selection.endIndex + spaces.length,
..afterSelection = Selection.collapsed(
Position(
path: selection.end.path,
offset: selection.endIndex + spaces.length,
),
);
editorState.apply(transaction);
break;
@ -228,9 +229,11 @@ CommandShortcutEventHandler _tabToDeleteSpacesInCodeBlockCommandHandler =
index,
spaces.length, // two spaces
)
..afterSelection = Selection.collapse(
selection.end.path,
selection.endIndex - spaces.length,
..afterSelection = Selection.collapsed(
Position(
path: selection.end.path,
offset: selection.endIndex - spaces.length,
),
);
editorState.apply(transaction);
}

View File

@ -183,9 +183,8 @@ class OutlineItemWidget extends StatelessWidget {
void updateBlockSelection(BuildContext context) async {
final editorState = context.read<EditorState>();
editorState.selectionType = SelectionType.block;
editorState.selection = Selection.collapse(
node.path,
node.delta?.length ?? 0,
editorState.selection = Selection.collapsed(
Position(path: node.path, offset: node.delta?.length ?? 0),
);
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
editorState.selectionType = null;

View File

@ -57,7 +57,9 @@ CharacterShortcutEvent insertChildNodeInsideToggleList = CharacterShortcutEvent(
paragraphNode(),
)
..deleteNode(node)
..afterSelection = Selection.collapse(selection.start.path, 0);
..afterSelection = Selection.collapsed(
Position(path: selection.start.path, offset: 0),
);
} else {
// insert a toggle list block below the current toggle list block
transaction
@ -66,7 +68,9 @@ CharacterShortcutEvent insertChildNodeInsideToggleList = CharacterShortcutEvent(
selection.start.path.next,
toggleListBlockNode(collapsed: true, delta: slicedDelta),
)
..afterSelection = Selection.collapse(selection.start.path.next, 0);
..afterSelection = Selection.collapsed(
Position(path: selection.start.path.next, offset: 0),
);
}
} else {
// insert a paragraph block inside the current toggle list block
@ -76,7 +80,9 @@ CharacterShortcutEvent insertChildNodeInsideToggleList = CharacterShortcutEvent(
selection.start.path + [0],
paragraphNode(delta: slicedDelta),
)
..afterSelection = Selection.collapse(selection.start.path + [0], 0);
..afterSelection = Selection.collapsed(
Position(path: selection.start.path + [0], offset: 0),
);
}
await editorState.apply(transaction);
return true;