index.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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("saveMessage").style.display = "block";
  177. // document.getElementById("metadataButton").style.display = "none";
  178. };
  179. showNoMetadata();
  180. document.getElementById("saveMessage").style.display = "none";
  181. var hideNoMetadata = function () {
  182. document.getElementById("saveFormTitle").readOnly = true;
  183. document.getElementById("saveFormDescription").readOnly = true;
  184. document.getElementById("saveFormTags").readOnly = true;
  185. document.getElementById("saveFormButtonOk").style.display = "none";
  186. document.getElementById("saveMessage").style.display = "none";
  187. document.getElementById("metadataButton").style.display = "block";
  188. };
  189. compileAndRun = function () {
  190. try {
  191. if (!BABYLON.Engine.isSupported()) {
  192. showError("Your browser does not support WebGL", null);
  193. return;
  194. }
  195. if (engine) {
  196. engine.dispose();
  197. engine = null;
  198. }
  199. var canvas = document.getElementById("renderCanvas");
  200. engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
  201. document.getElementById("errorZone").style.display = 'none';
  202. document.getElementById("errorZone").innerHTML = "";
  203. document.getElementById("statusBar").innerHTML = "Loading assets...Please wait";
  204. engine.runRenderLoop(function () {
  205. if (engine.scenes.length === 0) {
  206. return;
  207. }
  208. if (canvas.width !== canvas.clientWidth) {
  209. engine.resize();
  210. }
  211. var scene = engine.scenes[0];
  212. if (scene.activeCamera || scene.activeCameras.length > 0) {
  213. scene.render();
  214. }
  215. fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
  216. });
  217. var code = jsEditor.getValue();
  218. var scene;
  219. if (code.indexOf("createScene") !== -1) { // createScene
  220. eval(code);
  221. scene = createScene();
  222. if (!scene) {
  223. showError("createScene function must return a scene.", null);
  224. return;
  225. }
  226. zipCode = code + "\r\n\r\nvar scene = createScene();";
  227. } else if (code.indexOf("CreateScene") !== -1) { // CreateScene
  228. eval(code);
  229. scene = CreateScene();
  230. if (!scene) {
  231. showError("CreateScene function must return a scene.", null);
  232. return;
  233. }
  234. zipCode = code + "\r\n\r\nvar scene = CreateScene();";
  235. } else if (code.indexOf("createscene") !== -1) { // createscene
  236. eval(code);
  237. scene = createscene();
  238. if (!scene) {
  239. showError("createscene function must return a scene.", null);
  240. return;
  241. }
  242. zipCode = code + "\r\n\r\nvar scene = createscene();";
  243. } else { // Direct code
  244. scene = new BABYLON.Scene(engine);
  245. eval("runScript = function(scene, canvas) {" + code + "}");
  246. runScript(scene, canvas);
  247. zipCode = "var scene = new BABYLON.Scene(engine);\r\n\r\n" + code;
  248. }
  249. if (engine.scenes.length === 0) {
  250. showError("You must at least create a scene.", null);
  251. return;
  252. }
  253. if (engine.scenes[0].activeCamera == null) {
  254. showError("You must at least create a camera.", null);
  255. return;
  256. }
  257. engine.scenes[0].executeWhenReady(function () {
  258. document.getElementById("statusBar").innerHTML = "";
  259. });
  260. } catch (e) {
  261. showError(e.message, e);
  262. }
  263. };
  264. window.addEventListener("resize",
  265. function () {
  266. if (engine) {
  267. engine.resize();
  268. }
  269. });
  270. // Load scripts list
  271. loadScriptsList();
  272. // Zip
  273. var addContentToZip = function (zip, name, url, replace, buffer, then) {
  274. var xhr = new XMLHttpRequest();
  275. xhr.open('GET', url, true);
  276. if (buffer) {
  277. xhr.responseType = "arraybuffer";
  278. }
  279. xhr.onreadystatechange = function () {
  280. if (xhr.readyState === 4) {
  281. if (xhr.status === 200) {
  282. var text;
  283. if (!buffer) {
  284. if (replace) {
  285. var splits = replace.split("\r\n");
  286. for (var index = 0; index < splits.length; index++) {
  287. splits[index] = " " + splits[index];
  288. }
  289. replace = splits.join("\r\n");
  290. text = xhr.responseText.replace("####INJECT####", replace);
  291. } else {
  292. text = xhr.responseText;
  293. }
  294. }
  295. zip.file(name, buffer ? xhr.response : text);
  296. then();
  297. }
  298. }
  299. };
  300. xhr.send(null);
  301. }
  302. var addTexturesToZip = function (zip, index, textures, folder, then) {
  303. if (index === textures.length) {
  304. then();
  305. return;
  306. }
  307. if (textures[index].isRenderTarget || textures[index] instanceof BABYLON.DynamicTexture) {
  308. addTexturesToZip(zip, index + 1, textures, folder, then);
  309. return;
  310. }
  311. if (textures[index].isCube) {
  312. if (textures[index]._extensions) {
  313. for (var i = 0; i < 6; i++) {
  314. textures.push({ name: textures[index].name + textures[index]._extensions[i] });
  315. }
  316. }
  317. else {
  318. textures.push({ name: textures[index].name });
  319. }
  320. addTexturesToZip(zip, index + 1, textures, folder, then);
  321. return;
  322. }
  323. if (folder == null) {
  324. folder = zip.folder("textures");
  325. }
  326. var url;
  327. if (textures[index].video) {
  328. url = textures[index].video.currentSrc;
  329. } else {
  330. url = textures[index].name;
  331. }
  332. var name = url.substr(url.lastIndexOf("/") + 1);
  333. addContentToZip(folder,
  334. name,
  335. url,
  336. null,
  337. true,
  338. function () {
  339. addTexturesToZip(zip, index + 1, textures, folder, then);
  340. });
  341. }
  342. var addImportedFilesToZip = function (zip, index, importedFiles, folder, then) {
  343. if (index === importedFiles.length) {
  344. then();
  345. return;
  346. }
  347. if (!folder) {
  348. folder = zip.folder("scenes");
  349. }
  350. var url = importedFiles[index];
  351. var name = url.substr(url.lastIndexOf("/") + 1);
  352. addContentToZip(folder,
  353. name,
  354. url,
  355. null,
  356. true,
  357. function () {
  358. addImportedFilesToZip(zip, index + 1, importedFiles, folder, then);
  359. });
  360. }
  361. var getZip = function () {
  362. if (engine.scenes.length === 0) {
  363. return;
  364. }
  365. var zip = new JSZip();
  366. var scene = engine.scenes[0];
  367. var textures = scene.textures;
  368. var importedFiles = scene.importedMeshesFiles;
  369. document.getElementById("statusBar").innerHTML = "Creating archive...Please wait";
  370. if (zipCode.indexOf("textures/worldHeightMap.jpg") !== -1) {
  371. textures.push({ name: "textures/worldHeightMap.jpg" });
  372. }
  373. addContentToZip(zip,
  374. "index.html",
  375. "zipContent/index.html",
  376. zipCode,
  377. false,
  378. function () {
  379. addTexturesToZip(zip,
  380. 0,
  381. textures,
  382. null,
  383. function () {
  384. addImportedFilesToZip(zip,
  385. 0,
  386. importedFiles,
  387. null,
  388. function () {
  389. var blob = zip.generate({ type: "blob" });
  390. saveAs(blob, "sample.zip");
  391. document.getElementById("statusBar").innerHTML = "";
  392. });
  393. });
  394. });
  395. }
  396. // Versions
  397. setVersion = function (version) {
  398. switch (version) {
  399. case "2.5":
  400. location.href = "index2_5.html" + location.hash;
  401. break;
  402. default:
  403. location.href = "index.html" + location.hash;
  404. break;
  405. }
  406. }
  407. // Fonts
  408. setFontSize = function (size) {
  409. fontSize = size;
  410. document.querySelector(".view-lines").style.fontSize = size + "px";
  411. document.getElementById("currentFontSize").innerHTML = "Font: " + size;
  412. };
  413. // Fullscreen
  414. var goFullscreen = function () {
  415. if (engine) {
  416. engine.switchFullscreen(true);
  417. }
  418. }
  419. var toggleEditor = function () {
  420. var editorButton = document.getElementById("editorButton");
  421. var scene = engine.scenes[0];
  422. // If the editor is present
  423. if (editorButton.classList.contains('checked')) {
  424. editorButton.classList.remove('checked');
  425. splitInstance.collapse(0);
  426. editorButton.innerHTML = 'Editor <i class="fa fa-square-o" aria-hidden="true"></i>';
  427. } else {
  428. editorButton.classList.add('checked');
  429. splitInstance.setSizes([50, 50]); // Reset
  430. editorButton.innerHTML = 'Editor <i class="fa fa-check-square" aria-hidden="true"></i>';
  431. }
  432. engine.resize();
  433. if (scene.debugLayer.isVisible()) {
  434. scene.debugLayer.hide();
  435. scene.debugLayer.show();
  436. }
  437. }
  438. /**
  439. * Toggle the dark theme
  440. */
  441. var toggleTheme = function (theme) {
  442. // Monaco
  443. var vsTheme;
  444. if (theme == 'dark') {
  445. vsTheme = 'vs-dark'
  446. } else {
  447. vsTheme = 'vs'
  448. }
  449. let oldCode = jsEditor.getValue();
  450. jsEditor.dispose();
  451. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  452. value: "",
  453. language: "javascript",
  454. lineNumbers: true,
  455. tabSize: "auto",
  456. insertSpaces: "auto",
  457. roundedSelection: true,
  458. scrollBeyondLastLine: false,
  459. automaticLayout: true,
  460. readOnly: false,
  461. theme: vsTheme,
  462. contextmenu: false
  463. });
  464. jsEditor.setValue(oldCode);
  465. setFontSize(fontSize);
  466. for (var index = 0; index < elementToTheme.length; index++) {
  467. var obj = elementToTheme[index];
  468. let domObjArr = document.querySelectorAll(obj);
  469. for (var domObjIndex = 0; domObjIndex < domObjArr.length; domObjIndex++) {
  470. var domObj = domObjArr[domObjIndex];
  471. domObj.classList.remove('light');
  472. domObj.classList.remove('dark');
  473. domObj.classList.add(theme);
  474. }
  475. }
  476. localStorage.setItem("bjs-playground-theme", theme);
  477. }
  478. var toggleDebug = function () {
  479. var debugButton = document.getElementById("debugButton");
  480. var scene = engine.scenes[0];
  481. if (debugButton.classList.contains('uncheck')) {
  482. debugButton.classList.remove('uncheck');
  483. scene.debugLayer.show();
  484. } else {
  485. debugButton.classList.add('uncheck');
  486. scene.debugLayer.hide();
  487. }
  488. }
  489. var toggleMetadata = function () {
  490. // var metadataButton = document.getElementById("metadataButton");
  491. var scene = engine.scenes[0];
  492. // metadataButton.classList.add('checked');
  493. document.getElementById("saveLayer").style.display = "block";
  494. }
  495. // UI
  496. document.getElementById("runButton").addEventListener("click", compileAndRun);
  497. document.getElementById("zipButton").addEventListener("click", getZip);
  498. document.getElementById("fullscreenButton").addEventListener("click", goFullscreen);
  499. document.getElementById("newButton").addEventListener("click", createNewScript);
  500. document.getElementById("clearButton").addEventListener("click", clear);
  501. document.getElementById("editorButton").addEventListener("click", toggleEditor);
  502. document.getElementById("debugButton").addEventListener("click", toggleDebug);
  503. document.getElementById("metadataButton").addEventListener("click", toggleMetadata);
  504. document.getElementById("darkTheme").addEventListener("click", toggleTheme.bind(this, 'dark'));
  505. document.getElementById("lightTheme").addEventListener("click", toggleTheme.bind(this, 'light'));
  506. // Restore theme
  507. var theme = localStorage.getItem("bjs-playground-theme") || 'light';
  508. toggleTheme(theme);
  509. //Navigation Overwrites
  510. var exitPrompt = function (e) {
  511. var safeToggle = document.getElementById("safemodeToggle");
  512. if (safeToggle.classList.contains('checked')) {
  513. e = e || window.event;
  514. var message =
  515. 'This page is asking you to confirm that you want to leave - data you have entered may not be saved.';
  516. if (e) {
  517. e.returnValue = message;
  518. }
  519. return message;
  520. }
  521. };
  522. window.onbeforeunload = exitPrompt;
  523. // Snippet
  524. var save = function () {
  525. // Retrieve title if necessary
  526. if (document.getElementById("saveLayer")) {
  527. currentSnippetTitle = document.getElementById("saveFormTitle").value;
  528. currentSnippetDescription = document.getElementById("saveFormDescription").value;
  529. currentSnippetTags = document.getElementById("saveFormTags").value;
  530. }
  531. var xmlHttp = new XMLHttpRequest();
  532. xmlHttp.onreadystatechange = function () {
  533. if (xmlHttp.readyState === 4) {
  534. if (xmlHttp.status === 201) {
  535. var baseUrl = location.href.replace(location.hash, "").replace(location.search, "");
  536. var snippet = JSON.parse(xmlHttp.responseText);
  537. var newUrl = baseUrl + "#" + snippet.id;
  538. currentSnippetToken = snippet.id;
  539. if (snippet.version && snippet.version !== "0") {
  540. newUrl += "#" + snippet.version;
  541. }
  542. location.href = newUrl;
  543. // Hide the complete title & co message
  544. hideNoMetadata();
  545. compileAndRun();
  546. } else {
  547. showError("Unable to save your code. It may be too long.", null);
  548. }
  549. }
  550. }
  551. xmlHttp.open("POST", snippetUrl + (currentSnippetToken ? "/" + currentSnippetToken : ""), true);
  552. xmlHttp.setRequestHeader("Content-Type", "application/json");
  553. var dataToSend = {
  554. payload: {
  555. code: jsEditor.getValue()
  556. },
  557. name: currentSnippetTitle,
  558. description: currentSnippetDescription,
  559. tags: currentSnippetTags
  560. };
  561. xmlHttp.send(JSON.stringify(dataToSend));
  562. }
  563. document.getElementById("saveButton").addEventListener("click", function () {
  564. if (currentSnippetTitle == null
  565. && currentSnippetDescription == null
  566. && currentSnippetTags == null) {
  567. document.getElementById("saveLayer").style.display = "block";
  568. }
  569. else {
  570. save();
  571. }
  572. });
  573. document.getElementById("saveFormButtonOk").addEventListener("click", function () {
  574. document.getElementById("saveLayer").style.display = "none";
  575. save();
  576. });
  577. document.getElementById("saveFormButtonCancel").addEventListener("click", function () {
  578. document.getElementById("saveLayer").style.display = "none";
  579. });
  580. document.getElementById("saveMessage").addEventListener("click", function () {
  581. document.getElementById("saveMessage").style.display = "none";
  582. });
  583. document.getElementById("mainTitle").innerHTML = "v" + BABYLON.Engine.Version;
  584. var previousHash = "";
  585. var cleanHash = function () {
  586. var splits = decodeURIComponent(location.hash.substr(1)).split("#");
  587. if (splits.length > 2) {
  588. splits.splice(2, splits.length - 2);
  589. }
  590. location.hash = splits.join("#");
  591. }
  592. var checkHash = function (firstTime) {
  593. if (location.hash) {
  594. if (previousHash !== location.hash) {
  595. cleanHash();
  596. previousHash = location.hash;
  597. try {
  598. var xmlHttp = new XMLHttpRequest();
  599. xmlHttp.onreadystatechange = function () {
  600. if (xmlHttp.readyState === 4) {
  601. if (xmlHttp.status === 200) {
  602. var snippet = JSON.parse(xmlHttp.responseText)[0];
  603. blockEditorChange = true;
  604. jsEditor.setValue(JSON.parse(snippet.jsonPayload).code.toString());
  605. // Check if title / descr / tags are already set
  606. if ((snippet.name != null && snippet.name != "")
  607. || (snippet.description != null && snippet.description != "")
  608. || (snippet.tags != null && snippet.tags != "")) {
  609. currentSnippetTitle = snippet.name;
  610. currentSnippetDescription = snippet.description;
  611. currentSnippetTags = snippet.tags;
  612. if (document.getElementById("saveLayer")) {
  613. var elem = document.getElementById("saveLayer");
  614. document.getElementById("saveFormTitle").value = currentSnippetTitle;
  615. document.getElementById("saveFormDescription").value = currentSnippetDescription;
  616. document.getElementById("saveFormTags").value = currentSnippetTags;
  617. hideNoMetadata();
  618. }
  619. }
  620. else {
  621. currentSnippetTitle = null;
  622. currentSnippetDescription = null;
  623. currentSnippetTags = null;
  624. showNoMetadata();
  625. }
  626. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  627. blockEditorChange = false;
  628. compileAndRun();
  629. document.getElementById("currentScript").innerHTML = "Custom";
  630. } else if (firstTime) {
  631. location.href = location.href.replace(location.hash, "");
  632. if (scripts) {
  633. loadScriptFromIndex(0);
  634. }
  635. }
  636. }
  637. };
  638. var hash = location.hash.substr(1);
  639. currentSnippetToken = hash.split("#")[0];
  640. if (!hash.split("#")[1]) hash += "#0";
  641. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  642. xmlHttp.send();
  643. } catch (e) {
  644. }
  645. }
  646. }
  647. setTimeout(checkHash, 200);
  648. }
  649. checkHash(true);
  650. }
  651. // Monaco
  652. var xhr = new XMLHttpRequest();
  653. xhr.open('GET', "babylon.d.txt", true);
  654. xhr.onreadystatechange = function () {
  655. if (xhr.readyState === 4) {
  656. if (xhr.status === 200) {
  657. require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' } });
  658. require(['vs/editor/editor.main'], function () {
  659. monaco.languages.typescript.javascriptDefaults.addExtraLib(xhr.responseText, 'babylon.d.ts');
  660. jsEditor = monaco.editor.create(document.getElementById('jsEditor'), {
  661. value: "",
  662. language: "javascript",
  663. lineNumbers: true,
  664. tabSize: "auto",
  665. insertSpaces: "auto",
  666. roundedSelection: true,
  667. scrollBeyondLastLine: false,
  668. automaticLayout: true,
  669. readOnly: false,
  670. theme: "vs",
  671. contextmenu: false
  672. });
  673. run();
  674. });
  675. }
  676. }
  677. };
  678. xhr.send(null);
  679. })();