UNPKG

core-graphics

Version:

A core library for creating shape-based graphic editors

116 lines (96 loc) 4.39 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = moveStateReducer; var _input = require('../events/input'); var _modes = require('../../declarations/modes'); var _editorAction = require('../../editor-state/editor-action'); var _historyEventsWrappers = require('../../history/history-events-wrappers'); var _grid = require('../../utils/grid'); 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; } } function getShapesMoveEventFromMode(persistedScene, mode) { var _mode$payload = mode.payload; var ids = _mode$payload.ids; var startX = _mode$payload.startX; var startY = _mode$payload.startY; var endX = _mode$payload.endX; var endY = _mode$payload.endY; var dx = endX - startX; var dy = endY - startY; // single shape if (ids.length === 1) { var id = ids[0]; var shapeData = persistedScene.shapes[id]; return (0, _historyEventsWrappers.getShapeMoveEvent)({ dx: dx, dy: dy }, shapeData); } // many shapes var shapesData = ids.map(function (id) { return persistedScene.shapes[id]; }); return (0, _historyEventsWrappers.getShapesMoveEvent)({ dx: dx, dy: dy }, shapesData); } var REDUCERS = [moveModeEnterReducer, moveModeStateReducer, movePreviewStateReducer, moveModeFinishReducer]; function moveStateReducer(currentState, ev) { return REDUCERS.reduce(function (res, fn) { return fn(res, ev); }, currentState); } // entering moving mode function moveModeEnterReducer(currentState, ev) { var mode = currentState.mode; if ((0, _modes.isSelectedMode)(mode)) { if (!currentState.modifierKeys[_input.SceneModifierKey.Shift] && (0, _input.isEditorInputEvent)(ev) && currentState.mouse.down && (0, _input.isMouseMoveEvent)(ev)) { var ids = mode.payload.ids; var _snapCoordsIfNeeded = (0, _grid.snapCoordsIfNeeded)(currentState.grid, currentState.mouse); var mx = _snapCoordsIfNeeded.x; var my = _snapCoordsIfNeeded.y; var _snapEditorInputEvent = (0, _grid.snapEditorInputEventCoordsIfNeeded)(currentState.grid, ev).payload; var x = _snapEditorInputEvent.x; var y = _snapEditorInputEvent.y; return i.assoc(currentState, 'mode', (0, _modes.modeMoving)({ ids: ids, startX: mx, startY: my, endX: x, endY: y })); } return currentState; } return currentState; } // mode updates when moving function moveModeStateReducer(currentState, ev) { var mode = currentState.mode; if ((0, _modes.isMovingMode)(mode) && (0, _input.isEditorInputEvent)(ev) && (0, _input.isMouseMoveEvent)(ev)) { var _snapEditorInputEvent2 = (0, _grid.snapEditorInputEventCoordsIfNeeded)(currentState.grid, ev).payload; var x = _snapEditorInputEvent2.x; var y = _snapEditorInputEvent2.y; return i.merge(currentState, { mode: { payload: { endX: x, endY: y } } }); } return currentState; } // updating previewed events function movePreviewStateReducer(currentState, ev) { var mode = currentState.mode; var persistedScene = currentState.persistedScene; if ((0, _modes.isMovingMode)(mode) && currentState.mouse.down) { var event = getShapesMoveEventFromMode(persistedScene, mode); return i.assoc(currentState, 'previewedHistoryEvents', [event]); } return currentState; } // finished moving function moveModeFinishReducer(currentState, ev) { var mode = currentState.mode; if ((0, _modes.isMovingMode)(mode) && (0, _input.isEditorInputEvent)(ev) && (0, _input.isMouseUpEvent)(ev)) { var ids = mode.payload.ids; var historyEvent = getShapesMoveEventFromMode(currentState.persistedScene, mode); var editorAction = (0, _editorAction.pushHistoryEventAction)(historyEvent); return i.chain(currentState).assoc('mode', (0, _modes.modeSelected)(ids)).assoc('editorActionsQueue', [editorAction]).value(); } return currentState; }