UNPKG

@atlaskit/editor-plugin-extension

Version:

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

233 lines • 11.8 kB
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; } // eslint-disable-next-line @atlaskit/platform/prefer-crypto-random-uuid -- Use crypto.randomUUID instead import { v4 as uuid } from 'uuid'; import { extension, extensionWithAnnotationStage0 } from '@atlaskit/adf-schema/extension'; import { inlineExtension } from '@atlaskit/adf-schema/inline-extension'; import { extensionFrame, multiBodiedExtension } from '@atlaskit/adf-schema/multi-bodied-extension'; import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled'; import { fg } from '@atlaskit/platform-feature-flags/fg'; 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'; 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 extensionLoadingHandlers = {}; 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: fg('cc_maui_annotations_on_extensions') ? extensionWithAnnotationStage0 : extension }, { name: 'bodiedExtension', node: bodiedExtensionSpecWithFixedToDOM() }, { name: 'inlineExtension', node: inlineExtension }, { name: 'extensionFrame', node: extensionFrame }, { name: 'multiBodiedExtension', node: multiBodiedExtension }]; return extensionNodes; }, getSharedState: function getSharedState(state) { var _pluginState$shouldAn; if (!state) { return undefined; } var pluginState = pluginKey.getState(state); return _objectSpread(_objectSpread({ showContextPanel: pluginState === null || pluginState === void 0 ? void 0 : pluginState.showContextPanel }, isExperimentEnabled('cwr_remix_animate_stream_only') && { shouldAnimateInsertedContent: (_pluginState$shouldAn = pluginState === null || pluginState === void 0 ? void 0 : pluginState.shouldAnimateInsertedContent) !== null && _pluginState$shouldAn !== void 0 ? _pluginState$shouldAn : false }), {}, { extensionProvider: pluginState === null || pluginState === void 0 ? void 0 : pluginState.extensionProvider, processParametersAfter: pluginState === null || pluginState === void 0 ? void 0 : pluginState.processParametersAfter }); }, commands: { setInsertAnimationEnabled: function setInsertAnimationEnabled(enabled) { return function (_ref2) { var tr = _ref2.tr; return isExperimentEnabled('cwr_remix_animate_stream_only') ? tr.setMeta(pluginKey, { type: 'UPDATE_STATE', data: { shouldAnimateInsertedContent: enabled } }).setMeta('addToHistory', false) : null; }; } }, pmPlugins: function pmPlugins() { return [{ name: 'extension', plugin: function plugin(_ref3) { var dispatch = _ref3.dispatch, providerFactory = _ref3.providerFactory, portalProviderAPI = _ref3.portalProviderAPI, eventDispatcher = _ref3.eventDispatcher, getIntl = _ref3.getIntl; var extensionHandlers = options.extensionHandlers || {}; return createPlugin(dispatch, providerFactory, extensionHandlers, extensionLoadingHandlers, portalProviderAPI, eventDispatcher, _api, options.useLongPressSelection, { appearance: options.appearance, getExtensionHeight: options.getExtensionHeight, allowAIGeneratedContentMotion: options.allowAIGeneratedContentMotion }, featureFlags, options === null || options === void 0 ? void 0 : options.__rendererExtensionOptions, getIntl()); } }, { 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(_ref4) { var dispatch = _ref4.dispatch, providerFactory = _ref4.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 }); }, getExtensionLoadingHandlers: function getExtensionLoadingHandlers() { return extensionLoadingHandlers; }, insertMacroFromMacroBrowser: insertMacroFromMacroBrowser(_api === null || _api === void 0 || (_api$analytics2 = _api.analytics) === null || _api$analytics2 === void 0 ? void 0 : _api$analytics2.actions), insertOrReplaceExtension: function insertOrReplaceExtension(_ref5) { var editorView = _ref5.editorView, action = _ref5.action, attrs = _ref5.attrs, content = _ref5.content, position = _ref5.position, size = _ref5.size, tr = _ref5.tr; return _insertOrReplaceExtension({ editorView: editorView, action: action, attrs: attrs, content: content, position: position, size: size, tr: tr }); }, insertOrReplaceBodiedExtension: function insertOrReplaceBodiedExtension(_ref6) { var editorView = _ref6.editorView, action = _ref6.action, attrs = _ref6.attrs, content = _ref6.content, position = _ref6.position, size = _ref6.size, tr = _ref6.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 }), registerExtensionLoadingHandler: function registerExtensionLoadingHandler(_ref7) { var extensionType = _ref7.extensionType, handler = _ref7.handler; extensionLoadingHandlers[extensionType] = handler; return function () { if (extensionLoadingHandlers[extensionType] === handler) { delete extensionLoadingHandlers[extensionType]; } }; }, runMacroAutoConvert: runMacroAutoConvert, forceAutoSave: forceAutoSave }, pluginsOptions: { floatingToolbar: getToolbarConfig({ breakoutEnabled: options.breakoutEnabled, copyEnabled: options.copyEnabled, deleteEnabled: options.deleteEnabled, extensionApi: _api, getUnsupportedContent: options.getUnsupportedContent }), contextPanel: !showContextPanel ? getContextPanel(function () { var _editorViewRef$curren; return (_editorViewRef$curren = editorViewRef.current) !== null && _editorViewRef$curren !== void 0 ? _editorViewRef$curren : undefined; })(_api, featureFlags) : undefined }, usePluginHook: showContextPanel ? function (_ref8) { var editorView = _ref8.editorView; useConfigPanelPluginHook({ api: _api, configPanelId: configPanelId, editorView: editorView }); } : undefined }; };