Skip to content

Commit

Permalink
feat: adding an ability to have a link check before embedding (#603)
Browse files Browse the repository at this point in the history
* feat: adding an ability to have a link check before embedding

* feat: remove url validator and replace with isURL

---------

Co-authored-by: Johan Sutrisno <[email protected]>
  • Loading branch information
johansutrisno and johanzeroone authored Jan 1, 2024
1 parent 87068d1 commit ada2a31
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
12 changes: 10 additions & 2 deletions lib/src/editor/toolbar/desktop/items/link/link_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -90,11 +91,12 @@ class _LinkMenuState extends State<LinkMenu> {
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),
Expand All @@ -113,6 +115,12 @@ class _LinkMenuState extends State<LinkMenu> {
borderRadius: BorderRadius.all(Radius.circular(12.0)),
),
),
validator: (value) {
if (value == null || value.isEmpty || !isURL(value)) {
return AppFlowyEditorL10n.current.incorrectLink;
}
return null;
},
),
);
}
Expand Down
11 changes: 7 additions & 4 deletions lib/src/editor/toolbar/desktop/items/link/link_toolbar_item.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ada2a31

Please sign in to comment.