UNPKG

@atlaskit/editor-core

Version:

A package contains Atlassian editor core functionality

123 lines 4.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var prosemirror_1 = require("../../../prosemirror"); exports.pluginKey = new prosemirror_1.PluginKey('collabEditPlugin'); // TODO: Keep track of telepointers and participants here (ED-2574) var PluginState = (function () { function PluginState() { } PluginState.prototype.apply = function (tr) { return new PluginState(); }; PluginState.init = function (config) { return new PluginState(); }; return PluginState; }()); var handleInit = function (data, view) { var doc = data.doc, json = data.json; if (doc) { var state = view.state, _a = view.state, schema_1 = _a.schema, tr = _a.tr; var content = (doc.content || []).map(function (child) { return schema_1.nodeFromJSON(child); }); if (content.length) { var newState = state.apply(tr .setMeta('addToHistory', false) .replaceWith(0, state.doc.nodeSize - 2, content) .scrollIntoView()); view.updateState(newState); } } else if (json) { applyRemoteSteps(json, view); } }; var applyRemoteData = function (data, view) { var json = data.json, newState = data.newState; if (json) { applyRemoteSteps(json, view); } else if (newState) { view.updateState(newState); } }; var applyRemoteSteps = function (json, view) { var state = view.state, schema = view.state.schema; var tr = state.tr; json.forEach(function (stepJson) { var step = prosemirror_1.Step.fromJSON(schema, stepJson); tr.step(step); }); tr.setMeta('addToHistory', false); tr.scrollIntoView(); var newState = state.apply(tr); view.updateState(newState); }; exports.createPlugin = function (providerFactory) { var collabEditProvider; var isReady = false; return new prosemirror_1.Plugin({ key: exports.pluginKey, state: { init: PluginState.init, apply: function (tr, prevPluginState, oldState, newState) { if (tr.getMeta('isLocal')) { if (collabEditProvider) { collabEditProvider.send(tr, oldState, newState); } } var pluginState = prevPluginState.apply(tr); return pluginState; }, }, filterTransaction: function (tr, state) { if (!isReady) { return false; } return true; }, view: function (view) { var _this = this; providerFactory.subscribe('collabEditProvider', function (name, providerPromise) { return tslib_1.__awaiter(_this, void 0, void 0, function () { return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: if (!providerPromise) return [3 /*break*/, 2]; return [4 /*yield*/, providerPromise]; case 1: collabEditProvider = _a.sent(); // Initialize provider collabEditProvider .on('init', function (data) { isReady = true; handleInit(data, view); }) .on('data', function (data) { return applyRemoteData(data, view); }) .on('error', function (err) { // TODO: Handle errors propery (ED-2580) }) .initialize(function () { return view.state; }); return [3 /*break*/, 3]; case 2: collabEditProvider = null; isReady = false; _a.label = 3; case 3: return [2 /*return*/]; } }); }); }); return { destroy: function () { providerFactory.unsubscribeAll('collabEditProvider'); collabEditProvider = null; } }; } }); }; var collabEditPlugin = { pmPlugins: function () { return [ { rank: 1000, plugin: function (schema, props, dispatch, providerFactory) { return exports.createPlugin(providerFactory); } } ]; } }; exports.default = collabEditPlugin; //# sourceMappingURL=index.js.map