index.js 32 KB

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