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
feat: attachment mobile toolbar item (#6868)
Mathias Mogensen55f12d535829
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/add_attachment_item.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/add_attachment_item.dartnew file mode 100644index 0000000000..a20b727a12--- /dev/null+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/add_attachment_item.dart@@ -0,0 +1,241 @@+import 'dart:async';++import 'package:appflowy/generated/flowy_svgs.g.dart';+import 'package:appflowy/generated/locale_keys.g.dart';+import 'package:appflowy/mobile/presentation/bottom_sheet/show_mobile_bottom_sheet.dart';+import 'package:appflowy/mobile/presentation/widgets/flowy_mobile_quick_action_button.dart';+import 'package:appflowy/plugins/document/application/document_bloc.dart';+import 'package:appflowy/plugins/document/presentation/editor_plugins/file/file_util.dart';+import 'package:appflowy/plugins/document/presentation/editor_plugins/image/common.dart';+import 'package:appflowy/plugins/document/presentation/editor_plugins/image/image_util.dart';+import 'package:appflowy/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/aa_menu/_toolbar_theme.dart';+import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';+import 'package:appflowy/shared/permission/permission_checker.dart';+import 'package:appflowy/startup/startup.dart';+import 'package:appflowy/startup/tasks/app_widget.dart';+import 'package:appflowy_backend/log.dart';+import 'package:appflowy_editor/appflowy_editor.dart';+import 'package:easy_localization/easy_localization.dart';+import 'package:flowy_infra/file_picker/file_picker_service.dart';+import 'package:flutter/material.dart';+import 'package:flutter_bloc/flutter_bloc.dart';+import 'package:image_picker/image_picker.dart';++@visibleForTesting+const addAttachmentToolbarItemKey = ValueKey('add_attachment_toolbar_item');++final addAttachmentItem = AppFlowyMobileToolbarItem(+ itemBuilder: (context, editorState, service, _, onAction) {+ return AppFlowyMobileToolbarIconItem(+ key: addAttachmentToolbarItemKey,+ editorState: editorState,+ icon: FlowySvgs.media_s,+ onTap: () {+ final documentId = context.read<DocumentBloc>().documentId;+ final isLocalMode = context.read<DocumentBloc>().isLocalMode;++ final selection = editorState.selection;+ service.closeKeyboard();++ // delay to wait the keyboard closed.+ Future.delayed(const Duration(milliseconds: 100), () async {+ unawaited(+ editorState.updateSelectionWithReason(+ selection,+ extraInfo: {+ selectionExtraInfoDisableMobileToolbarKey: true,+ selectionExtraInfoDisableFloatingToolbar: true,+ selectionExtraInfoDoNotAttachTextService: true,+ },+ ),+ );++ keepEditorFocusNotifier.increase();+ final didAddAttachment = await showAddAttachmentMenu(+ AppGlobals.rootNavKey.currentContext!,+ documentId: documentId,+ isLocalMode: isLocalMode,+ editorState: editorState,+ selection: selection!,+ );++ if (didAddAttachment != true) {+ unawaited(editorState.updateSelectionWithReason(selection));+ }+ });+ },+ );+ },+);++Future<bool?> showAddAttachmentMenu(+ BuildContext context, {+ required String documentId,+ required bool isLocalMode,+ required EditorState editorState,+ required Selection selection,+}) async =>+ showMobileBottomSheet<bool>(+ context,+ showDragHandle: true,+ barrierColor: Colors.transparent,+ backgroundColor:+ ToolbarColorExtension.of(context).toolbarMenuBackgroundColor,+ elevation: 20,+ isScrollControlled: false,+ enableDraggableScrollable: true,+ builder: (_) => Padding(+ padding: EdgeInsets.all(16 * context.scale),+ child: _AddAttachmentMenu(+ documentId: documentId,+ isLocalMode: isLocalMode,+ editorState: editorState,+ selection: selection,+ ),+ ),+ );++class _AddAttachmentMenu extends StatelessWidget {+ const _AddAttachmentMenu({+ required this.documentId,+ required this.isLocalMode,+ required this.editorState,+ required this.selection,+ });++ final String documentId;+ final bool isLocalMode;+ final EditorState editorState;+ final Selection selection;++ @override+ Widget build(BuildContext context) {+ return SingleChildScrollView(+ child: Column(+ mainAxisSize: MainAxisSize.min,+ children: [+ MobileQuickActionButton(+ text: LocaleKeys.document_attachmentMenu_choosePhoto.tr(),+ icon: FlowySvgs.image_rounded_s,+ iconSize: const Size.square(20),+ onTap: () async => selectPhoto(context),+ ),+ const Divider(height: 8.5, thickness: 0.5),+ MobileQuickActionButton(+ text: LocaleKeys.document_attachmentMenu_takePicture.tr(),+ icon: FlowySvgs.camera_s,+ iconSize: const Size.square(20),+ onTap: () async => selectCamera(context),+ ),+ const Divider(height: 8.5, thickness: 0.5),+ MobileQuickActionButton(+ text: LocaleKeys.document_attachmentMenu_chooseFile.tr(),+ icon: FlowySvgs.file_s,+ iconSize: const Size.square(20),+ onTap: () async => selectFile(context),+ ),+ const Divider(height: 8.5, thickness: 0.5),+ ],+ ),+ );+ }++ Future<void> _insertNode(Node node) async {+ Future.delayed(+ const Duration(milliseconds: 100),+ () async {+ // if current selected block is a empty paragraph block, replace it with the new block.+ if (selection.isCollapsed) {+ final path = selection.end.path;+ final currentNode = editorState.getNodeAtPath(path);+ final text = currentNode?.delta?.toPlainText();+ if (currentNode != null &&+ currentNode.type == ParagraphBlockKeys.type &&+ text != null &&+ text.isEmpty) {+ final transaction = editorState.transaction;+ transaction.insertNode(path.next, node);+ transaction.deleteNode(currentNode);+ transaction.afterSelection =+ Selection.collapsed(Position(path: path));+ transaction.selectionExtraInfo = {};+ return editorState.apply(transaction);+ }+ }++ await editorState.insertBlockAfterCurrentSelection(selection, node);+ },+ );+ }++ Future<void> insertImage(BuildContext context, XFile image) async {+ CustomImageType type = CustomImageType.local;+ String? path;+ if (isLocalMode) {+ path = await saveImageToLocalStorage(image.path);+ } else {+ (path, _) = await saveImageToCloudStorage(image.path, documentId);+ type = CustomImageType.internal;+ }++ if (path != null) {+ final node = customImageNode(url: path, type: type);+ await _insertNode(node);+ }+ }++ Future<void> selectPhoto(BuildContext context) async {+ final image = await ImagePicker().pickImage(source: ImageSource.gallery);++ if (image != null && context.mounted) {+ await insertImage(context, image);+ }++ if (context.mounted) {+ Navigator.pop(context);+ }+ }++ Future<void> selectCamera(BuildContext context) async {+ final cameraPermission =+ await PermissionChecker.checkCameraPermission(context);+ if (!cameraPermission) {+ Log.error('Has no permission to access the camera');+ return;+ }++ final image = await ImagePicker().pickImage(source: ImageSource.camera);++ if (image != null && context.mounted) {+ await insertImage(context, image);+ }++ if (context.mounted) {+ Navigator.pop(context);+ }+ }++ Future<void> selectFile(BuildContext context) async {+ final result = await getIt<FilePickerService>().pickFiles();+ final file = result?.files.first.xFile;+ if (file != null) {+ FileUrlType type = FileUrlType.local;+ String? path;+ if (isLocalMode) {+ path = await saveFileToLocalStorage(file.path);+ } else {+ (path, _) = await saveFileToCloudStorage(file.path, documentId);+ type = FileUrlType.cloud;+ }++ if (path != null) {+ final node = fileNode(url: path, type: type, name: file.name);+ await _insertNode(node);+ }+ }++ if (context.mounted) {+ Navigator.pop(context);+ }+ }+}diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/add_block_toolbar_item.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/add_block_toolbar_item.dartindex 95c6305623..7032c1b116 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/add_block_toolbar_item.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/add_block_toolbar_item.dart@@ -392,22 +392,3 @@ class AddBlockMenu extends StatelessWidget {};}}--extension on EditorState {- Future<void> insertBlockAfterCurrentSelection(- Selection selection,- Node node,- ) async {- final path = selection.end.path.next;- final transaction = this.transaction;- transaction.insertNode(- path,- node,- );- transaction.afterSelection = Selection.collapsed(- Position(path: path),- );- transaction.selectionExtraInfo = {};- await apply(transaction);- }-}diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/biusc_toolbar_item.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/basic_toolbar_item.dartsimilarity index 100%rename from frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/biusc_toolbar_item.dartrename to frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/basic_toolbar_item.dartdiff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/toolbar_item_builder.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/toolbar_item_builder.dartindex adb1feeb35..35a3c37e74 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/toolbar_item_builder.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/toolbar_item_builder.dart@@ -12,6 +12,7 @@ final _defaultToolbarItems = [aaToolbarItem,todoListToolbarItem,bulletedListToolbarItem,+ addAttachmentItem,numberedListToolbarItem,boldToolbarItem,italicToolbarItem,@@ -35,6 +36,7 @@ final _listToolbarItems = [underlineToolbarItem,strikethroughToolbarItem,colorToolbarItem,+ addAttachmentItem,undoToolbarItem,redoToolbarItem,];diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/util.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/util.dartindex a016a0863f..6d2e2c9f08 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/util.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/mobile_toolbar_v3/util.dart@@ -339,4 +339,21 @@ extension MobileToolbarEditorState on EditorState {await apply(transaction);}++ Future<void> insertBlockAfterCurrentSelection(+ Selection selection,+ Node node,+ ) async {+ final path = selection.end.path.next;+ final transaction = this.transaction;+ transaction.insertNode(+ path,+ node,+ );+ transaction.afterSelection = Selection.collapsed(+ Position(path: path),+ );+ transaction.selectionExtraInfo = {};+ await apply(transaction);+ }}diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/plugins.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/plugins.dartindex af50b15f63..3102bceea8 100644--- a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/plugins.dart+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_plugins/plugins.dart@@ -41,10 +41,11 @@ export 'math_equation/mobile_math_equation_toolbar_item.dart';export 'mention/mention_block.dart';export 'mobile_floating_toolbar/custom_mobile_floating_toolbar.dart';export 'mobile_toolbar_v3/aa_toolbar_item.dart';+export 'mobile_toolbar_v3/add_attachment_item.dart';export 'mobile_toolbar_v3/add_block_toolbar_item.dart';export 'mobile_toolbar_v3/appflowy_mobile_toolbar.dart';export 'mobile_toolbar_v3/appflowy_mobile_toolbar_item.dart';-export 'mobile_toolbar_v3/biusc_toolbar_item.dart';+export 'mobile_toolbar_v3/basic_toolbar_item.dart';export 'mobile_toolbar_v3/indent_outdent_toolbar_item.dart';export 'mobile_toolbar_v3/list_toolbar_item.dart';export 'mobile_toolbar_v3/more_toolbar_item.dart';diff --git a/frontend/appflowy_flutter/lib/shared/permission/permission_checker.dart b/frontend/appflowy_flutter/lib/shared/permission/permission_checker.dartindex 7ae91fc73e..df260bad71 100644--- a/frontend/appflowy_flutter/lib/shared/permission/permission_checker.dart+++ b/frontend/appflowy_flutter/lib/shared/permission/permission_checker.dart@@ -61,4 +61,43 @@ class PermissionChecker {return true;}++ static Future<bool> checkCameraPermission(BuildContext context) async {+ // check the permission first+ final status = await Permission.camera.status;+ // if the permission is permanently denied, we should open the app settings+ if (status.isPermanentlyDenied && context.mounted) {+ unawaited(+ showFlowyMobileConfirmDialog(+ context,+ title: FlowyText.semibold(+ LocaleKeys.pageStyle_cameraPermissionTitle.tr(),+ maxLines: 3,+ textAlign: TextAlign.center,+ ),+ content: FlowyText(+ LocaleKeys.pageStyle_cameraPermissionDescription.tr(),+ maxLines: 5,+ textAlign: TextAlign.center,+ fontSize: 12.0,+ ),+ actionAlignment: ConfirmDialogActionAlignment.vertical,+ actionButtonTitle: LocaleKeys.pageStyle_openSettings.tr(),+ actionButtonColor: Colors.blue,+ cancelButtonTitle: LocaleKeys.pageStyle_doNotAllow.tr(),+ cancelButtonColor: Colors.blue,+ onActionButtonPressed: openAppSettings,+ ),+ );++ return false;+ } else if (status.isDenied) {+ final newStatus = await Permission.camera.request();+ if (newStatus.isDenied) {+ return false;+ }+ }++ return true;+ }}diff --git a/frontend/resources/flowy_icons/16x/camera.svg b/frontend/resources/flowy_icons/16x/camera.svgnew file mode 100644index 0000000000..ce3eda4fb9--- /dev/null+++ b/frontend/resources/flowy_icons/16x/camera.svg@@ -0,0 +1 @@+<svg viewBox="-0.5 -0.5 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" id="Camera--Streamline-Solar-Ar.svg" height="16" width="16"><desc>Camera Streamline Icon: https://streamlinehq.com</desc><path stroke="#000000" d="M5.625 8.125a1.875 1.875 0 1 0 3.75 0 1.875 1.875 0 1 0 -3.75 0" stroke-width="1"></path><path d="M6.1111125 13.125h2.7777625c1.9506875000000001 0 2.926 0 3.6266249999999998 -0.45962500000000006 0.30325 -0.199 0.5636875 -0.4546875 0.766375 -0.7524375 0.468125 -0.687875 0.468125 -1.6455 0.468125 -3.5606875 0 -1.915125 0 -2.87274375 -0.468125 -3.560625 -0.2026875 -0.2977875 -0.463125 -0.553475 -0.766375 -0.7524500000000001 -0.45025000000000004 -0.29534375 -1.0138125 -0.40090624999999996 -1.8767500000000001 -0.4386375 -0.4118125 0 -0.7663125 -0.3063625 -0.8470624999999999 -0.7028125000000001C9.6705 2.30305625 9.1386875 1.875 8.5210625 1.875h-2.042125c-0.61765625 0 -1.14946875 0.42805625 -1.2706062500000002 1.022725 -0.08075625 0.39644999999999997 -0.4353 0.7028125000000001 -0.84708125 0.7028125000000001 -0.8629187500000001 0.03773125 -1.42653125 0.14329375 -1.876725 0.4386375 -0.30330625 0.19897499999999999 -0.563725 0.45466249999999997 -0.7663875 0.7524500000000001C1.25 5.47950625 1.25 6.437125 1.25 8.35225c0 1.9151874999999998 0 2.8728124999999998 0.4681375 3.5606875 0.2026625 0.29775 0.46308125 0.5534375 0.7663875 0.7524375C3.18515 13.125 4.16046875 13.125 6.1111125 13.125Z" stroke="#000000" stroke-width="1"></path><path d="M11.875 6.25h-0.625" stroke="#000000" stroke-linecap="round" stroke-width="1"></path></svg>\ No newline at end of filediff --git a/frontend/resources/flowy_icons/16x/file.svg b/frontend/resources/flowy_icons/16x/file.svgnew file mode 100644index 0000000000..acf1094ee8--- /dev/null+++ b/frontend/resources/flowy_icons/16x/file.svg@@ -0,0 +1,2 @@++<svg viewBox="-0.5 -0.5 16 16" fill="none" xmlns="http://www.w3.org/2000/svg" id="Folder-2--Streamline-Solar-Ar.svg" height="16" width="16"><desc>Folder 2 Streamline Icon: https://streamlinehq.com</desc><path d="M13.75 6.875 1.25 6.875" stroke="#000000" stroke-linecap="round" stroke-width="1"></path><path d="M1.25 4.34359375c0 -0.55158125 0 -0.8273750000000001 0.04334375 -1.0571 0.19080625 -1.0112999999999999 0.9818499999999999 -1.80234375 1.99315 -1.99315C3.5162187499999997 1.25 3.7920125 1.25 4.34359375 1.25c0.24166875000000002 0 0.36250625000000003 0 0.47863749999999994 0.0108625 0.500675 0.046818750000000006 0.97559375 0.24353750000000002 1.36273125 0.56445625 0.0897875 0.0744375 0.175225 0.1598875 0.3461 0.33077500000000004L6.875 2.5c0.509875 0.5098625 0.7648125 0.76479375 1.0700625 0.93464375 0.1676875 0.0933 0.345625 0.16698125000000003 0.5301875 0.21959375C8.811187499999999 3.75 9.171687499999999 3.75 9.89275 3.75h0.23356249999999998c1.64525 0 2.4678125 0 3.0025625 0.4809125 0.0491875 0.0442375 0.09599999999999999 0.09105 0.1401875 0.14023125C13.75 4.90584375 13.75 5.7284625 13.75 7.3736875V8.75c0 2.3569999999999998 0 3.5355625 -0.73225 4.26775C12.285562500000001 13.75 11.107 13.75 8.75 13.75h-2.5c-2.357025 0 -3.53553125 0 -4.26776875 -0.73225C1.25 12.285562500000001 1.25 11.107 1.25 8.75V4.34359375Z" stroke="#000000" stroke-width="1"></path></svg>\ No newline at end of filediff --git a/frontend/resources/translations/en.json b/frontend/resources/translations/en.jsonindex e2d295cec7..6b7314ba12 100644--- a/frontend/resources/translations/en.json+++ b/frontend/resources/translations/en.json@@ -1983,6 +1983,11 @@"title": "Select page","failedToLoad": "Failed to load page list","noPagesFound": "No pages found"+ },+ "attachmentMenu": {+ "choosePhoto": "Choose photo",+ "takePicture": "Take a picture",+ "chooseFile": "Choose file"}},"board": {@@ -2473,9 +2478,11 @@"unsplash": "Unsplash","pageCover": "Page cover","none": "None",- "photoPermissionDescription": "@:appName needs access to your photos to let you add images to your documents","openSettings": "Open Settings","photoPermissionTitle": "@:appName would like to access your photo library",+ "photoPermissionDescription": "@:appName needs access to your photos to let you add images to your documents",+ "cameraPermissionTitle": "@:appName would like to access your camera",+ "cameraPermissionDescription": "@:appName needs access to your camera to let you add images to your documents from the camera","doNotAllow": "Don't Allow","image": "Image"},