mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-25 02:13:00 +08:00
refactor: rewrite blocksuite dnd (#9595)
### Changed
- Refactored BlockSuite drag-and-drop using @atlaskit/pragmatic-drag-and-drop/element/adapter.
- Updated block dragging to use the new drag-and-drop infrastructure.
### BlockSuite DND API
Access the BlockSuite drag-and-drop API via `std.dnd`. This is a lightweight wrapper around pragmatic-drag-and-drop, offering convenient generic types and more intuitive option names.
#### Drag payload structure
There's some constrain about drag payload. The whole drag payload looks like this:
```typescript
type DragPayload = {
entity: {
type: string
},
from: {
at: 'blocksuite',
docId: string
}
}
```
- The `from` field is auto-generated—no need for manual handling.
- The `entity` field is customizable, but it must include a `type`.
All drag-and-drop methods accept a generic type for entity, ensuring more accurate payloads in event handlers.
```typescript
type BlockEntity = {
type: 'blocks',
blockIds: string[]
}
dnd.draggable<BlockEntity>({
element: someElement,
setDragData: () => {
// the return type must satisfy the generic type
// in this case, it's BlockEntity
return {
type: 'blocks',
blockIds: []
}
}
});
dnd.monitor<BlockEntity>({
// the arguments is same for other event handler
onDrag({ source }) {
// the type of this is BlockEntity
source.data.entity
}
})
```
#### Drop payload
When hover on droppable target. You can set drop payload as well. All drag-and-drop methods accept a second generic type for drop payload.
The drop payload is customizable. Additionally, the DND system will add an `edge` field to the final payload object, indicating the nearest edge of the drop target relative to the current drag position.
```typescript
type DropPayload = {
blockId: string;
}
dnd.dropTarget<BlockEntity, DropPayload>({
getData() {
// the type should be DropPayload
return {
blockId: 'someId'
}
}
});
dnd.monitor<BlockEntity, DropPayload>({
// drag over on drop target
onDrag({ location }) {
const target = location.current.dropTargets[0];
// the type is DropPayload
target.data;
// retrieve the nearest edge of the drop target relative to the current drop position.
target.data.edge;
}
})
```
This commit is contained in:
@@ -58,6 +58,8 @@ export class FileDropExtension extends LifeCycleWatcher {
|
||||
|
||||
point$ = signal<Point | null>(null);
|
||||
|
||||
private _disableIndicator = false;
|
||||
|
||||
closestElement$ = signal<BlockComponent | null>(null);
|
||||
|
||||
dropTarget$ = computed<DropTarget | null>(() => {
|
||||
@@ -196,7 +198,9 @@ export class FileDropExtension extends LifeCycleWatcher {
|
||||
|
||||
std.event.disposables.add(
|
||||
this.dropTarget$.subscribe(target => {
|
||||
FileDropExtension.indicator.rect = target?.rect ?? null;
|
||||
FileDropExtension.indicator.rect = this._disableIndicator
|
||||
? null
|
||||
: (target?.rect ?? null);
|
||||
})
|
||||
);
|
||||
|
||||
@@ -210,6 +214,17 @@ export class FileDropExtension extends LifeCycleWatcher {
|
||||
this.dragging$.value = false;
|
||||
})
|
||||
);
|
||||
std.event.disposables.add(
|
||||
std.dnd.monitor({
|
||||
onDragStart: () => {
|
||||
this._disableIndicator = true;
|
||||
},
|
||||
onDrop: () => {
|
||||
this._disableIndicator = false;
|
||||
},
|
||||
})
|
||||
);
|
||||
|
||||
std.event.disposables.add(
|
||||
std.event.add('nativeDragOver', context => {
|
||||
const event = context.get('dndState').raw;
|
||||
|
||||
Reference in New Issue
Block a user