fix: only adding http(s) scheme if needed (#4871)

This commit is contained in:
Lucas.Xu 2024-03-11 16:43:43 +08:00 committed by GitHub
parent b4c8011881
commit dd3f8b247a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 11 deletions

View File

@ -15,23 +15,34 @@ Future<bool> afLaunchUrl(
OnFailureCallback? onFailure, OnFailureCallback? onFailure,
launcher.LaunchMode mode = launcher.LaunchMode.platformDefault, launcher.LaunchMode mode = launcher.LaunchMode.platformDefault,
String? webOnlyWindowName, String? webOnlyWindowName,
bool addingHttpSchemeWhenFailed = false,
}) async { }) async {
// try to launch the uri directly // try to launch the uri directly
bool result; bool result;
try { try {
result = await launcher.launchUrl(uri); result = await launcher.launchUrl(
uri,
mode: mode,
webOnlyWindowName: webOnlyWindowName,
);
} on PlatformException catch (e) { } on PlatformException catch (e) {
Log.error('Failed to open uri: $e'); Log.error('Failed to open uri: $e');
} finally { } finally {
result = false; result = false;
} }
// if the uri is not a valid url, try to launch it with https scheme // if the uri is not a valid url, try to launch it with http scheme
final url = uri.toString(); final url = uri.toString();
if (!result && !isURL(url, {'require_protocol': true})) { if (addingHttpSchemeWhenFailed &&
!result &&
!isURL(url, {'require_protocol': true})) {
try { try {
final uriWithScheme = Uri.parse('https://$url'); final uriWithScheme = Uri.parse('http://$url');
result = await launcher.launchUrl(uriWithScheme); result = await launcher.launchUrl(
uriWithScheme,
mode: mode,
webOnlyWindowName: webOnlyWindowName,
);
} on PlatformException catch (e) { } on PlatformException catch (e) {
Log.error('Failed to open uri: $e'); Log.error('Failed to open uri: $e');
if (context != null && context.mounted) { if (context != null && context.mounted) {
@ -43,7 +54,10 @@ Future<bool> afLaunchUrl(
return result; return result;
} }
Future<bool> afLaunchUrlString(String url) async { Future<bool> afLaunchUrlString(
String url, {
bool addingHttpSchemeWhenFailed = false,
}) async {
final Uri uri; final Uri uri;
try { try {
uri = Uri.parse(url); uri = Uri.parse(url);
@ -53,7 +67,10 @@ Future<bool> afLaunchUrlString(String url) async {
} }
// try to launch the uri directly // try to launch the uri directly
return afLaunchUrl(uri); return afLaunchUrl(
uri,
addingHttpSchemeWhenFailed: addingHttpSchemeWhenFailed,
);
} }
void _errorHandler( void _errorHandler(

View File

@ -1,8 +1,5 @@
import 'dart:math'; import 'dart:math';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:appflowy/core/helpers/url_launcher.dart'; import 'package:appflowy/core/helpers/url_launcher.dart';
import 'package:appflowy/plugins/document/application/document_appearance_cubit.dart'; import 'package:appflowy/plugins/document/application/document_appearance_cubit.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_block.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_block.dart';
@ -15,6 +12,8 @@ import 'package:appflowy/workspace/application/settings/appearance/appearance_cu
import 'package:appflowy/workspace/application/settings/appearance/base_appearance.dart'; import 'package:appflowy/workspace/application/settings/appearance/base_appearance.dart';
import 'package:appflowy_editor/appflowy_editor.dart' hide Log; import 'package:appflowy_editor/appflowy_editor.dart' hide Log;
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
@ -294,7 +293,10 @@ class EditorStyleCustomizer {
..onTap = () { ..onTap = () {
final editorState = context.read<EditorState>(); final editorState = context.read<EditorState>();
if (editorState.selection == null) { if (editorState.selection == null) {
afLaunchUrlString(href); afLaunchUrlString(
href,
addingHttpSchemeWhenFailed: true,
);
return; return;
} }