index.js 31 KB

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