Skip to content

Commit 388c71a

Browse files
committed
refactor(element-selection): simplify the $handleAction function
1 parent 8a005ab commit 388c71a

File tree

1 file changed

+78
-76
lines changed

1 file changed

+78
-76
lines changed

packages/element-selection/src/index.ts

Lines changed: 78 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -373,93 +373,95 @@ export default class CropperSelection extends CropperElement {
373373
protected $handleAction(event: Event): void {
374374
const { currentTarget, detail } = event as CustomEvent;
375375

376-
if (currentTarget && detail) {
377-
const { relatedEvent } = detail;
378-
let { action } = detail;
379-
380-
// Switching to another selection
381-
if (!action && this.multiple) {
382-
// Get the `action` property from the focusing in selection
383-
action = this.$action || relatedEvent?.target.action;
384-
this.$action = action;
385-
}
386-
387-
if (!action
388-
|| (this.hidden && action !== ACTION_SELECT)
389-
|| (this.multiple && !this.active && action !== ACTION_SCALE)) {
390-
return;
391-
}
376+
if (!currentTarget || !detail) {
377+
return;
378+
}
392379

393-
const moveX = detail.endX - detail.startX;
394-
const moveY = detail.endY - detail.startY;
395-
const { width, height } = this;
396-
let { aspectRatio } = this;
380+
const { relatedEvent } = detail;
381+
let { action } = detail;
397382

398-
// Locking aspect ratio by holding shift key
399-
if (!isPositiveNumber(aspectRatio) && (event as PointerEvent).shiftKey) {
400-
aspectRatio = isPositiveNumber(width) && isPositiveNumber(height) ? width / height : 1;
401-
}
383+
// Switching to another selection
384+
if (!action && this.multiple) {
385+
// Get the `action` property from the focusing in selection
386+
action = this.$action || relatedEvent?.target.action;
387+
this.$action = action;
388+
}
402389

403-
switch (action) {
404-
case ACTION_SELECT: {
405-
const { $canvas } = this;
406-
const offset = getOffset(currentTarget as Element);
390+
if (!action
391+
|| (this.hidden && action !== ACTION_SELECT)
392+
|| (this.multiple && !this.active && action !== ACTION_SCALE)) {
393+
return;
394+
}
407395

408-
(this.multiple && !this.hidden ? this.$createSelection() : this).$change(
409-
detail.startX - offset.left,
410-
detail.startY - offset.top,
411-
moveX,
412-
moveY,
413-
aspectRatio,
414-
);
396+
const moveX = detail.endX - detail.startX;
397+
const moveY = detail.endY - detail.startY;
398+
const { width, height } = this;
399+
let { aspectRatio } = this;
415400

416-
action = ACTION_RESIZE_SOUTHEAST;
401+
// Locking aspect ratio by holding shift key
402+
if (!isPositiveNumber(aspectRatio) && (event as PointerEvent).shiftKey) {
403+
aspectRatio = isPositiveNumber(width) && isPositiveNumber(height) ? width / height : 1;
404+
}
417405

418-
if (moveX < 0) {
419-
if (moveY > 0) {
420-
action = ACTION_RESIZE_SOUTHWEST;
421-
} else if (moveY < 0) {
422-
action = ACTION_RESIZE_NORTHWEST;
423-
}
424-
} else if (moveX > 0) {
425-
if (moveY < 0) {
426-
action = ACTION_RESIZE_NORTHEAST;
427-
}
406+
switch (action) {
407+
case ACTION_SELECT: {
408+
const { $canvas } = this;
409+
const offset = getOffset(currentTarget as Element);
410+
411+
(this.multiple && !this.hidden ? this.$createSelection() : this).$change(
412+
detail.startX - offset.left,
413+
detail.startY - offset.top,
414+
moveX,
415+
moveY,
416+
aspectRatio,
417+
);
418+
419+
if (moveX < 0) {
420+
if (moveY > 0) {
421+
action = ACTION_RESIZE_SOUTHWEST;
422+
} else {
423+
action = ACTION_RESIZE_NORTHWEST;
428424
}
429-
430-
if ($canvas) {
431-
($canvas as any).$action = action;
425+
} else if (moveX > 0) {
426+
if (moveY < 0) {
427+
action = ACTION_RESIZE_NORTHEAST;
428+
} else {
429+
action = ACTION_RESIZE_SOUTHEAST;
432430
}
433-
break;
434431
}
435432

436-
case ACTION_MOVE:
437-
if (this.movable && (
438-
this.dynamic
439-
|| (this.$actionStartTarget && this.contains(this.$actionStartTarget as Node))
440-
)) {
441-
this.$move(moveX, moveY);
442-
}
443-
break;
444-
445-
case ACTION_SCALE:
446-
if (relatedEvent && this.zoomable && (
447-
this.dynamic
448-
|| this.contains(relatedEvent.target as Node)
449-
)) {
450-
const offset = getOffset(currentTarget as Element);
451-
452-
this.$zoom(
453-
detail.scale,
454-
relatedEvent.pageX - offset.left,
455-
relatedEvent.pageY - offset.top,
456-
);
457-
}
458-
break;
459-
460-
default:
461-
this.$resize(action, moveX, moveY, aspectRatio);
433+
if ($canvas) {
434+
($canvas as any).$action = action;
435+
}
436+
break;
462437
}
438+
439+
case ACTION_MOVE:
440+
if (this.movable && (
441+
this.dynamic
442+
|| (this.$actionStartTarget && this.contains(this.$actionStartTarget as Node))
443+
)) {
444+
this.$move(moveX, moveY);
445+
}
446+
break;
447+
448+
case ACTION_SCALE:
449+
if (relatedEvent && this.zoomable && (
450+
this.dynamic
451+
|| this.contains(relatedEvent.target as Node)
452+
)) {
453+
const offset = getOffset(currentTarget as Element);
454+
455+
this.$zoom(
456+
detail.scale,
457+
relatedEvent.pageX - offset.left,
458+
relatedEvent.pageY - offset.top,
459+
);
460+
}
461+
break;
462+
463+
default:
464+
this.$resize(action, moveX, moveY, aspectRatio);
463465
}
464466
}
465467

0 commit comments

Comments
 (0)