From 954ae8f76147128369f933ba8bbd7db95a640e6e Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Mon, 11 Sep 2023 21:28:10 +0700 Subject: [PATCH] fix: unable to paste html contains section --- .../command_shortcut_events/paste_command.dart | 15 +++++++++++---- lib/src/plugins/html/html_document_decoder.dart | 4 +++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/paste_command.dart b/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/paste_command.dart index 4fb91b46d..a81b21e76 100644 --- a/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/paste_command.dart +++ b/lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/paste_command.dart @@ -72,8 +72,14 @@ CommandShortcutEventHandler _pasteCommandHandler = (editorState) { final html = data.html; if (html != null && html.isNotEmpty) { await editorState.deleteSelectionIfNeeded(); - editorState.pasteHtml(html); - } else if (text != null && text.isNotEmpty) { + // if the html is pasted successfully, then return + // otherwise, paste the plain text + if (await editorState.pasteHtml(html)) { + return; + } + } + + if (text != null && text.isNotEmpty) { await editorState.deleteSelectionIfNeeded(); editorState.pastePlainText(text); } @@ -87,7 +93,7 @@ RegExp _hrefRegex = RegExp( ); extension on EditorState { - Future pasteHtml(String html) async { + Future pasteHtml(String html) async { final nodes = htmlToDocument(html).root.children.toList(); // remove the front and back empty line while (nodes.isNotEmpty && nodes.first.delta?.isEmpty == true) { @@ -97,13 +103,14 @@ extension on EditorState { nodes.removeLast(); } if (nodes.isEmpty) { - return; + return false; } if (nodes.length == 1) { await pasteSingleLineNode(nodes.first); } else { await pasteMultiLineNodes(nodes.toList()); } + return true; } Future pastePlainText(String plainText) async { diff --git a/lib/src/plugins/html/html_document_decoder.dart b/lib/src/plugins/html/html_document_decoder.dart index b8ef5d333..6545466fa 100644 --- a/lib/src/plugins/html/html_document_decoder.dart +++ b/lib/src/plugins/html/html_document_decoder.dart @@ -360,6 +360,7 @@ class HTMLTags { static const blockQuote = 'blockquote'; static const div = 'div'; static const divider = 'hr'; + static const section = 'section'; static List formattingElements = [ HTMLTags.anchor, @@ -385,7 +386,8 @@ class HTMLTags { HTMLTags.paragraph, HTMLTags.blockQuote, HTMLTags.checkbox, - HTMLTags.image + HTMLTags.image, + HTMLTags.section, ]; static bool isTopLevel(String tag) {