index.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. (function () {
  2. var jsEditor;
  3. var fontSize = 14;
  4. var elementToTheme = ['.wrapper .gutter', '.navbar', '.navbar .select .toDisplay .option', '.navbar .select .toDisplayBig', '.navbar .select .toDisplayBig a', '.navbar .select .toDisplayBig ul li', '.navbarBottom'];
  5. var run = function () {
  6. var blockEditorChange = false;
  7. jsEditor.onKeyDown(function (evt) {
  8. });
  9. jsEditor.onKeyUp(function (evt) {
  10. if (blockEditorChange) {
  11. return;
  12. }
  13. document.getElementById("currentScript").innerHTML = "Custom";
  14. document.getElementById('safemodeToggle').classList.add('checked');
  15. });
  16. var snippetUrl = "https://babylonjs-api2.azurewebsites.net/snippets";
  17. var currentSnippetToken;
  18. var currentSnippetTitle = null;
  19. var currentSnippetDescription = null;
  20. var currentSnippetTags = null;
  21. var engine;
  22. var fpsLabel = document.getElementById("fpsLabel");
  23. var scripts;
  24. var zipCode;
  25. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  26. var currentVersionElement = document.getElementById("currentVersion");
  27. if (currentVersionElement) {
  28. switch (BABYLON.Engine.Version) {
  29. case "2.5":
  30. currentVersionElement.innerHTML = "Version: " + BABYLON.Engine.Version;
  31. break;
  32. default:
  33. currentVersionElement.innerHTML = "Version: Latest";
  34. break;
  35. }
  36. }
  37. var loadScript = function (scriptURL, title) {
  38. var xhr = new XMLHttpRequest();
  39. xhr.open('GET', scriptURL, true);
  40. xhr.onreadystatechange = function () {
  41. if (xhr.readyState === 4) {
  42. if (xhr.status === 200) {
  43. blockEditorChange = true;
  44. jsEditor.setValue(xhr.responseText);
  45. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  46. blockEditorChange = false;
  47. compileAndRun();
  48. document.getElementById("currentScript").innerHTML = title;
  49. currentSnippetToken = null;
  50. }
  51. }
  52. };
  53. xhr.send(null);
  54. };
  55. var loadScriptFromIndex = function (index) {
  56. if (index === 0) {
  57. index = 1;
  58. }
  59. var script = scripts[index - 1].trim();
  60. loadScript("scripts/" + script + ".js", script);
  61. }
  62. var onScriptClick = function (evt) {
  63. loadScriptFromIndex(evt.target.scriptLinkIndex);
  64. }
  65. var loadScriptsList = function () {
  66. var xhr = new XMLHttpRequest();
  67. xhr.open('GET', 'scripts/scripts.txt', true);
  68. xhr.onreadystatechange = function () {
  69. if (xhr.readyState === 4) {
  70. if (xhr.status === 200) {
  71. scripts = xhr.responseText.split("\n");
  72. var ul = document.getElementById("scriptsList");
  73. var index;
  74. for (index = 0; index < scripts.length; index++) {
  75. var option = document.createElement("li");
  76. var a = document.createElement("a");
  77. a.href = "#";
  78. a.innerHTML = (index + 1) + " - " + scripts[index];
  79. a.scriptLinkIndex = index + 1;
  80. a.onclick = onScriptClick;
  81. option.appendChild(a);
  82. ul.appendChild(option);
  83. }
  84. if (!location.hash) {
  85. // Query string
  86. var queryString = window.location.search;
  87. if (queryString) {
  88. var query = queryString.replace("?", "");
  89. index = parseInt(query);
  90. if (!isNaN(index)) {
  91. loadScriptFromIndex(index);
  92. } else {
  93. loadScript("scripts/" + query + ".js", query);
  94. }
  95. } else {
  96. loadScript("scripts/basic scene.js", "Basic scene");
  97. }
  98. }
  99. }
  100. }
  101. };
  102. xhr.send(null);
  103. }
  104. var createNewScript = function () {
  105. location.hash = "";
  106. currentSnippetToken = null;
  107. currentSnippetTitle = null;
  108. currentSnippetDescription = null;
  109. currentSnippetTags = null;
  110. showNoMetadata();
  111. jsEditor.setValue('// You have to create a function called createScene. This function must return a BABYLON.Scene object\r\n// You can reference the following variables: scene, canvas\r\n// You must at least define a camera\r\n// More info here: https://doc.babylonjs.com/generals/The_Playground_Tutorial\r\n\r\nvar createScene = function() {\r\n\tvar scene = new BABYLON.Scene(engine);\r\n\tvar camera = new BABYLON.ArcRotateCamera("Camera", 0, Math.PI / 2, 12, BABYLON.Vector3.Zero(), scene);\r\n\tcamera.attachControl(canvas, true);\r\n\r\n\r\n\r\n\treturn scene;\r\n};');
  112. jsEditor.setPosition({ lineNumber: 11, column: 0 });
  113. jsEditor.focus();
  114. compileAndRun();
  115. }
  116. var clear = function () {
  117. location.hash = "";
  118. currentSnippetToken = null;
  119. jsEditor.setValue('');
  120. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  121. jsEditor.focus();
  122. }
  123. var showError = function (errorMessage, errorEvent) {
  124. var errorContent =
  125. '<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>';
  126. if (errorEvent) {
  127. var regEx = /\(.+:(\d+):(\d+)\)\n/g;
  128. var match = regEx.exec(errorEvent.stack);
  129. if (match) {
  130. errorContent += "Line ";
  131. var lineNumber = match[1];
  132. var columnNumber = match[2];
  133. errorContent += lineNumber + ':' + columnNumber + ' - ';
  134. }
  135. }
  136. errorContent += errorMessage + '</div>';
  137. document.getElementById("errorZone").style.display = 'block';
  138. document.getElementById("errorZone").innerHTML = errorContent;
  139. // Close button error
  140. document.getElementById("errorZone").querySelector('.close').addEventListener('click', function () {
  141. document.getElementById("errorZone").style.display = 'none';
  142. });
  143. }
  144. var showNoMetadata = function () {
  145. document.getElementById("saveFormTitle").value = '';
  146. document.getElementById("saveFormTitle").readOnly = false;
  147. document.getElementById("saveFormDescription").value = '';
  148. document.getElementById("saveFormDescription").readOnly = false;
  149. document.getElementById("saveFormTags").value = '';
  150. document.getElementById("saveFormTags").readOnly = false;
  151. document.getElementById("saveFormButtons").style.display = "block";
  152. document.getElementById("saveMessage").style.display = "block";
  153. document.getElementById("metadataButton").style.display = "none";
  154. };
  155. // showNoMetadata();
  156. var hideNoMetadata = function () {
  157. document.getElementById("saveFormTitle").readOnly = true;
  158. document.getElementById("saveFormDescription").readOnly = true;
  159. document.getElementById("saveFormTags").readOnly = true;
  160. document.getElementById("saveFormButtons").style.display = "none";
  161. document.getElementById("saveMessage").style.display = "none";
  162. document.getElementById("metadataButton").style.display = "inline-block";
  163. };
  164. compileAndRun = function () {
  165. try {
  166. if (!BABYLON.Engine.isSupported()) {
  167. showError("Your browser does not support WebGL", null);
  168. return;
  169. }
  170. if (engine) {
  171. engine.dispose();
  172. engine = null;
  173. }
  174. var canvas = document.getElementById("renderCanvas");
  175. engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
  176. document.getElementById("errorZone").style.display = 'none';
  177. document.getElementById("errorZone").innerHTML = "";
  178. document.getElementById("statusBar").innerHTML = "Loading assets...Please wait";
  179. engine.runRenderLoop(function () {
  180. if (engine.scenes.length === 0) {
  181. return;
  182. }
  183. if (canvas.width !== canvas.clientWidth) {
  184. engine.resize();
  185. }
  186. var scene = engine.scenes[0];
  187. if (scene.activeCamera || scene.activeCameras.length > 0) {
  188. scene.render();
  189. }
  190. fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
  191. });
  192. var code = jsEditor.getValue();
  193. var scene;
  194. if (code.indexOf("createScene") !== -1) { // createScene
  195. eval(code);
  196. scene = createScene();
  197. if (!scene) {
  198. showError("createScene function must return a scene.", null);
  199. return;
  200. }
  201. zipCode = code + "\r\n\r\nvar scene = createScene();";
  202. } else if (code.indexOf("CreateScene") !== -1) { // CreateScene
  203. eval(code);
  204. scene = CreateScene();
  205. if (!scene) {
  206. showError("CreateScene function must return a scene.", null);
  207. return;
  208. }
  209. zipCode = code + "\r\n\r\nvar scene = CreateScene();";
  210. } else if (code.indexOf("createscene") !== -1) { // createscene
  211. eval(code);
  212. scene = createscene();
  213. if (!scene) {
  214. showError("createscene function must return a scene.", null);
  215. return;
  216. }
  217. zipCode = code + "\r\n\r\nvar scene = createscene();";
  218. } else { // Direct code
  219. scene = new BABYLON.Scene(engine);
  220. eval("runScript = function(scene, canvas) {" + code + "}");
  221. runScript(scene, canvas);
  222. zipCode = "var scene = new BABYLON.Scene(engine);\r\n\r\n" + code;
  223. }
  224. if (engine.scenes.length === 0) {
  225. showError("You must at least create a scene.", null);
  226. return;
  227. }
  228. if (engine.scenes[0].activeCamera == null) {
  229. showError("You must at least create a camera.", null);
  230. return;
  231. }
  232. engine.scenes[0].executeWhenReady(function () {
  233. document.getElementById("statusBar").innerHTML = "";
  234. });
  235. } catch (e) {
  236. showError(e.message, e);
  237. }
  238. };
  239. window.addEventListener("resize",
  240. function () {
  241. if (engine) {
  242. engine.resize();
  243. }
  244. });
  245. // Load scripts list
  246. loadScriptsList();
  247. // Zip
  248. var addContentToZip = function (zip, name, url, replace, buffer, then) {
  249. var xhr = new XMLHttpRequest();
  250. xhr.open('GET', url, true);
  251. if (buffer) {
  252. xhr.responseType = "arraybuffer";
  253. }
  254. xhr.onreadystatechange = function () {
  255. if (xhr.readyState === 4) {
  256. if (xhr.status === 200) {
  257. var text;
  258. if (!buffer) {
  259. if (replace) {
  260. var splits = replace.split("\r\n");
  261. for (var index = 0; index < splits.length; index++) {
  262. splits[index] = " " + splits[index];
  263. }
  264. replace = splits.join("\r\n");
  265. text = xhr.responseText.replace("####INJECT####", replace);
  266. } else {
  267. text = xhr.responseText;
  268. }
  269. }
  270. zip.file(name, buffer ? xhr.response : text);
  271. then();
  272. }
  273. }
  274. };
  275. xhr.send(null);
  276. }
  277. var addTexturesToZip = function (zip, index, textures, folder, then) {
  278. if (index === textures.length) {
  279. then();
  280. return;
  281. }
  282. if (textures[index].isRenderTarget || textures[index] instanceof BABYLON.DynamicTexture) {
  283. addTexturesToZip(zip, index + 1, textures, folder, then);
  284. return;
  285. }
  286. if (textures[index].isCube) {
  287. if (textures[index]._extensions) {
  288. for (var i = 0; i < 6; i++) {
  289. textures.push({ name: textures[index].name + textures[index]._extensions[i] });
  290. }
  291. }
  292. else {
  293. textures.push({ name: textures[index].name });
  294. }
  295. addTexturesToZip(zip, index + 1, textures, folder, then);
  296. return;
  297. }
  298. if (folder == null) {
  299. folder = zip.folder("textures");
  300. }
  301. var url;
  302. if (textures[index].video) {
  303. url = textures[index].video.currentSrc;
  304. } else {
  305. url = textures[index].name;
  306. }
  307. var name = url.substr(url.lastIndexOf("/") + 1);
  308. addContentToZip(folder,
  309. name,
  310. url,
  311. null,
  312. true,
  313. function () {
  314. addTexturesToZip(zip, index + 1, textures, folder, then);
  315. });
  316. }
  317. var addImportedFilesToZip = function (zip, index, importedFiles, folder, then) {
  318. if (index === importedFiles.length) {
  319. then();
  320. return;
  321. }
  322. if (!folder) {
  323. folder = zip.folder("scenes");
  324. }
  325. var url = importedFiles[index];
  326. var name = url.substr(url.lastIndexOf("/") + 1);
  327. addContentToZip(folder,
  328. name,
  329. url,
  330. null,
  331. true,
  332. function () {
  333. addImportedFilesToZip(zip, index + 1, importedFiles, folder, then);
  334. });
  335. }
  336. var getZip = function () {
  337. if (engine.scenes.length === 0) {
  338. return;
  339. }
  340. var zip = new JSZip();
  341. var scene = engine.scenes[0];
  342. var textures = scene.textures;
  343. var importedFiles = scene.importedMeshesFiles;
  344. document.getElementById("statusBar").innerHTML = "Creating archive...Please wait";
  345. if (zipCode.indexOf("textures/worldHeightMap.jpg") !== -1) {
  346. textures.push({ name: "textures/worldHeightMap.jpg" });
  347. }
  348. addContentToZip(zip,
  349. "index.html",
  350. "zipContent/index.html",
  351. zipCode,
  352. false,
  353. function () {
  354. addTexturesToZip(zip,
  355. 0,
  356. textures,
  357. null,
  358. function () {
  359. addImportedFilesToZip(zip,
  360. 0,
  361. importedFiles,
  362. null,
  363. function () {
  364. var blob = zip.generate({ type: "blob" });
  365. saveAs(blob, "sample.zip");
  366. document.getElementById("statusBar").innerHTML = "";
  367. });
  368. });
  369. });
  370. }
  371. // Versions
  372. setVersion = function (version) {
  373. switch (version) {
  374. case "2.5":
  375. location.href = "index2_5.html" + location.hash;
  376. break;
  377. default:
  378. location.href = "index.html" + location.hash;
  379. break;
  380. }
  381. }
  382. // Fonts
  383. setFontSize = function (size) {
  384. fontSize = size;
  385. document.querySelector(".view-lines").style.fontSize = size + "px";
  386. document.getElementById("currentFontSize").innerHTML = "Font: " + size;
  387. };
  388. // Fullscreen
  389. var goFullscreen = function () {
  390. if (engine) {
  391. engine.switchFullscreen(true);
  392. }
  393. }
  394. var toggleEditor = function () {
  395. var editorButton = document.getElementById("editorButton");
  396. var scene = engine.scenes[0];
  397. // If the editor is present
  398. if (editorButton.classList.contains('checked')) {
  399. editorButton.classList.remove('checked');
  400. document.getElementById("jsEditor").style.display = "none";
  401. editorButton.innerHTML = 'Editor <i class="fa fa-square-o" aria-hidden="true"></i>';
  402. document.getElementById("canvasZone").style.width = "100%";
  403. } else {
  404. editorButton.classList.add('checked');
  405. editorButton.innerHTML = 'Editor <i class="fa fa-check-square" aria-hidden="true"></i>';
  406. document.getElementById("jsEditor").style.display = "block";
  407. document.getElementById("canvasZone").style.width = "50%";
  408. // document.getElementById("canvasZone").style.flexBasis = undefined;
  409. }
  410. engine.resize();
  411. if (scene.debugLayer.isVisible()) {
  412. scene.debugLayer.hide();
  413. scene.debugLayer.show();
  414. }
  415. }
  416. /**
  417. * Toggle the dark theme
  418. */
  419. var toggleTheme = function (theme) {
  420. // Monaco
  421. let oldCode = jsEditor.getValue();
  422. jsEditor.dispose();
  423. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  424. value: "",
  425. language: "javascript",
  426. lineNumbers: true,
  427. tabSize: "auto",
  428. insertSpaces: "auto",
  429. roundedSelection: true,
  430. scrollBeyondLastLine: false,
  431. automaticLayout: true,
  432. readOnly: false,
  433. theme: "vs-" + theme,
  434. contextmenu: false
  435. });
  436. jsEditor.setValue(oldCode);
  437. setFontSize(fontSize);
  438. for (var obj of elementToTheme) {
  439. let domObjArr = document.querySelectorAll(obj);
  440. for (let domObj of domObjArr) {
  441. domObj.classList.add(theme);
  442. }
  443. }
  444. }
  445. var toggleDebug = function () {
  446. var debugButton = document.getElementById("debugButton");
  447. var scene = engine.scenes[0];
  448. if (debugButton.classList.contains('uncheck')) {
  449. debugButton.classList.remove('uncheck');
  450. scene.debugLayer.show();
  451. } else {
  452. debugButton.classList.add('uncheck');
  453. scene.debugLayer.hide();
  454. }
  455. }
  456. var toggleMetadata = function () {
  457. var metadataButton = document.getElementById("metadataButton");
  458. var scene = engine.scenes[0];
  459. metadataButton.classList.add('checked');
  460. document.getElementById("saveLayer").style.display = "block";
  461. }
  462. // UI
  463. document.getElementById("runButton").addEventListener("click", compileAndRun);
  464. document.getElementById("zipButton").addEventListener("click", getZip);
  465. document.getElementById("fullscreenButton").addEventListener("click", goFullscreen);
  466. document.getElementById("newButton").addEventListener("click", createNewScript);
  467. document.getElementById("clearButton").addEventListener("click", clear);
  468. document.getElementById("editorButton").addEventListener("click", toggleEditor);
  469. document.getElementById("debugButton").addEventListener("click", toggleDebug);
  470. document.getElementById("metadataButton").addEventListener("click", toggleMetadata);
  471. document.getElementById("darkTheme").addEventListener("click", toggleTheme.bind(this, 'dark'));
  472. document.getElementById("lightTheme").addEventListener("click", toggleTheme.bind(this, 'light'));
  473. // document.getElementById("lightTheme").addEventListener("click", toggleLightTheme);
  474. //Navigation Overwrites
  475. var exitPrompt = function (e) {
  476. var safeToggle = document.getElementById("safemodeToggle");
  477. if (safeToggle.classList.contains('checked')) {
  478. e = e || window.event;
  479. var message =
  480. 'This page is asking you to confirm that you want to leave - data you have entered may not be saved.';
  481. if (e) {
  482. e.returnValue = message;
  483. }
  484. return message;
  485. }
  486. };
  487. window.onbeforeunload = exitPrompt;
  488. // Snippet
  489. var save = function () {
  490. // Retrieve title if necessary
  491. if (document.getElementById("saveLayer")) {
  492. currentSnippetTitle = document.getElementById("saveFormTitle").value;
  493. currentSnippetDescription = document.getElementById("saveFormDescription").value;
  494. currentSnippetTags = document.getElementById("saveFormTags").value;
  495. }
  496. var xmlHttp = new XMLHttpRequest();
  497. xmlHttp.onreadystatechange = function () {
  498. if (xmlHttp.readyState === 4) {
  499. if (xmlHttp.status === 201) {
  500. var baseUrl = location.href.replace(location.hash, "").replace(location.search, "");
  501. var snippet = JSON.parse(xmlHttp.responseText);
  502. var newUrl = baseUrl + "#" + snippet.id;
  503. currentSnippetToken = snippet.id;
  504. if (snippet.version && snippet.version !== "0") {
  505. newUrl += "#" + snippet.version;
  506. }
  507. location.href = newUrl;
  508. // Hide the complete title & co message
  509. hideNoMetadata();
  510. compileAndRun();
  511. } else {
  512. showError("Unable to save your code. It may be too long.", null);
  513. }
  514. }
  515. }
  516. xmlHttp.open("POST", snippetUrl + (currentSnippetToken ? "/" + currentSnippetToken : ""), true);
  517. xmlHttp.setRequestHeader("Content-Type", "application/json");
  518. var dataToSend = {
  519. payload: {
  520. code: jsEditor.getValue()
  521. },
  522. name: currentSnippetTitle,
  523. description: currentSnippetDescription,
  524. tags: currentSnippetTags
  525. };
  526. xmlHttp.send(JSON.stringify(dataToSend));
  527. }
  528. document.getElementById("saveButton").addEventListener("click", function () {
  529. if (currentSnippetTitle == null
  530. && currentSnippetDescription == null
  531. && currentSnippetTags == null) {
  532. document.getElementById("saveLayer").style.display = "block";
  533. }
  534. else {
  535. save();
  536. }
  537. });
  538. document.getElementById("saveFormButtonOk").addEventListener("click", function () {
  539. document.getElementById("saveLayer").style.display = "none";
  540. save();
  541. });
  542. document.getElementById("saveFormButtonCancel").addEventListener("click", function () {
  543. document.getElementById("saveLayer").style.display = "none";
  544. });
  545. document.getElementById("saveMessage").addEventListener("click", function () {
  546. document.getElementById("saveMessage").style.display = "none";
  547. });
  548. document.getElementById("mainTitle").innerHTML = "v" + BABYLON.Engine.Version;
  549. var previousHash = "";
  550. var cleanHash = function () {
  551. var splits = decodeURIComponent(location.hash.substr(1)).split("#");
  552. if (splits.length > 2) {
  553. splits.splice(2, splits.length - 2);
  554. }
  555. location.hash = splits.join("#");
  556. }
  557. var checkHash = function (firstTime) {
  558. if (location.hash) {
  559. if (previousHash !== location.hash) {
  560. cleanHash();
  561. previousHash = location.hash;
  562. try {
  563. var xmlHttp = new XMLHttpRequest();
  564. xmlHttp.onreadystatechange = function () {
  565. if (xmlHttp.readyState === 4) {
  566. if (xmlHttp.status === 200) {
  567. var snippet = JSON.parse(xmlHttp.responseText)[0];
  568. blockEditorChange = true;
  569. jsEditor.setValue(JSON.parse(snippet.jsonPayload).code.toString());
  570. // Check if title / descr / tags are already set
  571. if ((snippet.name != null && snippet.name != "")
  572. || (snippet.description != null && snippet.description != "")
  573. || (snippet.tags != null && snippet.tags != "")) {
  574. currentSnippetTitle = snippet.name;
  575. currentSnippetDescription = snippet.description;
  576. currentSnippetTags = snippet.tags;
  577. if (document.getElementById("saveLayer")) {
  578. var elem = document.getElementById("saveLayer");
  579. document.getElementById("saveFormTitle").value = currentSnippetTitle;
  580. document.getElementById("saveFormDescription").value = currentSnippetDescription;
  581. document.getElementById("saveFormTags").value = currentSnippetTags;
  582. hideNoMetadata();
  583. }
  584. }
  585. else {
  586. currentSnippetTitle = null;
  587. currentSnippetDescription = null;
  588. currentSnippetTags = null;
  589. showNoMetadata();
  590. }
  591. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  592. blockEditorChange = false;
  593. compileAndRun();
  594. document.getElementById("currentScript").innerHTML = "Custom";
  595. } else if (firstTime) {
  596. location.href = location.href.replace(location.hash, "");
  597. if (scripts) {
  598. loadScriptFromIndex(0);
  599. }
  600. }
  601. }
  602. };
  603. var hash = location.hash.substr(1);
  604. currentSnippetToken = hash.split("#")[0];
  605. if (!hash.split("#")[1]) hash += "#0";
  606. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  607. xmlHttp.send();
  608. } catch (e) {
  609. }
  610. }
  611. }
  612. setTimeout(checkHash, 200);
  613. }
  614. checkHash(true);
  615. }
  616. // Monaco
  617. var xhr = new XMLHttpRequest();
  618. xhr.open('GET', "babylon.d.txt", true);
  619. xhr.onreadystatechange = function () {
  620. if (xhr.readyState === 4) {
  621. if (xhr.status === 200) {
  622. require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' } });
  623. require(['vs/editor/editor.main'], function () {
  624. monaco.languages.typescript.javascriptDefaults.addExtraLib(xhr.responseText, 'babylon.d.ts');
  625. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  626. value: "",
  627. language: "javascript",
  628. lineNumbers: true,
  629. tabSize: "auto",
  630. insertSpaces: "auto",
  631. roundedSelection: true,
  632. scrollBeyondLastLine: false,
  633. automaticLayout: true,
  634. readOnly: false,
  635. theme: "vs",
  636. contextmenu: false
  637. });
  638. run();
  639. });
  640. }
  641. }
  642. };
  643. xhr.send(null);
  644. })();