UNPKG

core-graphics

Version:

A core library for creating shape-based graphic editors

85 lines (67 loc) 4.11 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; exports.default = graphicalContextStateReducer; var _graphicalContext = require('../events/graphical-context'); var _historyEvent = require('../../history/history-event'); var _editorAction = require('../../editor-state/editor-action'); var _icepick = require('icepick'); var i = _interopRequireWildcard(_icepick); var _object = require('object.pick'); var _object2 = _interopRequireDefault(_object); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 graphicalContextStateReducer(currentState, ev) { if (!(0, _graphicalContext.isEditorGraphicalContextEvent)(ev)) { return currentState; } else { if ((0, _graphicalContext.isSetGraphicalContextDataEvent)(ev)) { var value = ev.payload.value; return i.assoc(currentState, 'graphicalContextData', value); } if ((0, _graphicalContext.isSetShapeGraphicalContextDataEvent)(ev)) { var _ev$payload = ev.payload; var _value = _ev$payload.value; var id = _ev$payload.id; var persistedScene = currentState.persistedScene; var shapesDeclarations = currentState.shapesDeclarations; // TODO too complex ? var graphicalContextParams = shapesDeclarations[persistedScene.shapes[id].type].graphicalContextParams; var pickedValue = (0, _object2.default)(_value, graphicalContextParams); var shapeData = currentState.persistedScene.shapes[id]; var updatedShapeData = i.assoc(shapeData, 'graphicalContextData', pickedValue); var event = (0, _historyEvent.shapeUpdateEvent)(id, updatedShapeData); var action = (0, _editorAction.pushHistoryEventAction)(event); return i.assoc(currentState, 'editorActionsQueue', i.push(currentState.editorActionsQueue, action)); } if ((0, _graphicalContext.isMergeShapesGraphicalContextDataEvent)(ev)) { var _ret = function () { var _ev$payload2 = ev.payload; var value = _ev$payload2.value; var ids = _ev$payload2.ids; var persistedScene = currentState.persistedScene; var shapesDeclarations = currentState.shapesDeclarations; var shapes = persistedScene.shapes; // TODO extract var getGraphicalContextParams = function getGraphicalContextParams(id) { return shapesDeclarations[persistedScene.shapes[id].type].graphicalContextParams; }; var events = ids.map(function (id) { var shapeData = shapes[id]; var prevValue = shapeData.graphicalContextData; var mergedValue = (0, _object2.default)(i.merge(prevValue, value), getGraphicalContextParams(id)); var newShapeData = i.assoc(shapeData, 'graphicalContextData', mergedValue); return (0, _historyEvent.shapeUpdateEvent)(id, newShapeData); }); var action = (0, _editorAction.pushHistoryEventAction)((0, _historyEvent.historyEventsGroup)(events)); return { v: i.assoc(currentState, 'editorActionsQueue', i.push(currentState.editorActionsQueue, action)) }; }(); if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; } return currentState; } }