UNPKG

core-graphics

Version:

A core library for creating shape-based graphic editors

66 lines (53 loc) 2.72 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = contextMenuStateReducer; var _editorEvents = require('../editor-events'); var _declarations = require('../../declarations'); var _modes = require('../../helpers/modes'); var _icepick = require('icepick'); var i = _interopRequireWildcard(_icepick); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } var REDUCERS = [contextMenuLeaveReducer, contextMenuEnterReducer, contextMenuModeStateReducer]; function contextMenuStateReducer(currentState, ev) { return REDUCERS.reduce(function (res, fn) { return fn(res, ev); }, currentState); } function contextMenuEnterReducer(currentState, ev) { var mode = currentState.mode; var mouse = currentState.mouse; if ((0, _declarations.isMovingViewportMode)(mode)) { return currentState; } if ((0, _editorEvents.isEditorInputEvent)(ev) && (0, _editorEvents.isShapeMouseDownEvent)(ev) && ev.payload.button === _editorEvents.ButtonType.Right) { var x = mouse.x; var y = mouse.y; var id = ev.payload.id; if ((0, _declarations.isSelectedMode)(mode) && (0, _modes.isShapeSelected)(mode, id)) { var ids = mode.payload.ids; var nextContextMenuState = { open: true, x: x, y: y, target: (0, _declarations.shapesContextMenuTarget)(ids) }; return i.assoc(currentState, 'contextMenu', nextContextMenuState); } else { var _nextContextMenuState = { open: true, x: x, y: y, target: (0, _declarations.shapesContextMenuTarget)([id]) }; var nextMode = (0, _declarations.modeSelected)([id]); return i.chain(currentState).assoc('contextMenu', _nextContextMenuState).assoc('mode', nextMode).value(); } } return currentState; } function contextMenuModeStateReducer(currentState, ev) { if ((0, _editorEvents.isEditorContextMenuEvent)(ev) && (0, _editorEvents.isSetContextMenuEvent)(ev)) { var value = ev.payload.value; return i.assoc(currentState, 'contextMenu', value); } return currentState; } function contextMenuLeaveReducer(currentState, ev) { var contextMenu = currentState.contextMenu; if (contextMenu.open && (0, _editorEvents.isEditorInputEvent)(ev) && ((0, _editorEvents.isBackgroundMouseDownEvent)(ev) || (0, _editorEvents.isShapeMouseDownEvent)(ev))) { return i.merge(currentState, { contextMenu: { open: false } }); } return currentState; }