index.js 31 KB

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