UNPKG

@atlaskit/editor-plugin-text-color

Version:

Text color plugin for @atlaskit/editor-core

151 lines 6.93 kB
import React from 'react'; import { textColor } from '@atlaskit/adf-schema'; import { fg } from '@atlaskit/platform-feature-flags'; import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments'; import { changeColor as changeColorCommand } from './editor-commands/change-color'; import { setPalette as _setPalette } from './editor-commands/palette'; import { changeColor as _changeColor } from './pm-plugins/commands/change-color'; import { keymapPlugin } from './pm-plugins/keymap'; import { createPlugin, pluginKey as textColorPluginKey } from './pm-plugins/main'; import { FloatingToolbarComponent } from './ui/FloatingToolbarComponent'; import { PrimaryToolbarComponent } from './ui/PrimaryToolbarComponent'; import { getToolbarComponents } from './ui/toolbar-components'; var pluginConfig = function pluginConfig(textColorConfig) { if (!textColorConfig || typeof textColorConfig === 'boolean') { return undefined; } return textColorConfig; }; var isToolbarComponentEnabled = function isToolbarComponentEnabled(textColorConfig) { return textColorConfig === undefined || textColorConfig.toolbarConfig === undefined || textColorConfig.toolbarConfig.enabled !== false; }; export var textColorPlugin = function textColorPlugin(_ref) { var textColorConfig = _ref.config, api = _ref.api; var isToolbarAIFCEnabled = Boolean(api === null || api === void 0 ? void 0 : api.toolbar); var primaryToolbarComponent = function primaryToolbarComponent(_ref2) { var editorView = _ref2.editorView, popupsMountPoint = _ref2.popupsMountPoint, popupsBoundariesElement = _ref2.popupsBoundariesElement, popupsScrollableElement = _ref2.popupsScrollableElement, isToolbarReducedSpacing = _ref2.isToolbarReducedSpacing, dispatchAnalyticsEvent = _ref2.dispatchAnalyticsEvent, disabled = _ref2.disabled; if (!editorView) { return null; } return /*#__PURE__*/React.createElement(PrimaryToolbarComponent, { isReducedSpacing: isToolbarReducedSpacing, editorView: editorView, popupsMountPoint: popupsMountPoint, popupsBoundariesElement: popupsBoundariesElement, popupsScrollableElement: popupsScrollableElement, dispatchAnalyticsEvent: dispatchAnalyticsEvent, disabled: disabled, api: api }); }; if (isToolbarAIFCEnabled) { if (fg('platform_editor_toolbar_aifc_text_color_config') || fg('platform_editor_toolbar_aifc_text_color_config_jsm')) { var _api$toolbar; if (api !== null && api !== void 0 && (_api$toolbar = api.toolbar) !== null && _api$toolbar !== void 0 && _api$toolbar.actions.registerComponents && isToolbarComponentEnabled(pluginConfig(textColorConfig))) { api.toolbar.actions.registerComponents(getToolbarComponents(api)); } } else { var _api$toolbar2; if (api !== null && api !== void 0 && (_api$toolbar2 = api.toolbar) !== null && _api$toolbar2 !== void 0 && _api$toolbar2.actions.registerComponents) { api.toolbar.actions.registerComponents(getToolbarComponents(api)); } } } else { var _api$primaryToolbar; api === null || api === void 0 || (_api$primaryToolbar = api.primaryToolbar) === null || _api$primaryToolbar === void 0 || _api$primaryToolbar.actions.registerComponent({ name: 'textColor', component: primaryToolbarComponent }); } return { name: 'textColor', marks: function marks() { return [{ name: 'textColor', mark: textColor }]; }, pmPlugins: function pmPlugins() { return isToolbarAIFCEnabled ? [{ name: 'textColor', plugin: function plugin(_ref3) { var dispatch = _ref3.dispatch; return createPlugin(dispatch, pluginConfig(textColorConfig)); } }, { name: 'textColorKeymap', plugin: function plugin() { return keymapPlugin({ api: api }); } }] : [{ name: 'textColor', plugin: function plugin(_ref4) { var dispatch = _ref4.dispatch; return createPlugin(dispatch, pluginConfig(textColorConfig)); } }]; }, getSharedState: function getSharedState(editorState) { if (!editorState) { return undefined; } return textColorPluginKey.getState(editorState); }, actions: { changeColor: function changeColor(color, inputMethod) { var _api$analytics; return _changeColor(color, api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, inputMethod); } }, commands: { changeColor: function changeColor(color, inputMethod) { return changeColorCommand(color, api, inputMethod); }, setPalette: function setPalette(isPaletteOpen) { return _setPalette(isPaletteOpen); } }, pluginsOptions: !isToolbarAIFCEnabled ? { selectionToolbar: function selectionToolbar() { var _api$userPreferences, _api$selectionToolbar; var toolbarDocking = fg('platform_editor_use_preferences_plugin') ? api === null || api === void 0 || (_api$userPreferences = api.userPreferences) === null || _api$userPreferences === void 0 || (_api$userPreferences = _api$userPreferences.sharedState) === null || _api$userPreferences === void 0 || (_api$userPreferences = _api$userPreferences.currentState()) === null || _api$userPreferences === void 0 ? void 0 : _api$userPreferences.preferences.toolbarDockingPosition : api === null || api === void 0 || (_api$selectionToolbar = api.selectionToolbar) === null || _api$selectionToolbar === void 0 || (_api$selectionToolbar = _api$selectionToolbar.sharedState) === null || _api$selectionToolbar === void 0 || (_api$selectionToolbar = _api$selectionToolbar.currentState()) === null || _api$selectionToolbar === void 0 ? void 0 : _api$selectionToolbar.toolbarDocking; if (toolbarDocking === 'none' && editorExperiment('platform_editor_controls', 'variant1', { exposure: true })) { var toolbarCustom = { type: 'custom', render: function render(view, _idx, dispatchAnalyticsEvent) { if (!view) { return; } return /*#__PURE__*/React.createElement(FloatingToolbarComponent, { editorView: view, dispatchAnalyticsEvent: dispatchAnalyticsEvent, api: api }); }, fallback: [] }; return { isToolbarAbove: true, items: [toolbarCustom], rank: 6, pluginName: 'textColor' }; } else { return undefined; } } } : {}, primaryToolbarComponent: !(api !== null && api !== void 0 && api.primaryToolbar) ? primaryToolbarComponent : undefined }; };