chore: invalid url open behavior (#4850)

* chore: invalid url open behavior

* chore: use https instead of http by default
This commit is contained in:
Richard Shiue 2024-03-08 10:15:19 +08:00 committed by GitHub
parent 6bac0412eb
commit f9c1cadbe0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 14 deletions

View File

@ -1,7 +1,6 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:appflowy/core/helpers/url_launcher.dart';
import 'package:appflowy/generated/flowy_svgs.g.dart'; import 'package:appflowy/generated/flowy_svgs.g.dart';
import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/database/application/cell/bloc/url_cell_bloc.dart'; import 'package:appflowy/plugins/database/application/cell/bloc/url_cell_bloc.dart';
@ -177,21 +176,11 @@ class _VisitURLAccessoryState extends State<_VisitURLAccessory>
} }
@override @override
bool enable() { bool enable() => widget.cellDataNotifier.value.isNotEmpty;
return widget.cellDataNotifier.value.isNotEmpty;
}
@override @override
void onTap() { void onTap() =>
final content = widget.cellDataNotifier.value; openUrlCellLink(widget.cellDataNotifier.value);
if (content.isEmpty) {
return;
}
final shouldAddScheme =
!['http', 'https'].any((pattern) => content.startsWith(pattern));
final url = shouldAddScheme ? 'http://$content' : content;
afLaunchUrlString(url);
}
} }
class _URLAccessoryIconContainer extends StatelessWidget { class _URLAccessoryIconContainer extends StatelessWidget {

View File

@ -1,5 +1,6 @@
import 'dart:async'; import 'dart:async';
import 'package:appflowy/core/helpers/url_launcher.dart';
import 'package:appflowy/plugins/database/application/cell/cell_controller.dart'; import 'package:appflowy/plugins/database/application/cell/cell_controller.dart';
import 'package:appflowy/plugins/database/application/cell/cell_controller_builder.dart'; import 'package:appflowy/plugins/database/application/cell/cell_controller_builder.dart';
import 'package:appflowy/plugins/database/application/database_controller.dart'; import 'package:appflowy/plugins/database/application/database_controller.dart';
@ -15,6 +16,9 @@ import '../desktop_row_detail/desktop_row_detail_url_cell.dart';
import '../mobile_grid/mobile_grid_url_cell.dart'; import '../mobile_grid/mobile_grid_url_cell.dart';
import '../mobile_row_detail/mobile_row_detail_url_cell.dart'; import '../mobile_row_detail/mobile_row_detail_url_cell.dart';
const regexUrl =
r"[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:._\+-~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:_\+.~#?&\/\/=]*)";
abstract class IEditableURLCellSkin { abstract class IEditableURLCellSkin {
const IEditableURLCellSkin(); const IEditableURLCellSkin();
@ -130,3 +134,24 @@ class _GridURLCellState extends GridEditableTextCell<EditableURLCell> {
@override @override
String? onCopy() => cellBloc.state.content; String? onCopy() => cellBloc.state.content;
} }
void openUrlCellLink(String content) {
if (RegExp(regexUrl).hasMatch(content)) {
const linkPrefix = [
'http://',
'https://',
'file://',
'ftp://',
'ftps://',
'mailto:',
];
final shouldAddScheme =
!linkPrefix.any((pattern) => content.startsWith(pattern));
final url = shouldAddScheme ? 'https://$content' : content;
afLaunchUrlString(url);
} else {
afLaunchUrlString(
"https://www.google.com/search?q=${Uri.encodeComponent(content)}",
);
}
}