From 0abf916796c5fff1f4f5c3e624ae74a170d14aa6 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Fri, 2 Aug 2024 22:00:40 +0800 Subject: [PATCH] feat: support pasting image from Slack (#5864) --- .../document_copy_and_paste_test.dart | 7 +---- .../copy_and_paste/custom_paste_command.dart | 28 ++++++++++++------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/frontend/appflowy_flutter/integration_test/desktop/document/document_copy_and_paste_test.dart b/frontend/appflowy_flutter/integration_test/desktop/document/document_copy_and_paste_test.dart index 730d776460..9fe8d37070 100644 --- a/frontend/appflowy_flutter/integration_test/desktop/document/document_copy_and_paste_test.dart +++ b/frontend/appflowy_flutter/integration_test/desktop/document/document_copy_and_paste_test.dart @@ -1,11 +1,10 @@ import 'dart:io'; -import 'package:flutter/services.dart'; - import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/clipboard_service.dart'; import 'package:appflowy/startup/startup.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; @@ -246,10 +245,6 @@ void main() { expect(editorState.document.root.children.length, 2); final node = editorState.getNodeAtPath([0])!; expect(node.type, ImageBlockKeys.type); - expect( - node.attributes[ImageBlockKeys.url], - 'https://user-images.githubusercontent.com/9403740/262918875-603f4adb-58dd-49b5-8201-341d354935fd.png', - ); }, ); }, diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/custom_paste_command.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/custom_paste_command.dart index 53812fbfe4..e81cf2f325 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/custom_paste_command.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/custom_paste_command.dart @@ -1,5 +1,3 @@ -import 'package:flutter/material.dart'; - import 'package:appflowy/plugins/document/application/document_bloc.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/clipboard_service.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/editor_state_paste_node_extension.dart'; @@ -8,8 +6,10 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_p import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_in_app_json.dart'; import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_plain_text.dart'; import 'package:appflowy/startup/startup.dart'; -import 'package:appflowy_editor/appflowy_editor.dart'; +import 'package:appflowy_backend/log.dart'; +import 'package:appflowy_editor/appflowy_editor.dart' hide Log; import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart'; +import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:string_validator/string_validator.dart'; @@ -43,6 +43,7 @@ CommandShortcutEventHandler _pasteCommandHandler = (editorState) { // paste as link preview if (await _pasteAsLinkPreview(editorState, plainText)) { + Log.info('Pasted as link preview'); return; } @@ -56,17 +57,14 @@ CommandShortcutEventHandler _pasteCommandHandler = (editorState) { if (inAppJson != null && inAppJson.isNotEmpty) { await editorState.deleteSelectionIfNeeded(); if (await editorState.pasteInAppJson(inAppJson)) { + Log.info('Pasted in app json'); return; } } - if (html != null && html.isNotEmpty) { - await editorState.deleteSelectionIfNeeded(); - if (await editorState.pasteHtml(html)) { - return; - } - } - + // if the image data is not null, we should handle it first + // because the image URL in the HTML may not be reachable due to permission issues + // For example, when pasting an image from Slack, the image URL provided is not public. if (image != null && image.$2?.isNotEmpty == true) { final documentBloc = editorState.document.root.context?.read(); @@ -81,11 +79,21 @@ CommandShortcutEventHandler _pasteCommandHandler = (editorState) { documentId, ); if (result) { + Log.info('Pasted image'); + return; + } + } + + if (html != null && html.isNotEmpty) { + await editorState.deleteSelectionIfNeeded(); + if (await editorState.pasteHtml(html)) { + Log.info('Pasted html'); return; } } if (plainText != null && plainText.isNotEmpty) { + Log.info('Pasted plain text'); await editorState.pastePlainText(plainText); } }();