Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unable to paste html contains section #448

Merged
merged 1 commit into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@
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)) {

Check warning on line 77 in lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/paste_command.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/paste_command.dart#L77

Added line #L77 was not covered by tests
return;
}
}

if (text != null && text.isNotEmpty) {
await editorState.deleteSelectionIfNeeded();
editorState.pastePlainText(text);
}
Expand All @@ -87,7 +93,7 @@
);

extension on EditorState {
Future<void> pasteHtml(String html) async {
Future<bool> pasteHtml(String html) async {

Check warning on line 96 in lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/paste_command.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/editor/editor_component/service/shortcuts/command_shortcut_events/paste_command.dart#L96

Added line #L96 was not covered by tests
final nodes = htmlToDocument(html).root.children.toList();
// remove the front and back empty line
while (nodes.isNotEmpty && nodes.first.delta?.isEmpty == true) {
Expand All @@ -97,13 +103,14 @@
nodes.removeLast();
}
if (nodes.isEmpty) {
return;
return false;
}
if (nodes.length == 1) {
await pasteSingleLineNode(nodes.first);
} else {
await pasteMultiLineNodes(nodes.toList());
}
return true;
}

Future<void> pastePlainText(String plainText) async {
Expand Down
4 changes: 3 additions & 1 deletion lib/src/plugins/html/html_document_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ class HTMLTags {
static const blockQuote = 'blockquote';
static const div = 'div';
static const divider = 'hr';
static const section = 'section';

static List<String> formattingElements = [
HTMLTags.anchor,
Expand All @@ -385,7 +386,8 @@ class HTMLTags {
HTMLTags.paragraph,
HTMLTags.blockQuote,
HTMLTags.checkbox,
HTMLTags.image
HTMLTags.image,
HTMLTags.section,
];

static bool isTopLevel(String tag) {
Expand Down