From 40121b6ad548b22eeb2c5591131d16141f755743 Mon Sep 17 00:00:00 2001 From: doouding Date: Thu, 13 Feb 2025 01:35:33 +0000 Subject: [PATCH] feat: dragged blocks should set opacity (#10119) --- .../src/watchers/drag-event-watcher.ts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/blocksuite/affine/widget-drag-handle/src/watchers/drag-event-watcher.ts b/blocksuite/affine/widget-drag-handle/src/watchers/drag-event-watcher.ts index 0771feda7b..9b845a5310 100644 --- a/blocksuite/affine/widget-drag-handle/src/watchers/drag-event-watcher.ts +++ b/blocksuite/affine/widget-drag-handle/src/watchers/drag-event-watcher.ts @@ -99,6 +99,8 @@ export class DragEventWatcher { dropTargetCleanUps: Map void)[]> = new Map(); + resetOpacityCallbacks: (() => void)[] = []; + get host() { return this.widget.host; } @@ -133,6 +135,7 @@ export class DragEventWatcher { this._clearDropIndicator(); this.widget.hide(true); this.std.selection.setGroup('gfx', []); + this.resetOpacityCallbacks.forEach(callback => callback()); }; private readonly _onDragMove = ( @@ -1091,6 +1094,50 @@ export class DragEventWatcher { }); }; + private readonly _setOpacityOfDraggedBlocks = (snapshot: SliceSnapshot) => { + const OPACITY = 0.7; + const gfx = this.gfx; + const resetCallbacks: (() => void)[] = []; + + const traverse = (block: BlockSnapshot) => { + if (block.flavour === 'affine:surface') { + block.children.forEach(traverse); + Object.keys(block.props.elements as Record).forEach( + elemId => { + const element = gfx.getElementById( + elemId + ) as GfxPrimitiveElementModel; + + if (element) { + const originalOpacity = element.opacity; + element.opacity = OPACITY; + resetCallbacks.push(() => { + element.opacity = originalOpacity; + }); + } + } + ); + } else { + const blockView = this.std.view.getBlock(block.id); + + if (blockView) { + const originalOpacity = blockView.style.opacity; + blockView.style.opacity = `${OPACITY}`; + resetCallbacks.push(() => { + if (originalOpacity) { + blockView.style.opacity = originalOpacity; + } else { + blockView.style.removeProperty('opacity'); + } + }); + } + } + }; + + snapshot.content.forEach(traverse); + this.resetOpacityCallbacks = resetCallbacks; + }; + constructor(readonly widget: AffineDragHandleWidget) {} private async _dropToModel( @@ -1192,6 +1239,8 @@ export class DragEventWatcher { setDragData: () => { const { fromMode, snapshot } = this._getDraggedSnapshot(); + snapshot && this._setOpacityOfDraggedBlocks(snapshot); + return { type: 'blocks', fromMode,