UNPKG

@atlaskit/editor-plugin-loom

Version:

Loom plugin for @atlaskit/editor-core

88 lines (87 loc) 3.58 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { insertLoom as _insertLoom, recordVideo, setupLoom } from './pm-plugins/commands'; import { createPlugin, loomPluginKey } from './pm-plugins/main'; import { loomPrimaryToolbarComponent } from './ui/PrimaryToolbarButton'; import { getQuickInsertItem } from './ui/quickInsert'; import { getToolbarComponents } from './ui/toolbar-components'; export var loomPlugin = function loomPlugin(_ref) { var _api$analytics; var config = _ref.config, api = _ref.api; var editorAnalyticsAPI = api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions; var isNewToolbarEnabled = Boolean(api === null || api === void 0 ? void 0 : api.toolbar); // Workaround since we want to insert a loom via the `hyperlink` plugin for now. // The hyperlink plugin (and the card plugin) are deeply tied into using the Prosemirror Command // Ideally one day we refactor those and we can remove this. var editorViewRef = { current: null }; var primaryToolbarComponent = loomPrimaryToolbarComponent(config, api); if (isNewToolbarEnabled) { var _api$toolbar; api === null || api === void 0 || (_api$toolbar = api.toolbar) === null || _api$toolbar === void 0 || _api$toolbar.actions.registerComponents(getToolbarComponents(config, api)); } else { var _api$primaryToolbar; api === null || api === void 0 || (_api$primaryToolbar = api.primaryToolbar) === null || _api$primaryToolbar === void 0 || _api$primaryToolbar.actions.registerComponent({ name: 'loom', component: primaryToolbarComponent }); } return { name: 'loom', actions: { recordVideo: recordVideo, insertLoom: function insertLoom(video, positionType) { return _insertLoom(editorViewRef.current, api, video, positionType); }, initLoom: function initLoom(_ref2) { var loomProvider = _ref2.loomProvider; return setupLoom(loomProvider, api, editorViewRef.current, true); } }, pmPlugins: function pmPlugins() { return [{ name: 'loom', plugin: function plugin() { return createPlugin({ config: config, api: api }); } }, { name: 'loomViewRefWorkaround', 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 {}; } }); } }]; }, getSharedState: function getSharedState(editorState) { if (!editorState) { return; } return loomPluginKey.getState(editorState); }, pluginsOptions: { // Enable inserting Loom recordings through the slash command quickInsert: function quickInsert(intl) { if (config.loomProvider) { return getQuickInsertItem(editorAnalyticsAPI)(intl); } return []; } }, // Enable inserting Loom recordings through main toolbar primaryToolbarComponent: !(api !== null && api !== void 0 && api.primaryToolbar) && !isNewToolbarEnabled ? primaryToolbarComponent : undefined }; };