Skip to content

Commit

Permalink
feat: support exitting the link menu by pressing ESC
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzoursano committed Jul 3, 2023
1 parent 8bfb37f commit c8082d3
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"lightLightTint7": "Green",
"lightLightTint8": "Aqua",
"lightLightTint9": "Blue",
"urlHint": "URL",
"mobileHeading1": "Heading 1",
"mobileHeading2": "Heading 2",
"mobileHeading3": "Heading 3",
Expand Down
52 changes: 32 additions & 20 deletions lib/src/editor/toolbar/desktop/items/link/link_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:appflowy_editor/src/editor_state.dart';
import 'package:appflowy_editor/src/infra/flowy_svg.dart';
import 'package:appflowy_editor/src/l10n/l10n.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:appflowy_editor/src/editor/toolbar/desktop/items/utils/overlay_util.dart';

class LinkMenu extends StatefulWidget {
Expand All @@ -13,6 +14,7 @@ class LinkMenu extends StatefulWidget {
required this.onOpenLink,
required this.onCopyLink,
required this.onRemoveLink,
required this.onDismiss,
}) : super(key: key);

final String? linkText;
Expand All @@ -21,6 +23,7 @@ class LinkMenu extends StatefulWidget {
final VoidCallback onOpenLink;
final VoidCallback onCopyLink;
final VoidCallback onRemoveLink;
final VoidCallback onDismiss;

@override
State<LinkMenu> createState() => _LinkMenuState();
Expand Down Expand Up @@ -81,27 +84,36 @@ class _LinkMenuState extends State<LinkMenu> {
}

Widget _buildInput() {
return TextField(
focusNode: _focusNode,
textAlign: TextAlign.left,
controller: _textEditingController,
onSubmitted: widget.onSubmitted,
decoration: InputDecoration(
hintText: 'URL',
contentPadding: const EdgeInsets.all(16.0),
isDense: true,
suffixIcon: IconButton(
padding: const EdgeInsets.all(4.0),
icon: const FlowySvg(
name: 'clear',
width: 24,
height: 24,
return RawKeyboardListener(
focusNode: FocusNode(),
onKey: (key) {
if (key is RawKeyDownEvent &&
key.logicalKey == LogicalKeyboardKey.escape) {
widget.onDismiss();
}
},
child: TextField(
focusNode: _focusNode,
textAlign: TextAlign.left,
controller: _textEditingController,
onSubmitted: widget.onSubmitted,
decoration: InputDecoration(
hintText: AppFlowyEditorLocalizations.current.urlHint,
contentPadding: const EdgeInsets.all(16.0),
isDense: true,
suffixIcon: IconButton(
padding: const EdgeInsets.all(4.0),
icon: const FlowySvg(
name: 'clear',
width: 24,
height: 24,
),
onPressed: _textEditingController.clear,
splashRadius: 5,
),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
),
onPressed: _textEditingController.clear,
splashRadius: 5,
),
border: const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
),
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void showLinkMenu(
editorState.apply(transaction);
dismissOverlay();
},
onDismiss: dismissOverlay,
);
},
).build();
Expand Down
3 changes: 2 additions & 1 deletion lib/src/l10n/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class MessageLookup extends MessageLookupByLibrary {
"tint7": MessageLookupByLibrary.simpleMessage("Tint 7"),
"tint8": MessageLookupByLibrary.simpleMessage("Tint 8"),
"tint9": MessageLookupByLibrary.simpleMessage("Tint 9"),
"underline": MessageLookupByLibrary.simpleMessage("Underline")
"underline": MessageLookupByLibrary.simpleMessage("Underline"),
"urlHint": MessageLookupByLibrary.simpleMessage("URL")
};
}
10 changes: 10 additions & 0 deletions lib/src/l10n/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/src/service/selection_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class _AppFlowySelectionState extends State<AppFlowySelection>
currentSelectedNodes = [];
currentSelection.value = null;

_clearToolbar();
clearCursor();
// clear selection areas
_selectionAreas
Expand Down
33 changes: 33 additions & 0 deletions test/new/toolbar/desktop/items/link/link_menu_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:appflowy_editor/src/core/document/text_delta.dart';
import 'package:appflowy_editor/src/editor/toolbar/desktop/items/link/link_menu.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../../../infra/testable_editor.dart';

Expand All @@ -20,6 +21,7 @@ void main() async {
onSubmitted: (text) {
submittedText = text;
},
onDismiss: () {},
);
final editor = tester.editor;
await editor.startTesting();
Expand Down Expand Up @@ -92,5 +94,36 @@ void main() async {

await editor.dispose();
});

testWidgets('test dismiss link menu by pressing ESC', (tester) async {
var dismissed = false;

final linkMenu = LinkMenu(
onOpenLink: () {},
onCopyLink: () {},
onRemoveLink: () {},
onSubmitted: (text) {},
onDismiss: () {
dismissed = true;
},
);

await tester.pumpWidget(
MaterialApp(
home: Material(
child: linkMenu,
),
),
);

expect(find.byType(TextButton), findsNothing);
expect(find.byType(TextField), findsOneWidget);

// Simulate keyboard press event for the Escape key
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();

expect(dismissed, true);
});
});
}

0 comments on commit c8082d3

Please sign in to comment.