Skip to content

Commit 85c6e57

Browse files
committed
fix animation preview not stopping in non chromium browsers (#11143)
1 parent 4357c8e commit 85c6e57

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

pxtblocks/fields/field_animation.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions
4848
initView() {
4949
// Register mouseover events for animating preview
5050
(this.sourceBlock_ as Blockly.BlockSvg).getSvgRoot().addEventListener("mouseenter", this.onMouseEnter);
51-
(this.sourceBlock_ as Blockly.BlockSvg).getSvgRoot().addEventListener("mouseleave", this.onMouseLeave);
51+
(this.sourceBlock_ as Blockly.BlockSvg).getSvgRoot().addEventListener("mouseleave", this.cancelAnimation);
5252
}
5353

5454
showEditor_() {
@@ -156,7 +156,7 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions
156156
}
157157
}
158158

159-
protected onMouseEnter = () => {
159+
protected onMouseEnter = (e: MouseEvent) => {
160160
if (this.animateRef || !this.asset) return;
161161

162162
const assetInterval = this.getParentInterval() || this.asset.interval;
@@ -168,15 +168,27 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions
168168
if (this.preview && this.frames[index]) this.preview.src(this.frames[index]);
169169
index = (index + 1) % this.frames.length;
170170
}, interval);
171+
172+
document.addEventListener("mousemove", this.onDocumentMouseMove);
173+
}
174+
175+
protected onDocumentMouseMove = (e: MouseEvent) => {
176+
const rect = (this.sourceBlock_ as Blockly.BlockSvg).getSvgRoot().getBoundingClientRect();
177+
178+
if (e.clientX < rect.left || e.clientX > rect.right || e.clientY < rect.top || e.clientY > rect.bottom) {
179+
this.cancelAnimation();
180+
}
171181
}
172182

173-
protected onMouseLeave = () => {
183+
protected cancelAnimation = () => {
174184
if (this.animateRef) clearInterval(this.animateRef);
175185
this.animateRef = undefined;
176186

177187
if (this.preview && this.frames[0]) {
178188
this.preview.src(this.frames[0]);
179189
}
190+
191+
document.removeEventListener("mousemove", this.onDocumentMouseMove);
180192
}
181193

182194
protected getParentIntervalBlock(): Blockly.Block {
@@ -220,6 +232,13 @@ export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions
220232
protected parseFieldOptions(opts: FieldAnimationOptions): ParsedFieldAnimationOptions {
221233
return parseFieldOptions(opts);
222234
}
235+
236+
onDispose() {
237+
super.onDispose();
238+
(this.sourceBlock_ as Blockly.BlockSvg).getSvgRoot().removeEventListener("mouseenter", this.onMouseEnter);
239+
(this.sourceBlock_ as Blockly.BlockSvg).getSvgRoot().removeEventListener("mouseleave", this.cancelAnimation);
240+
document.removeEventListener("mousemove", this.onDocumentMouseMove);
241+
}
223242
}
224243

225244
function parseFieldOptions(opts: FieldAnimationOptions) {

0 commit comments

Comments
 (0)