@stackblitz_sdk.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // node_modules/.pnpm/@stackblitz+sdk@1.8.1/node_modules/@stackblitz/sdk/bundles/sdk.m.js
  2. var e = ["angular-cli", "create-react-app", "html", "javascript", "node", "polymer", "typescript", "vue"];
  3. var t = { clickToLoad: function(e2) {
  4. return r("ctl", e2);
  5. }, devToolsHeight: function(e2) {
  6. return i("devtoolsheight", e2);
  7. }, forceEmbedLayout: function(e2) {
  8. return r("embed", e2);
  9. }, hideDevTools: function(e2) {
  10. return r("hidedevtools", e2);
  11. }, hideExplorer: function(e2) {
  12. return r("hideExplorer", e2);
  13. }, hideNavigation: function(e2) {
  14. return r("hideNavigation", e2);
  15. }, showSidebar: function(e2) {
  16. return function(e3, t2) {
  17. return "boolean" == typeof t2 ? "showSidebar=" + (t2 ? "1" : "0") : "";
  18. }(0, e2);
  19. }, openFile: function(e2) {
  20. return function(e3, t2) {
  21. return (Array.isArray(t2) ? t2 : [t2]).filter(function(e4) {
  22. return "string" == typeof e4 && "" !== e4.trim();
  23. }).map(function(e4) {
  24. return "file=" + encodeURIComponent(e4.trim());
  25. });
  26. }(0, e2).join("&");
  27. }, terminalHeight: function(e2) {
  28. return i("terminalHeight", e2);
  29. }, theme: function(e2) {
  30. return o("theme", ["light", "dark"], e2);
  31. }, view: function(e2) {
  32. return o("view", ["preview", "editor"], e2);
  33. } };
  34. function n(e2) {
  35. void 0 === e2 && (e2 = {});
  36. var n2 = Object.entries(e2).map(function(e3) {
  37. var n3 = e3[0], r2 = e3[1];
  38. return null != r2 && t.hasOwnProperty(n3) ? t[n3](r2) : "";
  39. }).filter(Boolean);
  40. return n2.length ? "?" + n2.join("&") : "";
  41. }
  42. function r(e2, t2) {
  43. return true === t2 ? e2 + "=1" : "";
  44. }
  45. function i(e2, t2) {
  46. return "number" == typeof t2 && t2 >= 0 && t2 <= 100 ? e2 + "=" + Math.round(t2) : "";
  47. }
  48. function o(e2, t2, n2) {
  49. return "string" == typeof n2 && t2.includes(n2) ? e2 + "=" + n2 : "";
  50. }
  51. function a() {
  52. return Math.random().toString(36).slice(2, 6) + Math.random().toString(36).slice(2, 6);
  53. }
  54. function d(e2, t2) {
  55. return "" + u(t2) + e2 + n(t2);
  56. }
  57. function c(e2, t2) {
  58. var r2 = { forceEmbedLayout: true };
  59. return t2 && "object" == typeof t2 && Object.assign(r2, t2), "" + u(r2) + e2 + n(r2);
  60. }
  61. function u(e2) {
  62. return void 0 === e2 && (e2 = {}), "string" == typeof e2.origin ? e2.origin : "https://stackblitz.com";
  63. }
  64. function s(e2, t2, n2) {
  65. if (!t2 || !e2 || !e2.parentNode)
  66. throw new Error("Invalid Element");
  67. e2.id && (t2.id = e2.id), e2.className && (t2.className = e2.className), function(e3, t3) {
  68. t3 && "object" == typeof t3 && (Object.hasOwnProperty.call(t3, "height") && (e3.height = "" + t3.height), Object.hasOwnProperty.call(t3, "width") && (e3.width = "" + t3.width)), e3.height || (e3.height = "300"), e3.width || e3.setAttribute("style", "width:100%;");
  69. }(t2, n2), e2.parentNode.replaceChild(t2, e2);
  70. }
  71. function l(e2) {
  72. if ("string" == typeof e2) {
  73. var t2 = document.getElementById(e2);
  74. if (!t2)
  75. throw new Error("Could not find element with id '" + e2 + "'");
  76. return t2;
  77. }
  78. if (e2 instanceof HTMLElement)
  79. return e2;
  80. throw new Error("Invalid element: " + e2);
  81. }
  82. function p(e2) {
  83. return e2 && false === e2.newWindow ? "_self" : "_blank";
  84. }
  85. function f() {
  86. return f = Object.assign || function(e2) {
  87. for (var t2 = 1; t2 < arguments.length; t2++) {
  88. var n2 = arguments[t2];
  89. for (var r2 in n2)
  90. Object.prototype.hasOwnProperty.call(n2, r2) && (e2[r2] = n2[r2]);
  91. }
  92. return e2;
  93. }, f.apply(this, arguments);
  94. }
  95. var h = function() {
  96. function e2(e3) {
  97. this.port = void 0, this.pending = {}, this.port = e3, this.port.onmessage = this.messageListener.bind(this);
  98. }
  99. var t2 = e2.prototype;
  100. return t2.request = function(e3) {
  101. var t3 = this, n2 = e3.type, r2 = e3.payload, i2 = a();
  102. return new Promise(function(e4, o2) {
  103. t3.pending[i2] = { resolve: e4, reject: o2 }, t3.port.postMessage({ type: n2, payload: f({}, r2, { __reqid: i2 }) });
  104. });
  105. }, t2.messageListener = function(e3) {
  106. var t3;
  107. if ("string" == typeof (null == (t3 = e3.data.payload) ? void 0 : t3.__reqid)) {
  108. var n2 = e3.data, r2 = n2.type, i2 = n2.payload, o2 = i2.__reqid, a2 = i2.__error;
  109. this.pending[o2] && (i2.__success ? this.pending[o2].resolve(function(e4) {
  110. var t4 = f({}, e4);
  111. return delete t4.__reqid, delete t4.__success, delete t4.__error, Object.keys(t4).length ? t4 : null;
  112. }(i2)) : this.pending[o2].reject(a2 ? r2 + ": " + a2 : r2), delete this.pending[o2]);
  113. }
  114. }, e2;
  115. }();
  116. var m = function() {
  117. function e2(e3, t3) {
  118. var n2 = this;
  119. this._rdc = void 0, this.editor = { openFile: function(e4) {
  120. return n2._rdc.request({ type: "SDK_OPEN_FILE", payload: { path: e4 } });
  121. }, setCurrentFile: function(e4) {
  122. return n2._rdc.request({ type: "SDK_SET_CURRENT_FILE", payload: { path: e4 } });
  123. }, setTheme: function(e4) {
  124. return n2._rdc.request({ type: "SDK_SET_UI_THEME", payload: { theme: e4 } });
  125. }, setView: function(e4) {
  126. return n2._rdc.request({ type: "SDK_SET_UI_VIEW", payload: { view: e4 } });
  127. }, showSidebar: function(e4) {
  128. return void 0 === e4 && (e4 = true), n2._rdc.request({ type: "SDK_TOGGLE_SIDEBAR", payload: { visible: e4 } });
  129. } }, this.preview = { origin: "", getUrl: function() {
  130. return n2._rdc.request({ type: "SDK_GET_PREVIEW_URL", payload: {} }).then(function(e4) {
  131. var t4;
  132. return null != (t4 = null == e4 ? void 0 : e4.url) ? t4 : null;
  133. });
  134. }, setUrl: function(e4) {
  135. if (void 0 === e4 && (e4 = "/"), "string" != typeof e4 || !e4.startsWith("/"))
  136. throw new Error("Invalid argument: expected a path starting with '/', got '" + e4 + "'");
  137. return n2._rdc.request({ type: "SDK_SET_PREVIEW_URL", payload: { path: e4 } });
  138. } }, this._rdc = new h(e3), Object.defineProperty(this.preview, "origin", { value: "string" == typeof t3.previewOrigin ? t3.previewOrigin : null, writable: false });
  139. }
  140. var t2 = e2.prototype;
  141. return t2.applyFsDiff = function(e3) {
  142. var t3 = function(e4) {
  143. return null !== e4 && "object" == typeof e4;
  144. };
  145. if (!t3(e3) || !t3(e3.create))
  146. throw new Error("Invalid diff object: expected diff.create to be an object.");
  147. if (!Array.isArray(e3.destroy))
  148. throw new Error("Invalid diff object: expected diff.create to be an array.");
  149. return this._rdc.request({ type: "SDK_APPLY_FS_DIFF", payload: e3 });
  150. }, t2.getDependencies = function() {
  151. return this._rdc.request({ type: "SDK_GET_DEPS_SNAPSHOT", payload: {} });
  152. }, t2.getFsSnapshot = function() {
  153. return this._rdc.request({ type: "SDK_GET_FS_SNAPSHOT", payload: {} });
  154. }, e2;
  155. }();
  156. var v = [];
  157. var y = function(e2) {
  158. var t2 = this;
  159. this.element = void 0, this.id = void 0, this.pending = void 0, this.vm = void 0, this.id = a(), this.element = e2, this.pending = new Promise(function(e3, n2) {
  160. var r2 = function(n3) {
  161. var r3 = n3.data;
  162. "SDK_INIT_SUCCESS" === (null == r3 ? void 0 : r3.action) && r3.id === t2.id && (t2.vm = new m(n3.ports[0], r3.payload), e3(t2.vm), o2());
  163. }, i2 = function() {
  164. var e4;
  165. null == (e4 = t2.element.contentWindow) || e4.postMessage({ action: "SDK_INIT", id: t2.id }, "*");
  166. };
  167. function o2() {
  168. window.clearInterval(d2), window.removeEventListener("message", r2);
  169. }
  170. window.addEventListener("message", r2), i2();
  171. var a2 = 0, d2 = window.setInterval(function() {
  172. if (t2.vm)
  173. o2();
  174. else {
  175. if (a2 >= 20)
  176. return o2(), n2("Timeout: Unable to establish a connection with the StackBlitz VM"), void v.forEach(function(e4, n3) {
  177. e4.id === t2.id && v.splice(n3, 1);
  178. });
  179. a2++, i2();
  180. }
  181. }, 500);
  182. }), v.push(this);
  183. };
  184. function g(e2, t2) {
  185. var n2 = document.createElement("input");
  186. return n2.type = "hidden", n2.name = e2, n2.value = t2, n2;
  187. }
  188. function w(t2) {
  189. if (!e.includes(t2.template)) {
  190. var n2 = e.map(function(e2) {
  191. return "'" + e2 + "'";
  192. }).join(", ");
  193. console.warn("Unsupported project.template: must be one of " + n2);
  194. }
  195. var r2 = "node" === t2.template, i2 = document.createElement("form");
  196. return i2.method = "POST", i2.setAttribute("style", "display:none!important;"), i2.appendChild(g("project[title]", t2.title)), i2.appendChild(g("project[description]", t2.description)), i2.appendChild(g("project[template]", t2.template)), t2.dependencies && (r2 ? console.warn("Invalid project.dependencies: dependencies must be provided as a 'package.json' file when using the 'node' template.") : i2.appendChild(g("project[dependencies]", JSON.stringify(t2.dependencies)))), t2.settings && i2.appendChild(g("project[settings]", JSON.stringify(t2.settings))), Object.keys(t2.files).forEach(function(e2) {
  197. "string" == typeof t2.files[e2] && i2.appendChild(g("project[files][" + e2 + "]", t2.files[e2]));
  198. }), i2;
  199. }
  200. function _(e2) {
  201. var t2, n2, r2, i2;
  202. return null != e2 && e2.contentWindow ? (null != (i2 = (n2 = e2) instanceof Element ? "element" : "id", t2 = null != (r2 = v.find(function(e3) {
  203. return e3[i2] === n2;
  204. })) ? r2 : null) ? t2 : new y(e2)).pending : Promise.reject("Provided element is not an iframe.");
  205. }
  206. var b = { connect: _, embedGithubProject: function(e2, t2, n2) {
  207. var r2 = l(e2), i2 = document.createElement("iframe");
  208. return i2.src = c("/github/" + t2, n2), s(r2, i2, n2), _(i2);
  209. }, embedProject: function(e2, t2, n2) {
  210. var r2, i2 = l(e2), o2 = function(e3, t3) {
  211. var n3 = w(e3);
  212. return n3.action = c("/run", t3), n3.id = "sb", "<html><head><title></title></head><body>" + n3.outerHTML + "<script>document.getElementById('" + n3.id + "').submit();<\/script></body></html>";
  213. }(t2, n2), a2 = document.createElement("iframe");
  214. return s(i2, a2, n2), null == (r2 = a2.contentDocument) || r2.write(o2), _(a2);
  215. }, embedProjectId: function(e2, t2, n2) {
  216. var r2 = l(e2), i2 = document.createElement("iframe");
  217. return i2.src = c("/edit/" + t2, n2), s(r2, i2, n2), _(i2);
  218. }, openGithubProject: function(e2, t2) {
  219. var n2 = d("/github/" + e2, t2), r2 = p(t2);
  220. window.open(n2, r2);
  221. }, openProject: function(e2, t2) {
  222. !function(e3, t3) {
  223. var n2 = w(e3);
  224. n2.action = d("/run", t3), n2.target = p(t3), document.body.appendChild(n2), n2.submit(), document.body.removeChild(n2);
  225. }(e2, t2);
  226. }, openProjectId: function(e2, t2) {
  227. var n2 = d("/edit/" + e2, t2), r2 = p(t2);
  228. window.open(n2, r2);
  229. } };
  230. export {
  231. b as default
  232. };
  233. //# sourceMappingURL=@stackblitz_sdk.js.map