Skip to content

Commit

Permalink
fix: command shortcuts on the web platform should also respect the op…
Browse files Browse the repository at this point in the history
…erating system
  • Loading branch information
LucasXu0 committed Sep 24, 2023
1 parent 5c29132 commit c9adbb1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 166 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import 'dart:io';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

typedef CommandShortcutEventHandler = KeyEventResult Function(
Expand Down Expand Up @@ -67,23 +64,17 @@ class CommandShortcutEvent {
return;
}
var matched = false;
if (kIsWeb) {
// We shouldn't continue to run the below `else if` code in Web platform, it will throw an `_operatingSystem` exception.
if (command != null && command.isNotEmpty) {
this.command = command;
matched = true;
}
} else if (Platform.isWindows &&
if ((PlatformExtension.isWindows || PlatformExtension.isWebOnWindows) &&
windowsCommand != null &&
windowsCommand.isNotEmpty) {
this.command = windowsCommand;
matched = true;
} else if (Platform.isMacOS &&
} else if ((PlatformExtension.isMacOS || PlatformExtension.isWebOnMacOS) &&
macOSCommand != null &&
macOSCommand.isNotEmpty) {
this.command = macOSCommand;
matched = true;
} else if (Platform.isLinux &&
} else if ((PlatformExtension.isLinux || PlatformExtension.isWebOnLinux) &&
linuxCommand != null &&
linuxCommand.isNotEmpty) {
this.command = linuxCommand;
Expand Down
38 changes: 38 additions & 0 deletions lib/src/editor/util/platform_extension.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:universal_html/html.dart' show window;

// TODO(Xazin): Refactor to honor `Theme.platform`
extension PlatformExtension on Platform {
static String get _webPlatform =>
window.navigator.platform?.toLowerCase() ?? '';

Check warning on line 9 in lib/src/editor/util/platform_extension.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/util/platform_extension.dart#L8-L9

Added lines #L8 - L9 were not covered by tests

/// Returns true if the operating system is macOS and not running on Web platform.
static bool get isMacOS {
if (kIsWeb) {
return false;
}
return Platform.isMacOS;
}

/// Returns true if the operating system is Windows and not running on Web platform.
static bool get isWindows {
if (kIsWeb) {
return false;
}
return Platform.isWindows;
}

/// Returns true if the operating system is Linux and not running on Web platform.
static bool get isLinux {
if (kIsWeb) {
return false;
}
return Platform.isLinux;
}

/// Returns true if the operating system is macOS and running on Web platform.
static bool get isWebOnMacOS {
if (!kIsWeb) {
return false;
}
return _webPlatform.contains('mac') == true;

Check warning on line 40 in lib/src/editor/util/platform_extension.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/util/platform_extension.dart#L40

Added line #L40 was not covered by tests
}

/// Returns true if the operating system is Windows and running on Web platform.
static bool get isWebOnWindows {
if (!kIsWeb) {
return false;
}
return _webPlatform.contains('windows') == true;

Check warning on line 48 in lib/src/editor/util/platform_extension.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/util/platform_extension.dart#L48

Added line #L48 was not covered by tests
}

/// Returns true if the operating system is Linux and running on Web platform.
static bool get isWebOnLinux {
if (!kIsWeb) {
return false;
}
return _webPlatform.contains('linux') == true;

Check warning on line 56 in lib/src/editor/util/platform_extension.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/util/platform_extension.dart#L56

Added line #L56 was not covered by tests
}

static bool get isDesktopOrWeb {
if (kIsWeb) {
return true;
Expand Down
150 changes: 0 additions & 150 deletions lib/src/service/shortcut_event/shortcut_event.dart

This file was deleted.

1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies:
path: ^1.8.3
diff_match_patch: ^0.4.1
string_validator: ^1.0.0
universal_html: ^2.2.4

dev_dependencies:
flutter_test:
Expand Down
26 changes: 26 additions & 0 deletions test/editor/util/platform_extension_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:io';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('platform extension', () {
test('test safe platform judgement', () {
if (!kIsWeb && Platform.isLinux) {
expect(PlatformExtension.isLinux, true);
expect(PlatformExtension.isWebOnLinux, false);
}

if (!kIsWeb && Platform.isWindows) {
expect(PlatformExtension.isWindows, true);
expect(PlatformExtension.isWebOnWindows, false);
}

if (!kIsWeb && Platform.isMacOS) {
expect(PlatformExtension.isMacOS, true);
expect(PlatformExtension.isWebOnMacOS, false);
}
});
});
}
7 changes: 3 additions & 4 deletions test/service/shortcut_event/shortcut_event_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import 'dart:io';

import 'package:appflowy_editor/src/service/shortcut_event/shortcut_event.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:appflowy_editor/appflowy_editor.dart';

import '../../new/infra/testable_editor.dart';

Expand All @@ -15,10 +14,10 @@ void main() async {

group('shortcut_event.dart', () {
test('redefine shortcut event command', () {
final shortcutEvent = ShortcutEvent(
final shortcutEvent = CommandShortcutEvent(
key: 'Sample',
command: 'cmd+shift+alt+ctrl+a',
handler: (editorState, event) {
handler: (editorState) {
return KeyEventResult.handled;
},
);
Expand Down

0 comments on commit c9adbb1

Please sign in to comment.