@atlaskit/editor-plugin-undo-redo
Version:
Undo redo plugin for @atlaskit/editor-core
103 lines (102 loc) • 4.27 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import { ACTION_SUBJECT, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { withAnalytics } from '@atlaskit/editor-common/editor-analytics';
import { areNodesEqualIgnoreAttrs } from '@atlaskit/editor-common/utils/document';
import { InputSource } from './enums';
import { pluginKey as undoPluginKey } from './plugin-key';
export var attachInputMeta = function attachInputMeta(inputSource) {
return function (command) {
return function (state, dispatch) {
var customTr = state.tr;
var fakeDispatch = function fakeDispatch(tr) {
customTr = tr;
};
command(state, fakeDispatch);
if (!customTr || !customTr.docChanged) {
return false;
}
customTr.setMeta(undoPluginKey, inputSource);
if (dispatch) {
dispatch(customTr);
}
return true;
};
};
};
var inputSourceToInputMethod = function inputSourceToInputMethod(inputSource) {
switch (inputSource) {
case InputSource.EXTERNAL:
return INPUT_METHOD.EXTERNAL;
case InputSource.KEYBOARD:
return INPUT_METHOD.KEYBOARD;
case InputSource.TOOLBAR:
return INPUT_METHOD.TOOLBAR;
default:
return INPUT_METHOD.EXTERNAL;
}
};
function getNodesWithDifferingAttributes(_ref) {
var before = _ref.before,
after = _ref.after;
var allAttributeKeys = Object.keys(_objectSpread(_objectSpread({}, before.attrs), after.attrs));
var differingAttributes = allAttributeKeys.filter(function (key) {
return before.attrs[key] !== after.attrs[key];
});
var affectedNodes = differingAttributes.length ? [{
type: before.type.name,
attributes: differingAttributes
}] : [];
return before.children.reduce(function (acc, beforeChild, index) {
return [].concat(_toConsumableArray(acc), _toConsumableArray(getNodesWithDifferingAttributes({
before: beforeChild,
after: after.child(index)
})));
}, affectedNodes);
}
/**
* Analyzes changes between two ProseMirror document nodes.
*
* @param params - Object containing before and after node states
* @param params.before - The document node before changes
* @param params.after - The document node after changes
* @returns Object containing change analysis results with hasChanged boolean and affectedNodes array
*/
export function getChanges(_ref2) {
var before = _ref2.before,
after = _ref2.after;
var hasChanged = !areNodesEqualIgnoreAttrs(after, before);
var affectedNodes = hasChanged ? undefined : getNodesWithDifferingAttributes({
before: before,
after: after
})
// Limit to 25 nodes to avoid oversize payloads
.slice(0, 25);
return {
hasChanged: hasChanged,
affectedNodes: affectedNodes
};
}
export var attachInputMetaWithAnalytics = function attachInputMetaWithAnalytics(editorAnalyticsAPI) {
return function (inputSource, action) {
return function (command) {
return attachInputMeta(inputSource)(withAnalytics(editorAnalyticsAPI, function (_ref3, _ref4) {
var before = _ref3.doc;
var after = _ref4.currentDoc;
return {
eventType: EVENT_TYPE.TRACK,
action: action,
actionSubject: ACTION_SUBJECT.EDITOR,
attributes: _objectSpread({
inputMethod: inputSourceToInputMethod(inputSource)
}, getChanges({
before: before,
after: after
}))
};
})(command));
};
};
};