feat: insert date from slash menu (#3783)

This commit is contained in:
Mathias Mogensen 2023-10-25 19:31:21 +02:00 committed by GitHub
parent 147637fcf5
commit e66087861b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 0 deletions

View File

@ -2,6 +2,7 @@ import 'package:appflowy/plugins/document/application/doc_bloc.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/background_color/theme_background_color.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/i18n/editor_i18n.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/image/custom_image_block_component.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/slash_menu_items.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy/plugins/document/presentation/editor_style.dart';
import 'package:appflowy/plugins/inline_actions/handlers/date_reference.dart';
@ -517,6 +518,7 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
toggleListBlockItem,
emojiMenuItem,
autoGeneratorMenuItem,
dateMenuItem,
];
}

View File

@ -0,0 +1,45 @@
import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_block.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
SelectionMenuItem dateMenuItem = SelectionMenuItem(
name: 'Insert Date',
icon: (_, isSelected, style) => FlowySvg(
FlowySvgs.date_s,
color: isSelected
? style.selectionMenuItemSelectedIconColor
: style.selectionMenuItemIconColor,
),
keywords: ['insert date', 'date', 'time'],
handler: (editorState, menuService, context) =>
_insertDateReference(editorState),
);
Future<void> _insertDateReference(EditorState editorState) async {
final selection = editorState.selection;
if (selection == null || !selection.isCollapsed) {
return;
}
final node = editorState.getNodeAtPath(selection.end.path);
final delta = node?.delta;
if (node == null || delta == null) {
return;
}
final transaction = editorState.transaction
..replaceText(
node,
selection.start.offset,
0,
'\$',
attributes: {
MentionBlockKeys.mention: {
MentionBlockKeys.type: MentionType.date.name,
MentionBlockKeys.date: DateTime.now().toIso8601String(),
}
},
);
await editorState.apply(transaction);
}