core-graphics
Version:
A core library for creating shape-based graphic editors
97 lines (79 loc) • 4.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = historyStateReducer;
var _history = require('../events/history');
var _editorAction = require('../../editor-state/editor-action');
var _historyEvent = require('../../history/history-event');
var _historyReducer = require('../../history/history-reducer');
var _historyReducer2 = _interopRequireDefault(_historyReducer);
var _eventSourcing = require('../../event-sourcing/event-sourcing');
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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
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(_editorAction.isPushHistoryEventAction);
return pushEvents.length > 0;
}
function deleteUndoneHistoryEvents(currentState) {
var editorActionsQueue = currentState.editorActionsQueue;
var persistedHistory = currentState.persistedHistory;
var author = currentState.author;
var eventsToDeleteIds = getUnappliedEventsIds(author, persistedHistory);
var deleteEventsActions = eventsToDeleteIds.map(_editorAction.deleteHistoryEventAction);
var newQueue = [].concat(_toConsumableArray(deleteEventsActions), _toConsumableArray(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 (!(0, _history.isEditorHistoryEvent)(ev)) {
return currentState;
} else {
if ((0, _history.isUpdateHistoryEvent)(ev)) {
var newHistory = ev.payload.value;
var newPersistedScene = (0, _historyReducer2.default)(newHistory);
return i.chain(currentState).assoc('persistedHistory', newHistory).assoc('persistedScene', newPersistedScene).assoc('scene', newPersistedScene).value();
}
if ((0, _history.isUndoHistoryEvent)(ev)) {
var persistedHistory = currentState.persistedHistory;
var author = currentState.author;
var eventToUndo = (0, _eventSourcing.getUndoEvent)(persistedHistory, author);
if (!(0, _historyEvent.isPersistedHistoryEvent)(eventToUndo)) {
return currentState;
}
if ((0, _historyEvent.isPersistedHistoryEvent)(eventToUndo)) {
var undoneEvent = i.assoc(eventToUndo, 'applied', false);
var newQueue = i.push(currentState.editorActionsQueue, (0, _editorAction.updateHistoryEventAction)(eventToUndo.id, undoneEvent));
return i.merge(currentState, { editorActionsQueue: newQueue });
}
}
if ((0, _history.isRedoHistoryEvent)(ev)) {
var _persistedHistory = currentState.persistedHistory;
var _author = currentState.author;
var eventToRedo = (0, _eventSourcing.getRedoEvent)(_persistedHistory, _author);
if (!(0, _historyEvent.isPersistedHistoryEvent)(eventToRedo)) {
return currentState;
}
if ((0, _historyEvent.isPersistedHistoryEvent)(eventToRedo)) {
var redoneEvent = i.assoc(eventToRedo, 'applied', true);
var _newQueue = i.push(currentState.editorActionsQueue, (0, _editorAction.updateHistoryEventAction)(eventToRedo.id, redoneEvent));
return i.merge(currentState, { editorActionsQueue: _newQueue });
}
}
return currentState;
}
}