UNPKG

@atlaskit/editor-plugin-alignment

Version:

Alignment plugin for @atlaskit/editor-core

110 lines 4.62 kB
import React from 'react'; import { alignment } from '@atlaskit/adf-schema'; import { fg } from '@atlaskit/platform-feature-flags'; import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments'; import { keymapPlugin } from './pm-plugins/keymap'; import { createPlugin, pluginKey } from './pm-plugins/main'; import { FloatingToolbarComponent } from './ui/FloatingToolbarComponent'; import { PrimaryToolbarComponent } from './ui/PrimaryToolbarComponent'; import { getToolbarComponents } from './ui/toolbar-components'; const defaultConfig = { align: 'start' }; export const alignmentPlugin = ({ api }) => { const isToolbarAIFCEnabled = Boolean(api === null || api === void 0 ? void 0 : api.toolbar); const primaryToolbarComponent = ({ editorView, popupsMountPoint, popupsBoundariesElement, popupsScrollableElement, disabled, isToolbarReducedSpacing }) => { if (!editorView) { return null; } return /*#__PURE__*/React.createElement(PrimaryToolbarComponent, { api: api, editorView: editorView, disabled: disabled, popupsMountPoint: popupsMountPoint, popupsBoundariesElement: popupsBoundariesElement, popupsScrollableElement: popupsScrollableElement, isToolbarReducedSpacing: isToolbarReducedSpacing }); }; 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: 'alignment', component: primaryToolbarComponent }); } return { name: 'alignment', marks() { return [{ name: 'alignment', mark: alignment }]; }, getSharedState(editorState) { if (!editorState) { return undefined; } const pluginState = pluginKey.getState(editorState); return pluginState ? { align: pluginState.align, isEnabled: pluginState.isEnabled } : undefined; }, pmPlugins() { return [{ name: 'alignmentPlugin', plugin: ({ dispatch }) => createPlugin(dispatch, defaultConfig) }, { name: 'annotationKeymap', plugin: () => keymapPlugin(api) }]; }, pluginsOptions: { selectionToolbar: isToolbarAIFCEnabled ? undefined : () => { var _api$userPreferences, _api$userPreferences$, _api$userPreferences$2, _api$selectionToolbar, _api$selectionToolbar2, _api$selectionToolbar3; const toolbarDocking = fg('platform_editor_use_preferences_plugin') ? api === null || api === void 0 ? void 0 : (_api$userPreferences = api.userPreferences) === null || _api$userPreferences === void 0 ? void 0 : (_api$userPreferences$ = _api$userPreferences.sharedState.currentState()) === null || _api$userPreferences$ === void 0 ? void 0 : (_api$userPreferences$2 = _api$userPreferences$.preferences) === null || _api$userPreferences$2 === void 0 ? void 0 : _api$userPreferences$2.toolbarDockingPosition : api === null || api === void 0 ? void 0 : (_api$selectionToolbar = api.selectionToolbar) === null || _api$selectionToolbar === void 0 ? void 0 : (_api$selectionToolbar2 = _api$selectionToolbar.sharedState) === null || _api$selectionToolbar2 === void 0 ? void 0 : (_api$selectionToolbar3 = _api$selectionToolbar2.currentState()) === null || _api$selectionToolbar3 === void 0 ? void 0 : _api$selectionToolbar3.toolbarDocking; if (toolbarDocking === 'none' && editorExperiment('platform_editor_controls', 'variant1', { exposure: true })) { const toolbarCustom = { type: 'custom', render: view => { if (!view) { return; } return /*#__PURE__*/React.createElement(FloatingToolbarComponent, { api: api, editorView: view }); }, fallback: [] }; return { isToolbarAbove: true, items: [toolbarCustom], rank: 4, pluginName: 'alignment' }; } else { return undefined; } } }, primaryToolbarComponent: !(api !== null && api !== void 0 && api.primaryToolbar) && !isToolbarAIFCEnabled ? primaryToolbarComponent : undefined }; };