index.js 31 KB

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