@atlaskit/editor-plugin-show-diff
Version:
ShowDiff plugin for @atlaskit/editor-core
105 lines • 3.81 kB
JavaScript
import React from 'react';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { getDeletedWidgets } from './pm-plugins/getDeletedWidgets';
import { getScrollableDecorations } from './pm-plugins/getScrollableDecorations';
import { createPlugin, showDiffPluginKey } from './pm-plugins/main';
import { IndicatorBarContentComponent } from './ui/IndicatorBar/IndicatorBarContentComponent';
export const showDiffPlugin = ({
api,
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.
let editorView;
const setEditorView = view => {
editorView = view;
};
return {
name: 'showDiff',
actions: {
getDeletedWidgets: range => editorView ? getDeletedWidgets(editorView.state, range) : []
},
commands: {
showDiff: params => ({
tr
}) => {
if (expValEquals('platform_editor_diff_plugin_extended', 'isEnabled', true)) {
var _api$userIntent;
api === null || api === void 0 ? void 0 : (_api$userIntent = api.userIntent) === null || _api$userIntent === void 0 ? void 0 : _api$userIntent.commands.setCurrentUserIntent('viewingDiff')({
tr
});
}
return tr.setMeta(showDiffPluginKey, {
...params,
action: 'SHOW_DIFF'
});
},
hideDiff: ({
tr
}) => {
if (expValEquals('platform_editor_diff_plugin_extended', 'isEnabled', true)) {
var _api$userIntent2;
api === null || api === void 0 ? void 0 : (_api$userIntent2 = api.userIntent) === null || _api$userIntent2 === void 0 ? void 0 : _api$userIntent2.commands.setCurrentUserIntent('default')({
tr
});
}
return tr.setMeta(showDiffPluginKey, {
steps: [],
action: 'HIDE_DIFF'
});
},
scrollToNext: ({
tr
}) => {
return tr.setMeta(showDiffPluginKey, {
action: 'SCROLL_TO_NEXT'
});
},
scrollToPrevious: ({
tr
}) => {
return tr.setMeta(showDiffPluginKey, {
action: 'SCROLL_TO_PREVIOUS'
});
}
},
pmPlugins() {
return [{
name: 'showDiffPlugin',
plugin: ({
getIntl
}) => createPlugin(config, getIntl, api, setEditorView)
}];
},
contentComponent: () => {
if (!expValEquals('platform_editor_diff_plugin_extended', 'isEnabled', true)) {
return null;
}
return /*#__PURE__*/React.createElement(IndicatorBarContentComponent, {
api: api
});
},
getSharedState: editorState => {
if (!editorState) {
return {
isDisplayingChanges: false,
activeIndex: undefined,
...(expValEquals('platform_editor_diff_plugin_extended', 'isEnabled', true) ? {
diffDescriptors: []
} : {})
};
}
const pluginState = showDiffPluginKey.getState(editorState);
const decorationCount = getScrollableDecorations(pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorations, editorState.doc);
return {
isDisplayingChanges: decorationCount.length > 0,
activeIndex: pluginState === null || pluginState === void 0 ? void 0 : pluginState.activeIndex,
numberOfChanges: decorationCount.length,
...(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
} : {})
};
}
};
};