Home Reference Source

cables_dev/cables_ui/src/ui/components/filemanager_edit.js

  1. import { utils } from "cables";
  2. import { TalkerAPI } from "cables-shared-client";
  3. import { editorSession } from "../elements/tabpanel/editor_session.js";
  4. import { gui } from "../gui.js";
  5. import { platform } from "../platform.js";
  6. import { createEditor } from "./editor.js";
  7. /** Editor for text files, opened from the file manager. */
  8. export default class FileManagerEditor
  9. {
  10. constructor()
  11. {
  12. editorSession.addListener("editAssetFile",
  13. (name, data) =>
  14. {
  15. this.editAssetTextFile(data.filename, data.syntax, data.patchId);
  16. }
  17. );
  18. }
  19. editAssetTextFile(filename, syntax, patchId)
  20. {
  21. patchId = patchId || gui.project()._id;
  22. let url = filename;
  23. if (!filename.startsWith("file:"))
  24. {
  25. url = platform.getSandboxUrl() + "/assets/" + patchId + "/" + filename;
  26. }
  27. if (!syntax) syntax = "text";
  28. if (syntax == "javascript")syntax = "js";
  29. if (syntax == "shader")syntax = "glsl";
  30. utils.ajax(
  31. url,
  32. (err2, _data, xhr2) =>
  33. {
  34. const name = filename;
  35. let editorObj = editorSession.rememberOpenEditor("editAssetFile", name, { "filename": filename, "patchId": patchId, "syntax": syntax }, true);
  36. // new EditorTab(
  37. createEditor(
  38. {
  39. "title": name,
  40. "content": _data,
  41. "editorObj": editorObj,
  42. "syntax": syntax.toLowerCase(),
  43. "singleton": true,
  44. "onClose": (which) =>
  45. {
  46. if (editorSession)
  47. {
  48. if (which && which.editorObj) editorObj = which.editorObj;
  49. editorSession.remove(editorObj.type, editorObj.name);
  50. }
  51. },
  52. "onSave": function (setStatus, content)
  53. {
  54. gui.jobs().start({ "id": "saveeditorcontent" + filename, "title": "saving file " + filename });
  55. platform.talkerAPI.send(
  56. TalkerAPI.CMD_UPDATE_FILE,
  57. {
  58. "fileName": filename,
  59. "content": content,
  60. },
  61. (err3, res3) =>
  62. {
  63. gui.savedState.setSaved("editorOnChangeFile");
  64. gui.jobs().finish("saveeditorcontent" + filename);
  65. setStatus("Saved");
  66. }
  67. );
  68. },
  69. "onChange": function (ev)
  70. {
  71. gui.savedState.setUnSaved("editorOnChangeFile");
  72. },
  73. "onFinished": () =>
  74. {
  75. // gui.mainTabs.activateTabByName(name);
  76. }
  77. });
  78. });
  79. }
  80. }