Skip to content

Commit

Permalink
feat: drag view into document to link to view (AppFlowy-IO#6991)
Browse files Browse the repository at this point in the history
* feat: drag view into document

* chore: remove redundant method call
  • Loading branch information
Xazin authored Dec 26, 2024
1 parent e67e9cc commit 802a667
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:appflowy/plugins/document/presentation/editor_plugins/mention/mention_page_block.dart';
import 'package:appflowy/plugins/document/presentation/editor_plugins/plugins.dart';
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
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';
Expand Down Expand Up @@ -37,12 +37,34 @@ class EditorDropHandler extends StatelessWidget {
@override
Widget build(BuildContext context) {
final childWidget = Consumer<EditorDropManagerState>(
builder: (context, dropState, _) => DropTarget(
enable: dropState.isDropEnabled,
onDragExited: (_) => editorState.selectionService.removeDropTarget(),
onDragUpdated: _onDragUpdated,
onDragDone: _onDragDone,
child: child,
builder: (context, dropState, _) => DragTarget<ViewPB>(
onLeave: (_) => editorState.selectionService.removeDropTarget(),
onMove: (details) {
if (details.data.id == viewId) {
return;
}

_onDragUpdated(details.offset);
},
onWillAcceptWithDetails: (details) {
if (!dropState.isDropEnabled) {
return false;
}

if (details.data.id == viewId) {
return false;
}

return true;
},
onAcceptWithDetails: _onDragViewDone,
builder: (context, _, __) => DropTarget(
enable: dropState.isDropEnabled,
onDragExited: (_) => editorState.selectionService.removeDropTarget(),
onDragUpdated: (details) => _onDragUpdated(details.globalPosition),
onDragDone: _onDragDone,
child: child,
),
),
);

Expand All @@ -68,9 +90,8 @@ class EditorDropHandler extends StatelessWidget {
);
}

void _onDragUpdated(DropEventDetails details) {
final data = editorState.selectionService
.getDropTargetRenderData(details.globalPosition);
void _onDragUpdated(Offset position) {
final data = editorState.selectionService.getDropTargetRenderData(position);

if (data != null &&
data.dropPath != null &&
Expand All @@ -79,8 +100,7 @@ class EditorDropHandler extends StatelessWidget {
// how we can exclude them from the Drop Target
!_excludeFromDropTarget.contains(data.cursorNode?.type)) {
// Render the drop target
editorState.selectionService
.renderDropTargetForOffset(details.globalPosition);
editorState.selectionService.renderDropTargetForOffset(position);
} else {
editorState.selectionService.removeDropTarget();
}
Expand Down Expand Up @@ -113,4 +133,26 @@ class EditorDropHandler extends StatelessWidget {
}
}
}

void _onDragViewDone(DragTargetDetails<ViewPB> details) {
editorState.selectionService.removeDropTarget();

final data =
editorState.selectionService.getDropTargetRenderData(details.offset);
if (data != null) {
final cursorNode = data.cursorNode;
final dropPath = data.dropPath;

if (cursorNode != null && dropPath != null) {
if (_excludeFromDropTarget.contains(cursorNode.type)) {
return;
}

final view = details.data;
final node = pageMentionNode(view.id);
final t = editorState.transaction..insertNode(dropPath, node);
editorState.apply(t);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class _DraggableViewItemState extends State<DraggableViewItem> {
onMove: (data) {
final renderBox = context.findRenderObject() as RenderBox;
final offset = renderBox.globalToLocal(data.offset);

if (offset.dx > renderBox.size.width) {
return;
}

final position = _computeHoverPosition(offset, renderBox.size);
if (!_shouldAccept(data.data, position)) {
return;
Expand Down

0 comments on commit 802a667

Please sign in to comment.