@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
245 lines • 13.3 kB
JavaScript
import { Button, InputState } from "./../../../Input/InputManager/IInputManager";
import { NewBaseTextItemHandler, GroupItemHandler, PlaceholderItemHandler } from "./../../../ItemHandlers";
import { MultiSelectMode } from "./../../../Commands/SelectionCommand/SelectItemsCommand";
import { BaseTextItem, PlaceholderItem } from "@aurigma/design-atoms-model/Product/Items";
import { FrontEndLogger, LogSource } from "./../../../Services/FrontEndLogger";
import { CoordinatesConvertUtils } from "./../../../Utils/CoordinatesConvertUtils";
import { CoordinateSystem } from "../../../Viewer/CoordinateSystem";
import { SelectionCommand } from "@aurigma/design-atoms-interfaces";
export class PointerInputHandlerHelper {
constructor(_canvas, _commandManager, _selectionHandler, _hitTestManager, _eventManager, _dndHandler, _rotateHandler) {
this._canvas = _canvas;
this._commandManager = _commandManager;
this._selectionHandler = _selectionHandler;
this._hitTestManager = _hitTestManager;
this._eventManager = _eventManager;
this._dndHandler = _dndHandler;
this._rotateHandler = _rotateHandler;
this._processInPlaceEditingMouseEvent = async (params, handler = null, pointerOverCurrent = null) => {
if (handler == null) {
const item = this.getItemToSelect(params.workspace, false);
if (item == null || !this._isInPlaceAvailableForItem(item))
return false;
handler = this._canvas.handlerFactory.get(item);
}
if (pointerOverCurrent == null)
pointerOverCurrent = handler.hitTest(params.workspace).body;
this._commandManager.execute(SelectionCommand.selectItems, { items: [handler.item], multiSelectMode: MultiSelectMode.None });
return await handler.processInPlaceEditingMouseEvent(params, pointerOverCurrent);
};
this._contextMenuNotify = (params) => {
this._eventManager.contextMenuEvent.fire({ pointerParams: params, items: [...this._selectedItems] });
FrontEndLogger.debugLog(`Context menu fired!`, LogSource.InputManager);
};
this._handleRotating = (params) => {
this._selectionHandler.rotateByPoint(this._getRotatingArgs(params), params.state, params.startWorkspace);
this._rotateHandler.updateView(this._selectionHandler.selectedItemHandlers.get(0), params.workspace, params.state, this._selectionHandler.rectangle.angle);
};
this._getRotatingArgs = (params) => {
const args = {
finished: params.state === InputState.Finished,
point: params.workspace,
rotationStep: params.shiftKey ? 15 : 90,
tolerance: params.ctrlKey ? 0 : (params.shiftKey ? 7.5 : 5)
};
return args;
};
this._handleMoving = (params) => {
const args = {
finished: params.state === InputState.Finished,
point: params.workspace
};
this._selectionHandler.moveByPoint(args, params.state, params.ctrlKey, params.startWorkspace);
};
this._selectItemForMoving = async (params, dragOnlySelectedHandler) => {
if ((dragOnlySelectedHandler && !this._canvas.autoPlaceholderEditModeEnabled) || this._canvas.contentEditingPlaceholderItemHandler != null)
return;
const itemHandler = this.getHandlerToSelect(params.startWorkspace, false);
const item = itemHandler === null || itemHandler === void 0 ? void 0 : itemHandler.item;
if (item == null)
return;
const isManipulationAllowed = !this._canvas.simpleMode && item.manipulationPermissions.allowMove;
const isPlaceholderEditModeEnabled = itemHandler instanceof PlaceholderItemHandler && this._canvas.autoPlaceholderEditModeEnabled;
if (!isManipulationAllowed && !isPlaceholderEditModeEnabled)
return;
const args = {
items: [item]
};
await this._commandManager.execute(SelectionCommand.selectItems, args);
if (isPlaceholderEditModeEnabled)
itemHandler.editing = true;
return item;
};
this._handleResizing = (params, ht) => {
if (params.state === InputState.Started)
this._resizeStartedHitTestResult = ht;
let arbitraryResize = this._resizeStartedHitTestResult.arbitraryResize;
if (params.shiftKey) {
if (this._resizeStartedHitTestResult.arbitraryResize && this._resizeStartedHitTestResult.cornerProportionalResize) {
arbitraryResize = false;
}
else if (!arbitraryResize && this._resizeStartedHitTestResult.cornerArbitraryResize) {
arbitraryResize = true;
}
}
const args = {
arbitraryResize: arbitraryResize,
finished: params.state === InputState.Finished,
point: params.workspace,
resizeIndex: this._resizeStartedHitTestResult.resizeIndex
};
this._selectionHandler.resizeByPoint(args, params.state, params.startWorkspace);
if (params.state === InputState.Finished)
this._resizeStartedHitTestResult = null;
};
}
async onPointerClick(params, multiselect) {
var _a;
if (this._canvas.inPlaceEditingHandler != null && await this._processInPlaceEditingMouseEvent(params, this._canvas.inPlaceEditingHandler))
return;
const productPoint = CoordinatesConvertUtils.workspaceToProduct(params.workspace, this._canvas.offset);
const hitTestResult = this._selectionHandler.selectionHitTestManager.hitTest(params.workspace);
if (hitTestResult.resize || hitTestResult.rotate)
return;
const clickInsideSelection = ((_a = this._selectionHandler.rectangle) === null || _a === void 0 ? void 0 : _a.contains(productPoint));
const shiftRightClick = params.shiftKey && params.button === Button.Secondary;
if (shiftRightClick) {
if (clickInsideSelection)
this._contextMenuNotify(params);
return;
}
if (clickInsideSelection) {
await this._processClickInsideSelection(params, multiselect);
}
else {
await this._processClickOutsideSelection(params, multiselect);
}
if (params.button === Button.Secondary) {
this._contextMenuNotify(params);
}
}
async onPointerMove(params, dragOnlySelectedHandler) {
if (this._canvas.inPlaceEditingHandler != null && await this._processInPlaceEditingMouseEvent(params, this._canvas.inPlaceEditingHandler))
return;
if (this._dndHandler.isDragStarted())
return this._dndHandler.processMove(params);
const isIdle = this._selectionHandler.isIdle;
const hitTestResult = isIdle ? this._selectionHandler.selectionHitTestManager.hitTest(params.startWorkspace) : null;
let selectedPhItemHandler = this._selectionHandler.currentItemHandler;
if (selectedPhItemHandler != null && selectedPhItemHandler instanceof PlaceholderItemHandler && this._canvas.autoPlaceholderEditModeEnabled && (hitTestResult === null || hitTestResult === void 0 ? void 0 : hitTestResult.drag))
selectedPhItemHandler.editing = true;
if (this._selectionHandler.isRotating || (hitTestResult === null || hitTestResult === void 0 ? void 0 : hitTestResult.rotate))
return this._handleRotating(params);
if (this._selectionHandler.isResizing || (hitTestResult === null || hitTestResult === void 0 ? void 0 : hitTestResult.resize))
return this._handleResizing(params, hitTestResult);
if (this._selectionHandler.isDragging || (hitTestResult === null || hitTestResult === void 0 ? void 0 : hitTestResult.drag))
return this._handleMoving(params);
else {
const movingItem = await this._selectItemForMoving(params, dragOnlySelectedHandler);
if (movingItem != null)
return this._handleMoving(params);
}
if (params.state !== InputState.Started || this._canvas.contentEditingPlaceholderItemHandler != null)
return;
const selectedItems = this._selectedItems;
if (!dragOnlySelectedHandler || (selectedItems.length === 1 && selectedItems[0] instanceof PlaceholderItem))
this._dndHandler.processMove(params);
}
async onDoubleClick(params) {
const item = this.getItemToSelect(params.workspace, false);
await this._processInPlaceEditingMouseEvent(params, null, true);
this._eventManager.doubleClickEvent.fire(item);
}
async onPointerDown(params) {
if (this._canvas.inPlaceEditingHandler != null)
await this._processInPlaceEditingMouseEvent(params, this._canvas.inPlaceEditingHandler);
}
async onPointerHover(params) {
if (this._canvas.inPlaceEditingHandler != null)
await this._processInPlaceEditingMouseEvent(params, this._canvas.inPlaceEditingHandler);
}
async _processClickInsideSelection(params, multiselect) {
const selectedItems = this._selectedItems;
const isCurrentSelectionMultiple = selectedItems.length > 1;
const item = this.getItemToSelect(params.workspace, !isCurrentSelectionMultiple && params.button === Button.Primary);
if (item == null)
return;
if (!multiselect && selectedItems.length === 1) {
if (selectedItems[0] === item) {
if (params.button === Button.Primary && this._isInPlaceAvailableForItem(item))
return await this._processInPlaceEditingMouseEvent(params, this._canvas.handlerFactory.get(item), true);
}
else {
await this._selectItems([item], MultiSelectMode.None);
}
}
if (!multiselect)
return;
await this._selectItems([item], MultiSelectMode.Invert);
}
async _processClickOutsideSelection(params, multiselect) {
const item = this.getItemToSelect(params.workspace, false);
if (item == null)
return await this._commandManager.execute(SelectionCommand.clearSelection);
await this._selectItems([item], multiselect ? MultiSelectMode.Invert : MultiSelectMode.None);
}
getItemToSelect(pt, expandGroup) {
var _a;
return (_a = this.getHandlerToSelect(pt, expandGroup)) === null || _a === void 0 ? void 0 : _a.item;
}
getHandlerToSelect(pt, expandGroup) {
var _a;
const handlers = this._getHandlersByPoint(pt);
if (handlers.length === 0)
return null;
const selectedHandlers = this._selectionHandler.selectedItemHandlers;
const first = selectedHandlers.firstOrDefault();
if (expandGroup && this._selectedItems.length === 1 && handlers.includes(first) && first instanceof GroupItemHandler) {
const index = handlers.indexOf(first);
const targetHandler = (_a = handlers[index + 1]) !== null && _a !== void 0 ? _a : handlers[index];
return targetHandler;
}
const getFirstDirectChildItem = (parent) => {
for (let handler of handlers) {
if (handler.parentGroupItemHandler == parent)
return handler;
}
return null;
};
let currentParent = first === null || first === void 0 ? void 0 : first.parentGroupItemHandler;
while (currentParent != null) {
let sibling = getFirstDirectChildItem(currentParent);
if (sibling)
return sibling;
if (handlers.includes(currentParent))
return currentParent;
currentParent = currentParent.parentGroupItemHandler;
}
return handlers[0];
}
_getHandlersByPoint(pt) {
return this._hitTestManager.getItemHandlersByHitTest(pt, CoordinateSystem.workspace);
}
get _selectedItems() {
return this._canvas.getSelectedItemHandlers().toArray().map(h => h.item);
}
_getItems(pt) {
return this._canvas.getSelectedItemHandlers().toArray().map(h => h.item);
}
_isInPlaceAvailableForItem(item) {
if (!(item instanceof BaseTextItem))
return false;
const handler = this._canvas.handlerFactory.get(item);
if (!(handler instanceof NewBaseTextItemHandler))
return false;
return handler.inPlaceEditingEnabled;
}
async _selectItems(items, mode) {
const selectionArgs = {
items: items,
multiSelectMode: mode
};
await this._commandManager.execute(SelectionCommand.selectItems, selectionArgs);
}
}
//# sourceMappingURL=PointerInputHandlerHelper.js.map