fix: disable built-in toolbar items for non-built-in widget

This commit is contained in:
Lucas.Xu 2022-09-27 11:14:58 +08:00
parent 5421c156c3
commit 31ba12d289
3 changed files with 26 additions and 20 deletions

View File

@ -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;
};

View File

@ -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);
}

View File

@ -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,
// ) {}
}