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 web issues #498

Merged
merged 4 commits into from
Sep 24, 2023
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.4.1
* fix: platform issue on Web by @LucasXu0 in ([#498](https://github.com/AppFlowy-IO/appflowy-editor/pull/498))

## 1.4.1
* fix: build error on Flutter 3.13 by @LucasXu0 in ([#488](https://github.com/AppFlowy-IO/appflowy-editor/pull/488))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:io';
import 'dart:math';

import 'package:appflowy_editor/appflowy_editor.dart';
Expand Down Expand Up @@ -79,7 +78,7 @@

// the set editing state will update the text editing value in macOS.
// we just skip the unnecessary update.
if (Platform.isMacOS) {
if (PlatformExtension.isMacOS) {
skipUpdateEditingValue += 1;
}

Expand All @@ -96,7 +95,7 @@

@override
void updateEditingValue(TextEditingValue value) {
if (Platform.isMacOS && skipUpdateEditingValue > 0) {
if (PlatformExtension.isMacOS && skipUpdateEditingValue > 0) {

Check warning on line 98 in lib/src/editor/editor_component/service/ime/non_delta_input_service.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/ime/non_delta_input_service.dart#L98

Added line #L98 was not covered by tests
skipUpdateEditingValue -= 1;
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ class KeyboardServiceWidgetState extends State<KeyboardServiceWidget>
'keyboard service - focus changed: ${focusNode.hasFocus}}',
);

/// On web, we don't need to close the keyboard when the focus is lost.
if (kIsWeb) {
return;
}

// clear the selection when the focus is lost.
if (!focusNode.hasFocus) {
if (PlatformExtension.isDesktopOrWeb) {
Expand Down
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
45 changes: 45 additions & 0 deletions lib/src/editor/util/platform_extension.dart
Original file line number Diff line number Diff line change
@@ -1,16 +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.

3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: appflowy_editor
description: A highly customizable rich-text editor for Flutter. The AppFlowy Editor project for AppFlowy and beyond.
version: 1.4.1
version: 1.4.2
homepage: https://github.com/AppFlowy-IO/appflowy-editor

platforms:
Expand Down 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
Loading