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.

250 lines 11.7 kB
import { BaseInputHandler } from "../BaseInputHandler"; import { InputState } from "./../../../Input/InputManager/IInputManager"; import { CreateInputHandlerStateMachine, CreateInputHandlerState } from "./CreateInputHandlerStateMachine"; import { ArgumentException } from "@aurigma/design-atoms-model/Exception"; import { Cursor } from "./../../../Utils/Common"; import { EventObject } from "@aurigma/design-atoms-model/EventObject"; import { RectangleF, RotatedRectangleF, PointF, Path } from "@aurigma/design-atoms-model/Math"; import { BarcodeFormat } from "@aurigma/design-atoms-model/Product/Items"; import { Deferred } from "./../../../Utils/Deferred"; import { ItemsCommand, ItemType, OriginPointType, SurfaceCommand } from "@aurigma/design-atoms-interfaces"; export class CreateInputHandler extends BaseInputHandler { constructor(_args, _viewer, inputManager) { super(inputManager); this._args = _args; this._viewer = _viewer; this._onCompleted = new EventObject(); this._onStateChanged = (e) => { switch (e.state) { case CreateInputHandlerState.Initial: break; case CreateInputHandlerState.PointSelected: this._onPointSelectedState(e.point); break; case CreateInputHandlerState.RectangleSelectionStarted: this._startPoint = e.point; this._onRectangleSelectionStarted(e.point); break; case CreateInputHandlerState.RectangleSelectionChanged: this._endPoint = e.point; this._onRectangleSelectionChanged(); break; case CreateInputHandlerState.RectangleSelected: this._endPoint = e.point; this._onRectangleSelected(); break; } }; if (this._args == null) throw new ArgumentException("CreateInputHandler: CreateInputHandler args cannot be null"); if (this._args.item == null) throw new ArgumentException("CreateInputHandler: args.createdItem cannot be null!"); const extendedCreateArgs = this._args.item; if (extendedCreateArgs.selection != null || extendedCreateArgs.click != null) { if (extendedCreateArgs.click == null || extendedCreateArgs.selection == null) throw new ArgumentException("CreateInputHandler: Point and Rectangle create args must be exist both or must be null"); if (extendedCreateArgs.click.type == null || extendedCreateArgs.selection.type == null) throw new ArgumentException("CreateInputHandler: Item type cannot be null"); } else { const createItemArgs = this._args.item; if (createItemArgs.type == null) throw new ArgumentException("CreateInputHandler: Item type cannot be null"); } this._stateMachine = new CreateInputHandlerStateMachine(); this._stateMachine.addStateChanged(this._onStateChanged); this._selection.lock(); this._viewer.setCursor(Cursor.cross); this._selection.ignoreSimpleMode = true; } addOnCompleted(handler) { this._onCompleted.add(handler); } removeOnCompleted(handler) { this._onCompleted.remove(handler); } dispose() { super.dispose(); this._stateMachine.removeStateChanged(this._onStateChanged); this._selection.ignoreSimpleMode = false; } async _onClick(params) { this._currentPointerParams = params; this._stateMachine.onClick(params); } async _onMove(params) { this._currentPointerParams = params; this._stateMachine.onMove(params); } async _onLongTap(params) { this._currentPointerParams = params; this._stateMachine.onLongTap(params); } async _onTransform(params) { } async _onWheel(params) { } async _onPointerDown(params) { } async _onHover(params) { } async _onKey(params) { } async _onDoubleClick(params) { } get _rubberbandHandler() { return this._viewer.canvas.rubberbandHandler; } get _selection() { return this._viewer.canvas.selection; } get _selectionRect() { const startPoint = this._applyPrintAreaMargins(this._startPoint); const endPoint = this._applyPrintAreaMargins(this._endPoint); return new RectangleF(Math.min(startPoint.x, endPoint.x), Math.min(startPoint.y, endPoint.y), Math.abs(startPoint.x - endPoint.x), Math.abs(startPoint.y - endPoint.y)); } get _selectAfterCreate() { return this._args.selectAfterCreate != null ? this._args.selectAfterCreate : true; } async _onRectangleSelectionStarted(point) { this._viewer.history.pause(); const args = this._getCreateItemArgs(point, true); this._selection.unlock(); this._arbitraryResize = args.type !== ItemType.BarcodeItem; const selectionFirst = args.type === ItemType.BoundedTextItem || this._isDatabar(args); if (!selectionFirst) { this._itemAdded = new Deferred(); await this._addItem(args); const selectionArgs = { arbitraryResize: this._arbitraryResize, finished: false, point: point.clone(), resizeIndex: 3 }; this._selection.resizeByPoint(selectionArgs, InputState.Started, point.clone()); this._itemAdded.resolve(); } else { this._rubberbandHandler.updateRubberband(point.clone(), point.clone(), InputState.Started); } this._selection.update(); } async _onRectangleSelectionChanged() { if (this._itemAdded != null) { await this._itemAdded.promise; } if (this._rubberbandHandler.isActive) { this._rubberbandHandler.updateRubberband(this._currentPointerParams.workspace, this._startPoint, InputState.InProgress); } else { const selectionArgs = { arbitraryResize: this._arbitraryResize, finished: false, point: this._currentPointerParams.workspace.clone(), resizeIndex: 3 }; this._selection.resizeByPoint(selectionArgs, InputState.InProgress, this._startPoint); } this._viewer.canvas.redrawDesign(); } async _onRectangleSelected() { const currentPointerParams = Object.assign({}, this._currentPointerParams); if (this._itemAdded != null) { await this._itemAdded.promise; } const createItemArgs = this._getCreateItemArgs(this._startPoint, true); const isBounded = createItemArgs.type === ItemType.BoundedTextItem; const isDatabar = this._isDatabar(createItemArgs); const rect = this._selectionRect; if (isBounded) { const boundedTextItem = await this._viewer.commandManager.execute(ItemsCommand.createItem, createItemArgs); await this._viewer.commandManager.execute(SurfaceCommand.addItems, { items: [boundedTextItem] }); await this._textWorkaround(rect, boundedTextItem); } else if (isDatabar) { const data = createItemArgs; data.itemData.location.x = rect.left; data.itemData.location.y = rect.top; data.itemData.width = rect.width; data.itemData.height = rect.height; const barcodeItem = await this._viewer.commandManager.execute(ItemsCommand.createItem, data); await this._viewer.commandManager.execute(SurfaceCommand.addItems, { items: [barcodeItem] }); } else { const handler = this._viewer.getHandler(this._item); await handler.waitUpdate(); } if (this._rubberbandHandler.isActive) { this._rubberbandHandler.updateRubberband(currentPointerParams.workspace, this._startPoint, InputState.Finished); } else { const selectionArgs = { arbitraryResize: this._arbitraryResize, finished: true, point: this._currentPointerParams.workspace.clone(), resizeIndex: 3 }; this._selection.resizeByPoint(selectionArgs, InputState.Finished, this._startPoint); } this._onCompleted.notify(rect); if (!this._selectAfterCreate) { this._selection.clearSelectedItemHandlers(); } this._viewer.history.resume(); } async _onPointSelectedState(pt) { const args = this._getCreateItemArgs(pt, false); if (args != null) { await this._addItem(args); } this._onCompleted.notify(pt); if (!this._selectAfterCreate) this._selection.clearSelectedItemHandlers(); } //bad workaround, wait for inplace text async _textWorkaround(rect, item) { const handler = this._viewer.getHandler(item); await handler.waitUpdate(); item.sourceRectangle = rect; item.textRectangle = rect; item.sourcePath = Path.rectangle(rect.left, rect.top, rect.width, rect.height); item.transform = RotatedRectangleF.fromRectangleF(rect) .getTransform(RotatedRectangleF.fromRectangleF(item.sourceRectangle)); item.transform.rotate(-this._viewer.contentAngle); handler.update(); } async _addItem(args) { const item = await this._viewer.commandManager.execute(ItemsCommand.createItem, args); this._item = item; await this._viewer.commandManager.execute(SurfaceCommand.addItems, { items: [item] }); await this._viewer.canvas.waitUpdate(); } _getCreateItemArgs(point, isRectangle = false) { const extendedCreateArgs = this._args.item; if (extendedCreateArgs.click == null && extendedCreateArgs.selection == null) return this._getUpdatedArgs(this._args.item, point, isRectangle); return this._getUpdatedArgs(isRectangle ? extendedCreateArgs.selection : extendedCreateArgs.click, point, isRectangle); } _getUpdatedArgs(args, point, isRectangle = false) { const argsClone = Object.assign({}, args); argsClone.itemData = Object.assign({}, args.itemData || {}); const data = argsClone.itemData; //clone if (data.location == null) data.location = {}; point = this._applyPrintAreaMargins(point); data.location.x = point.x; data.location.y = point.y; data.location.originX = OriginPointType.Left; data.location.originY = OriginPointType.Top; if (isRectangle) { data.width = 1; data.height = 1; } argsClone.relativeToPrintArea = false; return argsClone; } _isDatabar(args) { var _a, _b; return args.type === ItemType.BarcodeItem && ((_b = (_a = args.itemData) === null || _a === void 0 ? void 0 : _a.barcodeContent) === null || _b === void 0 ? void 0 : _b.barcodeFormat) == BarcodeFormat.DATABAR_EXPANDED_STACKED; } _applyPrintAreaMargins(point) { const offset = this._viewer.canvas.offset; return new PointF(point.x - offset.x, point.y - offset.y); } } //# sourceMappingURL=CreateInputHandler.js.map