Skip to content

Commit 9aab8a6

Browse files
committed
refactor(element-shade): bind change event on the selection directly
1 parent 2dab6ff commit 9aab8a6

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

packages/element-shade/src/index.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ import {
1717
import style from './style';
1818

1919
const canvasCache = new WeakMap();
20+
const selectionCache = new WeakMap();
2021

2122
export default class CropperShade extends CropperElement {
2223
static $name = CROPPER_SHADE;
2324

2425
static $version = '__VERSION__';
2526

26-
protected $onCanvasChange: EventListener | null = null;
27-
2827
protected $onCanvasActionEnd: EventListener | null = null;
2928

3029
protected $onCanvasActionStart: EventListener | null = null;
3130

31+
protected $onSelectionChange: EventListener | null = null;
32+
3233
protected $style = style;
3334

3435
x = 0;
@@ -51,6 +52,14 @@ export default class CropperShade extends CropperElement {
5152
return canvasCache.get(this);
5253
}
5354

55+
protected set $selection(element: CropperSelection) {
56+
selectionCache.set(this, element);
57+
}
58+
59+
protected get $selection(): CropperSelection {
60+
return selectionCache.get(this);
61+
}
62+
5463
protected static get observedAttributes(): string[] {
5564
return super.observedAttributes.concat([
5665
'height',
@@ -74,6 +83,7 @@ export default class CropperShade extends CropperElement {
7483
);
7584

7685
if ($selection) {
86+
this.$selection = $selection;
7787
this.$onCanvasActionStart = (event) => {
7888
if ($selection.hidden && (event as CustomEvent).detail.action === ACTION_SELECT) {
7989
this.hidden = false;
@@ -84,7 +94,7 @@ export default class CropperShade extends CropperElement {
8494
this.hidden = true;
8595
}
8696
};
87-
this.$onCanvasChange = () => {
97+
this.$onSelectionChange = () => {
8898
const {
8999
x,
90100
y,
@@ -100,17 +110,17 @@ export default class CropperShade extends CropperElement {
100110
};
101111
on($canvas, EVENT_ACTION_START, this.$onCanvasActionStart);
102112
on($canvas, EVENT_ACTION_END, this.$onCanvasActionEnd);
103-
on($canvas, EVENT_CHANGE, this.$onCanvasChange);
113+
on($selection, EVENT_CHANGE, this.$onSelectionChange);
104114
}
105115
}
106116

107117
this.$render();
108118
}
109119

110120
protected disconnectedCallback(): void {
111-
const { $canvas } = this;
121+
const { $canvas, $selection } = this;
112122

113-
if ($canvas) {
123+
if ($canvas && $selection) {
114124
if (this.$onCanvasActionStart) {
115125
off($canvas, EVENT_ACTION_START, this.$onCanvasActionStart);
116126
this.$onCanvasActionStart = null;
@@ -121,9 +131,9 @@ export default class CropperShade extends CropperElement {
121131
this.$onCanvasActionEnd = null;
122132
}
123133

124-
if (this.$onCanvasChange) {
125-
off($canvas, EVENT_CHANGE, this.$onCanvasChange);
126-
this.$onCanvasChange = null;
134+
if (this.$onSelectionChange) {
135+
off($selection, EVENT_CHANGE, this.$onSelectionChange);
136+
this.$onSelectionChange = null;
127137
}
128138
}
129139

0 commit comments

Comments
 (0)