@atlaskit/editor-plugin-expand
Version:
Expand plugin for @atlaskit/editor-core
135 lines (134 loc) • 6.31 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.expandPlugin = void 0;
var _react = _interopRequireDefault(require("react"));
var _adfSchema = require("@atlaskit/adf-schema");
var _analytics = require("@atlaskit/editor-common/analytics");
var _blockMenu = require("@atlaskit/editor-common/block-menu");
var _messages = require("@atlaskit/editor-common/messages");
var _quickInsert = require("@atlaskit/editor-common/quick-insert");
var _utils = require("@atlaskit/editor-common/utils");
var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals");
var _experiments = require("@atlaskit/tmp-editor-statsig/experiments");
var _toggleExpandRange = require("../editor-commands/toggleExpandRange");
var _ExpandBlockMenuItem = require("../ui/ExpandBlockMenuItem");
var _commands = require("./commands");
var _keymap = require("./pm-plugins/keymap");
var _main = require("./pm-plugins/main");
var _toolbar = require("./toolbar");
var EXPAND_NODE_NAME = 'expand';
// Ignored via go/ees005
// eslint-disable-next-line prefer-const
var expandPlugin = exports.expandPlugin = function expandPlugin(_ref) {
var _api$analytics, _api$analytics2;
var _ref$config = _ref.config,
options = _ref$config === void 0 ? {} : _ref$config,
api = _ref.api;
if ((0, _experiments.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: _blockMenu.TRANSFORM_STRUCTURE_EXPAND_MENU_ITEM.key,
parent: {
type: 'block-menu-section',
key: _blockMenu.TRANSFORM_STRUCTURE_MENU_SECTION.key,
rank: _blockMenu.TRANSFORM_STRUCTURE_MENU_SECTION_RANK[_blockMenu.TRANSFORM_STRUCTURE_EXPAND_MENU_ITEM.key]
},
component: (0, _ExpandBlockMenuItem.createExpandBlockMenuItem)(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(EXPAND_NODE_NAME));
}
}]);
}
return {
name: 'expand',
nodes: function nodes() {
return [{
name: 'expand',
node: _adfSchema.expandWithNestedExpand
}, {
name: 'nestedExpand',
node: _adfSchema.nestedExpand
}];
},
actions: {
insertExpand: (0, _commands.insertExpand)(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions),
insertExpandWithInputMethod: (0, _commands.insertExpandWithInputMethod)(api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions)
},
commands: {
toggleExpandWithMatch: function toggleExpandWithMatch(selection) {
return (0, _commands.toggleExpandWithMatch)(selection);
},
toggleExpandRange: _toggleExpandRange.toggleExpandRange
},
getSharedState: function getSharedState() {
var _options$allowInserti;
return (0, _expValEquals.expValEquals)('platform_editor_expand_paste_in_comment_editor', 'isEnabled', true) ? {
allowInsertion: (_options$allowInserti = options === null || options === void 0 ? void 0 : options.allowInsertion) !== null && _options$allowInserti !== void 0 ? _options$allowInserti : true
} : undefined;
},
pmPlugins: function pmPlugins() {
return [{
name: 'expand',
plugin: function plugin(_ref2) {
var _options$allowInterac;
var dispatch = _ref2.dispatch,
getIntl = _ref2.getIntl,
nodeViewPortalProviderAPI = _ref2.nodeViewPortalProviderAPI;
return (0, _main.createPlugin)(dispatch, getIntl, options.appearance, options.useLongPressSelection, api, nodeViewPortalProviderAPI, (_options$allowInterac = options.allowInteractiveExpand) !== null && _options$allowInterac !== void 0 ? _options$allowInterac : true, options.__livePage);
}
}, {
name: 'expandKeymap',
plugin: function plugin() {
return (0, _keymap.expandKeymap)(api, {
__livePage: options.__livePage
});
}
}];
},
pluginsOptions: {
floatingToolbar: (0, _toolbar.getToolbarConfig)(api),
quickInsert: function quickInsert(_ref3) {
var formatMessage = _ref3.formatMessage;
if (options && options.allowInsertion !== true) {
return [];
}
return [{
id: 'expand',
title: formatMessage(_messages.toolbarInsertBlockMessages.expand),
description: formatMessage(_messages.toolbarInsertBlockMessages.expandDescription),
keywords: ['accordion', 'collapse'],
priority: 600,
icon: function icon() {
return /*#__PURE__*/_react.default.createElement(_quickInsert.IconExpand, null);
},
action: function action(insert, state) {
var _api$analytics3;
var node = (0, _commands.createExpandNode)(state);
if (!node) {
return false;
}
var tr = state.selection.empty ? insert(node) : (0, _utils.createWrapSelectionTransaction)({
state: state,
type: node.type
});
api === null || api === void 0 || (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 || _api$analytics3.actions.attachAnalyticsEvent({
action: _analytics.ACTION.INSERTED,
actionSubject: _analytics.ACTION_SUBJECT.DOCUMENT,
actionSubjectId: node.type === state.schema.nodes.nestedExpand ? _analytics.ACTION_SUBJECT_ID.NESTED_EXPAND : _analytics.ACTION_SUBJECT_ID.EXPAND,
attributes: {
inputMethod: _analytics.INPUT_METHOD.QUICK_INSERT
},
eventType: _analytics.EVENT_TYPE.TRACK
})(tr);
return tr;
}
}];
}
}
};
};