UNPKG

@atlaskit/editor-plugin-history

Version:

History plugin for @atlaskit/editor-core

169 lines (168 loc) 6.36 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _safePlugin = require("@atlaskit/editor-common/safe-plugin"); var _utils = require("@atlaskit/editor-common/utils"); var _undo = require("@atlaskit/prosemirror-history/undo"); var _actions = require("./editor-actions/actions"); var _reducer = _interopRequireDefault(require("./editor-actions/reducer")); var _utils2 = require("./editor-actions/utils"); var _pluginKey = require("./pm-plugins/plugin-key"); var getInitialState = function getInitialState() { return { canUndo: false, canRedo: false }; }; var _pluginFactory = (0, _utils.pluginFactory)(_pluginKey.historyPluginKey, _reducer.default), createPluginState = _pluginFactory.createPluginState, getPluginState = _pluginFactory.getPluginState; /** * Expose prosemirror-history's `undo` as an `EditorCommand`. `undo(state, dispatch)` * builds its own transaction, so capture that transaction and return it in place of the * one passed in. Nothing is re-implemented: the returned transaction is exactly what * pressing Cmd+Z would dispatch, and callers may add metadata to it before it is * dispatched by `api.core.actions.execute`. */ var undoCommand = function undoCommand(editorViewRef) { return function (_ref) { var _editorViewRef$curren; var tr = _ref.tr; var state = (_editorViewRef$curren = editorViewRef.current) === null || _editorViewRef$curren === void 0 ? void 0 : _editorViewRef$curren.state; // The caller's `tr` is discarded, so refuse if it already carries changes or is stale. if (!state || tr.steps.length > 0 || tr.before !== state.doc) { return null; } var undoTr = null; (0, _undo.undo)(state, function (built) { undoTr = built; }); return undoTr; }; }; var createPlugin = function createPlugin(dispatch, editorViewRef) { return new _safePlugin.SafePlugin({ state: createPluginState(dispatch, getInitialState), key: _pluginKey.historyPluginKey, view: function view(editorView) { editorViewRef.current = editorView; return { destroy: function destroy() { if (editorViewRef.current === editorView) { editorViewRef.current = undefined; } } }; }, appendTransaction: function appendTransaction(transactions, oldState, newState) { if (transactions.find(function (tr) { return tr.docChanged && tr.getMeta('addToHistory') !== false || tr.getMeta('endHistorySlice'); })) { var pmHistoryPluginState = (0, _utils2.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: _actions.HistoryActionTypes.UPDATE, canUndo: canUndo, canRedo: canRedo }; return newState.tr.setMeta(_pluginKey.historyPluginKey, action); } } } }); }; var historyPlugin = function historyPlugin(_ref2) { var api = _ref2.api; var currentId = null; var editorViewRef = { current: undefined }; return { name: 'history', pmPlugins: function pmPlugins() { return [{ name: 'history', plugin: function plugin(_ref3) { var dispatch = _ref3.dispatch; return createPlugin(dispatch, editorViewRef); } }]; }, getSharedState: function getSharedState(editorState) { var _getPmHistoryPluginSt, _done$eventCount, _undone$eventCount; if (!editorState) { return undefined; } var historyPluginState = _pluginKey.historyPluginKey.getState(editorState); if (!historyPluginState) { return undefined; } var _ref4 = (_getPmHistoryPluginSt = (0, _utils2.getPmHistoryPluginState)(editorState)) !== null && _getPmHistoryPluginSt !== void 0 ? _getPmHistoryPluginSt : {}, done = _ref4.done, undone = _ref4.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: { undo: undoCommand(editorViewRef), updatePluginState: function updatePluginState(_ref5) { var _api$history$sharedSt; var tr = _ref5.tr; var _ref6 = (_api$history$sharedSt = api === null || api === void 0 ? void 0 : api.history.sharedState.currentState()) !== null && _api$history$sharedSt !== void 0 ? _api$history$sharedSt : {}, done = _ref6.done, undone = _ref6.undone; if (done === undefined || undone === undefined) { return tr; } var canUndo = done.eventCount > 0; var canRedo = undone.eventCount > 0; var action = { type: _actions.HistoryActionTypes.UPDATE, canUndo: canUndo, canRedo: canRedo }; return tr.setMeta(_pluginKey.historyPluginKey, action); }, startHistorySlice: function startHistorySlice(id) { return function (_ref7) { var tr = _ref7.tr; if (currentId) { return null; } currentId = id; return tr.setMeta('startHistorySlice', true); }; }, endHistorySlice: function endHistorySlice(id) { return function (_ref8) { var tr = _ref8.tr; if (currentId !== id) { return null; } currentId = null; return tr.setMeta('endHistorySlice', true); }; } } }; }; var _default = exports.default = historyPlugin;