@atlaskit/editor-plugin-panel
Version:
Panel plugin for @atlaskit/editor-core.
238 lines • 9.11 kB
JavaScript
import React from 'react';
import { extendedPanel, extendedPanelC1, extendedPanelC1WithLocalId, extendedPanelWithLocalId, PanelType } from '@atlaskit/adf-schema';
import { TRANSFORM_STRUCTURE_PANEL_MENU_ITEM, TRANSFORM_STRUCTURE_MENU_SECTION, TRANSFORM_STRUCTURE_MENU_SECTION_RANK } from '@atlaskit/editor-common/block-menu';
import { blockTypeMessages } from '@atlaskit/editor-common/messages';
import { IconCustomPanel, IconPanel, IconPanelError, IconPanelNote, IconPanelSuccess, IconPanelWarning } from '@atlaskit/editor-common/quick-insert';
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
import { createPanelAction } from './pm-plugins/commands/create-panel-action';
import keymap from './pm-plugins/keymaps';
import { createPlugin } from './pm-plugins/main';
import { createPanelBlockMenuItem } from './ui/panelBlockMenuItem';
import { getPanelQuickInsertComponents } from './ui/quick-insert/getPanelQuickInsertComponents';
import { getToolbarConfig } from './ui/toolbar';
const PANEL_NODE_NAME = 'panel';
const panelPlugin = ({
config: {
allowCustomPanel = false,
allowCustomPanelEdit = false
} = {},
api
}) => {
if (editorExperiment('platform_editor_block_menu', true)) {
var _api$blockMenu;
api === null || api === void 0 ? void 0 : (_api$blockMenu = api.blockMenu) === null || _api$blockMenu === void 0 ? void 0 : _api$blockMenu.actions.registerBlockMenuComponents([{
type: 'block-menu-item',
key: TRANSFORM_STRUCTURE_PANEL_MENU_ITEM.key,
parent: {
type: 'block-menu-section',
key: TRANSFORM_STRUCTURE_MENU_SECTION.key,
rank: TRANSFORM_STRUCTURE_MENU_SECTION_RANK[TRANSFORM_STRUCTURE_PANEL_MENU_ITEM.key]
},
component: createPanelBlockMenuItem(api),
isHidden: () => {
var _api$blockMenu2;
return Boolean(api === null || api === void 0 ? void 0 : (_api$blockMenu2 = api.blockMenu) === null || _api$blockMenu2 === void 0 ? void 0 : _api$blockMenu2.actions.isTransformOptionDisabled(PANEL_NODE_NAME));
}
}]);
}
const isRegisteredSlashCommandEnabled = isExperimentEnabled('platform_editor_slash_command');
if (isRegisteredSlashCommandEnabled) {
var _api$uiControlRegistr;
api === null || api === void 0 ? void 0 : (_api$uiControlRegistr = api.uiControlRegistry) === null || _api$uiControlRegistr === void 0 ? void 0 : _api$uiControlRegistr.actions.register(getPanelQuickInsertComponents({
allowCustomPanel,
allowCustomPanelEdit,
api
}));
}
return {
name: 'panel',
nodes() {
if (fg('platform_editor_adf_with_localid')) {
return [{
name: 'panel',
node: {
...extendedPanelWithLocalId(!!allowCustomPanel),
definingAsContext: true
}
}, ...(expValEquals('platform_editor_nest_table_in_panel', 'isEnabled', true) ? [{
name: 'panel_c1',
node: {
...extendedPanelC1WithLocalId(!!allowCustomPanel),
definingAsContext: true
}
}] : [])];
}
return [{
name: 'panel',
node: extendedPanel(!!allowCustomPanel)
}, ...(expValEquals('platform_editor_nest_table_in_panel', 'isEnabled', true) ? [{
name: 'panel_c1',
node: extendedPanelC1(!!allowCustomPanel)
}] : [])];
},
pmPlugins() {
return [{
name: 'panel',
plugin: ({
providerFactory,
dispatch,
nodeViewPortalProviderAPI
}) => createPlugin(dispatch, providerFactory, {
allowCustomPanel,
allowCustomPanelEdit
}, api, nodeViewPortalProviderAPI)
}, {
name: 'panelKeyMap',
plugin: () => keymap()
}];
},
actions: {
insertPanel(inputMethod) {
return function (state, dispatch) {
const tr = createPanelAction({
state,
attributes: {
panelType: PanelType.INFO
},
api,
inputMethod
});
if (!tr) {
return false;
}
if (dispatch) {
dispatch(tr);
}
return true;
};
}
},
pluginsOptions: {
...(isRegisteredSlashCommandEnabled ? {} : {
quickInsert: ({
formatMessage
}) => {
const quickInsertOptions = [{
id: 'infopanel',
title: formatMessage(blockTypeMessages.infoPanel),
keywords: ['panel'],
description: formatMessage(blockTypeMessages.infoPanelDescription),
priority: 800,
icon: () => /*#__PURE__*/React.createElement(IconPanel, null),
action(typeAheadInsert, state) {
return createPanelAction({
state,
attributes: {
panelType: PanelType.INFO
},
api,
typeAheadInsert
});
}
}, {
id: 'notepanel',
title: formatMessage(blockTypeMessages.notePanel),
description: formatMessage(blockTypeMessages.notePanelDescription),
priority: 1000,
icon: () => /*#__PURE__*/React.createElement(IconPanelNote, null),
action(typeAheadInsert, state) {
return createPanelAction({
state,
attributes: {
panelType: PanelType.NOTE
},
api,
typeAheadInsert
});
}
}, {
id: 'successpanel',
title: formatMessage(blockTypeMessages.successPanel),
description: formatMessage(blockTypeMessages.successPanelDescription),
keywords: ['tip'],
priority: 1000,
icon: () => /*#__PURE__*/React.createElement(IconPanelSuccess, null),
action(typeAheadInsert, state) {
return createPanelAction({
state,
attributes: {
panelType: PanelType.SUCCESS
},
api,
typeAheadInsert
});
}
}, {
id: 'warningpanel',
title: formatMessage(blockTypeMessages.warningPanel),
description: formatMessage(blockTypeMessages.warningPanelDescription),
priority: 1000,
icon: () => /*#__PURE__*/React.createElement(IconPanelWarning, null),
action(typeAheadInsert, state) {
return createPanelAction({
state,
attributes: {
panelType: PanelType.WARNING
},
api,
typeAheadInsert
});
}
}, {
id: 'errorpanel',
title: formatMessage(blockTypeMessages.errorPanel),
description: formatMessage(blockTypeMessages.errorPanelDescription),
priority: 1000,
icon: () => /*#__PURE__*/React.createElement(IconPanelError, null),
action(typeAheadInsert, state) {
return createPanelAction({
state,
attributes: {
panelType: PanelType.ERROR
},
api,
typeAheadInsert
});
}
}];
if (allowCustomPanel && allowCustomPanelEdit) {
quickInsertOptions.push({
id: 'custompanel',
title: formatMessage(blockTypeMessages.customPanel),
description: formatMessage(blockTypeMessages.customPanelDescription),
priority: 1000,
icon: () => /*#__PURE__*/React.createElement(IconCustomPanel, null),
action(typeAheadInsert, state) {
return createPanelAction({
state,
attributes: {
panelType: PanelType.CUSTOM,
panelIcon: ':rainbow:',
panelIconId: '1f308',
panelIconText: '🌈',
// Ignored via go/ees007
// eslint-disable-next-line @atlaskit/editor/enforce-todo-comment-format
// TODO: https://product-fabric.atlassian.net/browse/DSP-7268
// eslint-disable-next-line @atlaskit/design-system/ensure-design-token-usage
panelColor: '#E6FCFF'
},
api,
typeAheadInsert
});
}
});
}
return quickInsertOptions;
}
}),
floatingToolbar: (state, intl, providerFactory) => getToolbarConfig(state, intl, {
allowCustomPanel,
allowCustomPanelEdit
}, providerFactory, api)
}
};
};
export default panelPlugin;