Skip to content

Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.

fix(flutter): implement mention date transaction handler (#6933)

Richard Shiuef5e46967ec28
diff --git a/frontend/appflowy_flutter/integration_test/desktop/document/document_with_date_reminder_test.dart b/frontend/appflowy_flutter/integration_test/desktop/document/document_with_date_reminder_test.dart
index c7fea448f7..ccfdbae76e 100644
--- a/frontend/appflowy_flutter/integration_test/desktop/document/document_with_date_reminder_test.dart
+++ b/frontend/appflowy_flutter/integration_test/desktop/document/document_with_date_reminder_test.dart
@@ -1,14 +1,21 @@
+import 'dart:io';
+
import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_date_block.dart';
+import 'package:appflowy/startup/startup.dart';
+import 'package:appflowy/user/application/reminder/reminder_bloc.dart';
import 'package:appflowy/workspace/application/settings/date_time/date_format_ext.dart';
import 'package:appflowy/workspace/presentation/widgets/date_picker/desktop_date_picker.dart';
import 'package:appflowy/workspace/presentation/widgets/toggle/toggle.dart';
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
+import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
+import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
+import 'package:table_calendar/table_calendar.dart';
import '../../shared/util.dart';
@@ -18,7 +25,7 @@ void main() {
TestWidgetsFlutterBinding.ensureInitialized();
});
- group('date or reminder block in document', () {
+ group('date or reminder block in document:', () {
testWidgets("insert date with time block", (tester) async {
await tester.initializeAppFlowy();
await tester.tapAnonymousSignInButton();
@@ -121,5 +128,339 @@ void main() {
expect(find.text('@$formattedDate'), findsOneWidget);
expect(find.byFlowySvg(FlowySvgs.reminder_clock_s), findsOneWidget);
});
+
+ testWidgets("copy, cut and paste a date mention", (tester) async {
+ await tester.initializeAppFlowy();
+ await tester.tapAnonymousSignInButton();
+
+ // create a new document
+ await tester.createNewPageWithNameUnderParent(
+ name: 'copy, cut and paste a date mention',
+ );
+
+ // tap the first line of the document
+ await tester.editor.tapLineOfEditorAt(0);
+ await tester.editor.showSlashMenu();
+ await tester.editor.tapSlashMenuItemWithName(
+ LocaleKeys.document_slashMenu_name_dateOrReminder.tr(),
+ );
+
+ final dateTimeSettings = DateTimeSettingsPB(
+ dateFormat: UserDateFormatPB.Friendly,
+ timeFormat: UserTimeFormatPB.TwentyFourHour,
+ );
+ final DateTime currentDateTime = DateTime.now();
+ final String formattedDate =
+ dateTimeSettings.dateFormat.formatDate(currentDateTime, false);
+
+ // get current date in editor
+ expect(find.byType(MentionDateBlock), findsOneWidget);
+ expect(find.text('@$formattedDate'), findsOneWidget);
+
+ // update selection and copy
+ await tester.editor.updateSelection(
+ Selection(
+ start: Position(path: [0]),
+ end: Position(path: [0], offset: 1),
+ ),
+ );
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyC,
+ isControlPressed: Platform.isLinux || Platform.isWindows,
+ isMetaPressed: Platform.isMacOS,
+ );
+ await tester.pumpAndSettle();
+
+ // update selection and paste
+ await tester.editor.updateSelection(
+ Selection.collapsed(Position(path: [0], offset: 1)),
+ );
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyV,
+ isControlPressed: Platform.isLinux || Platform.isWindows,
+ isMetaPressed: Platform.isMacOS,
+ );
+ await tester.pumpAndSettle();
+
+ expect(find.byType(MentionDateBlock), findsNWidgets(2));
+ expect(find.text('@$formattedDate'), findsNWidgets(2));
+
+ // update selection and cut
+ await tester.editor.updateSelection(
+ Selection(
+ start: Position(path: [0], offset: 1),
+ end: Position(path: [0], offset: 2),
+ ),
+ );
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyX,
+ isControlPressed: Platform.isLinux || Platform.isWindows,
+ isMetaPressed: Platform.isMacOS,
+ );
+ await tester.pumpAndSettle();
+
+ expect(find.byType(MentionDateBlock), findsOneWidget);
+ expect(find.text('@$formattedDate'), findsOneWidget);
+
+ // update selection and paste
+ await tester.editor.updateSelection(
+ Selection.collapsed(Position(path: [0], offset: 1)),
+ );
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyV,
+ isControlPressed: Platform.isLinux || Platform.isWindows,
+ isMetaPressed: Platform.isMacOS,
+ );
+ await tester.pumpAndSettle();
+
+ expect(find.byType(MentionDateBlock), findsNWidgets(2));
+ expect(find.text('@$formattedDate'), findsNWidgets(2));
+ });
+
+ testWidgets("copy, cut and paste a reminder mention", (tester) async {
+ await tester.initializeAppFlowy();
+ await tester.tapAnonymousSignInButton();
+
+ // create a new document
+ await tester.createNewPageWithNameUnderParent(
+ name: 'copy, cut and paste a reminder mention',
+ );
+
+ // tap the first line of the document
+ await tester.editor.tapLineOfEditorAt(0);
+ await tester.editor.showSlashMenu();
+ await tester.editor.tapSlashMenuItemWithName(
+ LocaleKeys.document_slashMenu_name_dateOrReminder.tr(),
+ );
+
+ // trigger popup
+ await tester.tapButton(find.byType(MentionDateBlock));
+ await tester.pumpAndSettle();
+
+ // set date to be fifteenth of the next month
+ await tester.tap(
+ find.descendant(
+ of: find.byType(DesktopAppFlowyDatePicker),
+ matching: find.byFlowySvg(FlowySvgs.arrow_right_s),
+ ),
+ );
+ await tester.pumpAndSettle();
+ await tester.tap(
+ find.descendant(
+ of: find.byType(TableCalendar),
+ matching: find.text(15.toString()),
+ ),
+ );
+ await tester.pumpAndSettle();
+ await tester.simulateKeyEvent(LogicalKeyboardKey.escape);
+ await tester.pumpAndSettle();
+
+ // add a reminder
+ await tester.tap(find.byType(MentionDateBlock));
+ await tester.pumpAndSettle();
+ await tester.tap(find.text(LocaleKeys.datePicker_reminderLabel.tr()));
+ await tester.pumpAndSettle();
+ await tester.tap(
+ find.textContaining(
+ LocaleKeys.datePicker_reminderOptions_oneDayBefore.tr(),
+ ),
+ );
+ await tester.pumpAndSettle();
+ await tester.simulateKeyEvent(LogicalKeyboardKey.escape);
+ await tester.pumpAndSettle();
+
+ // verify
+ final dateTimeSettings = DateTimeSettingsPB(
+ dateFormat: UserDateFormatPB.Friendly,
+ timeFormat: UserTimeFormatPB.TwentyFourHour,
+ );
+ final now = DateTime.now();
+ final fifteenthOfNextMonth = DateTime(now.year, now.month + 1, 15);
+ final formattedDate =
+ dateTimeSettings.dateFormat.formatDate(fifteenthOfNextMonth, false);
+
+ expect(find.byType(MentionDateBlock), findsOneWidget);
+ expect(find.text('@$formattedDate'), findsOneWidget);
+ expect(find.byFlowySvg(FlowySvgs.reminder_clock_s), findsOneWidget);
+ expect(getIt<ReminderBloc>().state.reminders.map((e) => e.id).length, 1);
+
+ // update selection and copy
+ await tester.editor.updateSelection(
+ Selection(
+ start: Position(path: [0]),
+ end: Position(path: [0], offset: 1),
+ ),
+ );
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyC,
+ isControlPressed: Platform.isLinux || Platform.isWindows,
+ isMetaPressed: Platform.isMacOS,
+ );
+ await tester.pumpAndSettle();
+
+ // update selection and paste
+ await tester.editor.updateSelection(
+ Selection.collapsed(Position(path: [0], offset: 1)),
+ );
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyV,
+ isControlPressed: Platform.isLinux || Platform.isWindows,
+ isMetaPressed: Platform.isMacOS,
+ );
+ await tester.pumpAndSettle();
+
+ expect(find.byType(MentionDateBlock), findsNWidgets(2));
+ expect(find.text('@$formattedDate'), findsNWidgets(2));
+ expect(find.byFlowySvg(FlowySvgs.reminder_clock_s), findsNWidgets(2));
+ expect(
+ getIt<ReminderBloc>().state.reminders.map((e) => e.id).toSet().length,
+ 2,
+ );
+
+ // update selection and cut
+ await tester.editor.updateSelection(
+ Selection(
+ start: Position(path: [0], offset: 1),
+ end: Position(path: [0], offset: 2),
+ ),
+ );
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyX,
+ isControlPressed: Platform.isLinux || Platform.isWindows,
+ isMetaPressed: Platform.isMacOS,
+ );
+ await tester.pumpAndSettle();
+
+ expect(find.byType(MentionDateBlock), findsOneWidget);
+ expect(find.text('@$formattedDate'), findsOneWidget);
+ expect(find.byFlowySvg(FlowySvgs.reminder_clock_s), findsOneWidget);
+ expect(getIt<ReminderBloc>().state.reminders.map((e) => e.id).length, 1);
+
+ // update selection and paste
+ await tester.editor.updateSelection(
+ Selection.collapsed(Position(path: [0], offset: 1)),
+ );
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyV,
+ isControlPressed: Platform.isLinux || Platform.isWindows,
+ isMetaPressed: Platform.isMacOS,
+ );
+ await tester.pumpAndSettle();
+
+ expect(find.byType(MentionDateBlock), findsNWidgets(2));
+ expect(find.text('@$formattedDate'), findsNWidgets(2));
+ expect(find.byType(MentionDateBlock), findsNWidgets(2));
+ expect(find.text('@$formattedDate'), findsNWidgets(2));
+ expect(find.byFlowySvg(FlowySvgs.reminder_clock_s), findsNWidgets(2));
+ expect(
+ getIt<ReminderBloc>().state.reminders.map((e) => e.id).toSet().length,
+ 2,
+ );
+ });
+
+ testWidgets("delete, undo and redo a reminder mention", (tester) async {
+ await tester.initializeAppFlowy();
+ await tester.tapAnonymousSignInButton();
+
+ // create a new document
+ await tester.createNewPageWithNameUnderParent(
+ name: 'delete, undo and redo a reminder mention',
+ );
+
+ // tap the first line of the document
+ await tester.editor.tapLineOfEditorAt(0);
+ await tester.editor.showSlashMenu();
+ await tester.editor.tapSlashMenuItemWithName(
+ LocaleKeys.document_slashMenu_name_dateOrReminder.tr(),
+ );
+
+ // trigger popup
+ await tester.tapButton(find.byType(MentionDateBlock));
+ await tester.pumpAndSettle();
+
+ // set date to be fifteenth of the next month
+ await tester.tap(
+ find.descendant(
+ of: find.byType(DesktopAppFlowyDatePicker),
+ matching: find.byFlowySvg(FlowySvgs.arrow_right_s),
+ ),
+ );
+ await tester.pumpAndSettle();
+ await tester.tap(
+ find.descendant(
+ of: find.byType(TableCalendar),
+ matching: find.text(15.toString()),
+ ),
+ );
+ await tester.pumpAndSettle();
+ await tester.simulateKeyEvent(LogicalKeyboardKey.escape);
+ await tester.pumpAndSettle();
+
+ // add a reminder
+ await tester.tap(find.byType(MentionDateBlock));
+ await tester.pumpAndSettle();
+ await tester.tap(find.text(LocaleKeys.datePicker_reminderLabel.tr()));
+ await tester.pumpAndSettle();
+ await tester.tap(
+ find.textContaining(
+ LocaleKeys.datePicker_reminderOptions_oneDayBefore.tr(),
+ ),
+ );
+ await tester.pumpAndSettle();
+ await tester.simulateKeyEvent(LogicalKeyboardKey.escape);
+ await tester.pumpAndSettle();
+
+ // verify
+ final dateTimeSettings = DateTimeSettingsPB(
+ dateFormat: UserDateFormatPB.Friendly,
+ timeFormat: UserTimeFormatPB.TwentyFourHour,
+ );
+ final now = DateTime.now();
+ final fifteenthOfNextMonth = DateTime(now.year, now.month + 1, 15);
+ final formattedDate =
+ dateTimeSettings.dateFormat.formatDate(fifteenthOfNextMonth, false);
+
+ expect(find.byType(MentionDateBlock), findsOneWidget);
+ expect(find.text('@$formattedDate'), findsOneWidget);
+ expect(find.byFlowySvg(FlowySvgs.reminder_clock_s), findsOneWidget);
+ expect(getIt<ReminderBloc>().state.reminders.map((e) => e.id).length, 1);
+
+ // update selection and backspace to delete the mention
+ await tester.editor.updateSelection(
+ Selection.collapsed(Position(path: [0], offset: 1)),
+ );
+ await tester.simulateKeyEvent(LogicalKeyboardKey.backspace);
+ await tester.pumpAndSettle();
+
+ expect(find.byType(MentionDateBlock), findsNothing);
+ expect(find.text('@$formattedDate'), findsNothing);
+ expect(find.byFlowySvg(FlowySvgs.reminder_clock_s), findsNothing);
+ expect(getIt<ReminderBloc>().state.reminders.isEmpty, isTrue);
+
+ // undo
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyZ,
+ isControlPressed: Platform.isWindows || Platform.isLinux,
+ isMetaPressed: Platform.isMacOS,
+ );
+
+ expect(find.byType(MentionDateBlock), findsOneWidget);
+ expect(find.text('@$formattedDate'), findsOneWidget);
+ expect(find.byFlowySvg(FlowySvgs.reminder_clock_s), findsOneWidget);
+ expect(getIt<ReminderBloc>().state.reminders.map((e) => e.id).length, 1);
+
+ // redo
+ await tester.simulateKeyEvent(
+ LogicalKeyboardKey.keyZ,
+ isControlPressed: Platform.isWindows || Platform.isLinux,
+ isMetaPressed: Platform.isMacOS,
+ isShiftPressed: true,
+ );
+
+ expect(find.byType(MentionDateBlock), findsNothing);
+ expect(find.text('@$formattedDate'), findsNothing);
+ expect(find.byFlowySvg(FlowySvgs.reminder_clock_s), findsNothing);
+ expect(getIt<ReminderBloc>().state.reminders.isEmpty, isTrue);
+ });
});
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/child_page_transaction_handler.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/child_page_transaction_handler.dart
index 491bf10fa5..ef32ad1098 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/child_page_transaction_handler.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/child_page_transaction_handler.dart
@@ -16,11 +16,12 @@ import '../transaction_handler/mention_transaction_handler.dart';
const _pasteIdentifier = 'child_page_transaction';
class ChildPageTransactionHandler extends MentionTransactionHandler {
- ChildPageTransactionHandler() : super(subType: MentionType.childPage.name);
+ ChildPageTransactionHandler();
@override
Future<void> onTransaction(
BuildContext context,
+ String viewId,
EditorState editorState,
List<MentionBlockData> added,
List<MentionBlockData> removed, {
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/date_transaction_handler.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/date_transaction_handler.dart
new file mode 100644
index 0000000000..972ed229dd
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/date_transaction_handler.dart
@@ -0,0 +1,263 @@
+import 'package:appflowy/shared/clipboard_state.dart';
+import 'package:appflowy/startup/startup.dart';
+import 'package:appflowy/user/application/reminder/reminder_bloc.dart';
+import 'package:appflowy/user/application/reminder/reminder_extension.dart';
+import 'package:appflowy/workspace/presentation/widgets/date_picker/widgets/reminder_selector.dart';
+import 'package:appflowy_backend/log.dart';
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:collection/collection.dart';
+import 'package:fixnum/fixnum.dart';
+import 'package:flutter/material.dart';
+import 'package:nanoid/nanoid.dart';
+import 'package:provider/provider.dart';
+
+import '../plugins.dart';
+import '../transaction_handler/mention_transaction_handler.dart';
+
+const _pasteIdentifier = 'date_transaction';
+
+class DateTransactionHandler extends MentionTransactionHandler {
+ DateTransactionHandler();
+
+ @override
+ Future<void> onTransaction(
+ BuildContext context,
+ String viewId,
+ EditorState editorState,
+ List<MentionBlockData> added,
+ List<MentionBlockData> removed, {
+ bool isCut = false,
+ bool isUndoRedo = false,
+ bool isPaste = false,
+ bool isDraggingNode = false,
+ bool isTurnInto = false,
+ String? parentViewId,
+ }) async {
+ if (isDraggingNode || isTurnInto) {
+ return;
+ }
+
+ // Remove the mentions that were both added and removed in the same transaction.
+ // These were just moved around.
+ final moved = <MentionBlockData>[];
+ for (final mention in added) {
+ if (removed.any((r) => r.$2 == mention.$2)) {
+ moved.add(mention);
+ }
+ }
+
+ for (final mention in removed) {
+ if (!context.mounted || moved.any((m) => m.$2 == mention.$2)) {
+ return;
+ }
+
+ if (mention.$2[MentionBlockKeys.type] != MentionType.date.name) {
+ continue;
+ }
+
+ _handleDeletion(context, mention);
+ }
+
+ if (isPaste || isUndoRedo) {
+ if (context.mounted) {
+ context.read<ClipboardState>().startHandlingPaste(_pasteIdentifier);
+ }
+
+ for (final mention in added) {
+ if (!context.mounted || moved.any((m) => m.$2 == mention.$2)) {
+ return;
+ }
+
+ if (mention.$2[MentionBlockKeys.type] != MentionType.date.name) {
+ continue;
+ }
+
+ _handleAddition(
+ context,
+ viewId,
+ editorState,
+ mention,
+ isPaste,
+ isCut,
+ );
+ }
+
+ if (context.mounted) {
+ context.read<ClipboardState>().endHandlingPaste(_pasteIdentifier);
+ }
+ }
+ }
+
+ void _handleDeletion(
+ BuildContext context,
+ MentionBlockData data,
+ ) {
+ final reminderId = data.$2[MentionBlockKeys.reminderId];
+
+ if (reminderId case String _ when reminderId.isNotEmpty) {
+ getIt<ReminderBloc>().add(ReminderEvent.remove(reminderId: reminderId));
+ }
+ }
+
+ void _handleAddition(
+ BuildContext context,
+ String viewId,
+ EditorState editorState,
+ MentionBlockData data,
+ bool isPaste,
+ bool isCut,
+ ) {
+ final dateData = _MentionDateBlockData.fromData(data.$2);
+ if (dateData.dateString.isEmpty) {
+ Log.error("mention date block doesn't have a valid date string");
+ return;
+ }
+
+ if (isPaste && !isCut) {
+ _handlePasteFromCopy(
+ context,
+ viewId,
+ editorState,
+ data.$1,
+ data.$3,
+ dateData,
+ );
+ } else {
+ _handlePasteFromCut(viewId, data.$1, dateData);
+ }
+ }
+
+ void _handlePasteFromCut(
+ String viewId,
+ Node node,
+ _MentionDateBlockData data,
+ ) {
+ final dateTime = DateTime.tryParse(data.dateString);
+
+ if (data.reminderId == null || dateTime == null) {
+ return;
+ }
+
+ getIt<ReminderBloc>().add(
+ ReminderEvent.addById(
+ reminderId: data.reminderId!,
+ objectId: viewId,
+ scheduledAt: Int64(
+ data.reminderOption
+ .getNotificationDateTime(dateTime)
+ .millisecondsSinceEpoch ~/
+ 1000,
+ ),
+ meta: {
+ ReminderMetaKeys.includeTime: data.includeTime.toString(),
+ ReminderMetaKeys.blockId: node.id,
+ },
+ ),
+ );
+ }
+
+ void _handlePasteFromCopy(
+ BuildContext context,
+ String viewId,
+ EditorState editorState,
+ Node node,
+ int index,
+ _MentionDateBlockData data,
+ ) async {
+ final dateTime = DateTime.tryParse(data.dateString);
+
+ if (node.delta == null) {
+ return;
+ }
+
+ if (data.reminderId == null || dateTime == null) {
+ return;
+ }
+
+ final reminderId = nanoid();
+ getIt<ReminderBloc>().add(
+ ReminderEvent.addById(
+ reminderId: reminderId,
+ objectId: viewId,
+ scheduledAt: Int64(
+ data.reminderOption
+ .getNotificationDateTime(dateTime)
+ .millisecondsSinceEpoch ~/
+ 1000,
+ ),
+ meta: {
+ ReminderMetaKeys.includeTime: data.includeTime.toString(),
+ ReminderMetaKeys.blockId: node.id,
+ },
+ ),
+ );
+
+ final newMentionAttributes = {
+ MentionBlockKeys.mention: {
+ MentionBlockKeys.type: MentionType.date.name,
+ MentionBlockKeys.date: dateTime.toIso8601String(),
+ MentionBlockKeys.reminderId: reminderId,
+ MentionBlockKeys.includeTime: data.includeTime,
+ MentionBlockKeys.reminderOption: data.reminderOption.name,
+ },
+ };
+
+ // The index is the index of the delta, to get the index of the mention character
+ // in all the text, we need to calculate it based on the deltas before the current delta.
+ int mentionIndex = 0;
+ for (final (i, delta) in node.delta!.indexed) {
+ if (i >= index) {
+ break;
+ }
+
+ mentionIndex += delta.length;
+ }
+
+ // Required to prevent editing the same spot at the same time
+ await Future.delayed(const Duration(milliseconds: 100));
+
+ final transaction = editorState.transaction
+ ..formatText(
+ node,
+ mentionIndex,
+ MentionBlockKeys.mentionChar.length,
+ newMentionAttributes,
+ );
+
+ await editorState.apply(
+ transaction,
+ options: const ApplyOptions(recordUndo: false),
+ );
+ }
+}
+
+/// A helper class to parse and store the mention date block data
+class _MentionDateBlockData {
+ _MentionDateBlockData.fromData(Map<String, dynamic> data) {
+ dateString = switch (data[MentionBlockKeys.date]) {
+ final String string when DateTime.tryParse(string) != null => string,
+ _ => "",
+ };
+ includeTime = switch (data[MentionBlockKeys.includeTime]) {
+ final bool flag => flag,
+ _ => false,
+ };
+ reminderOption = switch (data[MentionBlockKeys.reminderOption]) {
+ final String name =>
+ ReminderOption.values.firstWhereOrNull((o) => o.name == name) ??
+ ReminderOption.none,
+ _ => ReminderOption.none,
+ };
+ reminderId = switch (data[MentionBlockKeys.reminderId]) {
+ final String id
+ when id.isNotEmpty && reminderOption != ReminderOption.none =>
+ id,
+ _ => null,
+ };
+ }
+
+ late final String dateString;
+ late final bool includeTime;
+ late final String? reminderId;
+ late final ReminderOption reminderOption;
+}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/mention_block.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/mention_block.dart
index cf2f0b8e11..a5bc340f71 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/mention_block.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mention/mention_block.dart
@@ -8,7 +8,6 @@ import 'package:provider/provider.dart';
enum MentionType {
page,
- reminder,
date,
childPage;
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/sub_page/sub_page_transaction_handler.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/sub_page/sub_page_transaction_handler.dart
index b7d784d534..c5c7398bdb 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/sub_page/sub_page_transaction_handler.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/sub_page/sub_page_transaction_handler.dart
@@ -21,6 +21,7 @@ class SubPageTransactionHandler extends BlockTransactionHandler {
@override
Future<void> onTransaction(
BuildContext context,
+ String viewId,
EditorState editorState,
List<Node> added,
List<Node> removed, {
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_handler.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_handler.dart
index 334f649341..243532e8ce 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_handler.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_handler.dart
@@ -25,6 +25,7 @@ abstract class EditorTransactionHandler<T> {
Future<void> onTransaction(
BuildContext context,
+ String viewId,
EditorState editorState,
List<T> added,
List<T> removed, {
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_service.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_service.dart
index 29cc7435ff..b56066ae8b 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_service.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_service.dart
@@ -2,20 +2,23 @@ import 'dart:async';
import 'package:appflowy/plugins/document/presentation/editor_notification.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/child_page_transaction_handler.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/date_transaction_handler.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/sub_page/sub_page_transaction_handler.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/transaction_handler/editor_transaction_handler.dart';
-import 'package:appflowy/plugins/document/presentation/editor_plugins/transaction_handler/mention_transaction_handler.dart';
import 'package:appflowy/shared/clipboard_state.dart';
import 'package:appflowy/shared/feature_flags.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
+import 'mention_transaction_handler.dart';
+
final _transactionHandlers = <EditorTransactionHandler>[
if (FeatureFlag.inlineSubPageMention.isOn) ...[
SubPageTransactionHandler(),
ChildPageTransactionHandler(),
],
+ DateTransactionHandler(),
];
/// Handles delegating transactions to appropriate handlers.
@@ -148,10 +151,16 @@ class _EditorTransactionServiceState extends State<EditorTransactionService> {
handler.type: handler.livesInDelta ? <MentionBlockData>[] : <Node>[],
};
+ // based on the type of the transaction handler
+ final uniqueTransactionHandlers = <String, EditorTransactionHandler>{};
+ for (final handler in _transactionHandlers) {
+ uniqueTransactionHandlers.putIfAbsent(handler.type, () => handler);
+ }
+
for (final op in transaction.operations) {
if (op is InsertOperation) {
for (final n in op.nodes) {
- for (final handler in _transactionHandlers) {
+ for (final handler in uniqueTransactionHandlers.values) {
if (handler.livesInDelta) {
added[handler.type]!
.addAll(extractMentionsForType(n, handler.type));
@@ -163,7 +172,7 @@ class _EditorTransactionServiceState extends State<EditorTransactionService> {
}
} else if (op is DeleteOperation) {
for (final n in op.nodes) {
- for (final handler in _transactionHandlers) {
+ for (final handler in uniqueTransactionHandlers.values) {
if (handler.livesInDelta) {
removed[handler.type]!.addAll(
extractMentionsForType(n, handler.type, false),
@@ -191,8 +200,9 @@ class _EditorTransactionServiceState extends State<EditorTransactionService> {
final (add, del) = diffDeltas(deltaBefore, deltaAfter);
+ bool fetchedMentions = false;
for (final handler in _transactionHandlers) {
- if (!handler.livesInDelta) {
+ if (!handler.livesInDelta || fetchedMentions) {
continue;
}
@@ -212,6 +222,8 @@ class _EditorTransactionServiceState extends State<EditorTransactionService> {
removed[handler.type]!.addAll(mentionBlockDatas);
}
+
+ fetchedMentions = true;
}
}
}
@@ -226,6 +238,7 @@ class _EditorTransactionServiceState extends State<EditorTransactionService> {
handler.onTransaction(
context,
+ widget.viewId,
widget.editorState,
additions,
removals,
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/mention_transaction_handler.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/mention_transaction_handler.dart
index 632561589e..d08ce05510 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/mention_transaction_handler.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/transaction_handler/mention_transaction_handler.dart
@@ -12,12 +12,6 @@ typedef MentionBlockData = (Node, Map<String, dynamic>, int);
abstract class MentionTransactionHandler
extends EditorTransactionHandler<MentionBlockData> {
- const MentionTransactionHandler({
- required this.subType,
- })
+ const MentionTransactionHandler()
: super(type: MentionBlockKeys.mention, livesInDelta: true);
-
- final String subType;
-
- MentionType get mentionType => MentionType.fromString(subType);
}