forked from AppFlowy-IO/appflowy-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* main: feat: table navigation using TAB key (AppFlowy-IO#627) feat: add markdown link syntax formatting (AppFlowy-IO#618) fix:text_decoration_mobile_toolbar_padding (AppFlowy-IO#621) fix: active hover on upload image (AppFlowy-IO#597) feat: adding an ability to have a link check before embedding (AppFlowy-IO#603) fix: node_iterator toList encounter Dangling Node trigger dead loop. (AppFlowy-IO#623)
- Loading branch information
Showing
11 changed files
with
639 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...src/editor/editor_component/service/shortcuts/character/markdown_link_shortcut_event.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import 'package:appflowy_editor/appflowy_editor.dart'; | ||
|
||
/// format the markdown link syntax to hyperlink | ||
final CharacterShortcutEvent formatMarkdownLinkToLink = CharacterShortcutEvent( | ||
key: 'format the text surrounded by double asterisks to bold', | ||
character: ')', | ||
handler: (editorState) async => handleFormatMarkdownLinkToLink( | ||
editorState: editorState, | ||
), | ||
); | ||
|
||
final _linkRegex = RegExp(r'\[([^\]]*)\]\((.*?)\)'); | ||
|
||
bool handleFormatMarkdownLinkToLink({ | ||
required EditorState editorState, | ||
}) { | ||
final selection = editorState.selection; | ||
// if the selection is not collapsed or the cursor is at the first 5 index range, we don't need to format it. | ||
// we should return false to let the IME handle it. | ||
if (selection == null || !selection.isCollapsed || selection.end.offset < 6) { | ||
return false; | ||
} | ||
|
||
final path = selection.end.path; | ||
final node = editorState.getNodeAtPath(path); | ||
final delta = node?.delta; | ||
// if the node doesn't contain the delta(which means it isn't a text) | ||
// we don't need to format it. | ||
if (node == null || delta == null) { | ||
return false; | ||
} | ||
|
||
final plainText = '${delta.toPlainText()})'; | ||
|
||
// Determine if regex matches the plainText. | ||
if (!_linkRegex.hasMatch(plainText)) { | ||
return false; | ||
} | ||
|
||
final matches = _linkRegex.allMatches(plainText); | ||
final lastMatch = matches.last; | ||
final title = lastMatch.group(1); | ||
final link = lastMatch.group(2); | ||
|
||
// if all the conditions are met, we should format the text to a link. | ||
final transaction = editorState.transaction | ||
..deleteText( | ||
node, | ||
lastMatch.start, | ||
lastMatch.end - lastMatch.start - 1, | ||
) | ||
..insertText( | ||
node, | ||
lastMatch.start, | ||
title!, | ||
attributes: { | ||
AppFlowyRichTextKeys.href: link, | ||
}, | ||
); | ||
editorState.apply(transaction); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.