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