@atlaskit/editor-plugin-collab-edit
Version:
Collab Edit plugin for @atlaskit/editor-core
168 lines (161 loc) • 8.83 kB
JavaScript
"use strict";
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createPlugin = void 0;
var adfCustomSteps = _interopRequireWildcard(require("@atlaskit/adf-schema/steps"));
var atlaskKitCustomSteps = _interopRequireWildcard(require("@atlaskit/custom-steps"));
var _analytics = require("@atlaskit/editor-common/analytics");
var _safePlugin = require("@atlaskit/editor-common/safe-plugin");
var _transform = require("@atlaskit/editor-prosemirror/transform");
var _analytics2 = require("../analytics");
var _initialize = require("../events/initialize");
var _pluginKey = require("./plugin-key");
var _pluginState = require("./plugin-state");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
// It is important to get all steps in that package
// Ignored via go/ees005
// eslint-disable-next-line import/no-namespace
// It is important to get all steps in that package
// Ignored via go/ees005
// eslint-disable-next-line import/no-namespace
var enforceCustomStepRegisters = function enforceCustomStepRegisters() {
var tryToRegisterStep = function tryToRegisterStep(obj) {
for (var _i = 0, _Object$values = Object.values(obj); _i < _Object$values.length; _i++) {
var _customStep$prototype;
var customStep = _Object$values[_i];
// I know this seems awful
// But unfortunate ProseMirror does not expose the jsonID property p
// @ts-expect-error
var id = customStep === null || customStep === void 0 || (_customStep$prototype = customStep.prototype) === null || _customStep$prototype === void 0 ? void 0 : _customStep$prototype.jsonID;
if (typeof id === 'string') {
try {
// We are trying to re-register the steps here
// in the normal flow, those custom step should be already registred
// So, it should throw an expeception
// @ts-expect-error
_transform.Step.jsonID(id, customStep);
} catch (e) {
// This mean the step was already registred.
// It is ok to ignore this exception.
}
}
}
};
// @ts-expect-error
tryToRegisterStep(atlaskKitCustomSteps);
// @ts-expect-error
tryToRegisterStep(adfCustomSteps);
};
var createPlugin = exports.createPlugin = function createPlugin(dispatch, providerFactory, providerResolver, collabProviderCallback, options, featureFlags, pluginInjectionApi) {
enforceCustomStepRegisters();
return new _safePlugin.SafePlugin({
key: _pluginKey.pluginKey,
state: {
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
init: function init(config) {
return _pluginState.PluginState.init(config);
},
apply: function apply(transaction, prevPluginState, _oldEditorState, _newEditorState) {
var pluginState = prevPluginState.apply(transaction);
dispatch(_pluginKey.pluginKey, pluginState);
return pluginState;
}
},
props: {
decorations: function decorations(state) {
var _pluginKey$getState;
return (_pluginKey$getState = _pluginKey.pluginKey.getState(state)) === null || _pluginKey$getState === void 0 ? void 0 : _pluginKey$getState.decorations;
},
handleDOMEvents: {
click: function click(view, event) {
var _pluginKey$getState2, _pluginInjectionApi$a, _pluginInjectionApi$a2;
if (!(event.target instanceof HTMLElement)) {
return false;
}
if (!view.state.selection.empty) {
return false;
}
var pos = view.state.tr.selection.$from.pos;
if (!pos) {
return false;
}
// check if the pos is the same pos as a telepointer decoration
var decorations = (_pluginKey$getState2 = _pluginKey.pluginKey.getState(view.state)) === null || _pluginKey$getState2 === void 0 || (_pluginKey$getState2 = _pluginKey$getState2.decorations) === null || _pluginKey$getState2 === void 0 ? void 0 : _pluginKey$getState2.find(pos, pos);
if (!(decorations !== null && decorations !== void 0 && decorations.length)) {
return false;
}
// analytics to track telepointer clicks as they sometimes cause broken selections
var fireAnalyticsCallback = (0, _analytics.fireAnalyticsEvent)((_pluginInjectionApi$a = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a2 = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a2 === void 0 || (_pluginInjectionApi$a2 = _pluginInjectionApi$a2.sharedState.currentState()) === null || _pluginInjectionApi$a2 === void 0 ? void 0 : _pluginInjectionApi$a2.createAnalyticsEvent) !== null && _pluginInjectionApi$a !== void 0 ? _pluginInjectionApi$a : undefined);
fireAnalyticsCallback({
payload: {
action: _analytics.ACTION.CLICKED,
actionSubject: _analytics.ACTION_SUBJECT.TELEPOINTER,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.TELEPOINTER,
eventType: _analytics.EVENT_TYPE.TRACK
}
});
}
}
},
filterTransaction: function filterTransaction(tr, state) {
var pluginState = _pluginKey.pluginKey.getState(state);
var collabInitialiseTr = tr.getMeta('collabInitialised');
// Don't allow transactions that modifies the document before
// collab-plugin is ready.
if (collabInitialiseTr) {
return true;
}
if (!(pluginState !== null && pluginState !== void 0 && pluginState.isReady) && tr.docChanged) {
return false;
}
return true;
},
view: function view(_view) {
var _pluginInjectionApi$a3, _pluginInjectionApi$a6;
var addErrorAnalytics = (0, _analytics2.addSynchronyErrorAnalytics)(_view.state, _view.state.tr, featureFlags, pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a3 = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a3 === void 0 ? void 0 : _pluginInjectionApi$a3.actions);
var onSyncUpError = function onSyncUpError(attributes) {
var _pluginInjectionApi$a4, _pluginInjectionApi$a5;
var fireAnalyticsCallback = (0, _analytics.fireAnalyticsEvent)((_pluginInjectionApi$a4 = pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a5 = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a5 === void 0 || (_pluginInjectionApi$a5 = _pluginInjectionApi$a5.sharedState.currentState()) === null || _pluginInjectionApi$a5 === void 0 ? void 0 : _pluginInjectionApi$a5.createAnalyticsEvent) !== null && _pluginInjectionApi$a4 !== void 0 ? _pluginInjectionApi$a4 : undefined);
fireAnalyticsCallback({
payload: {
action: _analytics.ACTION.NEW_COLLAB_SYNC_UP_ERROR_NO_STEPS,
actionSubject: _analytics.ACTION_SUBJECT.EDITOR,
eventType: _analytics.EVENT_TYPE.OPERATIONAL,
attributes: attributes
}
});
};
options.onSyncUpError = onSyncUpError;
var cleanup = collabProviderCallback((0, _initialize.initialize)({
view: _view,
options: options,
providerFactory: providerFactory,
featureFlags: featureFlags,
pluginInjectionApi: pluginInjectionApi,
editorAnalyticsApi: pluginInjectionApi === null || pluginInjectionApi === void 0 || (_pluginInjectionApi$a6 = pluginInjectionApi.analytics) === null || _pluginInjectionApi$a6 === void 0 ? void 0 : _pluginInjectionApi$a6.actions
}), addErrorAnalytics);
providerFactory && providerFactory.subscribe('collabEditProvider', function (_name, providerPromise) {
if (providerPromise) {
providerPromise.then(function (provider) {
return providerResolver(provider);
});
}
});
return {
destroy: function destroy() {
providerFactory.unsubscribeAll('collabEditProvider');
if (cleanup) {
cleanup.then(function (unsubscribe) {
if (unsubscribe) {
unsubscribe();
}
});
}
}
};
}
});
};