Skip to content

Add link support #520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/zefyr/lib/src/widgets/buttons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'scope.dart';
import 'theme.dart';
import 'toolbar.dart';

const List<String> _kSchemes = ['http', 'https', 'tel', 'whatsapp', 'mailto'];

/// A button used in [ZefyrToolbar].
///
/// Create an instance of this widget with [ZefyrButton.icon] or
Expand Down Expand Up @@ -361,10 +363,10 @@ class _LinkButtonState extends State<LinkButton> {
if (_inputController.text.isNotEmpty) {
try {
var uri = Uri.parse(_inputController.text);
if ((uri.isScheme('https') || uri.isScheme('http')) &&
uri.host.isNotEmpty) {
if (_kSchemes.contains(uri.scheme) && uri.host.isNotEmpty) {
toolbar.editor.formatSelection(
NotusAttribute.link.fromString(_inputController.text));
NotusAttribute.link.fromString(_inputController.text),
);
} else {
error = true;
}
Expand Down
27 changes: 27 additions & 0 deletions packages/zefyr/lib/src/widgets/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/gestures.dart';
import 'package:notus/notus.dart';
import 'package:url_launcher/url_launcher.dart';

import 'editable_box.dart';
import 'horizontal_rule.dart';
Expand Down Expand Up @@ -36,6 +38,13 @@ class ZefyrLine extends StatefulWidget {

class _ZefyrLineState extends State<ZefyrLine> {
final LayerLink _link = LayerLink();
TapGestureRecognizer _recognizer;

@override
void dispose() {
_recognizer?.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -131,6 +140,24 @@ class _ZefyrLineState extends State<ZefyrLine> {
final TextNode segment = node;
final attrs = segment.style;

if (attrs.contains(NotusAttribute.link)) {
_recognizer ??= TapGestureRecognizer();

_recognizer.onTap = () async {
final url = attrs.value(NotusAttribute.link);

await canLaunch(url).then((isOk) {
if (isOk) launch(url);
});
};

return TextSpan(
text: segment.value,
style: _getTextStyle(attrs, theme),
recognizer: _recognizer,
);
}

return TextSpan(
text: segment.value,
style: _getTextStyle(attrs, theme),
Expand Down