@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
112 lines (111 loc) • 5.47 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.showDiffPlugin = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _getDeletedWidgets2 = require("./pm-plugins/getDeletedWidgets");
var _getScrollableDecorations = require("./pm-plugins/getScrollableDecorations");
var _main = require("./pm-plugins/main");
var _IndicatorBarContentComponent = require("./ui/IndicatorBar/IndicatorBarContentComponent");
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) { (0, _defineProperty2.default)(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; }
var showDiffPlugin = exports.showDiffPlugin = function showDiffPlugin(_ref) {
var api = _ref.api,
config = _ref.config;
// Captured from the plugin's view lifecycle (see `createPlugin`) so read-only actions can access
// the current state without exposing the plugin key to consumers.
var editorView;
var setEditorView = function setEditorView(view) {
editorView = view;
};
return {
name: 'showDiff',
actions: {
getDeletedWidgets: function getDeletedWidgets(range) {
return editorView ? (0, _getDeletedWidgets2.getDeletedWidgets)(editorView.state, range) : [];
}
},
commands: {
showDiff: function showDiff(params) {
return function (_ref2) {
var tr = _ref2.tr;
if ((0, _expValEquals.expValEquals)('platform_editor_diff_plugin_extended', 'isEnabled', true)) {
var _api$userIntent;
api === null || api === void 0 || (_api$userIntent = api.userIntent) === null || _api$userIntent === void 0 || _api$userIntent.commands.setCurrentUserIntent('viewingDiff')({
tr: tr
});
}
return tr.setMeta(_main.showDiffPluginKey, _objectSpread(_objectSpread({}, params), {}, {
action: 'SHOW_DIFF'
}));
};
},
hideDiff: function hideDiff(_ref3) {
var tr = _ref3.tr;
if ((0, _expValEquals.expValEquals)('platform_editor_diff_plugin_extended', 'isEnabled', true)) {
var _api$userIntent2;
api === null || api === void 0 || (_api$userIntent2 = api.userIntent) === null || _api$userIntent2 === void 0 || _api$userIntent2.commands.setCurrentUserIntent('default')({
tr: tr
});
}
return tr.setMeta(_main.showDiffPluginKey, {
steps: [],
action: 'HIDE_DIFF'
});
},
scrollToNext: function scrollToNext(_ref4) {
var tr = _ref4.tr;
return tr.setMeta(_main.showDiffPluginKey, {
action: 'SCROLL_TO_NEXT'
});
},
scrollToPrevious: function scrollToPrevious(_ref5) {
var tr = _ref5.tr;
return tr.setMeta(_main.showDiffPluginKey, {
action: 'SCROLL_TO_PREVIOUS'
});
}
},
pmPlugins: function pmPlugins() {
return [{
name: 'showDiffPlugin',
plugin: function plugin(_ref6) {
var getIntl = _ref6.getIntl;
return (0, _main.createPlugin)(config, getIntl, api, setEditorView);
}
}];
},
contentComponent: function contentComponent() {
if (!(0, _expValEquals.expValEquals)('platform_editor_diff_plugin_extended', 'isEnabled', true)) {
return null;
}
return /*#__PURE__*/_react.default.createElement(_IndicatorBarContentComponent.IndicatorBarContentComponent, {
api: api
});
},
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return _objectSpread({
isDisplayingChanges: false,
activeIndex: undefined
}, (0, _expValEquals.expValEquals)('platform_editor_diff_plugin_extended', 'isEnabled', true) ? {
diffDescriptors: []
} : {});
}
var pluginState = _main.showDiffPluginKey.getState(editorState);
var decorationCount = (0, _getScrollableDecorations.getScrollableDecorations)(pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorations, editorState.doc);
return _objectSpread({
isDisplayingChanges: decorationCount.length > 0,
activeIndex: pluginState === null || pluginState === void 0 ? void 0 : pluginState.activeIndex,
numberOfChanges: decorationCount.length
}, (0, _expValEquals.expValEquals)('platform_editor_diff_plugin_extended', 'isEnabled', true) ? {
diffDescriptors: pluginState === null || pluginState === void 0 ? void 0 : pluginState.diffDescriptors,
showIndicators: pluginState === null || pluginState === void 0 ? void 0 : pluginState.showIndicators
} : {});
}
};
};