index.js 32 KB

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