From 96ececbebed89f19adcd4fd0e0132c0df0d7cd20 Mon Sep 17 00:00:00 2001 From: Xin Yao Date: Sun, 28 Feb 2021 02:31:17 -0800 Subject: [PATCH] Improve link handling for tel, mailto and etc (#47) --- CHANGELOG.md | 5 ++++- lib/widgets/editor.dart | 22 ++++++++++++---------- pubspec.yaml | 2 +- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f23d572..a47fbff6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -102,4 +102,7 @@ * Fix cursor focus issue when keyboard is on. ## [0.3.3] -* More fix on cursor focus issue when keyboard is on. \ No newline at end of file +* More fix on cursor focus issue when keyboard is on. + +## [0.3.4] +* Improve link handling for tel, mailto and etc. \ No newline at end of file diff --git a/lib/widgets/editor.dart b/lib/widgets/editor.dart index 14d81146..5c87b4e6 100644 --- a/lib/widgets/editor.dart +++ b/lib/widgets/editor.dart @@ -31,8 +31,12 @@ import 'cursor.dart'; import 'default_styles.dart'; import 'delegate.dart'; -const urlPattern = - r"^((https?|http)://)?([-A-Z0-9.]+)(/[-A-Z0-9+&@#/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#/%=~_|!:‌​,.;]*)?$"; +const linkPrefixes = [ + 'mailto:', // email + 'tel:', // telephone + 'sms:', // SMS + 'http' +]; abstract class EditorState extends State { TextEditingValue getTextEditingValue(); @@ -317,8 +321,6 @@ class _QuillEditorState extends State class _QuillEditorSelectionGestureDetectorBuilder extends EditorTextSelectionGestureDetectorBuilder { - static final urlRegExp = new RegExp(urlPattern, caseSensitive: false); - final _QuillEditorState _state; _QuillEditorSelectionGestureDetectorBuilder(this._state) : super(_state); @@ -394,9 +396,12 @@ class _QuillEditorSelectionGestureDetectorBuilder launchUrl = _launchUrl; } String link = segment.style.attributes[Attribute.link.key].value; - if (getEditor().widget.readOnly && - link != null && - urlRegExp.firstMatch(link.trim()) != null) { + if (getEditor().widget.readOnly && link != null) { + link = link.trim(); + if (!linkPrefixes + .any((linkPrefix) => link.toLowerCase().startsWith(linkPrefix))) { + link = 'https://$link'; + } launchUrl(link); } return false; @@ -452,9 +457,6 @@ class _QuillEditorSelectionGestureDetectorBuilder } void _launchUrl(String url) async { - if (!url.startsWith('http')) { - url = 'https://$url'; - } await launch(url); } diff --git a/pubspec.yaml b/pubspec.yaml index d6a5a3d0..844a833d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_quill description: One client and affiliated collaborator of Flutter Quill is Bullet Journal App. -version: 0.3.3 +version: 0.3.4 #author: bulletjournal homepage: https://bulletjournal.us/home/index.html repository: https://github.com/singerdmx/flutter-quill.git