@gorpacrate/core-graphics
Version:
A core library for creating shape-based graphic editors
72 lines • 3.68 kB
JavaScript
;
exports.__esModule = true;
var i = require("icepick");
var editor_action_1 = require("../../editor-state/editor-action");
var event_sourcing_1 = require("../../event-sourcing/event-sourcing");
var history_event_1 = require("../../history/history-event");
var history_reducer_1 = require("../../history/history-reducer");
var history_1 = require("../events/history");
function getUnappliedEventsIds(author, history) {
return history
.filter(function (ev) { return ((ev.author === author) && (ev.applied === false)); })
.map(function (ev) { return ev.id; });
}
function mustDeleteHistoryEvents(currentState) {
var editorActionsQueue = currentState.editorActionsQueue;
var pushEvents = editorActionsQueue.filter(editor_action_1.isPushHistoryEventAction);
return (pushEvents.length > 0);
}
function deleteUndoneHistoryEvents(currentState) {
var editorActionsQueue = currentState.editorActionsQueue, persistedHistory = currentState.persistedHistory, author = currentState.author;
var eventsToDeleteIds = getUnappliedEventsIds(author, persistedHistory);
var deleteEventsActions = eventsToDeleteIds.map(editor_action_1.deleteHistoryEventAction);
var newQueue = deleteEventsActions.concat(editorActionsQueue);
return i.assoc(currentState, 'editorActionsQueue', newQueue);
}
function historyStateReducer(currentState, ev) {
// delete events if can redo and has non-empty push actions queue
if (mustDeleteHistoryEvents(currentState)) {
return deleteUndoneHistoryEvents(currentState);
}
if (!history_1.isEditorHistoryEvent(ev)) {
return currentState;
}
else {
if (history_1.isUpdateHistoryEvent(ev)) {
var newHistory = ev.payload.value;
var newPersistedScene = history_reducer_1["default"](newHistory);
return i.chain(currentState)
.assoc('persistedHistory', newHistory)
.assoc('persistedScene', newPersistedScene)
.assoc('scene', newPersistedScene)
.value();
}
if (history_1.isUndoHistoryEvent(ev)) {
var persistedHistory = currentState.persistedHistory, author = currentState.author;
var eventToUndo = event_sourcing_1.getUndoEvent(persistedHistory, author);
if (!history_event_1.isPersistedHistoryEvent(eventToUndo)) {
return currentState;
}
if (history_event_1.isPersistedHistoryEvent(eventToUndo)) {
var undoneEvent = i.assoc(eventToUndo, 'applied', false);
var newQueue = i.push(currentState.editorActionsQueue, editor_action_1.updateHistoryEventAction(eventToUndo.id, undoneEvent));
return i.merge(currentState, { editorActionsQueue: newQueue });
}
}
if (history_1.isRedoHistoryEvent(ev)) {
var persistedHistory = currentState.persistedHistory, author = currentState.author;
var eventToRedo = event_sourcing_1.getRedoEvent(persistedHistory, author);
if (!history_event_1.isPersistedHistoryEvent(eventToRedo)) {
return currentState;
}
if (history_event_1.isPersistedHistoryEvent(eventToRedo)) {
var redoneEvent = i.assoc(eventToRedo, 'applied', true);
var newQueue = i.push(currentState.editorActionsQueue, editor_action_1.updateHistoryEventAction(eventToRedo.id, redoneEvent));
return i.merge(currentState, { editorActionsQueue: newQueue });
}
}
return currentState;
}
}
exports["default"] = historyStateReducer;
//# sourceMappingURL=history.js.map