@atlaskit/editor-plugin-loom
Version:
Loom plugin for @atlaskit/editor-core
135 lines (133 loc) • 5.72 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.loomPlugin = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _isExperimentEnabled = require("@atlaskit/platform-feature-experiments/is-experiment-enabled");
var _commands = require("./pm-plugins/commands");
var _main = require("./pm-plugins/main");
var _PrimaryToolbarButton = require("./ui/PrimaryToolbarButton");
var _getLoomQuickInsertComponents = require("./ui/quick-insert/getLoomQuickInsertComponents");
var _quickInsert = require("./ui/quickInsert");
var _toolbarComponents = require("./ui/toolbar-components");
var loomPlugin = exports.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 = (0, _PrimaryToolbarButton.loomPrimaryToolbarComponent)(config, api);
var isRegisteredSlashCommandEnabled = (0, _isExperimentEnabled.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((0, _getLoomQuickInsertComponents.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((0, _toolbarComponents.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: _commands.recordVideo,
insertLoom: function insertLoom(video, positionType) {
return (0, _commands.insertLoom)(editorViewRef.current, api, video, positionType);
},
initLoom: function () {
var _initLoom = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref2) {
var loomProvider, result;
return _regenerator.default.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
loomProvider = _ref2.loomProvider;
_context.next = 1;
return (0, _commands.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 (0, _main.createPlugin)({
config: config,
api: api
});
}
}, {
name: 'loomViewRefWorkaround',
plugin: function plugin() {
return new _safePlugin.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 _main.loomPluginKey.getState(editorState);
},
pluginsOptions: isRegisteredSlashCommandEnabled ? {} : {
// Enable inserting Loom recordings through the slash command
quickInsert: function quickInsert(intl) {
if (config.loomProvider) {
return (0, _quickInsert.getQuickInsertItem)(editorAnalyticsAPI)(intl);
}
return [];
}
},
// Enable inserting Loom recordings through main toolbar
primaryToolbarComponent: !(api !== null && api !== void 0 && api.primaryToolbar) && !isNewToolbarEnabled ? primaryToolbarComponent : undefined
};
};