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: disable built-in toolbar items for non-built-in widget

Lucas.Xu31ba12d289e8
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_item.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_item.dart
index 02eaddc68d..68bb5023ca 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_item.dart
+++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/render/toolbar/toolbar_item.dart
@@ -8,7 +8,6 @@ import 'package:appflowy_editor/src/service/default_text_operations/format_rich_
import 'package:flutter/material.dart';
import 'package:rich_clipboard/rich_clipboard.dart';
-import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart';
typedef ToolbarItemEventHandler = void Function(
EditorState editorState, BuildContext context);
@@ -120,7 +119,7 @@ List<ToolbarItem> defaultToolbarItems = [
name: 'toolbar/bold',
color: isHighlight ? Colors.lightBlue : null,
),
- validator: _showInTextSelection,
+ validator: _showInBuiltInTextSelection,
highlightCallback: (editorState) => _allSatisfy(
editorState,
BuiltInAttributeKey.bold,
@@ -136,7 +135,7 @@ List<ToolbarItem> defaultToolbarItems = [
name: 'toolbar/italic',
color: isHighlight ? Colors.lightBlue : null,
),
- validator: _showInTextSelection,
+ validator: _showInBuiltInTextSelection,
highlightCallback: (editorState) => _allSatisfy(
editorState,
BuiltInAttributeKey.italic,
@@ -152,7 +151,7 @@ List<ToolbarItem> defaultToolbarItems = [
name: 'toolbar/underline',
color: isHighlight ? Colors.lightBlue : null,
),
- validator: _showInTextSelection,
+ validator: _showInBuiltInTextSelection,
highlightCallback: (editorState) => _allSatisfy(
editorState,
BuiltInAttributeKey.underline,
@@ -168,7 +167,7 @@ List<ToolbarItem> defaultToolbarItems = [
name: 'toolbar/strikethrough',
color: isHighlight ? Colors.lightBlue : null,
),
- validator: _showInTextSelection,
+ validator: _showInBuiltInTextSelection,
highlightCallback: (editorState) => _allSatisfy(
editorState,
BuiltInAttributeKey.strikethrough,
@@ -184,7 +183,7 @@ List<ToolbarItem> defaultToolbarItems = [
name: 'toolbar/code',
color: isHighlight ? Colors.lightBlue : null,
),
- validator: _showInTextSelection,
+ validator: _showInBuiltInTextSelection,
highlightCallback: (editorState) => _allSatisfy(
editorState,
BuiltInAttributeKey.code,
@@ -248,7 +247,7 @@ List<ToolbarItem> defaultToolbarItems = [
name: 'toolbar/highlight',
color: isHighlight ? Colors.lightBlue : null,
),
- validator: _showInTextSelection,
+ validator: _showInBuiltInTextSelection,
highlightCallback: (editorState) => _allSatisfy(
editorState,
BuiltInAttributeKey.backgroundColor,
@@ -262,13 +261,22 @@ List<ToolbarItem> defaultToolbarItems = [
];
ToolbarItemValidator _onlyShowInSingleTextSelection = (editorState) {
+ final result = _showInBuiltInTextSelection(editorState);
+ if (!result) {
+ return false;
+ }
final nodes = editorState.service.selectionService.currentSelectedNodes;
return (nodes.length == 1 && nodes.first is TextNode);
};
-ToolbarItemValidator _showInTextSelection = (editorState) {
+ToolbarItemValidator _showInBuiltInTextSelection = (editorState) {
final nodes = editorState.service.selectionService.currentSelectedNodes
- .whereType<TextNode>();
+ .whereType<TextNode>()
+ .where(
+ (textNode) =>
+ BuiltInAttributeKey.globalStyleKeys.contains(textNode.subtype) ||
+ textNode.subtype == null,
+ );
return nodes.isNotEmpty;
};
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/arrow_keys_handler.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/arrow_keys_handler.dart
index c04dafe986..010cbb5840 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/arrow_keys_handler.dart
+++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/internal_key_event_handlers/arrow_keys_handler.dart
@@ -341,12 +341,12 @@ Position? _goUp(EditorState editorState) {
final rect = rects.reduce(
(current, next) => current.bottom >= next.bottom ? current : next,
);
- offset = rect.topRight.translate(0, -rect.height);
+ offset = rect.topRight.translate(0, -rect.height * 1.3);
} else {
final rect = rects.reduce(
(current, next) => current.top <= next.top ? current : next,
);
- offset = rect.topLeft.translate(0, -rect.height);
+ offset = rect.topLeft.translate(0, -rect.height * 1.3);
}
return editorState.service.selectionService.getPositionInOffset(offset);
}
@@ -362,12 +362,12 @@ Position? _goDown(EditorState editorState) {
final rect = rects.reduce(
(current, next) => current.bottom >= next.bottom ? current : next,
);
- offset = rect.bottomRight.translate(0, rect.height);
+ offset = rect.bottomRight.translate(0, rect.height * 1.3);
} else {
final rect = rects.reduce(
(current, next) => current.top <= next.top ? current : next,
);
- offset = rect.bottomLeft.translate(0, rect.height);
+ offset = rect.bottomLeft.translate(0, rect.height * 1.3);
}
return editorState.service.selectionService.getPositionInOffset(offset);
}
diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart
index 6575dced25..3d3def574e 100644
--- a/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart
+++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/service/toolbar_service.dart
@@ -38,14 +38,17 @@ class _FlowyToolbarState extends State<FlowyToolbar>
@override
void showInOffset(Offset offset, LayerLink layerLink) {
hide();
-
+ final items = _filterItems(defaultToolbarItems);
+ if (items.isEmpty) {
+ return;
+ }
_toolbarOverlay = OverlayEntry(
builder: (context) => ToolbarWidget(
key: _toolbarWidgetKey,
editorState: widget.editorState,
layerLink: layerLink,
offset: offset,
- items: _filterItems(defaultToolbarItems),
+ items: items,
),
);
Overlay.of(context)?.insert(_toolbarOverlay!);
@@ -102,9 +105,4 @@ class _FlowyToolbarState extends State<FlowyToolbar>
}
return dividedItems;
}
-
- // List<ToolbarItem> _highlightItems(
- // List<ToolbarItem> items,
- // Selection selection,
- // ) {}
}