diff --git a/lib/src/editor/toolbar/desktop/items/link/link_menu.dart b/lib/src/editor/toolbar/desktop/items/link/link_menu.dart index 74ab53c8a..bafd25ee7 100644 --- a/lib/src/editor/toolbar/desktop/items/link/link_menu.dart +++ b/lib/src/editor/toolbar/desktop/items/link/link_menu.dart @@ -2,6 +2,7 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/editor/toolbar/desktop/items/utils/overlay_util.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:string_validator/string_validator.dart'; class LinkMenu extends StatefulWidget { const LinkMenu({ @@ -90,11 +91,12 @@ class _LinkMenuState extends State { widget.onDismiss(); } }, - child: TextField( + child: TextFormField( + autovalidateMode: AutovalidateMode.onUserInteraction, focusNode: _focusNode, textAlign: TextAlign.left, controller: _textEditingController, - onSubmitted: widget.onSubmitted, + onFieldSubmitted: widget.onSubmitted, decoration: InputDecoration( hintText: AppFlowyEditorL10n.current.urlHint, contentPadding: const EdgeInsets.all(16.0), @@ -113,6 +115,12 @@ class _LinkMenuState extends State { borderRadius: BorderRadius.all(Radius.circular(12.0)), ), ), + validator: (value) { + if (value == null || value.isEmpty || !isURL(value)) { + return AppFlowyEditorL10n.current.incorrectLink; + } + return null; + }, ), ); } diff --git a/lib/src/editor/toolbar/desktop/items/link/link_toolbar_item.dart b/lib/src/editor/toolbar/desktop/items/link/link_toolbar_item.dart index 1cba695ba..ac95a502c 100644 --- a/lib/src/editor/toolbar/desktop/items/link/link_toolbar_item.dart +++ b/lib/src/editor/toolbar/desktop/items/link/link_toolbar_item.dart @@ -1,6 +1,7 @@ import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor/src/editor/toolbar/desktop/items/link/link_menu.dart'; import 'package:flutter/material.dart'; +import 'package:string_validator/string_validator.dart'; const _menuWidth = 300; const _hasTextHeight = 244; @@ -83,10 +84,12 @@ void showLinkMenu( await safeLaunchUrl(linkText); }, onSubmitted: (text) async { - await editorState.formatDelta(selection, { - BuiltInAttributeKey.href: text, - }); - dismissOverlay(); + if (isURL(text)) { + await editorState.formatDelta(selection, { + BuiltInAttributeKey.href: text, + }); + dismissOverlay(); + } }, onCopyLink: () { AppFlowyClipboard.setData(text: linkText);