@stackblitz_sdk.js 10 KB

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