@atlaskit/editor-nodeview-data-provider
Version:
NodeView data provider for @atlaskit/editor-core plugins
130 lines (129 loc) • 6.32 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.buildCaches = buildCaches;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _traverse = require("@atlaskit/adf-utils/traverse");
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
/**
* Builds caches for {@link NodeViewDataProvider}s.
*
* It will traverse the document and call the resolve method for each node.
* When all promises are resolved, NodeViewDataProviders will have their caches populated.
*
* The providers will then be ready for use with an Editor.
*
* To limit the time spent building the cache, a signal can be provided to abort the request.
*
* ## Usage
*
* @example
* ```ts
* buildCaches({
* adf: doc,
* nodeViewDataProviders: [nodeViewDataProvider1, nodeViewDataProvider2],
* signal: AbortSignal.timeout(5000),
* });
*
* const { preset } = usePreset(() =>
* new EditorPresetBuilder()
* // ...
* .add([editorPlugin1, { nodeViewDataProvider: nodeViewDataProvider1 }])
* .add([editorPlugin2, { nodeViewDataProvider: nodeViewDataProvider2 }])
* );
* ```
*
* ### Using caches
*
* To make use of a cache in another provider (ie. for a cache created on the server), you can retrieve the cache
* from the provider and pass it to the new provider.
*
* @example
* ```tsx
* const provider1 = new ExampleNodeViewDataProvider();
* await buildCaches({adf, nodeViewDataProviders: [nodeViewDataProvider1]})
* provider1.cache // { 'key': 'value' }
*
* const provider2 = new ExampleNodeViewDataProvider({existingCache: provider1.cache});
* ```
*/
function buildCaches(_x) {
return _buildCaches.apply(this, arguments);
}
function _buildCaches() {
_buildCaches = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(_ref) {
var adf, nodeViewDataProviders, signal, visitors, promises, _iterator, _step, _loop;
return _regenerator.default.wrap(function _callee$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
adf = _ref.adf, nodeViewDataProviders = _ref.nodeViewDataProviders, signal = _ref.signal;
visitors = {};
promises = [];
_iterator = _createForOfIteratorHelper(nodeViewDataProviders);
_context2.prev = 4;
_loop = /*#__PURE__*/_regenerator.default.mark(function _loop() {
var nodeViewDataProvider;
return _regenerator.default.wrap(function _loop$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
nodeViewDataProvider = _step.value;
visitors[nodeViewDataProvider.nodeName] = function (node) {
// We need to cast the node to ReceivableNode as the type is not compatible due to not all nodes having
// attrs.
// Providers are only supported for nodes that have attrs.
var promise = nodeViewDataProvider.resolve(node, {
signal: signal
});
promises.push(promise);
promise.then(function (resolvedValue) {
var signalAborted = signal ? signal.aborted : false;
if (!signalAborted) {
nodeViewDataProvider.cache[nodeViewDataProvider.nodeToKey(node)] = resolvedValue;
}
});
return undefined;
};
case 2:
case "end":
return _context.stop();
}
}, _loop);
});
_iterator.s();
case 7:
if ((_step = _iterator.n()).done) {
_context2.next = 11;
break;
}
return _context2.delegateYield(_loop(), "t0", 9);
case 9:
_context2.next = 7;
break;
case 11:
_context2.next = 16;
break;
case 13:
_context2.prev = 13;
_context2.t1 = _context2["catch"](4);
_iterator.e(_context2.t1);
case 16:
_context2.prev = 16;
_iterator.f();
return _context2.finish(16);
case 19:
(0, _traverse.traverse)(adf, visitors);
_context2.next = 22;
return Promise.all(promises);
case 22:
case "end":
return _context2.stop();
}
}, _callee, null, [[4, 13, 16, 19]]);
}));
return _buildCaches.apply(this, arguments);
}