chore: remove dynamic methods

This commit is contained in:
Enzo Lizama 2022-10-05 23:49:41 -05:00
parent bfe61ea09d
commit 90ac7970bd
6 changed files with 51 additions and 37 deletions

View file

@ -6,7 +6,7 @@ import 'package:appflowy_editor/src/document/text_delta.dart';
import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart'; import 'package:appflowy_editor/src/document/built_in_attribute_keys.dart';
extension TextNodeExtension on TextNode { extension TextNodeExtension on TextNode {
dynamic getAttributeInSelection(Selection selection, String styleKey) { T? getAttributeInSelection<T>(Selection selection, String styleKey) {
final ops = delta.whereType<TextInsert>(); final ops = delta.whereType<TextInsert>();
final startOffset = final startOffset =
selection.isBackward ? selection.start.offset : selection.end.offset; selection.isBackward ? selection.start.offset : selection.end.offset;
@ -29,40 +29,42 @@ extension TextNodeExtension on TextNode {
} }
bool allSatisfyLinkInSelection(Selection selection) => bool allSatisfyLinkInSelection(Selection selection) =>
allSatisfyInSelection(selection, BuiltInAttributeKey.href, (value) { allSatisfyInSelection(selection, BuiltInAttributeKey.href, <bool>(value) {
return value != null; return value != null;
}); });
bool allSatisfyBoldInSelection(Selection selection) => bool allSatisfyBoldInSelection(Selection selection) =>
allSatisfyInSelection(selection, BuiltInAttributeKey.bold, (value) { allSatisfyInSelection(selection, BuiltInAttributeKey.bold, <bool>(value) {
return value == true; return value == true;
}); });
bool allSatisfyItalicInSelection(Selection selection) => bool allSatisfyItalicInSelection(Selection selection) =>
allSatisfyInSelection(selection, BuiltInAttributeKey.italic, (value) { allSatisfyInSelection(selection, BuiltInAttributeKey.italic,
<bool>(value) {
return value == true; return value == true;
}); });
bool allSatisfyUnderlineInSelection(Selection selection) => bool allSatisfyUnderlineInSelection(Selection selection) =>
allSatisfyInSelection(selection, BuiltInAttributeKey.underline, (value) { allSatisfyInSelection(selection, BuiltInAttributeKey.underline,
<bool>(value) {
return value == true; return value == true;
}); });
bool allSatisfyStrikethroughInSelection(Selection selection) => bool allSatisfyStrikethroughInSelection(Selection selection) =>
allSatisfyInSelection(selection, BuiltInAttributeKey.strikethrough, allSatisfyInSelection(selection, BuiltInAttributeKey.strikethrough,
(value) { <bool>(value) {
return value == true; return value == true;
}); });
bool allSatisfyCodeInSelection(Selection selection) => bool allSatisfyCodeInSelection(Selection selection) =>
allSatisfyInSelection(selection, BuiltInAttributeKey.code, (value) { allSatisfyInSelection(selection, BuiltInAttributeKey.code, <bool>(value) {
return value == true; return value == true;
}); });
bool allSatisfyInSelection( bool allSatisfyInSelection(
Selection selection, Selection selection,
String styleKey, String styleKey,
bool Function(dynamic value) test, bool Function<T>(T value) test,
) { ) {
if (BuiltInAttributeKey.globalStyleKeys.contains(styleKey)) { if (BuiltInAttributeKey.globalStyleKeys.contains(styleKey)) {
if (attributes.containsKey(styleKey)) { if (attributes.containsKey(styleKey)) {
@ -127,40 +129,40 @@ extension TextNodesExtension on List<TextNode> {
bool allSatisfyBoldInSelection(Selection selection) => allSatisfyInSelection( bool allSatisfyBoldInSelection(Selection selection) => allSatisfyInSelection(
selection, selection,
BuiltInAttributeKey.bold, BuiltInAttributeKey.bold,
(value) => value == true, <bool>(value) => value == true,
); );
bool allSatisfyItalicInSelection(Selection selection) => bool allSatisfyItalicInSelection(Selection selection) =>
allSatisfyInSelection( allSatisfyInSelection(
selection, selection,
BuiltInAttributeKey.italic, BuiltInAttributeKey.italic,
(value) => value == true, <bool>(value) => value == true,
); );
bool allSatisfyUnderlineInSelection(Selection selection) => bool allSatisfyUnderlineInSelection(Selection selection) =>
allSatisfyInSelection( allSatisfyInSelection(
selection, selection,
BuiltInAttributeKey.underline, BuiltInAttributeKey.underline,
(value) => value == true, <bool>(value) => value == true,
); );
bool allSatisfyStrikethroughInSelection(Selection selection) => bool allSatisfyStrikethroughInSelection(Selection selection) =>
allSatisfyInSelection( allSatisfyInSelection(
selection, selection,
BuiltInAttributeKey.strikethrough, BuiltInAttributeKey.strikethrough,
(value) => value == true, <bool>(value) => value == true,
); );
bool allSatisfyInSelection( bool allSatisfyInSelection(
Selection selection, Selection selection,
String styleKey, String styleKey,
bool Function(dynamic value) test, bool Function<T>(T value) test,
) { ) {
if (isEmpty) { if (isEmpty) {
return false; return false;
} }
if (length == 1) { if (length == 1) {
return first.allSatisfyInSelection(selection, styleKey, (value) { return first.allSatisfyInSelection(selection, styleKey, <bool>(value) {
return test(value); return test(value);
}); });
} else { } else {

View file

@ -73,7 +73,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.heading, BuiltInAttributeKey.heading,
(value) => value == BuiltInAttributeKey.h1, <bool>(value) => value == BuiltInAttributeKey.h1,
), ),
handler: (editorState, context) => handler: (editorState, context) =>
formatHeading(editorState, BuiltInAttributeKey.h1), formatHeading(editorState, BuiltInAttributeKey.h1),
@ -90,7 +90,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.heading, BuiltInAttributeKey.heading,
(value) => value == BuiltInAttributeKey.h2, <bool>(value) => value == BuiltInAttributeKey.h2,
), ),
handler: (editorState, context) => handler: (editorState, context) =>
formatHeading(editorState, BuiltInAttributeKey.h2), formatHeading(editorState, BuiltInAttributeKey.h2),
@ -107,7 +107,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.heading, BuiltInAttributeKey.heading,
(value) => value == BuiltInAttributeKey.h3, <bool>(value) => value == BuiltInAttributeKey.h3,
), ),
handler: (editorState, context) => handler: (editorState, context) =>
formatHeading(editorState, BuiltInAttributeKey.h3), formatHeading(editorState, BuiltInAttributeKey.h3),
@ -124,7 +124,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.bold, BuiltInAttributeKey.bold,
(value) => value == true, <bool>(value) => value == true,
), ),
handler: (editorState, context) => formatBold(editorState), handler: (editorState, context) => formatBold(editorState),
), ),
@ -140,7 +140,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.italic, BuiltInAttributeKey.italic,
(value) => value == true, <bool>(value) => value == true,
), ),
handler: (editorState, context) => formatItalic(editorState), handler: (editorState, context) => formatItalic(editorState),
), ),
@ -156,7 +156,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.underline, BuiltInAttributeKey.underline,
(value) => value == true, <bool>(value) => value == true,
), ),
handler: (editorState, context) => formatUnderline(editorState), handler: (editorState, context) => formatUnderline(editorState),
), ),
@ -172,7 +172,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.strikethrough, BuiltInAttributeKey.strikethrough,
(value) => value == true, <bool>(value) => value == true,
), ),
handler: (editorState, context) => formatStrikethrough(editorState), handler: (editorState, context) => formatStrikethrough(editorState),
), ),
@ -188,7 +188,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.code, BuiltInAttributeKey.code,
(value) => value == true, <bool>(value) => value == true,
), ),
handler: (editorState, context) => formatEmbedCode(editorState), handler: (editorState, context) => formatEmbedCode(editorState),
), ),
@ -204,7 +204,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.subtype, BuiltInAttributeKey.subtype,
(value) => value == BuiltInAttributeKey.quote, <bool>(value) => value == BuiltInAttributeKey.quote,
), ),
handler: (editorState, context) => formatQuote(editorState), handler: (editorState, context) => formatQuote(editorState),
), ),
@ -220,7 +220,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.subtype, BuiltInAttributeKey.subtype,
(value) => value == BuiltInAttributeKey.bulletedList, <bool>(value) => value == BuiltInAttributeKey.bulletedList,
), ),
handler: (editorState, context) => formatBulletedList(editorState), handler: (editorState, context) => formatBulletedList(editorState),
), ),
@ -236,7 +236,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.href, BuiltInAttributeKey.href,
(value) => value != null, <bool>(value) => value != null,
), ),
handler: (editorState, context) => showLinkMenu(context, editorState), handler: (editorState, context) => showLinkMenu(context, editorState),
), ),
@ -252,7 +252,7 @@ List<ToolbarItem> defaultToolbarItems = [
highlightCallback: (editorState) => _allSatisfy( highlightCallback: (editorState) => _allSatisfy(
editorState, editorState,
BuiltInAttributeKey.backgroundColor, BuiltInAttributeKey.backgroundColor,
(value) => value != null, <bool>(value) => value != null,
), ),
handler: (editorState, context) => formatHighlight( handler: (editorState, context) => formatHighlight(
editorState, editorState,
@ -284,7 +284,7 @@ ToolbarItemValidator _showInBuiltInTextSelection = (editorState) {
bool _allSatisfy( bool _allSatisfy(
EditorState editorState, EditorState editorState,
String styleKey, String styleKey,
bool Function(dynamic value) test, bool Function<T>(T value) test,
) { ) {
final selection = editorState.service.selectionService.currentSelection.value; final selection = editorState.service.selectionService.currentSelection.value;
return selection != null && return selection != null &&
@ -333,8 +333,10 @@ void showLinkMenu(
final textNode = node.first as TextNode; final textNode = node.first as TextNode;
String? linkText; String? linkText;
if (textNode.allSatisfyLinkInSelection(selection)) { if (textNode.allSatisfyLinkInSelection(selection)) {
linkText = linkText = textNode.getAttributeInSelection<String>(
textNode.getAttributeInSelection(selection, BuiltInAttributeKey.href); selection,
BuiltInAttributeKey.href,
);
} }
_linkMenuOverlay = OverlayEntry(builder: (context) { _linkMenuOverlay = OverlayEntry(builder: (context) {
return Positioned( return Positioned(

View file

@ -188,7 +188,7 @@ bool _allSatisfyInSelection(
return false; return false;
} }
return textNodes.allSatisfyInSelection(selection, styleKey, (value) { return textNodes.allSatisfyInSelection(selection, styleKey, <bool>(value) {
return value == matchValue; return value == matchValue;
}); });
} }

View file

@ -0,0 +1,10 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('TextNodeExtension::', () {
test('description', () {
final selecion = Selection.single(path: [0, 1, 2], startOffset: 0);
});
});
}

View file

@ -229,7 +229,7 @@ void main() async {
node.allSatisfyInSelection( node.allSatisfyInSelection(
code, code,
BuiltInAttributeKey.code, BuiltInAttributeKey.code,
(value) { <bool>(value) {
return value == true; return value == true;
}, },
), ),
@ -319,7 +319,7 @@ void main() async {
node.allSatisfyInSelection( node.allSatisfyInSelection(
selection, selection,
BuiltInAttributeKey.backgroundColor, BuiltInAttributeKey.backgroundColor,
(value) { <bool>(value) {
return value == blue; return value == blue;
}, },
), ),

View file

@ -111,7 +111,7 @@ Future<void> _testUpdateTextStyleByCommandX(
textNode.allSatisfyInSelection( textNode.allSatisfyInSelection(
selection, selection,
matchStyle, matchStyle,
(value) { <bool>(value) {
return value == matchValue; return value == matchValue;
}, },
), ),
@ -138,7 +138,7 @@ Future<void> _testUpdateTextStyleByCommandX(
textNode.allSatisfyInSelection( textNode.allSatisfyInSelection(
selection, selection,
matchStyle, matchStyle,
(value) { <bool>(value) {
return value == matchValue; return value == matchValue;
}, },
), ),
@ -192,7 +192,7 @@ Future<void> _testUpdateTextStyleByCommandX(
endOffset: text.length, endOffset: text.length,
), ),
matchStyle, matchStyle,
(value) { <bool>(value) {
return value == matchValue; return value == matchValue;
}, },
), ),
@ -266,7 +266,7 @@ Future<void> _testLinkMenuInSingleTextSelection(WidgetTester tester) async {
node.allSatisfyInSelection( node.allSatisfyInSelection(
selection, selection,
BuiltInAttributeKey.href, BuiltInAttributeKey.href,
(value) => value == link, <bool>(value) => value == link,
), ),
true); true);
@ -303,7 +303,7 @@ Future<void> _testLinkMenuInSingleTextSelection(WidgetTester tester) async {
node.allSatisfyInSelection( node.allSatisfyInSelection(
selection, selection,
BuiltInAttributeKey.href, BuiltInAttributeKey.href,
(value) => value == link, <bool>(value) => value == link,
), ),
false); false);
} }