@atlaskit/editor-plugin-history
Version:
History plugin for @atlaskit/editor-core
124 lines • 4.59 kB
JavaScript
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { pluginFactory } from '@atlaskit/editor-common/utils';
import { HistoryActionTypes } from './editor-actions/actions';
import reducer from './editor-actions/reducer';
import { getPmHistoryPluginState } from './editor-actions/utils';
import { historyPluginKey } from './pm-plugins/plugin-key';
var getInitialState = function getInitialState() {
return {
canUndo: false,
canRedo: false
};
};
var _pluginFactory = pluginFactory(historyPluginKey, reducer),
createPluginState = _pluginFactory.createPluginState,
getPluginState = _pluginFactory.getPluginState;
var createPlugin = function createPlugin(dispatch) {
return new SafePlugin({
state: createPluginState(dispatch, getInitialState),
key: historyPluginKey,
appendTransaction: function appendTransaction(transactions, oldState, newState) {
if (transactions.find(function (tr) {
return tr.docChanged && tr.getMeta('addToHistory') !== false || tr.getMeta('endHistorySlice');
})) {
var pmHistoryPluginState = getPmHistoryPluginState(newState);
if (!pmHistoryPluginState) {
return;
}
var canUndo = pmHistoryPluginState.done.eventCount > 0;
var canRedo = pmHistoryPluginState.undone.eventCount > 0;
var _getPluginState = getPluginState(newState),
prevCanUndo = _getPluginState.canUndo,
prevCanRedo = _getPluginState.canRedo;
if (canUndo !== prevCanUndo || canRedo !== prevCanRedo) {
var action = {
type: HistoryActionTypes.UPDATE,
canUndo: canUndo,
canRedo: canRedo
};
return newState.tr.setMeta(historyPluginKey, action);
}
}
}
});
};
var historyPlugin = function historyPlugin(_ref) {
var api = _ref.api;
var currentId = null;
return {
name: 'history',
pmPlugins: function pmPlugins() {
return [{
name: 'history',
plugin: function plugin(_ref2) {
var dispatch = _ref2.dispatch;
return createPlugin(dispatch);
}
}];
},
getSharedState: function getSharedState(editorState) {
var _getPmHistoryPluginSt, _done$eventCount, _undone$eventCount;
if (!editorState) {
return undefined;
}
var historyPluginState = historyPluginKey.getState(editorState);
if (!historyPluginState) {
return undefined;
}
var _ref3 = (_getPmHistoryPluginSt = getPmHistoryPluginState(editorState)) !== null && _getPmHistoryPluginSt !== void 0 ? _getPmHistoryPluginSt : {},
done = _ref3.done,
undone = _ref3.undone;
return {
canUndo: historyPluginState.canUndo,
canRedo: historyPluginState.canRedo,
done: {
eventCount: (_done$eventCount = done === null || done === void 0 ? void 0 : done.eventCount) !== null && _done$eventCount !== void 0 ? _done$eventCount : 0
},
undone: {
eventCount: (_undone$eventCount = undone === null || undone === void 0 ? void 0 : undone.eventCount) !== null && _undone$eventCount !== void 0 ? _undone$eventCount : 0
}
};
},
commands: {
updatePluginState: function updatePluginState(_ref4) {
var _api$history$sharedSt;
var tr = _ref4.tr;
var _ref5 = (_api$history$sharedSt = api === null || api === void 0 ? void 0 : api.history.sharedState.currentState()) !== null && _api$history$sharedSt !== void 0 ? _api$history$sharedSt : {},
done = _ref5.done,
undone = _ref5.undone;
if (done === undefined || undone === undefined) {
return tr;
}
var canUndo = done.eventCount > 0;
var canRedo = undone.eventCount > 0;
var action = {
type: HistoryActionTypes.UPDATE,
canUndo: canUndo,
canRedo: canRedo
};
return tr.setMeta(historyPluginKey, action);
},
startHistorySlice: function startHistorySlice(id) {
return function (_ref6) {
var tr = _ref6.tr;
if (currentId) {
return null;
}
currentId = id;
return tr.setMeta('startHistorySlice', true);
};
},
endHistorySlice: function endHistorySlice(id) {
return function (_ref7) {
var tr = _ref7.tr;
if (currentId !== id) {
return null;
}
currentId = null;
return tr.setMeta('endHistorySlice', true);
};
}
}
};
};
export default historyPlugin;