WebOrbiton
v1.5.0

AtheraStudio Code

59 lines · 1.8 KB
  1. (function () {
  2. "use strict";
  3. ​
  4. var desktop = window.atheraDesktop;
  5. var A = window.Athera;
  6. if (!desktop || !desktop.isDesktop || !A) return;
  7. ​
  8. function baseName(p) {
  9. return String(p).split(/[\\/]/).pop();
  10. }
  11. ​
  12. function makeHandle(filePath) {
  13. var name = baseName(filePath);
  14. return {
  15. kind: "file",
  16. name: name,
  17. _path: filePath,
  18. getFile: async function () {
  19. var r = await desktop.readFile(filePath);
  20. return new File([r.data], name, { lastModified: r.mtime || Date.now() });
  21. },
  22. createWritable: async function () {
  23. var chunks = [];
  24. return {
  25. write: async function (d) { chunks.push(d); },
  26. close: async function () {
  27. var buf = new Uint8Array(await new Blob(chunks).arrayBuffer());
  28. await desktop.writeFile(filePath, buf);
  29. },
  30. abort: async function () { chunks = []; }
  31. };
  32. },
  33. isSameEntry: async function (other) { return !!other && other._path === filePath; },
  34. queryPermission: async function () { return "granted"; },
  35. requestPermission: async function () { return "granted"; }
  36. };
  37. }
  38. ​
  39. async function openFromSystem(filePath) {
  40. try {
  41. for (var t of A.tabs.list) {
  42. if (t.handle && t.handle._path === filePath) return A.tabs.activate(t);
  43. }
  44. var handle = makeHandle(filePath);
  45. var file = await handle.getFile();
  46. var tab = await A.tabs.openFile({ name: handle.name, handle: null, file: file });
  47. if (tab && tab.kind) A.tabs.retarget(tab, tab.name, undefined, handle);
  48. } catch (e) {
  49. console.error(e);
  50. A.ui.toast(String((e && e.message) || e), "danger");
  51. }
  52. }
  53. ​
  54. var chain = Promise.resolve();
  55. desktop.onOpenFile(function (filePath) {
  56. chain = chain.then(function () { return openFromSystem(filePath); });
  57. });
  58. })();
  59. ​