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

View File

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

View File

@ -188,7 +188,7 @@ bool _allSatisfyInSelection(
return false;
}
return textNodes.allSatisfyInSelection(selection, styleKey, (value) {
return textNodes.allSatisfyInSelection(selection, styleKey, <bool>(value) {
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(
code,
BuiltInAttributeKey.code,
(value) {
<bool>(value) {
return value == true;
},
),
@ -319,7 +319,7 @@ void main() async {
node.allSatisfyInSelection(
selection,
BuiltInAttributeKey.backgroundColor,
(value) {
<bool>(value) {
return value == blue;
},
),

View File

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