UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

139 lines (137 loc) 5.35 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.createErrorReporter = createErrorReporter; exports.createPMPlugins = createPMPlugins; exports.fixExcludes = fixExcludes; exports.processPluginsList = processPluginsList; exports.sortByRank = sortByRank; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _errorReporter = require("@atlaskit/editor-common/error-reporter"); var _legacyRankPlugins = require("@atlaskit/editor-common/legacy-rank-plugins"); var _expValEquals = require("@atlaskit/tmp-editor-statsig/exp-val-equals"); var _editorNativeAnchorSupportPlugin = require("./editorNativeAnchorSupportPlugin"); var _editorStateNotificationPlugin = require("./editorStateNotificationPlugin"); function sortByRank(a, b) { return a.rank - b.rank; } function fixExcludes(marks) { var markKeys = Object.keys(marks); var markGroups = new Set(markKeys.map(function (mark) { return marks[mark].group; })); markKeys.forEach(function (markKey) { var mark = marks[markKey]; if (mark.excludes) { // eslint-disable-next-line @atlassian/perf-linting/no-expensive-split-replace -- Ignored via go/ees017 (to be fixed) mark.excludes = mark.excludes.split(' ').filter(function (group) { return markGroups.has(group); }).join(' '); } }); return marks; } function processPluginsList(plugins) { /** * First pass to collect pluginsOptions */ var pluginsOptions = plugins.reduce(function (acc, plugin) { if (plugin.pluginsOptions) { Object.keys(plugin.pluginsOptions).forEach(function (pluginName) { if (!acc[pluginName]) { acc[pluginName] = []; } // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion acc[pluginName].push(plugin.pluginsOptions[pluginName]); }); } return acc; }, {}); /** * Process plugins */ return plugins.reduce(function (acc, plugin) { if (plugin.pmPlugins) { var _acc$pmPlugins; (_acc$pmPlugins = acc.pmPlugins).push.apply(_acc$pmPlugins, (0, _toConsumableArray2.default)(plugin.pmPlugins(plugin.name ? pluginsOptions[plugin.name] : undefined))); } if (plugin.nodes) { var _acc$nodes; (_acc$nodes = acc.nodes).push.apply(_acc$nodes, (0, _toConsumableArray2.default)(plugin.nodes())); } if (plugin.marks) { var _acc$marks; (_acc$marks = acc.marks).push.apply(_acc$marks, (0, _toConsumableArray2.default)(plugin.marks())); } if (plugin.contentComponent) { acc.contentComponents.push(plugin.contentComponent); } if (plugin.usePluginHook) { // Wrap with .bind(null) so we can annotate the function with the // plugin name without mutating the plugin's original hook reference. // MountPluginHooks reads `pluginName` to derive a stable React key. var named = plugin.usePluginHook.bind(null); named.pluginName = plugin.name; acc.pluginHooks.push(named); } if (plugin.primaryToolbarComponent) { acc.primaryToolbarComponents.push(plugin.primaryToolbarComponent); } if (plugin.secondaryToolbarComponent) { acc.secondaryToolbarComponents.push(plugin.secondaryToolbarComponent); } if (plugin.onEditorViewStateUpdated) { acc.onEditorViewStateUpdatedCallbacks.push({ pluginName: plugin.name, callback: plugin.onEditorViewStateUpdated }); } return acc; }, { nodes: [], marks: [], pmPlugins: [], contentComponents: [], pluginHooks: [], primaryToolbarComponents: [], secondaryToolbarComponents: [], onEditorViewStateUpdatedCallbacks: [] }); } function createPMPlugins(config) { var editorConfig = config.editorConfig; var pmPlugins = editorConfig.pmPlugins.sort((0, _legacyRankPlugins.sortByOrder)('plugins')).map(function (_ref) { var plugin = _ref.plugin; return plugin({ schema: config.schema, dispatch: config.dispatch, eventDispatcher: config.eventDispatcher, providerFactory: config.providerFactory, errorReporter: config.errorReporter, portalProviderAPI: config.portalProviderAPI, nodeViewPortalProviderAPI: config.nodeViewPortalProviderAPI, dispatchAnalyticsEvent: config.dispatchAnalyticsEvent, featureFlags: config.featureFlags || {}, getIntl: config.getIntl }); }).filter(function (plugin) { return typeof plugin !== 'undefined'; }); if ((0, _expValEquals.expValEquals)('platform_editor_native_anchor_with_dnd', 'isEnabled', true)) { pmPlugins.push((0, _editorNativeAnchorSupportPlugin.createEditorNativeAnchorSupportPlugin)(config.schema)); } if (config.onEditorStateUpdated !== undefined) { return [(0, _editorStateNotificationPlugin.createEditorStateNotificationPlugin)(config.onEditorStateUpdated, config.editorConfig.onEditorViewStateUpdatedCallbacks)].concat((0, _toConsumableArray2.default)(pmPlugins)); } return pmPlugins; } function createErrorReporter(errorReporterHandler) { var errorReporter = new _errorReporter.ErrorReporter(); if (errorReporterHandler) { errorReporter.handler = errorReporterHandler; } return errorReporter; }