UNPKG

@gorpacrate/core-graphics

Version:

A core library for creating shape-based graphic editors

109 lines 3.92 kB
"use strict"; exports.__esModule = true; var i = require("icepick"); var history_event_1 = require("../history/history-event"); var math_1 = require("../utils/math"); function getWantedIndex(params) { var relative = params.relative, length = params.length, current = params.current, value = params.value; if (relative) { var unpadded = current + value; if (unpadded <= 0) { return 0; } if (unpadded >= (length - 1)) { return length - 1; } return unpadded; } return math_1.modulus(value, length); } exports.INITIAL_SCENE = { shapes: {}, shapesOrder: [] }; // persisted events function reducePersistedHistoryToScene(history, initialScene) { if (initialScene === void 0) { initialScene = exports.INITIAL_SCENE; } if (history.length === 0) { return initialScene; } return history.reduce(function (scene, persistedEvent) { if (!persistedEvent.applied) { return scene; } return reducer(scene, history_event_1.getHistoryEventFromPersisted(persistedEvent)); }, initialScene); } exports["default"] = reducePersistedHistoryToScene; // unpersisted events function reduceHistoryEventsToScene(history, initialScene) { if (initialScene === void 0) { initialScene = exports.INITIAL_SCENE; } if (history.length === 0) { return initialScene; } return history.reduce(reducer, initialScene); } exports.reduceHistoryEventsToScene = reduceHistoryEventsToScene; // unpersisted events reducer function reducer(scene, event) { var shapes = scene.shapes, shapesOrder = scene.shapesOrder; if (history_event_1.isShapeAddEvent(event)) { var addId = event.payload.id; var nextShapes = i.assoc(shapes, addId, event.payload); var nextShapesOrder = i.push(shapesOrder, addId); return i.chain(scene) .assoc('shapes', nextShapes) .assoc('shapesOrder', nextShapesOrder) .value(); } if (history_event_1.isShapeUpdateEvent(event)) { var _a = event.payload, updateId = _a.id, shapeData = _a.shapeData; if (!scene.shapes[updateId]) { return scene; } var nextShapes = i.assoc(shapes, updateId, shapeData); return i.assoc(scene, 'shapes', nextShapes); } if (history_event_1.isShapeRemoveEvent(event)) { var removeId_1 = event.payload.id; if (!shapes[removeId_1]) { return scene; } var nextShapes = i.dissoc(shapes, removeId_1); var nextShapesOrder = i.filter(function (id) { return (id !== removeId_1); }, shapesOrder); return i.chain(scene) .assoc('shapes', nextShapes) .assoc('shapesOrder', nextShapesOrder) .value(); } if (history_event_1.isShapeChangeOrderEvent(event)) { var castEvent = event; var _b = castEvent.payload, id = _b.id, value = _b.value, relative = _b.relative; var newOrder = i.thaw(shapesOrder); var length_1 = newOrder.length; var currentIndex = newOrder.indexOf(id); var wantedIndex = getWantedIndex({ relative: relative, length: length_1, current: currentIndex, value: value }); if (wantedIndex > currentIndex) { newOrder.splice(wantedIndex + 1, 0, id); newOrder.splice(currentIndex, 1); } else { newOrder.splice(currentIndex, 1); newOrder.splice(wantedIndex, 0, id); } return i.assoc(scene, 'shapesOrder', newOrder); } if (event.type === 5 /* EraseAll */) { return { shapes: {}, shapesOrder: [] }; } if (event.type === 6 /* Grouped */) { var events = event.payload; return events.reduce(reducer, scene); } return scene; } exports.reducer = reducer; //# sourceMappingURL=history-reducer.js.map