UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

177 lines 8.21 kB
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _regeneratorRuntime from "@babel/runtime/regenerator"; function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, 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 o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } import { getAutoConvertPatternsFromModule } from './module-helpers'; var DefaultExtensionProvider = /*#__PURE__*/function () { function DefaultExtensionProvider(manifests, /** * Allows for an optional list of pre compiled auto convert handlers to be passed. * Useful for performance improvements or to support legacy converters. * * Warning: If this attribute is passed, this provider will ignore auto convert patterns from the manifests. */ autoConvertHandlers) { _classCallCheck(this, DefaultExtensionProvider); _defineProperty(this, "manifestsCache", []); this.manifestsPromise = Promise.resolve(manifests); this.autoConvertHandlers = autoConvertHandlers; } return _createClass(DefaultExtensionProvider, [{ key: "getExtensions", value: function getExtensions() { return this.manifestsPromise; } }, { key: "preload", value: function () { var _preload = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() { var preloadCalls, _iterator, _step, _manifest$modules, manifest, nodes; return _regeneratorRuntime.wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.next = 2; return this.getExtensions(); case 2: this.manifestsCache = _context.sent; preloadCalls = []; _iterator = _createForOfIteratorHelper(this.manifestsCache); try { for (_iterator.s(); !(_step = _iterator.n()).done;) { manifest = _step.value; nodes = manifest === null || manifest === void 0 || (_manifest$modules = manifest.modules) === null || _manifest$modules === void 0 ? void 0 : _manifest$modules.nodes; if (nodes) { Object.values(nodes).forEach(function (node) { var _preloadRender, _ref; preloadCalls.push(node === null || node === void 0 || (_preloadRender = (_ref = node).preloadRender) === null || _preloadRender === void 0 ? void 0 : _preloadRender.call(_ref)); }); } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } _context.next = 8; return Promise.allSettled(preloadCalls); case 8: case "end": return _context.stop(); } }, _callee, this); })); function preload() { return _preload.apply(this, arguments); } return preload; }() }, { key: "getPreloadedExtension", value: function getPreloadedExtension(type, key) { if (!this.manifestsCache) { return; } return this.getExtensionFromManifest(this.manifestsCache, type, key); } }, { key: "getExtension", value: function () { var _getExtension = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(type, key) { return _regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.t0 = this; _context2.next = 3; return this.manifestsPromise; case 3: _context2.t1 = _context2.sent; _context2.t2 = type; _context2.t3 = key; return _context2.abrupt("return", _context2.t0.getExtensionFromManifest.call(_context2.t0, _context2.t1, _context2.t2, _context2.t3)); case 7: case "end": return _context2.stop(); } }, _callee2, this); })); function getExtension(_x, _x2) { return _getExtension.apply(this, arguments); } return getExtension; }() }, { key: "getExtensionFromManifest", value: function getExtensionFromManifest(manifests, type, key) { var extension = manifests.find(function (manifest) { return manifest.type === type && manifest.key === key; }); if (!extension) { throw new Error("Extension with type \"".concat(type, "\" and key \"").concat(key, "\" not found!")); } return extension; } }, { key: "search", value: function () { var _search = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(keyword) { var extensions; return _regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return this.manifestsPromise; case 2: extensions = _context3.sent.filter(function (manifest) { return manifest.title.toLowerCase().includes(keyword.toLowerCase()); }); return _context3.abrupt("return", extensions); case 4: case "end": return _context3.stop(); } }, _callee3, this); })); function search(_x3) { return _search.apply(this, arguments); } return search; }() }, { key: "getAutoConverter", value: function () { var _getAutoConverter = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() { var autoConverters; return _regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: if (!this.autoConvertHandlers) { _context4.next = 2; break; } return _context4.abrupt("return", this.autoConvertHandlers); case 2: _context4.t0 = getAutoConvertPatternsFromModule; _context4.next = 5; return this.manifestsPromise; case 5: _context4.t1 = _context4.sent; autoConverters = (0, _context4.t0)(_context4.t1); return _context4.abrupt("return", autoConverters); case 8: case "end": return _context4.stop(); } }, _callee4, this); })); function getAutoConverter() { return _getAutoConverter.apply(this, arguments); } return getAutoConverter; }() }]); }(); export { DefaultExtensionProvider as default };