mirror of
https://github.com/konvajs/konva.git
synced 2025-06-28 04:42:02 +08:00
feat: allow window global to be set for transformer
This commit is contained in:
parent
99180601fc
commit
59096bec7b
70
konva.js
70
konva.js
@ -8,7 +8,7 @@
|
||||
* Konva JavaScript Framework v8.3.12
|
||||
* http://konvajs.org/
|
||||
* Licensed under the MIT
|
||||
* Date: Mon Aug 29 2022
|
||||
* Date: Wed Sep 14 2022
|
||||
*
|
||||
* Original work Copyright (C) 2011 - 2013 by Eric Rowell (KineticJS)
|
||||
* Modified work Copyright (C) 2014 - present by Anton Lavrenov (Konva)
|
||||
@ -2406,14 +2406,28 @@
|
||||
});
|
||||
},
|
||||
};
|
||||
if (Konva$2.isBrowser) {
|
||||
window.addEventListener('mouseup', DD._endDragBefore, true);
|
||||
window.addEventListener('touchend', DD._endDragBefore, true);
|
||||
window.addEventListener('mousemove', DD._drag);
|
||||
window.addEventListener('touchmove', DD._drag);
|
||||
window.addEventListener('mouseup', DD._endDragAfter, false);
|
||||
window.addEventListener('touchend', DD._endDragAfter, false);
|
||||
}
|
||||
const registerDragAndDropListenersForWindowGlobal = (windowGlobal) => {
|
||||
if (Konva$2.isBrowser) {
|
||||
windowGlobal.console.log('registering events');
|
||||
// Remove them all first in case this is a duplicate call with the same
|
||||
// windowGlobal. If this is the first call, this will be a noop. This is to
|
||||
// avoid a situation where we're storing multiple Window handles and
|
||||
// possibly introducing a memory leak.
|
||||
windowGlobal.removeEventListener('mouseup', DD._endDragBefore, true);
|
||||
windowGlobal.removeEventListener('touchend', DD._endDragBefore, true);
|
||||
windowGlobal.removeEventListener('mousemove', DD._drag);
|
||||
windowGlobal.removeEventListener('touchmove', DD._drag);
|
||||
windowGlobal.removeEventListener('mouseup', DD._endDragAfter, false);
|
||||
windowGlobal.removeEventListener('touchend', DD._endDragAfter, false);
|
||||
// Go ahead and actually set up the listeners.
|
||||
windowGlobal.addEventListener('mouseup', DD._endDragBefore, true);
|
||||
windowGlobal.addEventListener('touchend', DD._endDragBefore, true);
|
||||
windowGlobal.addEventListener('mousemove', DD._drag);
|
||||
windowGlobal.addEventListener('touchmove', DD._drag);
|
||||
windowGlobal.addEventListener('mouseup', DD._endDragAfter, false);
|
||||
windowGlobal.addEventListener('touchend', DD._endDragAfter, false);
|
||||
}
|
||||
};
|
||||
|
||||
// CONSTANTS
|
||||
var ABSOLUTE_OPACITY = 'absoluteOpacity', ALL_LISTENERS = 'allEventListeners', ABSOLUTE_TRANSFORM = 'absoluteTransform', ABSOLUTE_SCALE = 'absoluteScale', CANVAS = 'canvas', CHANGE = 'Change', CHILDREN = 'children', KONVA = 'konva', LISTENING = 'listening', MOUSEENTER$1 = 'mouseenter', MOUSELEAVE$1 = 'mouseleave', SET = 'set', SHAPE = 'Shape', SPACE$1 = ' ', STAGE$1 = 'stage', TRANSFORM = 'transform', UPPER_STAGE = 'Stage', VISIBLE = 'visible', TRANSFORM_CHANGE_STR$1 = [
|
||||
@ -5904,6 +5918,7 @@
|
||||
checkNoClip(this.attrs);
|
||||
});
|
||||
this._checkVisibility();
|
||||
registerDragAndDropListenersForWindowGlobal(this.getWindowGlobal());
|
||||
}
|
||||
_validateAdd(child) {
|
||||
const isLayer = child.getType() === 'Layer';
|
||||
@ -6021,6 +6036,10 @@
|
||||
getContent() {
|
||||
return this.content;
|
||||
}
|
||||
getWindowGlobal() {
|
||||
var _a;
|
||||
return (_a = this.attrs.windowGlobal) !== null && _a !== void 0 ? _a : window;
|
||||
}
|
||||
_toKonvaCanvas(config) {
|
||||
config = config || {};
|
||||
config.x = config.x || 0;
|
||||
@ -6835,8 +6854,11 @@
|
||||
return this._getCache(SHADOW_RGBA, this._getShadowRGBA);
|
||||
}
|
||||
_getShadowRGBA() {
|
||||
if (this.hasShadow()) {
|
||||
var rgba = Util.colorToRGBA(this.shadowColor());
|
||||
if (!this.hasShadow()) {
|
||||
return;
|
||||
}
|
||||
var rgba = Util.colorToRGBA(this.shadowColor());
|
||||
if (rgba) {
|
||||
return ('rgba(' +
|
||||
rgba.r +
|
||||
',' +
|
||||
@ -15089,11 +15111,12 @@
|
||||
var hypotenuse = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2));
|
||||
this.sin = Math.abs(height / hypotenuse);
|
||||
this.cos = Math.abs(width / hypotenuse);
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('mousemove', this._handleMouseMove);
|
||||
window.addEventListener('touchmove', this._handleMouseMove);
|
||||
window.addEventListener('mouseup', this._handleMouseUp, true);
|
||||
window.addEventListener('touchend', this._handleMouseUp, true);
|
||||
const windowGlobal = e.target.getStage().getWindowGlobal();
|
||||
if (typeof windowGlobal !== 'undefined') {
|
||||
windowGlobal.addEventListener('mousemove', this._handleMouseMove);
|
||||
windowGlobal.addEventListener('touchmove', this._handleMouseMove);
|
||||
windowGlobal.addEventListener('mouseup', this._handleMouseUp, true);
|
||||
windowGlobal.addEventListener('touchend', this._handleMouseUp, true);
|
||||
}
|
||||
this._transforming = true;
|
||||
var ap = e.target.getAbsolutePosition();
|
||||
@ -15293,13 +15316,14 @@
|
||||
_removeEvents(e) {
|
||||
if (this._transforming) {
|
||||
this._transforming = false;
|
||||
if (typeof window !== 'undefined') {
|
||||
window.removeEventListener('mousemove', this._handleMouseMove);
|
||||
window.removeEventListener('touchmove', this._handleMouseMove);
|
||||
window.removeEventListener('mouseup', this._handleMouseUp, true);
|
||||
window.removeEventListener('touchend', this._handleMouseUp, true);
|
||||
}
|
||||
var node = this.getNode();
|
||||
const windowGlobal = node.getStage().getWindowGlobal();
|
||||
if (typeof windowGlobal !== 'undefined') {
|
||||
windowGlobal.removeEventListener('mousemove', this._handleMouseMove);
|
||||
windowGlobal.removeEventListener('touchmove', this._handleMouseMove);
|
||||
windowGlobal.removeEventListener('mouseup', this._handleMouseUp, true);
|
||||
windowGlobal.removeEventListener('touchend', this._handleMouseUp, true);
|
||||
}
|
||||
this._fire('transformend', { evt: e, target: node });
|
||||
if (node) {
|
||||
this._nodes.forEach((target) => {
|
||||
@ -15582,8 +15606,8 @@
|
||||
this.getStage().content && (this.getStage().content.style.cursor = '');
|
||||
}
|
||||
Group.prototype.destroy.call(this);
|
||||
this.detach();
|
||||
this._removeEvents();
|
||||
this.detach();
|
||||
return this;
|
||||
}
|
||||
// do not work as a container
|
||||
|
4
konva.min.js
vendored
4
konva.min.js
vendored
File diff suppressed because one or more lines are too long
@ -157,13 +157,26 @@ export const DD = {
|
||||
},
|
||||
};
|
||||
|
||||
if (Konva.isBrowser) {
|
||||
window.addEventListener('mouseup', DD._endDragBefore, true);
|
||||
window.addEventListener('touchend', DD._endDragBefore, true);
|
||||
export const registerDragAndDropListenersForWindowGlobal = (windowGlobal: Window) => {
|
||||
if (Konva.isBrowser) {
|
||||
(windowGlobal as any).console.log('registering events');
|
||||
// Remove them all first in case this is a duplicate call with the same
|
||||
// windowGlobal. If this is the first call, this will be a noop. This is to
|
||||
// avoid a situation where we're storing multiple Window handles and
|
||||
// possibly introducing a memory leak.
|
||||
windowGlobal.removeEventListener('mouseup', DD._endDragBefore, true);
|
||||
windowGlobal.removeEventListener('touchend', DD._endDragBefore, true);
|
||||
windowGlobal.removeEventListener('mousemove', DD._drag);
|
||||
windowGlobal.removeEventListener('touchmove', DD._drag);
|
||||
windowGlobal.removeEventListener('mouseup', DD._endDragAfter, false);
|
||||
windowGlobal.removeEventListener('touchend', DD._endDragAfter, false);
|
||||
|
||||
window.addEventListener('mousemove', DD._drag);
|
||||
window.addEventListener('touchmove', DD._drag);
|
||||
|
||||
window.addEventListener('mouseup', DD._endDragAfter, false);
|
||||
window.addEventListener('touchend', DD._endDragAfter, false);
|
||||
// Go ahead and actually set up the listeners.
|
||||
windowGlobal.addEventListener('mouseup', DD._endDragBefore, true);
|
||||
windowGlobal.addEventListener('touchend', DD._endDragBefore, true);
|
||||
windowGlobal.addEventListener('mousemove', DD._drag);
|
||||
windowGlobal.addEventListener('touchmove', DD._drag);
|
||||
windowGlobal.addEventListener('mouseup', DD._endDragAfter, false);
|
||||
windowGlobal.addEventListener('touchend', DD._endDragAfter, false);
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,13 @@ import { SceneCanvas, HitCanvas } from './Canvas';
|
||||
import { GetSet, Vector2d } from './types';
|
||||
import { Shape } from './Shape';
|
||||
import { Layer } from './Layer';
|
||||
import { DD } from './DragAndDrop';
|
||||
import { DD, registerDragAndDropListenersForWindowGlobal } from './DragAndDrop';
|
||||
import { _registerNode } from './Global';
|
||||
import * as PointerEvents from './PointerEvents';
|
||||
|
||||
export interface StageConfig extends ContainerConfig {
|
||||
container: HTMLDivElement | string;
|
||||
windowGlobal?: Window;
|
||||
}
|
||||
|
||||
// CONSTANTS
|
||||
@ -188,6 +189,7 @@ export class Stage extends Container<Layer> {
|
||||
}
|
||||
);
|
||||
this._checkVisibility();
|
||||
registerDragAndDropListenersForWindowGlobal(this.getWindowGlobal());
|
||||
}
|
||||
|
||||
_validateAdd(child) {
|
||||
@ -311,6 +313,9 @@ export class Stage extends Container<Layer> {
|
||||
getContent() {
|
||||
return this.content;
|
||||
}
|
||||
getWindowGlobal() {
|
||||
return this.attrs.windowGlobal ?? window;
|
||||
}
|
||||
_toKonvaCanvas(config) {
|
||||
config = config || {};
|
||||
|
||||
|
@ -635,11 +635,12 @@ export class Transformer extends Group {
|
||||
this.sin = Math.abs(height / hypotenuse);
|
||||
this.cos = Math.abs(width / hypotenuse);
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener('mousemove', this._handleMouseMove);
|
||||
window.addEventListener('touchmove', this._handleMouseMove);
|
||||
window.addEventListener('mouseup', this._handleMouseUp, true);
|
||||
window.addEventListener('touchend', this._handleMouseUp, true);
|
||||
const windowGlobal = e.target.getStage().getWindowGlobal();
|
||||
if (typeof windowGlobal !== 'undefined') {
|
||||
windowGlobal.addEventListener('mousemove', this._handleMouseMove);
|
||||
windowGlobal.addEventListener('touchmove', this._handleMouseMove);
|
||||
windowGlobal.addEventListener('mouseup', this._handleMouseUp, true);
|
||||
windowGlobal.addEventListener('touchend', this._handleMouseUp, true);
|
||||
}
|
||||
|
||||
this._transforming = true;
|
||||
@ -895,13 +896,14 @@ export class Transformer extends Group {
|
||||
_removeEvents(e?) {
|
||||
if (this._transforming) {
|
||||
this._transforming = false;
|
||||
if (typeof window !== 'undefined') {
|
||||
window.removeEventListener('mousemove', this._handleMouseMove);
|
||||
window.removeEventListener('touchmove', this._handleMouseMove);
|
||||
window.removeEventListener('mouseup', this._handleMouseUp, true);
|
||||
window.removeEventListener('touchend', this._handleMouseUp, true);
|
||||
}
|
||||
var node = this.getNode();
|
||||
const windowGlobal = node.getStage().getWindowGlobal();
|
||||
if (typeof windowGlobal !== 'undefined') {
|
||||
windowGlobal.removeEventListener('mousemove', this._handleMouseMove);
|
||||
windowGlobal.removeEventListener('touchmove', this._handleMouseMove);
|
||||
windowGlobal.removeEventListener('mouseup', this._handleMouseUp, true);
|
||||
windowGlobal.removeEventListener('touchend', this._handleMouseUp, true);
|
||||
}
|
||||
this._fire('transformend', { evt: e, target: node });
|
||||
|
||||
if (node) {
|
||||
@ -1209,8 +1211,8 @@ export class Transformer extends Group {
|
||||
this.getStage().content && (this.getStage().content.style.cursor = '');
|
||||
}
|
||||
Group.prototype.destroy.call(this);
|
||||
this.detach();
|
||||
this._removeEvents();
|
||||
this.detach();
|
||||
return this;
|
||||
}
|
||||
// do not work as a container
|
||||
|
Loading…
Reference in New Issue
Block a user