From 01271b0cd974f1c6d31effac183ec59627d37b9d Mon Sep 17 00:00:00 2001 From: spChief Date: Fri, 7 Apr 2023 10:22:31 +0700 Subject: [PATCH] customLinkPrefixes parameter - makes possible to open links with custom protocol (deeplinks) (#1164) --- lib/src/widgets/editor.dart | 8 ++++++++ lib/src/widgets/raw_editor.dart | 8 ++++++-- lib/src/widgets/text_block.dart | 3 +++ lib/src/widgets/text_line.dart | 4 +++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/src/widgets/editor.dart b/lib/src/widgets/editor.dart index e733c0e0..d808552d 100644 --- a/lib/src/widgets/editor.dart +++ b/lib/src/widgets/editor.dart @@ -182,6 +182,7 @@ class QuillEditor extends StatefulWidget { this.customShortcuts, this.customActions, this.detectWordBoundary = true, + this.customLinkPrefixes = const [], Key? key}) : super(key: key); @@ -401,6 +402,12 @@ class QuillEditor extends StatefulWidget { final bool detectWordBoundary; + /// Additional list if links prefixes, which must not be prepended + /// with "https://" when [LinkMenuAction.launch] happened + /// + /// Useful for deeplinks + final List customLinkPrefixes; + @override QuillEditorState createState() => QuillEditorState(); } @@ -498,6 +505,7 @@ class QuillEditorState extends State onImagePaste: widget.onImagePaste, customShortcuts: widget.customShortcuts, customActions: widget.customActions, + customLinkPrefixes: widget.customLinkPrefixes, ); final editor = I18n( diff --git a/lib/src/widgets/raw_editor.dart b/lib/src/widgets/raw_editor.dart index f27fd604..36af8938 100644 --- a/lib/src/widgets/raw_editor.dart +++ b/lib/src/widgets/raw_editor.dart @@ -78,7 +78,8 @@ class RawEditor extends StatefulWidget { this.linkActionPickerDelegate = defaultLinkActionPickerDelegate, this.customStyleBuilder, this.floatingCursorDisabled = false, - this.onImagePaste}) + this.onImagePaste, + this.customLinkPrefixes = const []}) : assert(maxHeight == null || maxHeight > 0, 'maxHeight cannot be null'), assert(minHeight == null || minHeight >= 0, 'minHeight cannot be null'), assert(maxHeight == null || minHeight == null || maxHeight >= minHeight, @@ -250,6 +251,7 @@ class RawEditor extends StatefulWidget { final LinkActionPickerDelegate linkActionPickerDelegate; final CustomStyleBuilder? customStyleBuilder; final bool floatingCursorDisabled; + final List customLinkPrefixes; @override State createState() => RawEditorState(); @@ -800,7 +802,8 @@ class RawEditorState extends EditorState clearIndents: clearIndents, onCheckboxTap: _handleCheckboxTap, readOnly: widget.readOnly, - customStyleBuilder: widget.customStyleBuilder); + customStyleBuilder: widget.customStyleBuilder, + customLinkPrefixes: widget.customLinkPrefixes); result.add(Directionality( textDirection: getDirectionOfNode(node), child: editableTextBlock)); @@ -824,6 +827,7 @@ class RawEditorState extends EditorState controller: controller, linkActionPicker: _linkActionPicker, onLaunchUrl: widget.onLaunchUrl, + customLinkPrefixes: widget.customLinkPrefixes, ); final editableTextLine = EditableTextLine( node, diff --git a/lib/src/widgets/text_block.dart b/lib/src/widgets/text_block.dart index 09b2af7c..b61ad9cf 100644 --- a/lib/src/widgets/text_block.dart +++ b/lib/src/widgets/text_block.dart @@ -73,6 +73,7 @@ class EditableTextBlock extends StatelessWidget { required this.readOnly, this.onLaunchUrl, this.customStyleBuilder, + this.customLinkPrefixes = const [], Key? key}); final Block block; @@ -95,6 +96,7 @@ class EditableTextBlock extends StatelessWidget { final bool clearIndents; final Function(int, bool) onCheckboxTap; final bool readOnly; + final List customLinkPrefixes; @override Widget build(BuildContext context) { @@ -148,6 +150,7 @@ class EditableTextBlock extends StatelessWidget { controller: controller, linkActionPicker: linkActionPicker, onLaunchUrl: onLaunchUrl, + customLinkPrefixes: customLinkPrefixes, ), _getIndentWidth(), _getSpacingForLine(line, index, count, defaultStyles), diff --git a/lib/src/widgets/text_line.dart b/lib/src/widgets/text_line.dart index b7174a2f..22623f4d 100644 --- a/lib/src/widgets/text_line.dart +++ b/lib/src/widgets/text_line.dart @@ -41,6 +41,7 @@ class TextLine extends StatefulWidget { required this.linkActionPicker, this.textDirection, this.customStyleBuilder, + this.customLinkPrefixes = const [], Key? key, }) : super(key: key); @@ -53,6 +54,7 @@ class TextLine extends StatefulWidget { final CustomStyleBuilder? customStyleBuilder; final ValueChanged? onLaunchUrl; final LinkActionPicker linkActionPicker; + final List customLinkPrefixes; @override State createState() => _TextLineState(); @@ -430,7 +432,7 @@ class _TextLineState extends State { launchUrl ??= _launchUrl; link = link.trim(); - if (!linkPrefixes + if (!(widget.customLinkPrefixes + linkPrefixes) .any((linkPrefix) => link!.toLowerCase().startsWith(linkPrefix))) { link = 'https://$link'; }