UNPKG

@gorpacrate/core-graphics

Version:

A core library for creating shape-based graphic editors

49 lines 2.9 kB
"use strict"; exports.__esModule = true; var i = require("icepick"); var editor_action_1 = require("../../editor-state/editor-action"); var history_event_1 = require("../../history/history-event"); var pick_1 = require("../../utils/pick"); var graphical_context_1 = require("../events/graphical-context"); function graphicalContextStateReducer(currentState, ev) { if (!graphical_context_1.isEditorGraphicalContextEvent(ev)) { return currentState; } else { if (graphical_context_1.isSetGraphicalContextDataEvent(ev)) { var value = ev.payload.value; return i.assoc(currentState, 'graphicalContextData', value); } if (graphical_context_1.isSetShapeGraphicalContextDataEvent(ev)) { var _a = ev.payload, value = _a.value, id = _a.id; var persistedScene = currentState.persistedScene, shapesDeclarations = currentState.shapesDeclarations; // TODO too complex ? var graphicalContextParams = shapesDeclarations[persistedScene.shapes[id].type].graphicalContextParams; var pickedValue = pick_1.pick(value, graphicalContextParams); var shapeData = currentState.persistedScene.shapes[id]; var updatedShapeData = i.assoc(shapeData, 'graphicalContextData', pickedValue); var event_1 = history_event_1.shapeUpdateEvent(id, updatedShapeData); var action = editor_action_1.pushHistoryEventAction(event_1); return i.assoc(currentState, 'editorActionsQueue', i.push(currentState.editorActionsQueue, action)); } if (graphical_context_1.isMergeShapesGraphicalContextDataEvent(ev)) { var _b = ev.payload, value_1 = _b.value, ids = _b.ids; var persistedScene_1 = currentState.persistedScene, shapesDeclarations_1 = currentState.shapesDeclarations; var shapes_1 = persistedScene_1.shapes; // TODO extract var getGraphicalContextParams_1 = function (id) { return shapesDeclarations_1[persistedScene_1.shapes[id].type].graphicalContextParams; }; var events = ids.map(function (id) { var shapeData = shapes_1[id]; var prevValue = shapeData.graphicalContextData; var mergedValue = pick_1.pick(i.merge(prevValue, value_1), getGraphicalContextParams_1(id)); var newShapeData = i.assoc(shapeData, 'graphicalContextData', mergedValue); return history_event_1.shapeUpdateEvent(id, newShapeData); }); var action = editor_action_1.pushHistoryEventAction(history_event_1.historyEventsGroup(events)); return i.assoc(currentState, 'editorActionsQueue', i.push(currentState.editorActionsQueue, action)); } return currentState; } } exports["default"] = graphicalContextStateReducer; //# sourceMappingURL=graphical-context.js.map