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: avoid using Platform code in Web #44 #48

Merged
merged 1 commit into from
Apr 10, 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
31 changes: 24 additions & 7 deletions lib/src/render/toolbar/toolbar_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:appflowy_editor/src/render/link_menu/link_menu.dart';
import 'package:appflowy_editor/src/extensions/text_node_extensions.dart';
import 'package:appflowy_editor/src/extensions/editor_state_extensions.dart';
import 'package:appflowy_editor/src/service/default_text_operations/format_rich_text_style.dart';
import 'package:flutter/foundation.dart';
import 'dart:io' show Platform;

import 'package:flutter/material.dart' hide Overlay, OverlayEntry;
Expand Down Expand Up @@ -129,7 +130,7 @@ List<ToolbarItem> defaultToolbarItems = [
id: 'appflowy.toolbar.bold',
type: 2,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.bold}\n${Platform.isMacOS ? "⌘ + B" : "CTRL + B"}",
"${AppFlowyEditorLocalizations.current.bold}${_shortcutTooltips("⌘ + B", "CTRL + B", "CTRL + B")}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/bold',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -146,7 +147,7 @@ List<ToolbarItem> defaultToolbarItems = [
id: 'appflowy.toolbar.italic',
type: 2,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.italic}\n${Platform.isMacOS ? "⌘ + I" : "CTRL + I"}",
"${AppFlowyEditorLocalizations.current.italic}${_shortcutTooltips("⌘ + I", "CTRL + I", "CTRL + I")}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/italic',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -163,7 +164,7 @@ List<ToolbarItem> defaultToolbarItems = [
id: 'appflowy.toolbar.underline',
type: 2,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.underline}\n${Platform.isMacOS ? "⌘ + U" : "CTRL + U"}",
"${AppFlowyEditorLocalizations.current.underline}${_shortcutTooltips("⌘ + U", "CTRL + U", "CTRL + U")}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/underline',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -180,7 +181,7 @@ List<ToolbarItem> defaultToolbarItems = [
id: 'appflowy.toolbar.strikethrough',
type: 2,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.strikethrough}\n${Platform.isMacOS ? "⌘ + SHIFT + S" : "CTRL + SHIFT + S"}",
"${AppFlowyEditorLocalizations.current.strikethrough}${_shortcutTooltips("⌘ + SHIFT + S", "CTRL + SHIFT + S", "CTRL + SHIFT + S")}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/strikethrough',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -197,7 +198,7 @@ List<ToolbarItem> defaultToolbarItems = [
id: 'appflowy.toolbar.code',
type: 2,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.embedCode}\n${Platform.isMacOS ? "⌘ + E" : "CTRL + E"}",
"${AppFlowyEditorLocalizations.current.embedCode}${_shortcutTooltips("⌘ + E", "CTRL + E", "CTRL + E")}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/code',
color: isHighlight ? Colors.lightBlue : null,
Expand Down Expand Up @@ -248,7 +249,7 @@ List<ToolbarItem> defaultToolbarItems = [
id: 'appflowy.toolbar.link',
type: 4,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.link}\n${Platform.isMacOS ? "⌘ + K" : "CTRL + K"}",
"${AppFlowyEditorLocalizations.current.link}${_shortcutTooltips("⌘ + K", "CTRL + K", "CTRL + K")}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/link',
color: isHighlight ? Colors.lightBlue : null,
Expand All @@ -265,7 +266,7 @@ List<ToolbarItem> defaultToolbarItems = [
id: 'appflowy.toolbar.highlight',
type: 4,
tooltipsMessage:
"${AppFlowyEditorLocalizations.current.highlight}\n${Platform.isMacOS ? "⌘ + SHIFT + H" : "CTRL + SHIFT + H"}",
"${AppFlowyEditorLocalizations.current.highlight}${_shortcutTooltips("⌘ + SHIFT + H", "CTRL + SHIFT + H", "CTRL + SHIFT + H")}",
iconBuilder: (isHighlight) => FlowySvg(
name: 'toolbar/highlight',
color: isHighlight ? Colors.lightBlue : null,
Expand Down Expand Up @@ -316,6 +317,22 @@ List<ToolbarItem> defaultToolbarItems = [
),
];

String _shortcutTooltips(
String? macOSString,
String? windowsString,
String? linuxString,
) {
if (kIsWeb) return '';
if (Platform.isMacOS && macOSString != null) {
return '\n$macOSString';
} else if (Platform.isWindows && windowsString != null) {
return '\n$windowsString';
} else if (Platform.isLinux && linuxString != null) {
return '\n$linuxString';
}
return '';
}

ToolbarItemValidator _onlyShowInSingleTextSelection = (editorState) {
final result = _showInBuiltInTextSelection(editorState);
if (!result) {
Expand Down
15 changes: 12 additions & 3 deletions lib/src/service/shortcut_event/shortcut_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,19 @@ class ShortcutEvent {
String? macOSCommand,
String? linuxCommand,
}) {
if (command == null &&
windowsCommand == null &&
macOSCommand == null &&
linuxCommand == null) {
return;
}
var matched = false;
if (kIsWeb && command != null && command.isNotEmpty) {
this.command = command;
matched = true;
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 &&
windowsCommand != null &&
windowsCommand.isNotEmpty) {
Expand Down