Bring projects, wikis, and teams together with AI. AppFlowy is the AI collaborative workspace where you achieve more without losing control of your data. The leading open source Notion alternative.
Dart blogconfluence-alternativecontent-managementcontent-servicesdocumentationflutternote-takingnotion-alternativeproject-managementtask-managementteam-collaborationwiki
fix: paste doesn't work sometimes in the first try (#6412)
Lucas99c81fed5689
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.dartindex 1fa38c09ea..9bd6a007f9 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@@ -226,15 +226,14 @@ void main() {});testWidgets('paste image(gif) from memory', (tester) async {- // It's not supported yet.- // final image = await rootBundle.load('assets/test/images/sample.gif');- // final bytes = image.buffer.asUint8List();- // await tester.pasteContent(image: ('gif', bytes), (editorState) {- // expect(editorState.document.root.children.length, 2);- // final node = editorState.getNodeAtPath([0])!;- // expect(node.type, ImageBlockKeys.type);- // expect(node.attributes[ImageBlockKeys.url], isNotNull);- // });+ final image = await rootBundle.load('assets/test/images/sample.gif');+ final bytes = image.buffer.asUint8List();+ await tester.pasteContent(image: ('gif', bytes), (editorState) {+ expect(editorState.document.root.children.length, 1);+ final node = editorState.getNodeAtPath([0])!;+ expect(node.type, ImageBlockKeys.type);+ expect(node.attributes[ImageBlockKeys.url], isNotNull);+ });});testWidgets(@@ -321,6 +320,31 @@ void main() {});},);++ testWidgets('paste the url without protocol', (tester) async {+ // paste the image that from local file+ const plainText = '1.jpg';+ final image = await rootBundle.load('assets/test/images/sample.jpeg');+ final bytes = image.buffer.asUint8List();+ await tester.pasteContent(plainText: plainText, image: ('jpeg', bytes),+ (editorState) {+ final node = editorState.getNodeAtPath([0])!;+ expect(node.type, ImageBlockKeys.type);+ expect(node.attributes[ImageBlockKeys.url], isNotEmpty);+ });+ });++ testWidgets('paste the image url', (tester) async {+ const plainText = 'https://appflowy.io/1.jpg';+ final image = await rootBundle.load('assets/test/images/sample.jpeg');+ final bytes = image.buffer.asUint8List();+ await tester.pasteContent(plainText: plainText, image: ('jpeg', bytes),+ (editorState) {+ final node = editorState.getNodeAtPath([0])!;+ expect(node.type, ImageBlockKeys.type);+ expect(node.attributes[ImageBlockKeys.url], isNotEmpty);+ });+ });}extension on WidgetTester {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.dartindex 5dd9a4b49a..d8cd2f62df 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@@ -5,6 +5,7 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_pimport '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/util/default_extensions.dart';import 'package:appflowy_backend/log.dart';import 'package:appflowy_editor/appflowy_editor.dart';import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';@@ -40,6 +41,12 @@ CommandShortcutEventHandler _pasteCommandHandler = (editorState) {final plainText = data.plainText;final image = data.image;+ // dump the length of the data here, don't log the data itself for privacy concerns+ Log.info('paste command: inAppJson: ${inAppJson?.length}');+ Log.info('paste command: html: ${html?.length}');+ Log.info('paste command: plainText: ${plainText?.length}');+ Log.info('paste command: image: ${image?.$2?.length}');+// paste as link previewif (await _pasteAsLinkPreview(editorState, plainText)) {Log.info('Pasted as link preview');@@ -54,7 +61,6 @@ CommandShortcutEventHandler _pasteCommandHandler = (editorState) {// try to paste the content in order, if any of them is failed, then try the next oneif (inAppJson != null && inAppJson.isNotEmpty) {- await editorState.deleteSelectionIfNeeded();if (await editorState.pasteInAppJson(inAppJson)) {Log.info('Pasted in app json');return;@@ -95,7 +101,10 @@ CommandShortcutEventHandler _pasteCommandHandler = (editorState) {if (plainText != null && plainText.isNotEmpty) {Log.info('Pasted plain text');await editorState.pastePlainText(plainText);+ return;}++ Log.info('unable to parse the clipboard content');}();return KeyEventResult.handled;@@ -105,7 +114,11 @@ Future<bool> _pasteAsLinkPreview(EditorState editorState,String? text,) async {- if (text == null || !isURL(text)) {+ // 1. the url should contains a protocol+ // 2. the url should not be an image url+ if (text == null ||+ !isURL(text, {'require_protocol': true}) ||+ text.isImageUrl()) {return false;}diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_in_app_json.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_in_app_json.dartindex 355dae2123..70948cd962 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_in_app_json.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_in_app_json.dart@@ -8,11 +8,14 @@ extension PasteFromInAppJson on EditorState {try {final nodes = Document.fromJson(jsonDecode(inAppJson)).root.children;if (nodes.isEmpty) {+ Log.info('pasteInAppJson: nodes is empty');return false;}if (nodes.length == 1) {+ Log.info('pasteInAppJson: single line node');await pasteSingleLineNode(nodes.first);} else {+ Log.info('pasteInAppJson: multi line nodes');await pasteMultiLineNodes(nodes.toList());}return true;diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/custom_image_block_component/custom_image_block_component.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/custom_image_block_component/custom_image_block_component.dartindex 55a2893d0f..ccba9b3f2e 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/custom_image_block_component/custom_image_block_component.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/custom_image_block_component/custom_image_block_component.dart@@ -10,8 +10,8 @@ import 'package:appflowy/plugins/document/presentation/editor_plugins/image/custimport 'package:appflowy/plugins/document/presentation/editor_plugins/image/image_placeholder.dart';import 'package:appflowy/plugins/document/presentation/editor_plugins/image/resizeable_image.dart';import 'package:appflowy/startup/startup.dart';-import 'package:appflowy/util/string_extension.dart';import 'package:appflowy/workspace/presentation/home/toast.dart';+import 'package:appflowy/workspace/presentation/widgets/dialogs.dart';import 'package:appflowy/workspace/presentation/widgets/image_viewer/image_provider.dart';import 'package:appflowy/workspace/presentation/widgets/image_viewer/interactive_image_viewer.dart';import 'package:appflowy_editor/appflowy_editor.dart' hide ResizableImage;@@ -206,6 +206,14 @@ class CustomImageBlockComponentState extends State<CustomImageBlockComponent>);}+ child = Padding(+ padding: padding,+ child: RepaintBoundary(+ key: imageKey,+ child: child,+ ),+ );+if (UniversalPlatform.isDesktopOrWeb) {child = BlockSelectionContainer(node: node,@@ -213,10 +221,8 @@ class CustomImageBlockComponentState extends State<CustomImageBlockComponent>listenable: editorState.selectionNotifier,blockColor: editorState.editorStyle.selectionColor,supportTypes: const [BlockSelectionType.block],- child: Padding(key: imageKey, padding: padding, child: child),+ child: child,);- } else {- child = Padding(key: imageKey, padding: padding, child: child);}if (widget.showActions && widget.actionBuilder != null) {@@ -352,24 +358,21 @@ class CustomImageBlockComponentState extends State<CustomImageBlockComponent>}return [- // disable the copy link button if the image is hosted on appflowy cloud- // because the url needs the verification token to be accessible- if (!url.isAppFlowyCloudUrl)- FlowyOptionTile.text(- showTopBorder: false,- text: LocaleKeys.editor_copyLink.tr(),- leftIcon: const FlowySvg(- FlowySvgs.m_field_copy_s,- ),- onTap: () async {- context.pop();- showSnackBarMessage(- context,- LocaleKeys.document_plugins_image_copiedToPasteBoard.tr(),- );- await getIt<ClipboardService>().setPlainText(url);- },+ FlowyOptionTile.text(+ showTopBorder: false,+ text: LocaleKeys.editor_copy.tr(),+ leftIcon: const FlowySvg(+ FlowySvgs.m_field_copy_s,),+ onTap: () async {+ context.pop();+ showToastNotification(+ context,+ message: LocaleKeys.document_plugins_image_copiedToPasteBoard.tr(),+ );+ await getIt<ClipboardService>().setPlainText(url);+ },+ ),FlowyOptionTile.text(showTopBorder: false,text: LocaleKeys.document_imageBlock_saveImageToGallery.tr(),diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/custom_image_block_component/image_menu.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/custom_image_block_component/image_menu.dartindex 06028b6e63..e5b67435ba 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/custom_image_block_component/image_menu.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/image/custom_image_block_component/image_menu.dart@@ -1,16 +1,16 @@-import 'package:flutter/material.dart';-import 'package:flutter/services.dart';+import 'dart:ui';import 'package:appflowy/generated/flowy_svgs.g.dart';import 'package:appflowy/generated/locale_keys.g.dart';import 'package:appflowy/plugins/document/application/document_bloc.dart';import 'package:appflowy/plugins/document/presentation/editor_plugins/align_toolbar_item/align_toolbar_item.dart';import 'package:appflowy/plugins/document/presentation/editor_plugins/block_menu/block_menu_button.dart';+import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/clipboard_service.dart';import 'package:appflowy/plugins/document/presentation/editor_plugins/image/common.dart';import 'package:appflowy/plugins/document/presentation/editor_plugins/image/custom_image_block_component/custom_image_block_component.dart';import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';-import 'package:appflowy/util/string_extension.dart';-import 'package:appflowy/workspace/presentation/home/toast.dart';+import 'package:appflowy/startup/startup.dart';+import 'package:appflowy/workspace/presentation/widgets/dialogs.dart';import 'package:appflowy/workspace/presentation/widgets/image_viewer/image_provider.dart';import 'package:appflowy/workspace/presentation/widgets/image_viewer/interactive_image_viewer.dart';import 'package:appflowy_editor/appflowy_editor.dart';@@ -18,10 +18,18 @@ import 'package:appflowy_popover/appflowy_popover.dart';import 'package:easy_localization/easy_localization.dart';import 'package:flowy_infra_ui/flowy_infra_ui.dart';import 'package:flowy_infra_ui/widget/ignore_parent_gesture.dart';+import 'package:flutter/material.dart';+import 'package:flutter/rendering.dart';+import 'package:flutter/services.dart';import 'package:provider/provider.dart';+import 'package:toastification/toastification.dart';class ImageMenu extends StatefulWidget {- const ImageMenu({super.key, required this.node, required this.state});+ const ImageMenu({+ super.key,+ required this.node,+ required this.state,+ });final Node node;final CustomImageBlockComponentState state;@@ -58,16 +66,12 @@ class _ImageMenuState extends State<ImageMenu> {onTap: openFullScreen,),const HSpace(4),- // disable the copy link button if the image is hosted on appflowy cloud- // because the url needs the verification token to be accessible- if (!(url?.isAppFlowyCloudUrl ?? false)) ...[- MenuBlockButton(- tooltip: LocaleKeys.editor_copyLink.tr(),- iconData: FlowySvgs.copy_s,- onTap: copyImageLink,- ),- const HSpace(4),- ],+ MenuBlockButton(+ tooltip: LocaleKeys.editor_copy.tr(),+ iconData: FlowySvgs.copy_s,+ onTap: copyImageLink,+ ),+ const HSpace(4),_ImageAlignButton(node: widget.node, state: widget.state),const _Divider(),MenuBlockButton(@@ -81,13 +85,35 @@ class _ImageMenuState extends State<ImageMenu> {);}- void copyImageLink() {+ Future<void> copyImageLink() async {if (url != null) {- Clipboard.setData(ClipboardData(text: url!));- showSnackBarMessage(- context,- LocaleKeys.document_plugins_image_copiedToPasteBoard.tr(),- );+ // paste the image url and the image data+ final imageData = await captureImage();++ try {+ // /image+ await getIt<ClipboardService>().setData(+ ClipboardServiceData(+ plainText: url!,+ image: ('png', imageData),+ ),+ );++ if (mounted) {+ showToastNotification(+ context,+ message: LocaleKeys.message_copy_success.tr(),+ );+ }+ } catch (e) {+ if (mounted) {+ showToastNotification(+ context,+ message: LocaleKeys.message_copy_fail.tr(),+ type: ToastificationType.error,+ );+ }+ }}}@@ -123,6 +149,17 @@ class _ImageMenuState extends State<ImageMenu> {),);}++ Future<Uint8List> captureImage() async {+ final boundary = widget.state.imageKey.currentContext?.findRenderObject()+ as RenderRepaintBoundary?;+ final image = await boundary?.toImage();+ final byteData = await image?.toByteData(format: ImageByteFormat.png);+ if (byteData == null) {+ return Uint8List(0);+ }+ return byteData.buffer.asUint8List();+ }}class _ImageAlignButton extends StatefulWidget {diff --git a/frontend/appflowy_flutter/lib/util/default_extensions.dart b/frontend/appflowy_flutter/lib/util/default_extensions.dartindex 36bbdcb6b4..d0d36d2698 100644--- a/frontend/appflowy_flutter/lib/util/default_extensions.dart+++ b/frontend/appflowy_flutter/lib/util/default_extensions.dart@@ -13,3 +13,13 @@ const List<String> defaultImageExtensions = ['webp','bmp',];++extension ImageStringExtension on String {+ bool isImageUrl() {+ final imagePattern = RegExp(+ r'\.(jpe?g|png|gif|webp|bmp)$',+ caseSensitive: false,+ );+ return imagePattern.hasMatch(this);+ }+}diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/widgets/dialogs.dart b/frontend/appflowy_flutter/lib/workspace/presentation/widgets/dialogs.dartindex 3465fd9b4f..f9cbaac48c 100644--- a/frontend/appflowy_flutter/lib/workspace/presentation/widgets/dialogs.dart+++ b/frontend/appflowy_flutter/lib/workspace/presentation/widgets/dialogs.dart@@ -331,6 +331,14 @@ void showToastNotification(context: context,type: type,style: ToastificationStyle.flat,+ closeButtonShowType: CloseButtonShowType.onHover,+ alignment: Alignment.bottomCenter,+ autoCloseDuration: const Duration(milliseconds: 3000),+ showProgressBar: false,+ backgroundColor: Theme.of(context).colorScheme.surface,+ borderSide: BorderSide(+ color: Colors.grey.withOpacity(0.4),+ ),title: FlowyText(message,maxLines: 3,@@ -343,13 +351,6 @@ void showToastNotification(maxLines: 3,): null,- alignment: Alignment.bottomCenter,- autoCloseDuration: const Duration(milliseconds: 3000),- showProgressBar: false,- backgroundColor: Theme.of(context).colorScheme.surface,- borderSide: BorderSide(- color: Colors.grey.withOpacity(0.4),- ),);}