Skip to content

Commit

Permalink
fix: avoid using Platform code in Web AppFlowy-IO#44 (AppFlowy-IO#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 authored and Xazin committed Apr 13, 2023
1 parent 46e6893 commit 16be1cd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
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

0 comments on commit 16be1cd

Please sign in to comment.