UNPKG

@atlaskit/editor-plugin-extension

Version:

editor-plugin-extension plugin for @atlaskit/editor-core

196 lines 9.44 kB
// eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead import uuid from 'uuid/v4'; import { extension, extensionFrame, inlineExtension, multiBodiedExtension } from '@atlaskit/adf-schema'; import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { fg } from '@atlaskit/platform-feature-flags'; import { createEditSelectedExtensionAction, insertOrReplaceBodiedExtension as _insertOrReplaceBodiedExtension, insertOrReplaceExtension as _insertOrReplaceExtension } from './editor-actions/actions'; import { forceAutoSave } from './editor-commands/commands'; import { createExtensionAPI } from './pm-plugins/extension-api'; import keymapPlugin from './pm-plugins/keymap'; import { insertMacroFromMacroBrowser, runMacroAutoConvert } from './pm-plugins/macro/actions'; import { createPlugin as createMacroPlugin } from './pm-plugins/macro/plugin'; import { createPlugin } from './pm-plugins/main'; import { pluginKey } from './pm-plugins/plugin-key'; import { bodiedExtensionSpecWithFixedToDOM } from './pm-plugins/toDOM-fixes/bodiedExtension'; import { getToolbarConfig } from './pm-plugins/toolbar'; import { createPlugin as createUniqueIdPlugin } from './pm-plugins/unique-id'; // Remove below line when cleaning up platform_editor_ai_object_sidebar_injection feature flag import { CONFIG_PANEL_ID } from './ui/ConfigPanel/constants'; import { getContextPanel } from './ui/context-panel'; import { useConfigPanelPluginHook } from './ui/useConfigPanelPluginHook'; export var extensionPlugin = function extensionPlugin(_ref) { var _api$featureFlags, _api$contextPanel, _api$analytics2, _api$analytics3, _api$contextPanel4; var _ref$config = _ref.config, options = _ref$config === void 0 ? {} : _ref$config, _api = _ref.api; // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead var configPanelId = "".concat(CONFIG_PANEL_ID, "-").concat(uuid()); var featureFlags = (_api === null || _api === void 0 || (_api$featureFlags = _api.featureFlags) === null || _api$featureFlags === void 0 ? void 0 : _api$featureFlags.sharedState.currentState()) || {}; //Note: This is a hack to get the editor view reference in the plugin. Copied from table plugin. //This is needed to get the current selection in the editor var editorViewRef = { current: null }; var showContextPanel = _api === null || _api === void 0 || (_api$contextPanel = _api.contextPanel) === null || _api$contextPanel === void 0 || (_api$contextPanel = _api$contextPanel.actions) === null || _api$contextPanel === void 0 ? void 0 : _api$contextPanel.showPanel; return { name: 'extension', nodes: function nodes() { var extensionNodes = [{ name: 'extension', node: extension }, { name: 'bodiedExtension', node: bodiedExtensionSpecWithFixedToDOM() }, { name: 'inlineExtension', node: inlineExtension }, { name: 'extensionFrame', node: extensionFrame }, { name: 'multiBodiedExtension', node: multiBodiedExtension }]; return extensionNodes; }, getSharedState: function getSharedState(state) { if (!state) { return undefined; } var pluginState = pluginKey.getState(state); return { showContextPanel: pluginState === null || pluginState === void 0 ? void 0 : pluginState.showContextPanel, extensionProvider: pluginState === null || pluginState === void 0 ? void 0 : pluginState.extensionProvider, processParametersAfter: pluginState === null || pluginState === void 0 ? void 0 : pluginState.processParametersAfter }; }, pmPlugins: function pmPlugins() { return [{ name: 'extension', plugin: function plugin(_ref2) { var dispatch = _ref2.dispatch, providerFactory = _ref2.providerFactory, portalProviderAPI = _ref2.portalProviderAPI, eventDispatcher = _ref2.eventDispatcher; var extensionHandlers = options.extensionHandlers || {}; return createPlugin(dispatch, providerFactory, extensionHandlers, portalProviderAPI, eventDispatcher, _api, options.useLongPressSelection, { appearance: options.appearance, getExtensionHeight: options.getExtensionHeight }, featureFlags, options === null || options === void 0 ? void 0 : options.__rendererExtensionOptions); } }, { name: 'extensionKeymap', plugin: function plugin() { var _api$contextPanel2; return keymapPlugin(_api === null || _api === void 0 || (_api$contextPanel2 = _api.contextPanel) === null || _api$contextPanel2 === void 0 ? void 0 : _api$contextPanel2.actions.applyChange); } }, { name: 'extensionUniqueId', plugin: function plugin() { return createUniqueIdPlugin(); } }, { name: 'extensionEditorViewRef', plugin: function plugin() { return new SafePlugin({ view: function view(editorView) { // Do not cleanup the editorViewRef on destroy // because some functions may point to a stale // reference and this means we will return null. // EditorView is assumed to be stable so we do not need to // cleanup. // See: #hot-106316 editorViewRef.current = editorView; return {}; } }); } }, { name: 'macro', plugin: function plugin(_ref3) { var dispatch = _ref3.dispatch, providerFactory = _ref3.providerFactory; return createMacroPlugin(dispatch, providerFactory); } }]; }, actions: { api: function api() { var _api$contextPanel3, _api$analytics; return createExtensionAPI({ // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion editorView: editorViewRef.current, applyChange: _api === null || _api === void 0 || (_api$contextPanel3 = _api.contextPanel) === null || _api$contextPanel3 === void 0 ? void 0 : _api$contextPanel3.actions.applyChange, editorAnalyticsAPI: _api === null || _api === void 0 || (_api$analytics = _api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions }); }, insertMacroFromMacroBrowser: insertMacroFromMacroBrowser(_api === null || _api === void 0 || (_api$analytics2 = _api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions), insertOrReplaceExtension: function insertOrReplaceExtension(_ref4) { var editorView = _ref4.editorView, action = _ref4.action, attrs = _ref4.attrs, content = _ref4.content, position = _ref4.position, size = _ref4.size, tr = _ref4.tr; return _insertOrReplaceExtension({ editorView: editorView, action: action, attrs: attrs, content: content, position: position, size: size, tr: tr }); }, insertOrReplaceBodiedExtension: function insertOrReplaceBodiedExtension(_ref5) { var editorView = _ref5.editorView, action = _ref5.action, attrs = _ref5.attrs, content = _ref5.content, position = _ref5.position, size = _ref5.size, tr = _ref5.tr; return _insertOrReplaceBodiedExtension({ editorView: editorView, action: action, attrs: attrs, content: content, position: position, size: size, tr: tr }); }, editSelectedExtension: createEditSelectedExtensionAction({ editorViewRef: editorViewRef, editorAnalyticsAPI: _api === null || _api === void 0 || (_api$analytics3 = _api.analytics) === null || _api$analytics3 === void 0 ? void 0 : _api$analytics3.actions, applyChangeToContextPanel: _api === null || _api === void 0 || (_api$contextPanel4 = _api.contextPanel) === null || _api$contextPanel4 === void 0 ? void 0 : _api$contextPanel4.actions.applyChange }), runMacroAutoConvert: runMacroAutoConvert, forceAutoSave: forceAutoSave }, pluginsOptions: { floatingToolbar: getToolbarConfig({ breakoutEnabled: options.breakoutEnabled, extensionApi: _api, getUnsupportedContent: options.getUnsupportedContent }), contextPanel: // if showContextPanel action is not available, or platform_editor_ai_object_sidebar_injection feature flag is off // then keep using old context panel. !showContextPanel || !fg('platform_editor_ai_object_sidebar_injection') ? getContextPanel(function () { var _editorViewRef$curren; return (_editorViewRef$curren = editorViewRef.current) !== null && _editorViewRef$curren !== void 0 ? _editorViewRef$curren : undefined; })(_api, featureFlags) : undefined }, usePluginHook: showContextPanel && fg('platform_editor_ai_object_sidebar_injection') ? function (_ref6) { var editorView = _ref6.editorView; useConfigPanelPluginHook({ api: _api, configPanelId: configPanelId, editorView: editorView }); } : undefined }; };