Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept multi-key combination for customizing shortcuts & removes duplicates #5414

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,18 @@ final List<CommandShortcutEvent> commandShortcutEvents = [
customPasteCommand,
customCutCommand,
...customTextAlignCommands,
...standardCommandShortcutEvents,

// remove standard shortcuts for copy, cut, paste, todo
...standardCommandShortcutEvents
..removeWhere(
(shortcut) => [
copyCommand,
cutCommand,
pasteCommand,
toggleTodoListCommand,
].contains(shortcut),
),

emojiShortcutEvent,
];

Expand Down Expand Up @@ -90,7 +101,6 @@ class AppFlowyEditorPage extends StatefulWidget {
final String Function(Node)? placeholderText;

/// Used to provide an initial selection on Page-load
///
final Selection? initialSelection;

final bool useViewInfoBloc;
Expand All @@ -111,15 +121,8 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
],
);

late final List<CommandShortcutEvent> commandShortcutEvents = [
toggleToggleListCommand,
...localizedCodeBlockCommands,
customCopyCommand,
customPasteCommand,
customCutCommand,
...customTextAlignCommands,
...standardCommandShortcutEvents,
emojiShortcutEvent,
late final List<CommandShortcutEvent> cmdShortcutEvents = [
...commandShortcutEvents,
..._buildFindAndReplaceCommands(),
];

Expand Down Expand Up @@ -309,7 +312,7 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
),
// customize the shortcuts
characterShortcutEvents: characterShortcutEvents,
commandShortcutEvents: commandShortcutEvents,
commandShortcutEvents: cmdShortcutEvents,
// customize the context menu items
contextMenuItems: customContextMenuItems,
// customize the header and footer.
Expand Down Expand Up @@ -407,7 +410,7 @@ class _AppFlowyEditorPageState extends State<AppFlowyEditorPage> {
final customizeShortcuts =
await settingsShortcutService.getCustomizeShortcuts();
await settingsShortcutService.updateCommandShortcuts(
commandShortcutEvents,
cmdShortcutEvents,
customizeShortcuts,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class ShortcutsCubit extends Cubit<ShortcutsState> {
error: '',
),
);

try {
final customizeShortcuts = await service.getCustomizeShortcuts();
await service.updateCommandShortcuts(
Expand All @@ -40,7 +41,9 @@ class ShortcutsCubit extends Cubit<ShortcutsState> {
);

//sort the shortcuts
commandShortcutEvents.sort((a, b) => a.key.compareTo(b.key));
commandShortcutEvents.sort(
(a, b) => a.key.toLowerCase().compareTo(b.key.toLowerCase()),
);

emit(
state.copyWith(
Expand Down Expand Up @@ -104,11 +107,11 @@ class ShortcutsCubit extends Cubit<ShortcutsState> {
}
}

///Checks if the new command is conflicting with other shortcut
///We also check using the key, whether this command is a codeblock
///shortcut, if so we only check a conflict with other codeblock shortcut.
/// Checks if the new command is conflicting with other shortcut
/// We also check using the key, whether this command is a codeblock
/// shortcut, if so we only check a conflict with other codeblock shortcut.
String getConflict(CommandShortcutEvent currentShortcut, String command) {
//check if currentShortcut is a codeblock shortcut.
// check if currentShortcut is a codeblock shortcut.
final isCodeBlockCommand = currentShortcut.isCodeBlockCommand;

for (final e in state.commandShortcutEvents) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,33 @@ class ShortcutsListView extends StatelessWidget {
}
}

class ShortcutsListTile extends StatelessWidget {
class ShortcutsListTile extends StatefulWidget {
const ShortcutsListTile({
super.key,
required this.shortcutEvent,
});

final CommandShortcutEvent shortcutEvent;

@override
State<ShortcutsListTile> createState() => _ShortcutsListTileState();
}

class _ShortcutsListTileState extends State<ShortcutsListTile> {
late final TextEditingController controller;

@override
void initState() {
controller = TextEditingController(text: widget.shortcutEvent.command);
super.initState();
}

@override
void dispose() {
controller.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Column(
Expand All @@ -100,16 +119,16 @@ class ShortcutsListTile extends StatelessWidget {
children: [
Expanded(
child: FlowyText.medium(
key: Key(shortcutEvent.key),
shortcutEvent.description!.capitalize(),
key: Key(widget.shortcutEvent.key),
widget.shortcutEvent.description!.capitalize(),
overflow: TextOverflow.ellipsis,
),
),
FlowyTextButton(
shortcutEvent.command,
widget.shortcutEvent.command,
fontColor: AFThemeExtension.of(context).textColor,
fillColor: Colors.transparent,
onPressed: () => showKeyListenerDialog(context),
onPressed: () => showKeyListenerDialog(context, controller),
),
],
),
Expand All @@ -120,8 +139,10 @@ class ShortcutsListTile extends StatelessWidget {
);
}

void showKeyListenerDialog(BuildContext widgetContext) {
final controller = TextEditingController(text: shortcutEvent.command);
void showKeyListenerDialog(
BuildContext widgetContext,
TextEditingController controller,
) {
showDialog(
context: widgetContext,
builder: (builderContext) {
Expand All @@ -131,9 +152,10 @@ class ShortcutsListTile extends StatelessWidget {
content: KeyboardListener(
focusNode: FocusNode(),
onKeyEvent: (key) {
if (key is! KeyDownEvent) return;
if (key.logicalKey == LogicalKeyboardKey.enter &&
!HardwareKeyboard.instance.isShiftPressed) {
if (controller.text == shortcutEvent.command) {
if (controller.text == widget.shortcutEvent.command) {
_dismiss(builderContext);
}
if (formKey.currentState!.validate()) {
Expand Down Expand Up @@ -166,12 +188,12 @@ class ShortcutsListTile extends StatelessWidget {
),
);
},
).then((_) => controller.dispose());
);
}

String? _validateForConflicts(BuildContext context, String command) {
final conflict = BlocProvider.of<ShortcutsCubit>(context).getConflict(
shortcutEvent,
widget.shortcutEvent,
command,
);
if (conflict.isEmpty) return null;
Expand All @@ -182,7 +204,7 @@ class ShortcutsListTile extends StatelessWidget {
}

void _updateKey(BuildContext context, String command) {
shortcutEvent.updateCommand(command: command);
widget.shortcutEvent.updateCommand(command: command);
BlocProvider.of<ShortcutsCubit>(context).updateAllShortcuts();
}

Expand Down
Loading