@atlaskit/editor-plugin-panel
Version:
Panel plugin for @atlaskit/editor-core.
294 lines (293 loc) • 11.9 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/defineProperty";
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
import React from 'react';
import { extendedPanel, extendedPanelWithLocalId, PanelType } from '@atlaskit/adf-schema';
import { ACTION, ACTION_SUBJECT, ACTION_SUBJECT_ID, EVENT_TYPE, INPUT_METHOD } from '@atlaskit/editor-common/analytics';
import { TRANSFORM_STRUCTURE_PANEL_MENU_ITEM, TRANSFORM_STRUCTURE_MENU_SECTION, TRANSFORM_STRUCTURE_MENU_SECTION_RANK } from '@atlaskit/editor-common/block-menu';
import { insertSelectedItem } from '@atlaskit/editor-common/insert';
import { blockTypeMessages } from '@atlaskit/editor-common/messages';
import { IconCustomPanel, IconPanel, IconPanelError, IconPanelNote, IconPanelSuccess, IconPanelWarning } from '@atlaskit/editor-common/quick-insert';
import { createWrapSelectionTransaction } from '@atlaskit/editor-common/utils';
import { fg } from '@atlaskit/platform-feature-flags';
import { editorExperiment } from '@atlaskit/tmp-editor-statsig/experiments';
import keymap from './pm-plugins/keymaps';
import { createPlugin } from './pm-plugins/main';
import { createPanelBlockMenuItem } from './ui/panelBlockMenuItem';
import { getToolbarConfig } from './ui/toolbar';
var PANEL_NODE_NAME = 'panel';
var panelPlugin = function panelPlugin(_ref) {
var _ref$config = _ref.config,
_ref$config2 = _ref$config === void 0 ? {} : _ref$config,
_ref$config2$allowCus = _ref$config2.allowCustomPanel,
allowCustomPanel = _ref$config2$allowCus === void 0 ? false : _ref$config2$allowCus,
_ref$config2$allowCus2 = _ref$config2.allowCustomPanelEdit,
allowCustomPanelEdit = _ref$config2$allowCus2 === void 0 ? false : _ref$config2$allowCus2,
api = _ref.api;
if (editorExperiment('platform_editor_block_menu', true)) {
var _api$blockMenu;
api === null || api === void 0 || (_api$blockMenu = api.blockMenu) === null || _api$blockMenu === 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: function isHidden() {
var _api$blockMenu2;
return Boolean(api === null || api === void 0 || (_api$blockMenu2 = api.blockMenu) === null || _api$blockMenu2 === void 0 ? void 0 : _api$blockMenu2.actions.isTransformOptionDisabled(PANEL_NODE_NAME));
}
}]);
}
return {
name: 'panel',
nodes: function nodes() {
if (fg('platform_editor_adf_with_localid')) {
return [{
name: 'panel',
node: _objectSpread(_objectSpread({}, extendedPanelWithLocalId(!!allowCustomPanel)), {}, {
definingAsContext: true
})
}];
}
return [{
name: 'panel',
node: extendedPanel(!!allowCustomPanel)
}];
},
pmPlugins: function pmPlugins() {
return [{
name: 'panel',
plugin: function plugin(_ref2) {
var providerFactory = _ref2.providerFactory,
dispatch = _ref2.dispatch,
nodeViewPortalProviderAPI = _ref2.nodeViewPortalProviderAPI;
return createPlugin(dispatch, providerFactory, {
allowCustomPanel: allowCustomPanel,
allowCustomPanelEdit: allowCustomPanelEdit
}, api, nodeViewPortalProviderAPI);
}
}, {
name: 'panelKeyMap',
plugin: function plugin() {
return keymap();
}
}];
},
actions: {
insertPanel: function insertPanel(inputMethod) {
return function (state, dispatch) {
var tr = createPanelAction({
state: state,
attributes: {
panelType: PanelType.INFO
},
api: api,
inputMethod: inputMethod
});
if (!tr) {
return false;
}
if (dispatch) {
dispatch(tr);
}
return true;
};
}
},
pluginsOptions: {
quickInsert: function quickInsert(_ref3) {
var formatMessage = _ref3.formatMessage;
var quickInsertOptions = [{
id: 'infopanel',
title: formatMessage(blockTypeMessages.infoPanel),
keywords: ['panel'],
description: formatMessage(blockTypeMessages.infoPanelDescription),
priority: 800,
icon: function icon() {
return /*#__PURE__*/React.createElement(IconPanel, null);
},
action: function action(typeAheadInsert, state) {
return createPanelAction({
state: state,
attributes: {
panelType: PanelType.INFO
},
api: api,
typeAheadInsert: typeAheadInsert
});
}
}, {
id: 'notepanel',
title: formatMessage(blockTypeMessages.notePanel),
description: formatMessage(blockTypeMessages.notePanelDescription),
priority: 1000,
icon: function icon() {
return /*#__PURE__*/React.createElement(IconPanelNote, null);
},
action: function action(typeAheadInsert, state) {
return createPanelAction({
state: state,
attributes: {
panelType: PanelType.NOTE
},
api: api,
typeAheadInsert: typeAheadInsert
});
}
}, {
id: 'successpanel',
title: formatMessage(blockTypeMessages.successPanel),
description: formatMessage(blockTypeMessages.successPanelDescription),
keywords: ['tip'],
priority: 1000,
icon: function icon() {
return /*#__PURE__*/React.createElement(IconPanelSuccess, null);
},
action: function action(typeAheadInsert, state) {
return createPanelAction({
state: state,
attributes: {
panelType: PanelType.SUCCESS
},
api: api,
typeAheadInsert: typeAheadInsert
});
}
}, {
id: 'warningpanel',
title: formatMessage(blockTypeMessages.warningPanel),
description: formatMessage(blockTypeMessages.warningPanelDescription),
priority: 1000,
icon: function icon() {
return /*#__PURE__*/React.createElement(IconPanelWarning, null);
},
action: function action(typeAheadInsert, state) {
return createPanelAction({
state: state,
attributes: {
panelType: PanelType.WARNING
},
api: api,
typeAheadInsert: typeAheadInsert
});
}
}, {
id: 'errorpanel',
title: formatMessage(blockTypeMessages.errorPanel),
description: formatMessage(blockTypeMessages.errorPanelDescription),
priority: 1000,
icon: function icon() {
return /*#__PURE__*/React.createElement(IconPanelError, null);
},
action: function action(typeAheadInsert, state) {
return createPanelAction({
state: state,
attributes: {
panelType: PanelType.ERROR
},
api: api,
typeAheadInsert: typeAheadInsert
});
}
}];
if (allowCustomPanel && allowCustomPanelEdit) {
quickInsertOptions.push({
id: 'custompanel',
title: formatMessage(blockTypeMessages.customPanel),
description: formatMessage(blockTypeMessages.customPanelDescription),
priority: 1000,
icon: function icon() {
return /*#__PURE__*/React.createElement(IconCustomPanel, null);
},
action: function action(typeAheadInsert, state) {
return createPanelAction({
state: 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: api,
typeAheadInsert: typeAheadInsert
});
}
});
}
return quickInsertOptions;
},
floatingToolbar: function floatingToolbar(state, intl, providerFactory) {
return getToolbarConfig(state, intl, {
allowCustomPanel: allowCustomPanel,
allowCustomPanelEdit: allowCustomPanelEdit
}, providerFactory, api);
}
}
};
};
/**
* Creates panel action and wrap selection transaction with analytics for the panel insertion.
*
* @example
* const tr = createPanelAction({
* state: editorState,
* attributes: { panelType: 'info' },
* });
* if (tr) {
* applyTransaction(tr);
* }
*/
function createPanelAction(_ref4) {
var state = _ref4.state,
attributes = _ref4.attributes,
api = _ref4.api,
typeAheadInsert = _ref4.typeAheadInsert,
_ref4$inputMethod = _ref4.inputMethod,
inputMethod = _ref4$inputMethod === void 0 ? INPUT_METHOD.QUICK_INSERT : _ref4$inputMethod;
var panel = state.schema.nodes.panel;
var tr;
// If the selection is empty, we want to insert the panel on a new line
if (state.selection.empty) {
var node = panel.createAndFill(_objectSpread({}, attributes));
if (!node) {
return false;
}
if (typeAheadInsert !== undefined) {
// If the type-ahead insert is provided, we should use that to insert the node
tr = typeAheadInsert(node);
} else {
var _insertSelectedItem;
// Otherwise we can use insertSelectedItem to insert the node
tr = (_insertSelectedItem = insertSelectedItem(node)(state, state.tr, state.selection.head)) === null || _insertSelectedItem === void 0 ? void 0 : _insertSelectedItem.scrollIntoView();
}
} else {
tr = createWrapSelectionTransaction({
state: state,
type: panel,
nodeAttributes: _objectSpread({}, attributes)
});
}
if (tr) {
var _api$analytics;
api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || _api$analytics.actions.attachAnalyticsEvent({
action: ACTION.INSERTED,
actionSubject: ACTION_SUBJECT.DOCUMENT,
actionSubjectId: ACTION_SUBJECT_ID.PANEL,
attributes: {
inputMethod: inputMethod,
panelType: attributes.panelType
},
eventType: EVENT_TYPE.TRACK
})(tr);
}
return tr !== null && tr !== void 0 ? tr : false;
}
export default panelPlugin;