interop.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // This is the interop file, exposing functions that can be called by the host game engine
  2. import editor from "./editor/editor";
  3. import * as editorConfig from "./editor/editorConfig";
  4. export function saveCode() {
  5. const data = {
  6. message: "saveCode",
  7. payload: editor.session.getValue()
  8. };
  9. window.atomicQuery({
  10. request: JSON.stringify(data),
  11. persistent: false,
  12. onSuccess: function(response) {/**/ },
  13. onFailure: function(error_code, error_message) {
  14. console.log("Error getting code");
  15. }
  16. });
  17. }
  18. export function codeLoaded(value: string, fileExt: string) {
  19. editor.session.setValue(value);
  20. editor.gotoLine(0);
  21. editor.getSession().on("change", function(e) {
  22. window.atomicQuery({
  23. request: "change",
  24. persistent: false,
  25. onSuccess: (response) => {/**/ },
  26. onFailure: (error_code, error_message) => {
  27. console.log("Error on change");
  28. }
  29. });
  30. });
  31. }
  32. export function loadCode(codeUrl: string) {
  33. const fileExt = codeUrl.split(".").pop();
  34. // go ahead and set the theme prior to pulling the file across
  35. editorConfig.configure(fileExt);
  36. const p = new Promise(function(resolve, reject) {
  37. const xmlHttp = new XMLHttpRequest();
  38. xmlHttp.onreadystatechange = () => {
  39. if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
  40. resolve(xmlHttp.responseText);
  41. }
  42. };
  43. xmlHttp.open("GET", codeUrl, true); // true for asynchronous
  44. xmlHttp.send(null);
  45. }).then(function(src: string) {
  46. codeLoaded(src, fileExt);
  47. });
  48. }
  49. // Set up the window object so the host can call into it
  50. window.loadCode = loadCode;
  51. window.saveCode = saveCode;