@atlaskit/editor-plugin-block-type
Version:
BlockType plugin for @atlaskit/editor-core
279 lines (278 loc) • 14.8 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.blockTypePlugin = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _blockquote = require("@atlaskit/adf-schema/blockquote");
var _fontSize = require("@atlaskit/adf-schema/font-size");
var _hardBreak = require("@atlaskit/adf-schema/hard-break");
var _heading = require("@atlaskit/adf-schema/heading");
var _analytics = require("@atlaskit/editor-common/analytics");
var _assets = require("@atlaskit/editor-common/assets");
var _keymaps = require("@atlaskit/editor-common/keymaps");
var _messages = require("@atlaskit/editor-common/messages");
var _types = require("@atlaskit/editor-common/types");
var _isExperimentEnabled = require("@atlaskit/platform-feature-experiments/is-experiment-enabled");
var _fg = require("@atlaskit/platform-feature-flags/fg");
var _editorExperiment = require("@atlaskit/tmp-editor-statsig/editor-experiment");
var _blockType = require("./pm-plugins/commands/block-type");
var _inputRule = _interopRequireDefault(require("./pm-plugins/input-rule"));
var _keymap = _interopRequireDefault(require("./pm-plugins/keymap"));
var _main = require("./pm-plugins/main");
var _FloatingToolbarComponent = require("./pm-plugins/ui/FloatingToolbarComponent");
var _PrimaryToolbarComponent = require("./pm-plugins/ui/PrimaryToolbarComponent");
var _toolbarComponents = require("./pm-plugins/ui/toolbar-components");
var _ui = require("./ui");
var _getBlockTypeQuickInsertComponents = require("./ui/quick-insert/getBlockTypeQuickInsertComponents");
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) { (0, _defineProperty2.default)(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; }
var headingPluginOptions = function headingPluginOptions(_ref, isAllowed, editorAnalyticsApi) {
var formatMessage = _ref.formatMessage;
if (!isAllowed) {
return [];
}
return Array.from({
length: 6
}, function (_v, idx) {
var level = idx + 1;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var descriptionDescriptor = _messages.blockTypeMessages["heading".concat(level, "Description")];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var keyshortcut = (0, _keymaps.tooltip)(_keymaps.keymap["toggleHeading".concat(level)]);
var id = "heading".concat(level);
return {
id: id,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
title: formatMessage(_messages.blockTypeMessages[id]),
description: formatMessage(descriptionDescriptor),
priority: 1300,
keywords: ["h".concat(level)],
keyshortcut: keyshortcut,
icon: function icon() {
return /*#__PURE__*/_react.default.createElement(_assets.IconHeading, {
level: level
});
},
action: function action(insert, state) {
var tr = insert(state.schema.nodes.heading.createChecked({
level: level
}));
editorAnalyticsApi === null || editorAnalyticsApi === void 0 || editorAnalyticsApi.attachAnalyticsEvent({
action: _analytics.ACTION.FORMATTED,
actionSubject: _analytics.ACTION_SUBJECT.TEXT,
eventType: _analytics.EVENT_TYPE.TRACK,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.FORMAT_HEADING,
attributes: {
inputMethod: _analytics.INPUT_METHOD.QUICK_INSERT,
newHeadingLevel: level
}
})(tr);
return tr;
}
};
});
};
var blockquotePluginOptions = function blockquotePluginOptions(_ref2, isAllowed, editorAnalyticsApi) {
var formatMessage = _ref2.formatMessage;
if (!isAllowed) {
return [];
}
return [{
id: 'blockquote',
title: formatMessage(_messages.blockTypeMessages.blockquote),
description: formatMessage(_messages.blockTypeMessages.blockquoteDescription),
priority: 1300,
keyshortcut: '>',
icon: function icon() {
return /*#__PURE__*/_react.default.createElement(_assets.IconQuote, null);
},
action: function action(insert, state) {
var tr = insert(state.schema.nodes.blockquote.createChecked({}, state.schema.nodes.paragraph.createChecked()));
editorAnalyticsApi === null || editorAnalyticsApi === void 0 || editorAnalyticsApi.attachAnalyticsEvent({
action: _analytics.ACTION.FORMATTED,
actionSubject: _analytics.ACTION_SUBJECT.TEXT,
eventType: _analytics.EVENT_TYPE.TRACK,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.FORMAT_BLOCK_QUOTE,
attributes: {
inputMethod: _analytics.INPUT_METHOD.QUICK_INSERT
}
})(tr);
return tr;
}
}];
};
var blockTypePlugin = exports.blockTypePlugin = function blockTypePlugin(_ref3) {
var _api$blockMenu;
var options = _ref3.config,
api = _ref3.api;
var isToolbarAIFCEnabled = Boolean(api === null || api === void 0 ? void 0 : api.toolbar);
var isRegisteredSlashCommandEnabled = (0, _isExperimentEnabled.isExperimentEnabled)('platform_editor_slash_command');
var primaryToolbarComponent = function primaryToolbarComponent(_ref4) {
var popupsMountPoint = _ref4.popupsMountPoint,
popupsBoundariesElement = _ref4.popupsBoundariesElement,
popupsScrollableElement = _ref4.popupsScrollableElement,
toolbarSize = _ref4.toolbarSize,
disabled = _ref4.disabled,
isToolbarReducedSpacing = _ref4.isToolbarReducedSpacing;
var isSmall = options && options.isUndoRedoButtonsEnabled ? toolbarSize < _types.ToolbarSize.XXL : toolbarSize < _types.ToolbarSize.XL;
if ((0, _fg.fg)('platform_editor_toolbar_responsive_fixes')) {
isSmall = toolbarSize < _types.ToolbarSize.XXL;
}
return /*#__PURE__*/_react.default.createElement(_PrimaryToolbarComponent.PrimaryToolbarComponent, {
isSmall: isSmall,
disabled: disabled,
isToolbarReducedSpacing: isToolbarReducedSpacing,
api: api,
popupsMountPoint: popupsMountPoint,
popupsBoundariesElement: popupsBoundariesElement,
popupsScrollableElement: popupsScrollableElement,
shouldUseDefaultRole: false
});
};
if (isToolbarAIFCEnabled) {
var _api$toolbar;
api === null || api === void 0 || (_api$toolbar = api.toolbar) === null || _api$toolbar === void 0 || _api$toolbar.actions.registerComponents((0, _toolbarComponents.getToolbarComponents)(api, options === null || options === void 0 ? void 0 : options.allowFontSize));
} else {
var _api$primaryToolbar;
api === null || api === void 0 || (_api$primaryToolbar = api.primaryToolbar) === null || _api$primaryToolbar === void 0 || _api$primaryToolbar.actions.registerComponent({
name: 'blockType',
component: primaryToolbarComponent
});
}
api === null || api === void 0 || (_api$blockMenu = api.blockMenu) === null || _api$blockMenu === void 0 || _api$blockMenu.actions.registerBlockMenuComponents((0, _ui.getBlockTypeComponents)(api, {
allowFontSize: options === null || options === void 0 ? void 0 : options.allowFontSize
}));
if (isRegisteredSlashCommandEnabled) {
var _api$uiControlRegistr, _options$allowBlockTy, _options$allowBlockTy2;
api === null || api === void 0 || (_api$uiControlRegistr = api.uiControlRegistry) === null || _api$uiControlRegistr === void 0 || _api$uiControlRegistr.actions.register((0, _getBlockTypeQuickInsertComponents.getBlockTypeQuickInsertComponents)({
api: api,
exclude: (_options$allowBlockTy = options === null || options === void 0 || (_options$allowBlockTy2 = options.allowBlockType) === null || _options$allowBlockTy2 === void 0 ? void 0 : _options$allowBlockTy2.exclude) !== null && _options$allowBlockTy !== void 0 ? _options$allowBlockTy : []
}));
}
return {
name: 'blockType',
nodes: function nodes() {
var nodes = [{
name: 'heading',
node: _heading.heading
}, {
name: 'blockquote',
node: (0, _fg.fg)('platform_editor_adf_with_localid') ? _blockquote.extendedBlockquoteWithLocalId : _blockquote.extendedBlockquote
}, {
name: 'hardBreak',
node: _hardBreak.hardBreak
}];
if (options && options.allowBlockType) {
var exclude = options.allowBlockType.exclude ? options.allowBlockType.exclude : [];
return nodes.filter(function (node) {
return exclude.indexOf(node.name) === -1;
});
}
return nodes;
},
marks: function marks() {
if (options !== null && options !== void 0 && options.allowFontSize) {
return [{
name: 'fontSize',
mark: _fontSize.fontSize
}];
}
return [];
},
pmPlugins: function pmPlugins() {
return [{
name: 'blockType',
plugin: function plugin(_ref5) {
var dispatch = _ref5.dispatch;
return (0, _main.createPlugin)(api, dispatch, options && options.lastNodeMustBeParagraph, options === null || options === void 0 ? void 0 : options.includeBlockQuoteAsTextstyleOption, options === null || options === void 0 ? void 0 : options.allowFontSize);
}
}, {
name: 'blockTypeInputRule',
plugin: function plugin(_ref6) {
var _api$analytics;
var schema = _ref6.schema,
featureFlags = _ref6.featureFlags;
return (0, _inputRule.default)(api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions, schema, featureFlags);
}
},
// Needs to be lower priority than editor-tables.tableEditing
// plugin as it is currently swallowing right/down arrow events inside tables
{
name: 'blockTypeKeyMap',
plugin: function plugin(_ref7) {
var _api$analytics2;
var schema = _ref7.schema,
featureFlags = _ref7.featureFlags;
return (0, _keymap.default)(api === null || api === void 0 || (_api$analytics2 = api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions, schema, featureFlags);
}
}];
},
actions: {
insertBlockQuote: function insertBlockQuote(inputMethod) {
var _api$analytics3;
return (0, _blockType.insertBlockQuoteWithAnalytics)(inputMethod, api === null || api === void 0 || (_api$analytics3 = api.analytics) === null || _api$analytics3 === void 0 ? void 0 : _api$analytics3.actions);
}
},
commands: {
setTextLevel: function setTextLevel(level, inputMethod) {
var _api$analytics4;
var fromBlockQuote = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
return (0, _blockType.setBlockTypeWithAnalytics)(level, inputMethod, api === null || api === void 0 || (_api$analytics4 = api.analytics) === null || _api$analytics4 === void 0 ? void 0 : _api$analytics4.actions, fromBlockQuote);
},
insertBlockQuote: function insertBlockQuote(inputMethod) {
var _api$analytics5;
return (0, _blockType.insertBlockQuoteWithAnalyticsCommand)(inputMethod, api === null || api === void 0 || (_api$analytics5 = api.analytics) === null || _api$analytics5 === void 0 ? void 0 : _api$analytics5.actions);
},
clearFormatting: function clearFormatting(inputMethod) {
var _api$analytics6;
return (0, _blockType.clearFormatting)(inputMethod, api === null || api === void 0 || (_api$analytics6 = api.analytics) === null || _api$analytics6 === void 0 ? void 0 : _api$analytics6.actions);
}
},
getSharedState: function getSharedState(editorState) {
if (!editorState) {
return;
}
return _main.pluginKey.getState(editorState);
},
primaryToolbarComponent: !(api !== null && api !== void 0 && api.primaryToolbar) ? primaryToolbarComponent : undefined,
pluginsOptions: _objectSpread(_objectSpread({}, !isToolbarAIFCEnabled && {
selectionToolbar: function selectionToolbar() {
var _api$userPreferences;
var toolbarDocking = 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;
if (toolbarDocking === 'none' && (0, _editorExperiment.editorExperiment)('platform_editor_controls', 'variant1', {
exposure: true
})) {
var toolbarCustom = {
type: 'custom',
render: function render(view, _idx, _dispatchAnalyticsEvent) {
if (!view) {
return;
}
return /*#__PURE__*/_react.default.createElement(_FloatingToolbarComponent.FloatingToolbarComponent, {
api: api
});
},
fallback: []
};
return {
isToolbarAbove: true,
items: [toolbarCustom],
rank: 8
};
} else {
return undefined;
}
}
}), !isRegisteredSlashCommandEnabled && {
quickInsert: function quickInsert(intl) {
var _api$analytics7, _api$analytics8;
var exclude = options && options.allowBlockType && options.allowBlockType.exclude ? options.allowBlockType.exclude : [];
return [].concat((0, _toConsumableArray2.default)(blockquotePluginOptions(intl, exclude.indexOf('blockquote') === -1, api === null || api === void 0 || (_api$analytics7 = api.analytics) === null || _api$analytics7 === void 0 ? void 0 : _api$analytics7.actions)), (0, _toConsumableArray2.default)(headingPluginOptions(intl, exclude.indexOf('heading') === -1, api === null || api === void 0 || (_api$analytics8 = api.analytics) === null || _api$analytics8 === void 0 ? void 0 : _api$analytics8.actions)));
}
})
};
};