Skip to content

Commit b751a84

Browse files
authored
fix: avoid using Platform code in Web #44 (#48)
1 parent 645e430 commit b751a84

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

lib/src/render/toolbar/toolbar_item.dart

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:appflowy_editor/src/render/link_menu/link_menu.dart';
88
import 'package:appflowy_editor/src/extensions/text_node_extensions.dart';
99
import 'package:appflowy_editor/src/extensions/editor_state_extensions.dart';
1010
import 'package:appflowy_editor/src/service/default_text_operations/format_rich_text_style.dart';
11+
import 'package:flutter/foundation.dart';
1112
import 'dart:io' show Platform;
1213

1314
import 'package:flutter/material.dart' hide Overlay, OverlayEntry;
@@ -129,7 +130,7 @@ List<ToolbarItem> defaultToolbarItems = [
129130
id: 'appflowy.toolbar.bold',
130131
type: 2,
131132
tooltipsMessage:
132-
"${AppFlowyEditorLocalizations.current.bold}\n${Platform.isMacOS ? "⌘ + B" : "CTRL + B"}",
133+
"${AppFlowyEditorLocalizations.current.bold}${_shortcutTooltips("⌘ + B", "CTRL + B", "CTRL + B")}",
133134
iconBuilder: (isHighlight) => FlowySvg(
134135
name: 'toolbar/bold',
135136
color: isHighlight ? Colors.lightBlue : null,
@@ -146,7 +147,7 @@ List<ToolbarItem> defaultToolbarItems = [
146147
id: 'appflowy.toolbar.italic',
147148
type: 2,
148149
tooltipsMessage:
149-
"${AppFlowyEditorLocalizations.current.italic}\n${Platform.isMacOS ? "⌘ + I" : "CTRL + I"}",
150+
"${AppFlowyEditorLocalizations.current.italic}${_shortcutTooltips("⌘ + I", "CTRL + I", "CTRL + I")}",
150151
iconBuilder: (isHighlight) => FlowySvg(
151152
name: 'toolbar/italic',
152153
color: isHighlight ? Colors.lightBlue : null,
@@ -163,7 +164,7 @@ List<ToolbarItem> defaultToolbarItems = [
163164
id: 'appflowy.toolbar.underline',
164165
type: 2,
165166
tooltipsMessage:
166-
"${AppFlowyEditorLocalizations.current.underline}\n${Platform.isMacOS ? "⌘ + U" : "CTRL + U"}",
167+
"${AppFlowyEditorLocalizations.current.underline}${_shortcutTooltips("⌘ + U", "CTRL + U", "CTRL + U")}",
167168
iconBuilder: (isHighlight) => FlowySvg(
168169
name: 'toolbar/underline',
169170
color: isHighlight ? Colors.lightBlue : null,
@@ -180,7 +181,7 @@ List<ToolbarItem> defaultToolbarItems = [
180181
id: 'appflowy.toolbar.strikethrough',
181182
type: 2,
182183
tooltipsMessage:
183-
"${AppFlowyEditorLocalizations.current.strikethrough}\n${Platform.isMacOS ? "⌘ + SHIFT + S" : "CTRL + SHIFT + S"}",
184+
"${AppFlowyEditorLocalizations.current.strikethrough}${_shortcutTooltips("⌘ + SHIFT + S", "CTRL + SHIFT + S", "CTRL + SHIFT + S")}",
184185
iconBuilder: (isHighlight) => FlowySvg(
185186
name: 'toolbar/strikethrough',
186187
color: isHighlight ? Colors.lightBlue : null,
@@ -197,7 +198,7 @@ List<ToolbarItem> defaultToolbarItems = [
197198
id: 'appflowy.toolbar.code',
198199
type: 2,
199200
tooltipsMessage:
200-
"${AppFlowyEditorLocalizations.current.embedCode}\n${Platform.isMacOS ? "⌘ + E" : "CTRL + E"}",
201+
"${AppFlowyEditorLocalizations.current.embedCode}${_shortcutTooltips("⌘ + E", "CTRL + E", "CTRL + E")}",
201202
iconBuilder: (isHighlight) => FlowySvg(
202203
name: 'toolbar/code',
203204
color: isHighlight ? Colors.lightBlue : null,
@@ -248,7 +249,7 @@ List<ToolbarItem> defaultToolbarItems = [
248249
id: 'appflowy.toolbar.link',
249250
type: 4,
250251
tooltipsMessage:
251-
"${AppFlowyEditorLocalizations.current.link}\n${Platform.isMacOS ? "⌘ + K" : "CTRL + K"}",
252+
"${AppFlowyEditorLocalizations.current.link}${_shortcutTooltips("⌘ + K", "CTRL + K", "CTRL + K")}",
252253
iconBuilder: (isHighlight) => FlowySvg(
253254
name: 'toolbar/link',
254255
color: isHighlight ? Colors.lightBlue : null,
@@ -265,7 +266,7 @@ List<ToolbarItem> defaultToolbarItems = [
265266
id: 'appflowy.toolbar.highlight',
266267
type: 4,
267268
tooltipsMessage:
268-
"${AppFlowyEditorLocalizations.current.highlight}\n${Platform.isMacOS ? "⌘ + SHIFT + H" : "CTRL + SHIFT + H"}",
269+
"${AppFlowyEditorLocalizations.current.highlight}${_shortcutTooltips("⌘ + SHIFT + H", "CTRL + SHIFT + H", "CTRL + SHIFT + H")}",
269270
iconBuilder: (isHighlight) => FlowySvg(
270271
name: 'toolbar/highlight',
271272
color: isHighlight ? Colors.lightBlue : null,
@@ -316,6 +317,22 @@ List<ToolbarItem> defaultToolbarItems = [
316317
),
317318
];
318319

320+
String _shortcutTooltips(
321+
String? macOSString,
322+
String? windowsString,
323+
String? linuxString,
324+
) {
325+
if (kIsWeb) return '';
326+
if (Platform.isMacOS && macOSString != null) {
327+
return '\n$macOSString';
328+
} else if (Platform.isWindows && windowsString != null) {
329+
return '\n$windowsString';
330+
} else if (Platform.isLinux && linuxString != null) {
331+
return '\n$linuxString';
332+
}
333+
return '';
334+
}
335+
319336
ToolbarItemValidator _onlyShowInSingleTextSelection = (editorState) {
320337
final result = _showInBuiltInTextSelection(editorState);
321338
if (!result) {

lib/src/service/shortcut_event/shortcut_event.dart

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,19 @@ class ShortcutEvent {
7575
String? macOSCommand,
7676
String? linuxCommand,
7777
}) {
78+
if (command == null &&
79+
windowsCommand == null &&
80+
macOSCommand == null &&
81+
linuxCommand == null) {
82+
return;
83+
}
7884
var matched = false;
79-
if (kIsWeb && command != null && command.isNotEmpty) {
80-
this.command = command;
81-
matched = true;
85+
if (kIsWeb) {
86+
// We shouldn't continue to run the below `else if` code in Web platform, it will throw an `_operatingSystem` exception.
87+
if (command != null && command.isNotEmpty) {
88+
this.command = command;
89+
matched = true;
90+
}
8291
} else if (Platform.isWindows &&
8392
windowsCommand != null &&
8493
windowsCommand.isNotEmpty) {

0 commit comments

Comments
 (0)