UNPKG

@atlaskit/editor-plugin-loom

Version:

Loom plugin for @atlaskit/editor-core

84 lines (83 loc) 3.26 kB
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { 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 const loomPlugin = ({ config, api }) => { var _api$analytics; const editorAnalyticsAPI = api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? void 0 : _api$analytics.actions; const 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. const editorViewRef = { current: null }; const primaryToolbarComponent = loomPrimaryToolbarComponent(config, api); if (isNewToolbarEnabled) { var _api$toolbar; api === null || api === void 0 ? void 0 : (_api$toolbar = api.toolbar) === null || _api$toolbar === void 0 ? void 0 : _api$toolbar.actions.registerComponents(getToolbarComponents(config, api)); } else { var _api$primaryToolbar; api === null || api === void 0 ? void 0 : (_api$primaryToolbar = api.primaryToolbar) === null || _api$primaryToolbar === void 0 ? void 0 : _api$primaryToolbar.actions.registerComponent({ name: 'loom', component: primaryToolbarComponent }); } return { name: 'loom', actions: { recordVideo, insertLoom: (video, positionType) => insertLoom(editorViewRef.current, api, video, positionType), initLoom: ({ loomProvider }) => { return setupLoom(loomProvider, api, editorViewRef.current, true); } }, pmPlugins: () => [{ name: 'loom', plugin: () => createPlugin({ config, api }) }, { name: 'loomViewRefWorkaround', plugin: () => { return new SafePlugin({ 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(editorState) { if (!editorState) { return; } return loomPluginKey.getState(editorState); }, pluginsOptions: { // Enable inserting Loom recordings through the slash command 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 }; };