Skip to content

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.

feat: enable drag and drop in row document (#6513)

Mathias Mogensen8d6c67206c50
diff --git a/frontend/appflowy_flutter/lib/plugins/database/widgets/row/row_document.dart b/frontend/appflowy_flutter/lib/plugins/database/widgets/row/row_document.dart
index edd2cc7b31..0d182c4aef 100644
--- a/frontend/appflowy_flutter/lib/plugins/database/widgets/row/row_document.dart
+++ b/frontend/appflowy_flutter/lib/plugins/database/widgets/row/row_document.dart
@@ -3,7 +3,7 @@ import 'package:flutter/material.dart';
import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/plugins/database/grid/application/row/row_document_bloc.dart';
import 'package:appflowy/plugins/document/application/document_bloc.dart';
-import 'package:appflowy/plugins/document/presentation/editor_drop_manager.dart';
+import 'package:appflowy/plugins/document/presentation/editor_drop_handler.dart';
import 'package:appflowy/plugins/document/presentation/editor_page.dart';
import 'package:appflowy/plugins/document/presentation/editor_style.dart';
import 'package:appflowy/shared/flowy_error_page.dart';
@@ -12,7 +12,6 @@ import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
-import 'package:provider/provider.dart';
class RowDocument extends StatelessWidget {
const RowDocument({
@@ -45,7 +44,7 @@ class RowDocument extends StatelessWidget {
),
),
finish: () => _RowEditor(
- viewPB: state.viewPB!,
+ view: state.viewPB!,
onIsEmptyChanged: (isEmpty) => context
.read<RowDocumentBloc>()
.add(RowDocumentEvent.updateIsEmpty(isEmpty)),
@@ -59,84 +58,72 @@ class RowDocument extends StatelessWidget {
class _RowEditor extends StatelessWidget {
const _RowEditor({
- required this.viewPB,
+ required this.view,
this.onIsEmptyChanged,
});
- final ViewPB viewPB;
+ final ViewPB view;
final void Function(bool)? onIsEmptyChanged;
@override
Widget build(BuildContext context) {
- return ChangeNotifierProvider(
- // Due to how DropTarget works, there is no way to differentiate if an overlay is
- // blocking the target visibly, so when we have an overlay with a drop target,
- // we should disable the drop target for the Editor, until it is closed.
- //
- // See FileBlockComponent for sample use.
- //
- // Relates to:
- // - https://github.com/MixinNetwork/flutter-plugins/issues/2
- // - https://github.com/MixinNetwork/flutter-plugins/issues/331
- //
- create: (_) => EditorDropManagerState(),
- child: BlocProvider(
- create: (context) => DocumentBloc(documentId: viewPB.id)
- ..add(const DocumentEvent.initial()),
- child: BlocConsumer<DocumentBloc, DocumentState>(
- listenWhen: (previous, current) =>
- previous.isDocumentEmpty != current.isDocumentEmpty,
- listener: (_, state) {
- if (state.isDocumentEmpty != null) {
- onIsEmptyChanged?.call(state.isDocumentEmpty!);
- }
- if (state.error != null) {
- Log.error('RowEditor error: ${state.error}');
- }
- if (state.editorState == null) {
- Log.error('RowEditor unable to get editorState');
- }
- },
- builder: (context, state) {
- if (state.isLoading) {
- return const Center(
- child: CircularProgressIndicator.adaptive(),
- );
- }
+ return BlocProvider(
+ create: (_) =>
+ DocumentBloc(documentId: view.id)..add(const DocumentEvent.initial()),
+ child: BlocConsumer<DocumentBloc, DocumentState>(
+ listenWhen: (previous, current) =>
+ previous.isDocumentEmpty != current.isDocumentEmpty,
+ listener: (_, state) {
+ if (state.isDocumentEmpty != null) {
+ onIsEmptyChanged?.call(state.isDocumentEmpty!);
+ }
+ if (state.error != null) {
+ Log.error('RowEditor error: ${state.error}');
+ }
+ if (state.editorState == null) {
+ Log.error('RowEditor unable to get editorState');
+ }
+ },
+ builder: (context, state) {
+ if (state.isLoading) {
+ return const Center(child: CircularProgressIndicator.adaptive());
+ }
- final editorState = state.editorState;
- final error = state.error;
- if (error != null || editorState == null) {
- return Center(
- child: AppFlowyErrorPage(error: error),
- );
- }
+ final editorState = state.editorState;
+ final error = state.error;
+ if (error != null || editorState == null) {
+ return Center(
+ child: AppFlowyErrorPage(error: error),
+ );
+ }
- return Consumer<EditorDropManagerState>(
- builder: (_, dropState, __) => BlocProvider<ViewInfoBloc>(
- create: (context) => ViewInfoBloc(view: viewPB),
- child: IntrinsicHeight(
- child: Container(
- constraints: const BoxConstraints(minHeight: 300),
- child: AppFlowyEditorPage(
- shrinkWrap: true,
- autoFocus: false,
- editorState: editorState,
- styleCustomizer: EditorStyleCustomizer(
- context: context,
- padding: const EdgeInsets.only(left: 16, right: 54),
- ),
- showParagraphPlaceholder: (editorState, node) =>
- editorState.document.isEmpty,
- placeholderText: (node) =>
- LocaleKeys.cardDetails_notesPlaceholder.tr(),
+ return BlocProvider<ViewInfoBloc>(
+ create: (context) => ViewInfoBloc(view: view),
+ child: IntrinsicHeight(
+ child: Container(
+ constraints: const BoxConstraints(minHeight: 300),
+ child: EditorDropHandler(
+ viewId: view.id,
+ editorState: editorState,
+ isLocalMode: context.read<DocumentBloc>().isLocalMode,
+ child: AppFlowyEditorPage(
+ shrinkWrap: true,
+ autoFocus: false,
+ editorState: editorState,
+ styleCustomizer: EditorStyleCustomizer(
+ context: context,
+ padding: const EdgeInsets.only(left: 16, right: 54),
),
+ showParagraphPlaceholder: (editorState, _) =>
+ editorState.document.isEmpty,
+ placeholderText: (_) =>
+ LocaleKeys.cardDetails_notesPlaceholder.tr(),
),
),
),
- );
- },
- ),
+ ),
+ );
+ },
),
);
}
diff --git a/frontend/appflowy_flutter/lib/plugins/document/document_page.dart b/frontend/appflowy_flutter/lib/plugins/document/document_page.dart
index efa4d5ce51..a97bad8484 100644
--- a/frontend/appflowy_flutter/lib/plugins/document/document_page.dart
+++ b/frontend/appflowy_flutter/lib/plugins/document/document_page.dart
@@ -1,22 +1,19 @@
import 'dart:async';
+import 'package:flutter/material.dart';
+
import 'package:appflowy/mobile/application/page_style/document_page_style_bloc.dart';
import 'package:appflowy/plugins/document/application/document_appearance_cubit.dart';
import 'package:appflowy/plugins/document/application/document_bloc.dart';
import 'package:appflowy/plugins/document/presentation/banner.dart';
-import 'package:appflowy/plugins/document/presentation/editor_drop_manager.dart';
+import 'package:appflowy/plugins/document/presentation/editor_drop_handler.dart';
import 'package:appflowy/plugins/document/presentation/editor_notification.dart';
import 'package:appflowy/plugins/document/presentation/editor_page.dart';
-import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_file.dart';
-import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/cover/document_immersive_cover.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/image/multi_image_block_component/multi_image_block_component.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/shared_context/shared_context.dart';
import 'package:appflowy/plugins/document/presentation/editor_style.dart';
import 'package:appflowy/shared/flowy_error_page.dart';
-import 'package:appflowy/shared/patterns/file_type_patterns.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/workspace/application/action_navigation/action_navigation_bloc.dart';
import 'package:appflowy/workspace/application/action_navigation/navigation_action.dart';
@@ -24,20 +21,10 @@ import 'package:appflowy/workspace/application/view/prelude.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
import 'package:appflowy_editor/appflowy_editor.dart';
-import 'package:cross_file/cross_file.dart';
-import 'package:desktop_drop/desktop_drop.dart';
-import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';
import 'package:universal_platform/universal_platform.dart';
-const _excludeFromDropTarget = [
- ImageBlockKeys.type,
- CustomImageBlockKeys.type,
- MultiImageBlockKeys.type,
- FileBlockKeys.type,
-];
-
class DocumentPage extends StatefulWidget {
const DocumentPage({
super.key,
@@ -96,55 +83,39 @@ class _DocumentPageState extends State<DocumentPage>
@override
Widget build(BuildContext context) {
- return ChangeNotifierProvider(
- // Due to how DropTarget works, there is no way to differentiate if an overlay is
- // blocking the target visibly, so when we have an overlay with a drop target,
- // we should disable the drop target for the Editor, until it is closed.
- //
- // See FileBlockComponent for sample use.
- //
- // Relates to:
- // - https://github.com/MixinNetwork/flutter-plugins/issues/2
- // - https://github.com/MixinNetwork/flutter-plugins/issues/331
- //
- create: (_) => EditorDropManagerState(),
- child: MultiBlocProvider(
- providers: [
- BlocProvider.value(value: getIt<ActionNavigationBloc>()),
- BlocProvider.value(value: documentBloc),
- ],
- child: BlocBuilder<DocumentBloc, DocumentState>(
- buildWhen: shouldRebuildDocument,
- builder: (context, state) {
- if (state.isLoading) {
- return const Center(child: CircularProgressIndicator.adaptive());
- }
+ return MultiBlocProvider(
+ providers: [
+ BlocProvider.value(value: getIt<ActionNavigationBloc>()),
+ BlocProvider.value(value: documentBloc),
+ ],
+ child: BlocBuilder<DocumentBloc, DocumentState>(
+ buildWhen: shouldRebuildDocument,
+ builder: (context, state) {
+ if (state.isLoading) {
+ return const Center(child: CircularProgressIndicator.adaptive());
+ }
- final editorState = state.editorState;
- this.editorState = editorState;
- final error = state.error;
- if (error != null || editorState == null) {
- Log.error(error);
- return Center(child: AppFlowyErrorPage(error: error));
- }
+ final editorState = state.editorState;
+ this.editorState = editorState;
+ final error = state.error;
+ if (error != null || editorState == null) {
+ Log.error(error);
+ return Center(child: AppFlowyErrorPage(error: error));
+ }
- if (state.forceClose) {
- widget.onDeleted();
- return const SizedBox.shrink();
- }
+ if (state.forceClose) {
+ widget.onDeleted();
+ return const SizedBox.shrink();
+ }
- editorState.transactionStream.listen(onEditorTransaction);
-
- return BlocListener<ActionNavigationBloc, ActionNavigationState>(
- listenWhen: (_, curr) => curr.action != null,
- listener: onNotificationAction,
- child: Consumer<EditorDropManagerState>(
- builder: (context, dropState, _) =>
- buildEditorPage(context, state, dropState),
- ),
- );
- },
- ),
+ editorState.transactionStream.listen(onEditorTransaction);
+
+ return BlocListener<ActionNavigationBloc, ActionNavigationState>(
+ listenWhen: (_, curr) => curr.action != null,
+ listener: onNotificationAction,
+ child: buildEditorPage(context, state),
+ );
+ },
),
);
}
@@ -152,7 +123,6 @@ class _DocumentPageState extends State<DocumentPage>
Widget buildEditorPage(
BuildContext context,
DocumentState state,
- EditorDropManagerState dropState,
) {
final width = context.read<DocumentAppearanceCubit>().state.width;
@@ -173,83 +143,10 @@ class _DocumentPageState extends State<DocumentPage>
),
);
} else {
- child = DropTarget(
- enable: dropState.isDropEnabled,
- onDragExited: (_) =>
- state.editorState!.selectionService.removeDropTarget(),
- onDragUpdated: (details) {
- final data = state.editorState!.selectionService
- .getDropTargetRenderData(details.globalPosition);
-
- if (data != null &&
- data.dropPath != null &&
-
- // We implement custom Drop logic for image blocks, this is
- // how we can exclude them from the Drop Target
- !_excludeFromDropTarget.contains(data.cursorNode?.type)) {
- // Render the drop target
- state.editorState!.selectionService
- .renderDropTargetForOffset(details.globalPosition);
- } else {
- state.editorState!.selectionService.removeDropTarget();
- }
- },
- onDragDone: (details) async {
- final editorState = state.editorState;
- if (editorState == null) {
- return;
- }
-
- editorState.selectionService.removeDropTarget();
-
- final data = editorState.selectionService
- .getDropTargetRenderData(details.globalPosition);
-
- if (data != null) {
- final cursorNode = data.cursorNode;
- final dropPath = data.dropPath;
-
- if (cursorNode != null && dropPath != null) {
- if (_excludeFromDropTarget.contains(cursorNode.type)) {
- return;
- }
-
- final node = editorState.getNodeAtPath(dropPath);
-
- if (node == null) {
- return;
- }
-
- final isLocalMode = context.read<DocumentBloc>().isLocalMode;
- final List<XFile> imageFiles = [];
- final List<XFile> otherFiles = [];
-
- for (final file in details.files) {
- final fileName = file.name.toLowerCase();
- if (file.mimeType?.startsWith('image/') ??
- false || imgExtensionRegex.hasMatch(fileName)) {
- imageFiles.add(file);
- } else {
- otherFiles.add(file);
- }
- }
-
- await editorState.dropImages(
- node,
- imageFiles,
- widget.view.id,
- isLocalMode,
- );
-
- await editorState.dropFiles(
- node,
- otherFiles,
- widget.view.id,
- isLocalMode,
- );
- }
- }
- },
+ child = EditorDropHandler(
+ viewId: widget.view.id,
+ editorState: state.editorState!,
+ isLocalMode: context.read<DocumentBloc>().isLocalMode,
child: AppFlowyEditorPage(
editorState: state.editorState!,
// if the view's name is empty, focus on the title
diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_drop_handler.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_drop_handler.dart
new file mode 100644
index 0000000000..cadfd36bac
--- /dev/null
+++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/editor_drop_handler.dart
@@ -0,0 +1,112 @@
+import 'package:flutter/material.dart';
+
+import 'package:appflowy/plugins/document/presentation/editor_drop_manager.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_file.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/copy_and_paste/paste_from_image.dart';
+import 'package:appflowy/plugins/document/presentation/editor_plugins/file/file_block_component.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/image/multi_image_block_component/multi_image_block_component.dart';
+import 'package:appflowy/shared/patterns/file_type_patterns.dart';
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:desktop_drop/desktop_drop.dart';
+import 'package:provider/provider.dart';
+
+const _excludeFromDropTarget = [
+ ImageBlockKeys.type,
+ CustomImageBlockKeys.type,
+ MultiImageBlockKeys.type,
+ FileBlockKeys.type,
+];
+
+class EditorDropHandler extends StatelessWidget {
+ const EditorDropHandler({
+ super.key,
+ required this.viewId,
+ required this.editorState,
+ required this.isLocalMode,
+ required this.child,
+ });
+
+ final String viewId;
+ final EditorState editorState;
+ final bool isLocalMode;
+ final Widget child;
+
+ @override
+ Widget build(BuildContext context) {
+ return ChangeNotifierProvider(
+ // Due to how DropTarget works, there is no way to differentiate if an overlay is
+ // blocking the target visibly, so when we have an overlay with a drop target,
+ // we should disable the drop target for the Editor, until it is closed.
+ //
+ // See FileBlockComponent for sample use.
+ //
+ // Relates to:
+ // - https://github.com/MixinNetwork/flutter-plugins/issues/2
+ // - https://github.com/MixinNetwork/flutter-plugins/issues/331
+ //
+ create: (_) => EditorDropManagerState(),
+ child: Consumer<EditorDropManagerState>(
+ builder: (context, dropState, _) => DropTarget(
+ enable: dropState.isDropEnabled,
+ onDragExited: (_) => editorState.selectionService.removeDropTarget(),
+ onDragUpdated: _onDragUpdated,
+ onDragDone: _onDragDone,
+ child: child,
+ ),
+ ),
+ );
+ }
+
+ void _onDragUpdated(DropEventDetails details) {
+ final data = editorState.selectionService
+ .getDropTargetRenderData(details.globalPosition);
+
+ if (data != null &&
+ data.dropPath != null &&
+
+ // We implement custom Drop logic for image blocks, this is
+ // how we can exclude them from the Drop Target
+ !_excludeFromDropTarget.contains(data.cursorNode?.type)) {
+ // Render the drop target
+ editorState.selectionService
+ .renderDropTargetForOffset(details.globalPosition);
+ } else {
+ editorState.selectionService.removeDropTarget();
+ }
+ }
+
+ Future<void> _onDragDone(DropDoneDetails details) async {
+ editorState.selectionService.removeDropTarget();
+
+ final data = editorState.selectionService
+ .getDropTargetRenderData(details.globalPosition);
+
+ if (data != null) {
+ final cursorNode = data.cursorNode;
+ final dropPath = data.dropPath;
+
+ if (cursorNode != null && dropPath != null) {
+ if (_excludeFromDropTarget.contains(cursorNode.type)) {
+ return;
+ }
+
+ final node = editorState.getNodeAtPath(dropPath);
+
+ if (node == null) {
+ return;
+ }
+
+ for (final file in details.files) {
+ final fileName = file.name.toLowerCase();
+ if (file.mimeType?.startsWith('image/') ??
+ false || imgExtensionRegex.hasMatch(fileName)) {
+ await editorState.dropImages(node, [file], viewId, isLocalMode);
+ } else {
+ await editorState.dropFiles(node, [file], viewId, isLocalMode);
+ }
+ }
+ }
+ }
+ }
+}