feat: optimize the align toolbar item (#4364)

This commit is contained in:
Lucas.Xu 2024-01-12 10:18:52 +08:00 committed by GitHub
parent cd82c13753
commit 690a3746fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 27 additions and 81 deletions

View File

@ -13,23 +13,27 @@ const _center = 'center';
const _right = 'right';
class AlignItems extends StatelessWidget {
const AlignItems({
AlignItems({
super.key,
required this.editorState,
});
final EditorState editorState;
final List<(String, FlowySvgData)> _alignMenuItems = [
(_left, FlowySvgs.m_aa_align_left_s),
(_center, FlowySvgs.m_aa_align_center_s),
(_right, FlowySvgs.m_aa_align_right_s),
];
@override
Widget build(BuildContext context) {
final currentAlignItem = _getCurrentAlignItem();
final alignMenuItems = _getAlignMenuItems();
final theme = ToolbarColorExtension.of(context);
return PopupMenu(
itemLength: alignMenuItems.length,
itemLength: _alignMenuItems.length,
onSelected: (index) {
editorState.alignBlock(
alignMenuItems[index].$1,
_alignMenuItems[index].$1,
selectionExtraInfo: {
selectionExtraInfoDoNotAttachTextService: true,
selectionExtraInfoDisableFloatingToolbar: true,
@ -37,7 +41,7 @@ class AlignItems extends StatelessWidget {
);
},
menuBuilder: (context, keys, currentIndex) {
final children = alignMenuItems
final children = _alignMenuItems
.mapIndexed(
(index, e) => [
PopupMenuItemWrapper(
@ -45,7 +49,7 @@ class AlignItems extends StatelessWidget {
isSelected: currentIndex == index,
icon: e.$2,
),
if (index != 0 && index != alignMenuItems.length - 1)
if (index != 0 && index != _alignMenuItems.length - 1)
const HSpace(12),
],
)
@ -84,36 +88,12 @@ class AlignItems extends StatelessWidget {
(String, FlowySvgData) _getCurrentAlignItem() {
final align = _getCurrentBlockAlign();
if (align == _center) {
return (_center, FlowySvgs.m_aa_align_center_s);
} else if (align == _right) {
return (_right, FlowySvgs.m_aa_align_right_s);
}
} else if (align == _right) {
return (_left, FlowySvgs.m_aa_align_left_s);
} else {
return (_center, FlowySvgs.m_aa_align_center_s);
}
List<(String, FlowySvgData)> _getAlignMenuItems() {
return [
(_left, FlowySvgs.m_aa_align_left_s),
(_center, FlowySvgs.m_aa_align_center_s),
(_right, FlowySvgs.m_aa_align_right_s),
];
// final align = _getCurrentBlockAlign();
// if (align == _center) {
// return [
// (_left, FlowySvgs.m_aa_align_left_s),
// (_right, FlowySvgs.m_aa_align_right_s),
// ];
// } else if (align == _right) {
// return [
// (_left, FlowySvgs.m_aa_align_left_s),
// (_center, FlowySvgs.m_aa_align_center_s),
// ];
// }
// return [
// (_center, FlowySvgs.m_aa_align_center_s),
// (_right, FlowySvgs.m_aa_align_right_s),
// ];
}
String _getCurrentBlockAlign() {

View File

@ -80,7 +80,7 @@ class _HeadingOrTextItem extends StatelessWidget {
}
Future<void> _convert(bool isSelected) async {
editorState.convertBlockType(
await editorState.convertBlockType(
blockType,
isSelected: isSelected,
extraAttributes: level != null
@ -90,6 +90,14 @@ class _HeadingOrTextItem extends StatelessWidget {
: null,
selectionExtraInfo: {
selectionExtraInfoDoNotAttachTextService: true,
selectionExtraInfoDisableFloatingToolbar: true,
},
);
await editorState.updateSelectionWithReason(
editorState.selection,
extraInfo: {
selectionExtraInfoDisableFloatingToolbar: true,
selectionExtraInfoDoNotAttachTextService: true,
},
);
}

View File

@ -1,23 +0,0 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
class CodeBlockNodeParser extends NodeParser {
const CodeBlockNodeParser();
@override
String get id => CodeBlockKeys.type;
@override
String transform(Node node, DocumentMarkdownEncoder? encoder) {
final delta = node.delta;
final language = node.attributes[CodeBlockKeys.language] ?? '';
if (delta == null) {
throw Exception('Delta is null');
}
final markdown = DeltaMarkdownEncoder().convert(delta);
final result = '```$language\n$markdown\n```';
final suffix = node.next == null ? '' : '\n';
return '$result$suffix';
}
}

View File

@ -1,13 +0,0 @@
import 'package:appflowy_editor/appflowy_editor.dart';
class DividerNodeParser extends NodeParser {
const DividerNodeParser();
@override
String get id => DividerBlockKeys.type;
@override
String transform(Node node, DocumentMarkdownEncoder? encoder) {
return '---\n';
}
}

View File

@ -1,6 +1,4 @@
export 'callout_node_parser.dart';
export 'code_block_node_parser.dart';
export 'custom_image_node_parser.dart';
export 'divider_node_parser.dart';
export 'math_equation_node_parser.dart';
export 'toggle_list_node_parser.dart';

View File

@ -1,5 +1,3 @@
export 'callout_node_parser.dart';
export 'code_block_node_parser.dart';
export 'divider_node_parser.dart';
export 'math_equation_node_parser.dart';
export 'toggle_list_node_parser.dart';

View File

@ -11,9 +11,7 @@ import 'package:dartz/dartz.dart';
import 'package:easy_localization/easy_localization.dart';
const List<NodeParser> _customParsers = [
DividerNodeParser(),
MathEquationNodeParser(),
CodeBlockNodeParser(),
CalloutNodeParser(),
ToggleListNodeParser(),
CustomImageNodeParser(),

View File

@ -53,11 +53,11 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "49575bc"
resolved-ref: "49575bc58f0332810e8aebbeae752eac77275d54"
ref: "3d373be"
resolved-ref: "3d373be64d28e13558e43d489728a6e0616821a9"
url: "https://github.com/AppFlowy-IO/appflowy-editor.git"
source: git
version: "2.2.0"
version: "2.3.0"
appflowy_editor_plugins:
dependency: "direct main"
description:

View File

@ -164,7 +164,7 @@ dependency_overrides:
appflowy_editor:
git:
url: https://github.com/AppFlowy-IO/appflowy-editor.git
ref: "49575bc"
ref: "3d373be"
file: ^7.0.0