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.

236 lines 12.4 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; import { InputState } from "../../InputManager/IInputManager"; import { PointF } from "@aurigma/design-atoms-model/Math"; import * as _ from "underscore"; import { PlaceholderItemHandler } from "../../../ItemHandlers"; import { ItemsCommand, SelectionCommand } from "@aurigma/design-atoms-interfaces"; var DefaultKeyInputHandler = /** @class */ (function () { function DefaultKeyInputHandler(_commandManager, _canvas, _selectionHandler) { this._commandManager = _commandManager; this._canvas = _canvas; this._selectionHandler = _selectionHandler; this._moveDirection = { left: false, right: false, up: false, down: false }; this._lastMoveDirection = { horizontal: null, vertical: null }; this._spacePressed = false; } DefaultKeyInputHandler.prototype.onKeyInput = function (params) { var _a; return __awaiter(this, void 0, void 0, function () { var inPlaceEditingHandler, _b, placeholder, history_1; return __generator(this, function (_c) { switch (_c.label) { case 0: inPlaceEditingHandler = this._canvas.inPlaceEditingHandler; if (inPlaceEditingHandler != null) { return [2 /*return*/, inPlaceEditingHandler.processInPlaceEditingKeyEvent(params)]; } _b = params.code; switch (_b) { case "Escape": return [3 /*break*/, 1]; case "KeyA": return [3 /*break*/, 6]; case "ArrowLeft": return [3 /*break*/, 9]; case "ArrowRight": return [3 /*break*/, 9]; case "ArrowUp": return [3 /*break*/, 9]; case "ArrowDown": return [3 /*break*/, 9]; case "Delete": return [3 /*break*/, 10]; case "Backspace": return [3 /*break*/, 10]; case "KeyZ": return [3 /*break*/, 11]; case "KeyY": return [3 /*break*/, 11]; case "Space": return [3 /*break*/, 12]; } return [3 /*break*/, 13]; case 1: if (!(params.state === InputState.Started)) return [3 /*break*/, 5]; placeholder = this._selectedPlaceholder(); if (!(placeholder && placeholder.editing)) return [3 /*break*/, 3]; return [4 /*yield*/, this._commandManager.execute(ItemsCommand.editPlaceholder, { placeholder: placeholder.item, state: false })]; case 2: _c.sent(); return [3 /*break*/, 5]; case 3: return [4 /*yield*/, this._commandManager.execute(SelectionCommand.clearSelection)]; case 4: _c.sent(); _c.label = 5; case 5: return [3 /*break*/, 13]; case 6: if (!(params.ctrlKey && params.state === InputState.Started)) return [3 /*break*/, 8]; params.preventDefault(); return [4 /*yield*/, this._commandManager.execute(SelectionCommand.selectAll)]; case 7: _c.sent(); _c.label = 8; case 8: return [3 /*break*/, 13]; case 9: this._moveItems(params); return [3 /*break*/, 13]; case 10: if (params.state === InputState.Started) this._tryToDeleteSelectedHandlers(); return [3 /*break*/, 13]; case 11: if (params.state !== InputState.Started) return [2 /*return*/]; history_1 = (_a = this._canvas) === null || _a === void 0 ? void 0 : _a.history; if (params.ctrlKey && history_1 != null) { return [2 /*return*/, params.code === "KeyY" ? history_1.redo() : history_1.undo()]; } return [3 /*break*/, 13]; case 12: if (params.state === InputState.Started) { this._spacePressed = true; } else if (params.state === InputState.Finished) { this._spacePressed = false; } params.preventDefault(); return [3 /*break*/, 13]; case 13: return [2 /*return*/]; } }); }); }; Object.defineProperty(DefaultKeyInputHandler.prototype, "spacePressed", { get: function () { return this._spacePressed; }, enumerable: true, configurable: true }); DefaultKeyInputHandler.prototype._canDeleteItemHandler = function (itemHandler) { var productHandler = this._canvas.viewer.productHandler; return (!this._canvas.simpleMode && itemHandler != null && itemHandler.layer != null && !itemHandler.isLocked() && productHandler.isInteractive(itemHandler.item) && !itemHandler.layer.locked && itemHandler.visible && itemHandler.layer.visible && itemHandler.getPermissions() && itemHandler.getPermissions().allowDelete); }; DefaultKeyInputHandler.prototype._tryToDeleteSelectedHandlers = function (force) { if (force === void 0) { force = false; } var canDeleteItemHandlers = true; if (!canDeleteItemHandlers) return; var canDelete = true; var selectedItemHandlers = this._selectedHandlers; for (var i = 0; i < selectedItemHandlers.length; i++) { var itemHandler = selectedItemHandlers.getItem(i); if (!this._canDeleteItemHandler(itemHandler)) { canDelete = false; break; } } if (!canDelete) return; this._commandManager.execute(ItemsCommand.deleteItems, { items: selectedItemHandlers.toArray().map(function (h) { return h.item; }), force: force }); }; DefaultKeyInputHandler.prototype._moveItems = function (params) { var items = this._selectedItems; if (items.length === 0) return; var deltaAmount = params.shiftKey ? 10 : 1; switch (params.code) { case "ArrowLeft": this._moveDirection.left = params.state !== InputState.Finished; this._lastMoveDirection.horizontal = this._moveDirection.left ? "left" : (this._moveDirection.right ? "right" : null); break; case "ArrowRight": this._moveDirection.right = params.state !== InputState.Finished; this._lastMoveDirection.horizontal = this._moveDirection.right ? "right" : (this._moveDirection.left ? "left" : null); break; case "ArrowUp": this._moveDirection.up = params.state !== InputState.Finished; this._lastMoveDirection.vertical = this._moveDirection.down ? "down" : (this._moveDirection.up ? "up" : null); break; case "ArrowDown": this._moveDirection.down = params.state !== InputState.Finished; this._lastMoveDirection.vertical = this._moveDirection.up ? "up" : (this._moveDirection.down ? "down" : null); break; } var delta = new PointF(); delta.x = this._lastMoveDirection.horizontal === "right" ? deltaAmount : (this._lastMoveDirection.horizontal === "left" ? -deltaAmount : 0); delta.y = this._lastMoveDirection.vertical === "down" ? deltaAmount : (this._lastMoveDirection.vertical === "up" ? -deltaAmount : 0); var canvasAngle = this._canvas.viewer.contentAngle; if (canvasAngle > 0) { delta.rotate(-canvasAngle); } var finished = !_.values(this._moveDirection).some(function (v) { return v; }); var args = { delta: delta.clone(), finished: finished }; this._selectionHandler.move(args, params.state); params.preventDefault(); }; Object.defineProperty(DefaultKeyInputHandler.prototype, "_selectedHandlers", { get: function () { return this._selectionHandler.selectedItemHandlers; }, enumerable: true, configurable: true }); Object.defineProperty(DefaultKeyInputHandler.prototype, "_selectedItems", { get: function () { return this._selectedHandlers.toArray().map(function (h) { return h.item; }); }, enumerable: true, configurable: true }); DefaultKeyInputHandler.prototype._selectedPlaceholder = function () { if (this._selectedHandlers.length == 1) { var handler = this._selectedHandlers.get(0); if (handler instanceof PlaceholderItemHandler) return handler; } return null; }; return DefaultKeyInputHandler; }()); export { DefaultKeyInputHandler }; //# sourceMappingURL=DefaultKeyInputHandler.js.map