UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

174 lines 7.74 kB
import { NativeEventType, Button } from "./IInputManager"; import Environment from "@aurigma/design-atoms-model/Utils/Environment"; import { PointF } from "@aurigma/design-atoms-model/Math"; var HtmlInputManagerAdapter = /** @class */ (function () { function HtmlInputManagerAdapter(_hostElement, _keyInputElement, _inputManager, _preventKeyHandleClass, _preventClickHandleClass) { var _this = this; if (_preventKeyHandleClass === void 0) { _preventKeyHandleClass = 'preventCanvasKeyHandle'; } if (_preventClickHandleClass === void 0) { _preventClickHandleClass = "preventCanvasClickHandle"; } this._hostElement = _hostElement; this._keyInputElement = _keyInputElement; this._inputManager = _inputManager; this._preventKeyHandleClass = _preventKeyHandleClass; this._preventClickHandleClass = _preventClickHandleClass; this._isTouchDevice = Environment.IsTouchDevice(); this._onTouchStart = function (e) { if (e.target instanceof Element && !_this._hostElement.contains(e.target)) return; _this._raiseTouchEvent(e, NativeEventType.TouchStart); }; this._onTouchEnd = function (e) { _this._raiseTouchEvent(e, NativeEventType.TouchEnd); }; this._onTouchMove = function (e) { _this._raiseTouchEvent(e, NativeEventType.TouchMove); }; this._onMouseDown = function (e) { if (e.target instanceof Element && !_this._hostElement.contains(e.target)) return; _this._raiseMouseEvent(e, NativeEventType.MouseDown); }; this._onMouseUp = function (e) { _this._raiseMouseEvent(e, NativeEventType.MouseUp); }; this._onMouseMove = function (e) { _this._raiseMouseEvent(e, NativeEventType.MouseMove); }; this._onKeyDown = function (e) { _this._raiseKeyEvent(e, NativeEventType.KeyDown); }; this._onKeyUp = function (e) { _this._raiseKeyEvent(e, NativeEventType.KeyUp); }; this._getButton = function (e) { var result; if (e.type === "mouseup" || e.type === "mousedown") { switch (e.button) { case 0: result = Button.Primary; break; case 1: result = Button.Tertiary; break; case 2: result = Button.Secondary; break; default: result = Button.None; break; } } else { switch (e.buttons) { case 1: case 3: result = Button.Primary; break; case 2: result = Button.Secondary; break; case 4: result = Button.Tertiary; break; default: result = Button.None; break; } } return result; }; this._onWheel = function (e) { var args = { altKey: e.altKey, ctrlKey: e.ctrlKey, delta: new PointF(e.deltaX, e.deltaY), eventType: NativeEventType.Wheel, shiftKey: e.shiftKey, metaKey: e.metaKey, preventDefault: function () { return e.preventDefault(); }, }; _this._inputManager.raiseNativeEvent(args); }; if (this._keyInputElement == null) this._keyInputElement = _hostElement; if (this._isTouchDevice) { document.addEventListener("touchstart", this._onTouchStart); document.addEventListener("touchend", this._onTouchEnd); document.addEventListener("touchmove", this._onTouchMove); } else { document.addEventListener("mousedown", this._onMouseDown); document.addEventListener("mouseup", this._onMouseUp); document.addEventListener("mousemove", this._onMouseMove); document.addEventListener("wheel", this._onWheel, { passive: false }); this._keyInputElement.addEventListener("keydown", this._onKeyDown); this._keyInputElement.addEventListener("keyup", this._onKeyUp); } } HtmlInputManagerAdapter.prototype._raiseMouseEvent = function (e, type) { if (this.checkIsEventExcluded(e)) { return; } var args = { eventType: type, coordinates: new PointF(e.clientX, e.clientY), preventDefault: function () { return e.preventDefault(); }, button: this._getButton(e), altKey: e.altKey, ctrlKey: e.ctrlKey, shiftKey: e.shiftKey, metaKey: e.metaKey }; this._inputManager.raiseNativeEvent(args); }; HtmlInputManagerAdapter.prototype._raiseTouchEvent = function (e, type) { var args = { eventType: type, preventDefault: function () { return e.preventDefault(); }, touches: Array.from(e.targetTouches).map(function (t) { return new PointF(t.clientX, t.clientY); }), changedTouches: Array.from(e.changedTouches).map(function (t) { return new PointF(t.clientX, t.clientY); }), altKey: e.altKey, ctrlKey: e.ctrlKey, shiftKey: e.shiftKey, metaKey: e.metaKey }; this._inputManager.raiseNativeEvent(args); }; HtmlInputManagerAdapter.prototype._raiseKeyEvent = function (e, type) { if (this.checkIsEventExcluded(e)) return; var args = { eventType: type, code: e.code, key: e.key, preventDefault: function () { return e.preventDefault(); }, altKey: e.altKey, ctrlKey: e.ctrlKey, shiftKey: e.shiftKey, metaKey: e.metaKey }; this._inputManager.raiseNativeEvent(args); }; HtmlInputManagerAdapter.prototype.dispose = function () { if (this._isTouchDevice) { document.removeEventListener("touchstart", this._onTouchStart); document.removeEventListener("touchend", this._onTouchEnd); document.removeEventListener("touchmove", this._onTouchMove); } else { document.removeEventListener("mousedown", this._onMouseDown); document.removeEventListener("mouseup", this._onMouseUp); document.removeEventListener("mousemove", this._onMouseMove); document.removeEventListener("wheel", this._onWheel); this._keyInputElement.removeEventListener("keydown", this._onKeyDown); this._keyInputElement.removeEventListener("keyup", this._onKeyUp); } }; HtmlInputManagerAdapter.prototype.checkIsEventExcluded = function (event) { var excludedElementClass = event.type === "keydown" || "keyup" ? this._preventKeyHandleClass : this._preventClickHandleClass; return event.target.closest("." + excludedElementClass) != null; }; return HtmlInputManagerAdapter; }()); export { HtmlInputManagerAdapter }; //# sourceMappingURL=HtmlInputManagerAdapter.js.map