mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: format the selected text to url if available (#3245)
This commit is contained in:
@ -53,7 +53,6 @@ CommandShortcutEventHandler _pasteCommandHandler = (editorState) {
|
||||
await editorState.deleteSelectionIfNeeded();
|
||||
await editorState.pasteImage(image.$1, image.$2!);
|
||||
} else if (plainText != null && plainText.isNotEmpty) {
|
||||
await editorState.deleteSelectionIfNeeded();
|
||||
await editorState.pastePlainText(plainText);
|
||||
}
|
||||
}();
|
||||
|
@ -7,6 +7,12 @@ RegExp _hrefRegex = RegExp(
|
||||
|
||||
extension PasteFromPlainText on EditorState {
|
||||
Future<void> pastePlainText(String plainText) async {
|
||||
if (await pasteHtmlIfAvailable(plainText)) {
|
||||
return;
|
||||
}
|
||||
|
||||
await deleteSelectionIfNeeded();
|
||||
|
||||
final nodes = plainText
|
||||
.split('\n')
|
||||
.map(
|
||||
@ -33,4 +39,26 @@ extension PasteFromPlainText on EditorState {
|
||||
await pasteMultiLineNodes(nodes.toList());
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> pasteHtmlIfAvailable(String plainText) async {
|
||||
final selection = this.selection;
|
||||
if (selection == null ||
|
||||
!selection.isSingle ||
|
||||
selection.isCollapsed ||
|
||||
!_hrefRegex.hasMatch(plainText)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final node = getNodeAtPath(selection.start.path);
|
||||
if (node == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final transaction = this.transaction;
|
||||
transaction.formatText(node, selection.startIndex, selection.length, {
|
||||
AppFlowyRichTextKeys.href: plainText,
|
||||
});
|
||||
await apply(transaction);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user