@atlaskit/editor-plugin-highlight
Version:
Highlight plugin for @atlaskit/editor-core
119 lines • 5.49 kB
JavaScript
import React from 'react';
import { backgroundColor } from '@atlaskit/adf-schema';
import { fg } from '@atlaskit/platform-feature-flags';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
import { changeColor } from './editor-commands/change-color';
import { createHighlightPaddingPlugin } from './pm-plugins/highlight-padding';
import { keymapPlugin } from './pm-plugins/keymap';
import { createPlugin, highlightPluginKey } from './pm-plugins/main';
import { FloatingToolbarHighlightColorWithIntl as FloatingToolbarHighlightColor } from './ui/FloatingToolbarHighlightColor';
import { PrimaryToolbarHighlightColorWithIntl as PrimaryToolbarHighlightColor } from './ui/PrimaryToolbarHighlightColor';
import { getToolbarComponent } from './ui/toolbar-component';
export var highlightPlugin = function highlightPlugin(_ref) {
var _api$analytics;
var api = _ref.api;
var editorAnalyticsAPI = api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions;
var isToolbarAIFCEnabled = Boolean(api === null || api === void 0 ? void 0 : api.toolbar);
var primaryToolbarComponent = function primaryToolbarComponent(_ref2) {
var popupsMountPoint = _ref2.popupsMountPoint,
popupsBoundariesElement = _ref2.popupsBoundariesElement,
popupsScrollableElement = _ref2.popupsScrollableElement,
disabled = _ref2.disabled,
isToolbarReducedSpacing = _ref2.isToolbarReducedSpacing,
editorView = _ref2.editorView;
if (!editorView) {
return null;
}
return /*#__PURE__*/React.createElement(PrimaryToolbarHighlightColor, {
popupsMountPoint: popupsMountPoint,
popupsBoundariesElement: popupsBoundariesElement,
popupsScrollableElement: popupsScrollableElement,
disabled: disabled,
isToolbarReducedSpacing: isToolbarReducedSpacing,
pluginInjectionApi: api,
editorView: editorView
});
};
if (!isToolbarAIFCEnabled) {
var _api$primaryToolbar;
api === null || api === void 0 || (_api$primaryToolbar = api.primaryToolbar) === null || _api$primaryToolbar === void 0 || _api$primaryToolbar.actions.registerComponent({
name: 'highlight',
component: primaryToolbarComponent
});
} else {
var _api$toolbar;
api === null || api === void 0 || (_api$toolbar = api.toolbar) === null || _api$toolbar === void 0 || _api$toolbar.actions.registerComponents(getToolbarComponent(api));
}
return {
name: 'highlight',
marks: function marks() {
return [{
name: 'backgroundColor',
mark: backgroundColor
}];
},
commands: {
changeColor: changeColor(editorAnalyticsAPI)
},
pmPlugins: function pmPlugins() {
var plugins = [{
name: 'highlight',
plugin: function plugin() {
return createPlugin({
api: api
});
}
}, {
name: 'highlightKeymap',
plugin: function plugin() {
return keymapPlugin({
api: api
});
}
}, {
name: 'highlightPadding',
plugin: function plugin() {
return createHighlightPaddingPlugin();
}
}];
return plugins;
},
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return;
}
return highlightPluginKey.getState(editorState);
},
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.currentState()) === null || _api$userPreferences === void 0 || (_api$userPreferences = _api$userPreferences.preferences) === null || _api$userPreferences === void 0 ? void 0 : _api$userPreferences.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) {
return /*#__PURE__*/React.createElement(FloatingToolbarHighlightColor, {
dispatchAnalyticsEvent: dispatchAnalyticsEvent,
pluginInjectionApi: api,
editorView: view
});
},
fallback: []
};
var rank = editorExperiment('platform_editor_controls', 'variant1') ? 5 : -9;
return {
rank: rank,
isToolbarAbove: true,
items: [toolbarCustom],
pluginName: 'highlight'
};
} else {
return undefined;
}
}
} : {},
primaryToolbarComponent: !(api !== null && api !== void 0 && api.primaryToolbar) ? primaryToolbarComponent : undefined
};
};