Skip to content

Commit

Permalink
chore: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone committed Oct 22, 2024
1 parent 2430372 commit 0d5f2b8
Showing 1 changed file with 39 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,50 +46,6 @@ export class PointerEventWatcher {
);
};

/**
* When click on drag handle
* Should select the block and show slash menu if current block is not selected
* Should clear selection if current block is the first selected block
*/
private _clickHandler: UIEventHandler = ctx => {
if (!this.widget.isHoverDragHandleVisible) return;

const state = ctx.get('pointerState');
const { target } = state.raw;
const element = captureEventTarget(target);
const insideDragHandle = !!element?.closest(AFFINE_DRAG_HANDLE_WIDGET);
if (!insideDragHandle) return;

const anchorBlockId = this.widget.anchorBlockId.peek();

if (!anchorBlockId) return;

const { selection } = this.widget.std;
const selectedBlocks = this.widget.selectionHelper.selectedBlocks;

// Should clear selection if current block is the first selected block
if (
selectedBlocks.length > 0 &&
!includeTextSelection(selectedBlocks) &&
selectedBlocks[0].blockId === anchorBlockId
) {
selection.clear(['block']);
this.widget.dragHoverRect = null;
this.showDragHandleOnHoverBlock();
return;
}

// Should select the block if current block is not selected
const block = this.widget.anchorBlockComponent.peek();
if (!block) return;

if (selectedBlocks.length > 1) {
this.showDragHandleOnHoverBlock();
}

this.widget.selectionHelper.setSelectedBlocks([block]);
};

private _containerStyle = computed(() => {
const draggingAreaRect = this.widget.draggingAreaRect.value;
if (!draggingAreaRect) return null;
Expand Down Expand Up @@ -148,6 +104,37 @@ export class PointerEventWatcher {

private _lastShowedBlock: { id: string; el: BlockComponent } | null = null;

private _onClick = () => {
const anchorBlockId = this.widget.anchorBlockId.peek();

if (!anchorBlockId) return;

const { selection } = this.widget.std;
const selectedBlocks = this.widget.selectionHelper.selectedBlocks;

// Should clear selection if current block is the first selected block
if (
selectedBlocks.length > 0 &&
!includeTextSelection(selectedBlocks) &&
selectedBlocks[0].blockId === anchorBlockId
) {
selection.clear(['block']);
this.widget.dragHoverRect = null;
this.showDragHandleOnHoverBlock();
return;
}

// Should select the block if current block is not selected
const block = this.widget.anchorBlockComponent.peek();
if (!block) return;

if (selectedBlocks.length > 1) {
this.showDragHandleOnHoverBlock();
}

this.widget.selectionHelper.setSelectedBlocks([block]);
};

/**
* When pointer move on block, should show drag handle
* And update hover block id and path
Expand Down Expand Up @@ -335,7 +322,14 @@ export class PointerEventWatcher {
// return true;
// });

this.widget.handleEvent('click', this._clickHandler);
requestAnimationFrame(() => {
this.widget.disposables.addFromEvent(
this.widget.dragHandleContainer,
'click',
this._onClick
);
});
// this.widget.handleEvent('click', this._clickHandler);
this.widget.handleEvent('pointerMove', this._throttledPointerMoveHandler);
this.widget.handleEvent('pointerOut', this._pointerOutHandler);
}
Expand Down

0 comments on commit 0d5f2b8

Please sign in to comment.