WebOrbiton
v1.4.2

AtheraStudio Code

157 lines · 5.5 KB
  1. (function () {
  2. "use strict";
  3. ​
  4. var A = window.Athera;
  5. var util = A.util;
  6. var fs = A.fs;
  7. ​
  8. var MIME_EXT = {
  9. "image/png": "png", "image/jpeg": "jpg", "image/gif": "gif", "image/webp": "webp",
  10. "image/avif": "avif", "image/bmp": "bmp", "image/svg+xml": "svg", "image/x-icon": "ico", "image/vnd.microsoft.icon": "ico"
  11. };
  12. ​
  13. function isImageFile(f) {
  14. return (!!f.type && f.type.indexOf("image/") === 0) || fs.isImage(f.name || "");
  15. }
  16. ​
  17. function imageFiles(list) {
  18. return Array.from(list || []).filter(isImageFile);
  19. }
  20. ​
  21. function activeText() {
  22. var tab = A.tabs && A.tabs.active;
  23. return tab && tab.kind === "text" ? tab : null;
  24. }
  25. ​
  26. function canStore(tab) {
  27. return !!tab && tab.path != null && !!fs.provider && fs.provider.kind === "disk";
  28. }
  29. ​
  30. function stamp() {
  31. var d = new Date();
  32. function p(n) { return n < 10 ? "0" + n : String(n); }
  33. return d.getFullYear() + p(d.getMonth() + 1) + p(d.getDate()) + "-" + p(d.getHours()) + p(d.getMinutes()) + p(d.getSeconds());
  34. }
  35. ​
  36. function fileName(file, index) {
  37. var ext = util.ext(file.name || "") || MIME_EXT[file.type] || "png";
  38. var base = (file.name || "").replace(/\.[^.]*$/, "");
  39. if (!base || /^image$/i.test(base)) base = "image-" + stamp() + (index ? "-" + (index + 1) : "");
  40. base = base.replace(/[\\/:*?"<>|]/g, "-").replace(/\s+/g, "-");
  41. return base + "." + ext;
  42. }
  43. ​
  44. function relPath(fromDir, toPath) {
  45. var a = fromDir ? fromDir.split("/") : [], b = toPath.split("/"), i = 0;
  46. while (i < a.length && i < b.length - 1 && a[i] === b[i]) i++;
  47. var up = [];
  48. for (var k = i; k < a.length; k++) up.push("..");
  49. return up.concat(b.slice(i)).join("/");
  50. }
  51. ​
  52. function encode(rel) {
  53. return rel.split("/").map(function (s) { return s === ".." ? s : encodeURIComponent(s); }).join("/");
  54. }
  55. ​
  56. function snippet(tab, rel) {
  57. var id = A.editor.langId(tab.name);
  58. var stem = util.basename(rel).replace(/\.[^.]*$/, "");
  59. if (id === "html" || id === "php") return '<img src="' + encode(rel) + '" alt="">';
  60. if (id === "svg") return '<image href="' + encode(rel) + '" width="100" height="100"/>';
  61. if (id === "css") return 'url("' + encode(rel) + '")';
  62. if (id === "md") return "![" + stem + "](" + encode(rel) + ")";
  63. return '"' + rel + '"';
  64. }
  65. ​
  66. function joiner(tab) {
  67. return /^(html|php|svg|md)$/.test(A.editor.langId(tab.name)) ? "\n" : " ";
  68. }
  69. ​
  70. function insert(view, tab, rels, pos) {
  71. if (A.tabs.active !== tab) return;
  72. var text = rels.map(function (r) { return snippet(tab, r); }).join(joiner(tab));
  73. var len = view.state.doc.length;
  74. var range = pos == null ? view.state.selection.main : { from: Math.min(pos, len), to: Math.min(pos, len) };
  75. view.dispatch({
  76. changes: { from: range.from, to: range.to, insert: text },
  77. selection: { anchor: range.from + text.length },
  78. userEvent: "input.paste",
  79. scrollIntoView: true
  80. });
  81. view.focus();
  82. }
  83. ​
  84. async function store(view, tab, files, pos) {
  85. if (!canStore(tab)) return A.ui.toast(A.t("editor.image_no_folder"), "warning");
  86. var dir = util.join(util.dirname(tab.path), "images");
  87. var rels = [], last = null;
  88. try {
  89. for (var i = 0; i < files.length; i++) {
  90. var name = await A.explorer.uniqueName(dir, fileName(files[i], files.length > 1 ? i : 0));
  91. var path = util.join(dir, name);
  92. await fs.provider.write(path, files[i]);
  93. rels.push(relPath(util.dirname(tab.path), path));
  94. last = path;
  95. }
  96. } catch (e) {
  97. A.ui.toast(A.t("editor.image_failed", { error: e.message || String(e) }), "danger");
  98. }
  99. if (!rels.length) return;
  100. insert(view, tab, rels, pos);
  101. A.ui.toast(A.tn("editor.images_added", rels.length, { dir: dir }), "success");
  102. await A.explorer.refresh(null);
  103. if (last) A.explorer.reveal(last);
  104. }
  105. ​
  106. function fromTree(view, tab, items, pos) {
  107. var dir = tab.path != null ? util.dirname(tab.path) : "";
  108. insert(view, tab, items.map(function (it) { return relPath(dir, it.path); }), pos);
  109. }
  110. ​
  111. function treeImages() {
  112. var items = A.explorer && A.explorer.dragging ? A.explorer.dragging() : null;
  113. if (!items || !items.length) return null;
  114. return items.every(function (it) { return it.kind === "file" && fs.isImage(it.path); }) ? items : null;
  115. }
  116. ​
  117. A.images = {
  118. accepts: function (e) {
  119. var dt = e.dataTransfer;
  120. if (!dt || !activeText() || !canStore(activeText())) return false;
  121. var items = Array.from(dt.items || []).filter(function (it) { return it.kind === "file"; });
  122. return items.length > 0 && items.every(function (it) { return it.type.indexOf("image/") === 0; });
  123. },
  124. ​
  125. onDrop: function (e, view) {
  126. var tab = activeText();
  127. if (!tab || !e.dataTransfer) return false;
  128. var pos = view.posAtCoords({ x: e.clientX, y: e.clientY });
  129. var items = treeImages();
  130. if (items) {
  131. e.preventDefault();
  132. fromTree(view, tab, items, pos);
  133. return true;
  134. }
  135. var all = Array.from(e.dataTransfer.files || []);
  136. var files = imageFiles(all);
  137. if (!files.length || files.length !== all.length) return false;
  138. e.preventDefault();
  139. e.athHandled = true;
  140. store(view, tab, files, pos);
  141. return true;
  142. },
  143. ​
  144. onPaste: function (e, view) {
  145. var tab = activeText();
  146. var dt = e.clipboardData;
  147. if (!tab || !dt) return false;
  148. if (dt.getData("text/plain")) return false;
  149. var files = imageFiles(dt.files);
  150. if (!files.length) return false;
  151. e.preventDefault();
  152. store(view, tab, files, null);
  153. return true;
  154. }
  155. };
  156. })();
  157. ​