@atlaskit/editor-plugin-undo-redo
Version:
Undo redo plugin for @atlaskit/editor-core
118 lines • 5.02 kB
JavaScript
import React from 'react';
import { ACTION } from '@atlaskit/editor-common/analytics';
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { fg } from '@atlaskit/platform-feature-flags';
import { redo, undo } from '@atlaskit/prosemirror-history';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
import { attachInputMetaWithAnalytics } from './pm-plugins/attach-input-meta';
import { InputSource } from './pm-plugins/enums';
import { keymapPlugin } from './pm-plugins/keymaps';
import { createPlugin } from './pm-plugins/main';
import { forceFocus } from './pm-plugins/utils';
// Ignored via go/ees005
// eslint-disable-next-line import/no-named-as-default
import ToolbarUndoRedo from './ui/ToolbarUndoRedo';
import { getToolbarComponents } from './ui/ToolbarUndoRedo/toolbar-components';
export const undoRedoPlugin = ({
api,
config
}) => {
var _config$showToolbarBu;
const showToolbarButton = (_config$showToolbarBu = config === null || config === void 0 ? void 0 : config.showToolbarButton) !== null && _config$showToolbarBu !== void 0 ? _config$showToolbarBu : true;
const editorViewRef = {
current: null
};
const isToolbarAIFCEnabled = Boolean(api === null || api === void 0 ? void 0 : api.toolbar);
const primaryToolbarComponent = ({
editorView,
disabled,
isToolbarReducedSpacing
}) => {
if (!editorView) {
return null;
}
return /*#__PURE__*/React.createElement(ToolbarUndoRedo, {
isReducedSpacing: isToolbarReducedSpacing,
disabled: disabled,
editorView: editorView,
api: api
});
};
if (fg('platform_editor_ai_add_undoredo_jira')) {
if (showToolbarButton) {
if (isToolbarAIFCEnabled) {
var _api$toolbar;
api === null || api === void 0 ? void 0 : (_api$toolbar = api.toolbar) === null || _api$toolbar === void 0 ? void 0 : _api$toolbar.actions.registerComponents(getToolbarComponents(api));
} else {
var _api$primaryToolbar;
api === null || api === void 0 ? void 0 : (_api$primaryToolbar = api.primaryToolbar) === null || _api$primaryToolbar === void 0 ? void 0 : _api$primaryToolbar.actions.registerComponent({
name: 'undoRedoPlugin',
component: primaryToolbarComponent
});
}
}
} else {
if (isToolbarAIFCEnabled) {
var _api$toolbar2;
api === null || api === void 0 ? void 0 : (_api$toolbar2 = api.toolbar) === null || _api$toolbar2 === void 0 ? void 0 : _api$toolbar2.actions.registerComponents(getToolbarComponents(api));
} else {
var _api$primaryToolbar2;
api === null || api === void 0 ? void 0 : (_api$primaryToolbar2 = api.primaryToolbar) === null || _api$primaryToolbar2 === void 0 ? void 0 : _api$primaryToolbar2.actions.registerComponent({
name: 'undoRedoPlugin',
component: primaryToolbarComponent
});
}
}
const handleUndo = inputSource => {
var _api$analytics;
if (!editorViewRef.current) {
return false;
}
return forceFocus(editorViewRef.current, api)(attachInputMetaWithAnalytics(api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions)(inputSource || InputSource.EXTERNAL, ACTION.UNDO_PERFORMED)(undo));
};
const handleRedo = inputSource => {
var _api$analytics2;
if (!editorViewRef.current) {
return false;
}
return forceFocus(editorViewRef.current, api)(attachInputMetaWithAnalytics(api === null || api === void 0 ? void 0 : (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions)(inputSource || InputSource.EXTERNAL, ACTION.REDO_PERFORMED)(redo));
};
const showPrimaryToolbarComponent = fg('platform_editor_ai_add_undoredo_jira') ? !(api !== null && api !== void 0 && api.primaryToolbar) && !isToolbarAIFCEnabled && showToolbarButton : !(api !== null && api !== void 0 && api.primaryToolbar) && !isToolbarAIFCEnabled;
return {
name: 'undoRedoPlugin',
actions: {
undo: handleUndo,
redo: handleRedo
},
pmPlugins() {
const plugins = [{
name: 'undoRedoKeyMap',
plugin: () => keymapPlugin(api)
}, {
name: 'undoRedoPlugin',
plugin: options => createPlugin(options)
}];
if (editorExperiment('platform_editor_controls', 'variant1', {
exposure: false
})) {
plugins.push({
name: 'undoRedoGetEditorViewReferencePlugin',
plugin: () => {
return new SafePlugin({
view: editorView => {
editorViewRef.current = editorView;
return {
destroy: () => {
editorViewRef.current = null;
}
};
}
});
}
});
}
return plugins;
},
primaryToolbarComponent: showPrimaryToolbarComponent ? primaryToolbarComponent : undefined
};
};