@atlaskit/editor-plugin-loom
Version:
Loom plugin for @atlaskit/editor-core
128 lines (127 loc) • 5.33 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
import { isExperimentEnabled } from '@atlaskit/platform-feature-experiments/is-experiment-enabled';
import { insertLoom as _insertLoom, recordVideo, setupLoom } from './pm-plugins/commands';
import { createPlugin, loomPluginKey } from './pm-plugins/main';
import { loomPrimaryToolbarComponent } from './ui/PrimaryToolbarButton';
import { getLoomQuickInsertComponents } from './ui/quick-insert/getLoomQuickInsertComponents';
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);
var isRegisteredSlashCommandEnabled = isExperimentEnabled('platform_editor_slash_command');
var isLoomMenuItemRegistered = false;
var registerLoomMenuItem = function registerLoomMenuItem() {
if (!isRegisteredSlashCommandEnabled || isLoomMenuItemRegistered || !(api !== null && api !== void 0 && api.uiControlRegistry)) {
return;
}
api.uiControlRegistry.actions.register(getLoomQuickInsertComponents({
api: api
}));
isLoomMenuItemRegistered = true;
};
if (config.loomProvider && isRegisteredSlashCommandEnabled) {
registerLoomMenuItem();
}
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 () {
var _initLoom = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(_ref2) {
var loomProvider, result;
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
loomProvider = _ref2.loomProvider;
_context.next = 1;
return setupLoom(loomProvider, api, editorViewRef.current, true);
case 1:
result = _context.sent;
if (!result.error) {
registerLoomMenuItem();
}
return _context.abrupt("return", result);
case 2:
case "end":
return _context.stop();
}
}, _callee);
}));
function initLoom(_x) {
return _initLoom.apply(this, arguments);
}
return initLoom;
}()
},
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: isRegisteredSlashCommandEnabled ? {} : {
// 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
};
};