WebOrbiton
v1.5.0

AtheraStudio Code

366 lines · 18.8 KB
  1. (function () {
  2. "use strict";
  3. ​
  4. var A = (window.Athera = window.Athera || {});
  5. A.version = A.version || "";
  6. ​
  7. var handlers = {};
  8. A.on = function (name, fn) {
  9. (handlers[name] = handlers[name] || []).push(fn);
  10. };
  11. A.emit = function (name, data) {
  12. (handlers[name] || []).forEach(function (fn) {
  13. try { fn(data); } catch (e) { console.error(e); }
  14. });
  15. };
  16. ​
  17. var util = (A.util = {
  18. ext: function (name) {
  19. var m = /\.([^./]+)$/.exec(name || "");
  20. return m ? m[1].toLowerCase() : "";
  21. },
  22. basename: function (path) {
  23. return String(path).split("/").pop();
  24. },
  25. dirname: function (path) {
  26. var i = String(path).lastIndexOf("/");
  27. return i < 0 ? "" : path.slice(0, i);
  28. },
  29. join: function (dir, name) {
  30. return dir ? dir + "/" + name : name;
  31. },
  32. resolve: function (fromDir, ref) {
  33. if (!ref) return null;
  34. ref = ref.trim().split("#")[0].split("?")[0];
  35. if (!ref || /^([a-z][a-z0-9+.-]*:|\/\/)/i.test(ref)) return null;
  36. try { ref = decodeURI(ref); } catch (e) { }
  37. var parts = ref.charAt(0) === "/" ? [] : (fromDir ? fromDir.split("/") : []);
  38. ref.split("/").forEach(function (seg) {
  39. if (!seg || seg === ".") return;
  40. if (seg === "..") parts.pop();
  41. else parts.push(seg);
  42. });
  43. return parts.join("/");
  44. },
  45. debounce: function (fn, ms) {
  46. var t;
  47. return function () {
  48. var args = arguments, self = this;
  49. clearTimeout(t);
  50. t = setTimeout(function () { fn.apply(self, args); }, ms);
  51. };
  52. },
  53. esc: function (s) {
  54. return String(s).replace(/[&<>"']/g, function (c) {
  55. return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c];
  56. });
  57. },
  58. el: function (tag, attrs, children) {
  59. var node = document.createElement(tag);
  60. Object.keys(attrs || {}).forEach(function (k) {
  61. var v = attrs[k];
  62. if (v == null || v === false) return;
  63. if (k.slice(0, 2) === "on") node.addEventListener(k.slice(2), v);
  64. else if (k === "html") node.innerHTML = v;
  65. else node.setAttribute(k, v === true ? "" : v);
  66. });
  67. [].concat(children || []).forEach(function (c) {
  68. if (c == null) return;
  69. node.appendChild(typeof c === "string" ? document.createTextNode(c) : c);
  70. });
  71. return node;
  72. }
  73. });
  74. ​
  75. A.store = {
  76. get: function (key, def) {
  77. try {
  78. var v = localStorage.getItem("athera." + key);
  79. return v == null ? def : JSON.parse(v);
  80. } catch (e) {
  81. return def;
  82. }
  83. },
  84. set: function (key, value) {
  85. try { localStorage.setItem("athera." + key, JSON.stringify(value)); } catch (e) { }
  86. }
  87. };
  88. ​
  89. var dbPromise = null;
  90. function db() {
  91. if (!dbPromise) {
  92. dbPromise = new Promise(function (resolve, reject) {
  93. if (!window.indexedDB) return reject(new Error("IndexedDB"));
  94. var req = indexedDB.open("athera-studio", 1);
  95. req.onupgradeneeded = function () { req.result.createObjectStore("kv"); };
  96. req.onsuccess = function () { resolve(req.result); };
  97. req.onerror = function () { reject(req.error); };
  98. });
  99. }
  100. return dbPromise;
  101. }
  102. function kv(mode, fn) {
  103. return db().then(function (d) {
  104. return new Promise(function (resolve, reject) {
  105. var tx = d.transaction("kv", mode);
  106. var req = fn(tx.objectStore("kv"));
  107. tx.oncomplete = function () { resolve(req && req.result); };
  108. tx.onerror = function () { reject(tx.error); };
  109. });
  110. });
  111. }
  112. A.idb = {
  113. get: function (key) { return kv("readonly", function (s) { return s.get(key); }).catch(function () { return undefined; }); },
  114. set: function (key, value) { return kv("readwrite", function (s) { return s.put(value, key); }).catch(function () {}); },
  115. del: function (key) { return kv("readwrite", function (s) { return s.delete(key); }).catch(function () {}); }
  116. };
  117. ​
  118. var P = {
  119. folder: '<path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/>',
  120. folderOpen: '<path d="M3 7a2 2 0 0 1 2-2h4l2 2h7a2 2 0 0 1 2 2v1"/><path d="M3 19l2.5-8h16L19 19z"/>',
  121. file: '<path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z"/><path d="M14 3v5h5"/>',
  122. filePlus: '<path d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z"/><path d="M14 3v5h5M12 11v6M9 14h6"/>',
  123. folderPlus: '<path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><path d="M12 10v6M9 13h6"/>',
  124. save: '<path d="M5 3h11l5 5v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2z"/><path d="M7 3v5h8V3M7 21v-7h10v7"/>',
  125. refresh: '<path d="M20 11a8 8 0 0 0-14.6-4.5L3 9"/><path d="M3 4v5h5M4 13a8 8 0 0 0 14.6 4.5L21 15"/><path d="M21 20v-5h-5"/>',
  126. collapse: '<path d="M7 9l5-5 5 5M7 15l5 5 5-5"/>',
  127. trash: '<path d="M4 7h16M10 11v6M14 11v6M6 7l1 12a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2l1-12M9 7V4h6v3"/>',
  128. pencil: '<path d="M4 20h4L19 9l-4-4L4 16z"/><path d="M13.5 6.5l4 4"/>',
  129. copy: '<rect x="8" y="8" width="12" height="12" rx="2"/><path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2"/>',
  130. x: '<path d="M6 6l12 12M18 6L6 18"/>',
  131. check: '<path d="M5 12.5l4.5 4.5L19 7.5"/>',
  132. menu: '<path d="M4 6h16M4 12h16M4 18h16"/>',
  133. eye: '<path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z"/><circle cx="12" cy="12" r="3"/>',
  134. sun: '<circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4"/>',
  135. moon: '<path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z"/>',
  136. palette: '<path d="M12 3a9 9 0 1 0 0 18c1.1 0 2-.9 2-2 0-.5-.2-1-.5-1.3-.3-.4-.5-.8-.5-1.3 0-1.1.9-2 2-2h2.4A4.6 4.6 0 0 0 22 9.8C22 6 17.5 3 12 3z"/><circle cx="7.5" cy="11" r="1"/><circle cx="10" cy="7" r="1"/><circle cx="15" cy="7" r="1"/>',
  137. globe: '<circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3a14 14 0 0 1 0 18M12 3a14 14 0 0 0 0 18"/>',
  138. chevron: '<path d="M9 6l6 6-6 6"/>',
  139. device: '<rect x="3" y="4" width="18" height="12" rx="2"/><path d="M8 20h8M12 16v4"/>',
  140. external: '<path d="M14 4h6v6M20 4l-9 9"/><path d="M18 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1h5"/>',
  141. sidebar: '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M9 4v16"/>',
  142. split: '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M12 4v16"/>',
  143. wrap: '<path d="M4 6h16M4 12h13a3 3 0 0 1 0 6h-4M4 18h5"/><path d="M15 16l-2 2 2 2"/>',
  144. upload: '<path d="M12 16V4M7 9l5-5 5 5M4 20h16"/>',
  145. download: '<path d="M12 4v12M7 11l5 5 5-5M4 20h16"/>',
  146. warning: '<path d="M12 3l10 18H2z"/><path d="M12 10v5M12 18h.01"/>',
  147. settings: '<circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.7 1.7 0 0 0 .3 1.8l.1.1a2 2 0 1 1-2.8 2.8l-.1-.1a1.7 1.7 0 0 0-1.8-.3 1.7 1.7 0 0 0-1 1.5V21a2 2 0 1 1-4 0v-.1a1.7 1.7 0 0 0-1.1-1.5 1.7 1.7 0 0 0-1.8.3l-.1.1a2 2 0 1 1-2.8-2.8l.1-.1a1.7 1.7 0 0 0 .3-1.8 1.7 1.7 0 0 0-1.5-1H3a2 2 0 1 1 0-4h.1a1.7 1.7 0 0 0 1.5-1.1 1.7 1.7 0 0 0-.3-1.8l-.1-.1a2 2 0 1 1 2.8-2.8l.1.1a1.7 1.7 0 0 0 1.8.3H9a1.7 1.7 0 0 0 1-1.5V3a2 2 0 1 1 4 0v.1a1.7 1.7 0 0 0 1 1.5 1.7 1.7 0 0 0 1.8-.3l.1-.1a2 2 0 1 1 2.8 2.8l-.1.1a1.7 1.7 0 0 0-.3 1.8V9a1.7 1.7 0 0 0 1.5 1H21a2 2 0 1 1 0 4h-.1a1.7 1.7 0 0 0-1.5 1z"/>',
  148. type: '<path d="M4 7V5h16v2M9 19h6M12 5v14"/>',
  149. search: '<circle cx="11" cy="11" r="7"/><path d="M20 20l-3.5-3.5"/>',
  150. replace: '<path d="M4 8h13l-3-3M20 16H7l3 3"/>',
  151. undo: '<path d="M9 14L4 9l5-5"/><path d="M4 9h10a6 6 0 0 1 0 12h-3"/>',
  152. rss: '<path d="M5 11a8 8 0 0 1 8 8M5 4a15 15 0 0 1 15 15"/><circle cx="6" cy="18" r="1.3"/>',
  153. sitemap: '<rect x="9" y="3" width="6" height="5" rx="1"/><rect x="2" y="16" width="6" height="5" rx="1"/><rect x="9" y="16" width="6" height="5" rx="1"/><rect x="16" y="16" width="6" height="5" rx="1"/><path d="M12 8v4M5 16v-4h14v4"/>',
  154. redo: '<path d="M15 14l5-5-5-5"/><path d="M20 9H10a6 6 0 0 0 0 12h3"/>',
  155. scissors: '<circle cx="6" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M20 4L8.12 15.88M14.47 14.48L20 20M8.12 8.12L12 12"/>',
  156. clipboard: '<rect x="8" y="2" width="8" height="4" rx="1"/><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/>',
  157. saveAll: '<path d="M7 7V4h9l4 4v8h-3"/><path d="M3 9a2 2 0 0 1 2-2h8l4 4v7a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><path d="M6 20v-4h6v4M6 7v3h5"/>',
  158. saveAs: '<path d="M13 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h4"/><path d="M13 3l5 5v2M13 3v5h5"/><path d="M13 20.5l.5-2.5 5.5-5.5 2 2L16 20z"/>',
  159. folderX: '<path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><path d="M10 11l4 4M14 11l-4 4"/>',
  160. terminal: '<path d="M4 17l6-6-6-6"/><path d="M12 19h8"/>',
  161. gotoLine: '<path d="M4 6h16M4 12h7M4 18h7"/><path d="M15 14l3 3 3-3M18 17v-6"/>',
  162. folderSearch: '<path d="M11 20H5a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v2"/><circle cx="17" cy="16" r="3"/><path d="M21 20l-2.1-2.1"/>',
  163. selectText: '<path d="M5 3a2 2 0 0 0-2 2M19 3a2 2 0 0 1 2 2M21 19a2 2 0 0 1-2 2M5 21a2 2 0 0 1-2-2M9 3h1M9 21h1M14 3h1M14 21h1M3 9v1M21 9v1M3 14v1M21 14v1"/><path d="M7 8h8M7 12h10M7 16h6"/>',
  164. comment: '<path d="M10 4L6 20M18 4l-4 16"/>',
  165. duplicate: '<rect x="4" y="3" width="16" height="7" rx="1.5"/><rect x="4" y="14" width="16" height="7" rx="1.5"/>',
  166. code: '<path d="M16 18l6-6-6-6M8 6l-6 6 6 6"/>',
  167. zoomIn: '<circle cx="11" cy="11" r="7"/><path d="M20 20l-3.5-3.5M8 11h6M11 8v6"/>',
  168. zoomOut: '<circle cx="11" cy="11" r="7"/><path d="M20 20l-3.5-3.5M8 11h6"/>',
  169. zoomReset: '<circle cx="11" cy="11" r="7"/><path d="M20 20l-3.5-3.5M8.5 10h5M8.5 12.5h5"/>',
  170. keyboard: '<rect x="2" y="5" width="20" height="14" rx="2"/><path d="M6 9h.01M10 9h.01M14 9h.01M18 9h.01M6 13h.01M18 13h.01M9 16h6"/>',
  171. info: '<circle cx="12" cy="12" r="9"/><path d="M12 11v5M12 8h.01"/>',
  172. lightbulb: '<path d="M9 18h6M10 21h4"/><path d="M12 3a6 6 0 0 0-4 10.5c.8.8 1 1.5 1 2.5h6c0-1 .2-1.7 1-2.5A6 6 0 0 0 12 3z"/>',
  173. foldAll: '<path d="M7 4l5 5 5-5M7 20l5-5 5 5"/>',
  174. indent: '<path d="M4 6h16M12 12h8M4 18h16"/><path d="M4 9l3 3-3 3"/>',
  175. outdent: '<path d="M4 6h16M12 12h8M4 18h16"/><path d="M7 9l-3 3 3 3"/>',
  176. arrowLeft: '<path d="M19 12H5M11 6l-6 6 6 6"/>',
  177. arrowRight: '<path d="M5 12h14M13 6l6 6-6 6"/>',
  178. xCircle: '<circle cx="12" cy="12" r="9"/><path d="M9 9l6 6M15 9l-6 6"/>',
  179. closeAll: '<path d="M4 4l7 7M11 4l-7 7"/><path d="M13 13l7 7M20 13l-7 7"/>',
  180. wand: '<path d="M15 4V2M15 16v-2M8 9h2M20 9h2M17.8 11.8l1.4 1.4M17.8 6.2l1.4-1.4M12.2 6.2l-1.4-1.4"/><path d="M15 9l-12 12"/>',
  181. sparkles: '<path d="M11 3l1.9 5.1L18 10l-5.1 1.9L11 17l-1.9-5.1L4 10l5.1-1.9z"/><path d="M19 15l.8 2.2L22 18l-2.2.8L19 21l-.8-2.2L16 18l2.2-.8z"/>',
  182. key: '<circle cx="8" cy="15" r="4"/><path d="M10.8 12.2L20 3M16 7l3 3M14 9l2 2"/>',
  183. shield: '<path d="M12 3l8 3v6c0 4.5-3.4 8.3-8 9-4.6-.7-8-4.5-8-9V6z"/><path d="M9 12l2 2 4-4"/>',
  184. chat: '<path d="M4 5h16v11H9l-5 4z"/><path d="M8 9h8M8 12h5"/>',
  185. coins: '<ellipse cx="12" cy="6" rx="7" ry="3"/><path d="M5 6v6c0 1.7 3.1 3 7 3s7-1.3 7-3V6M5 12v6c0 1.7 3.1 3 7 3s7-1.3 7-3v-6"/>',
  186. ban: '<circle cx="12" cy="12" r="9"/><path d="M5.6 5.6l12.8 12.8"/>',
  187. lock: '<rect x="5" y="11" width="14" height="10" rx="2"/><path d="M8 11V7a4 4 0 0 1 8 0v4"/>',
  188. plus: '<path d="M12 5v14M5 12h14"/>',
  189. power: '<path d="M12 3v9"/><path d="M6.3 6.3a8 8 0 1 0 11.4 0"/>',
  190. target: '<circle cx="12" cy="12" r="8"/><circle cx="12" cy="12" r="3"/><path d="M12 1v4M12 19v4M1 12h4M19 12h4"/>',
  191. layout: '<rect x="2" y="5" width="11" height="14" rx="1.5"/><rect x="15" y="8" width="7" height="11" rx="1.5"/><path d="M5 16h5"/>'
  192. };
  193. A.icon = function (name, cls) {
  194. return '<svg class="ath-icon' + (cls ? " " + cls : "") + '" viewBox="0 0 24 24" fill="none" stroke="currentColor" ' +
  195. 'stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' + (P[name] || "") + "</svg>";
  196. };
  197. A.hydrateIcons = function (root) {
  198. (root || document).querySelectorAll("[data-icon]").forEach(function (n) {
  199. n.insertAdjacentHTML("afterbegin", A.icon(n.getAttribute("data-icon")));
  200. n.removeAttribute("data-icon");
  201. });
  202. };
  203. ​
  204. A.asset = function (url) {
  205. return A.version ? url + (url.indexOf("?") < 0 ? "?" : "&") + "v=" + encodeURIComponent(A.version) : url;
  206. };
  207. ​
  208. var ui = (A.ui = {});
  209. ​
  210. ui.toast = function (message, tone) {
  211. if (window.Orbitarium) Orbitarium.toast(message, { tone: tone });
  212. };
  213. ​
  214. ui.dialog = function (opts) {
  215. return new Promise(function (resolve) {
  216. var opener = document.activeElement;
  217. var overlay = util.el("div", { class: "orb-modal-overlay is-open ath-dialog", role: "dialog", "aria-modal": "true" });
  218. var modal = util.el("div", { class: "orb-modal" + (opts.size ? " orb-modal-" + opts.size : "") });
  219. var closeBtn = util.el("button", { class: "orb-modal-close", type: "button", "aria-label": A.t("common.close"), html: A.icon("x") });
  220. modal.appendChild(util.el("div", { class: "orb-modal-header" }, [
  221. util.el("div", { class: "orb-modal-title" }, opts.title || ""),
  222. closeBtn
  223. ]));
  224. var body = util.el("div", { class: "orb-modal-body" });
  225. if (typeof opts.body === "string") body.innerHTML = opts.body;
  226. else if (opts.body) body.appendChild(opts.body);
  227. modal.appendChild(body);
  228. ​
  229. var actions = util.el("div", { class: "orb-modal-actions" });
  230. (opts.actions || []).forEach(function (a) {
  231. actions.appendChild(util.el("button", {
  232. type: "button",
  233. class: "orb-btn orb-btn-sm orb-btn-" + (a.kind || "secondary"),
  234. onclick: function () { done(a.value); }
  235. }, a.label));
  236. });
  237. if (opts.actions && opts.actions.length) modal.appendChild(actions);
  238. overlay.appendChild(modal);
  239. ​
  240. var finished = false;
  241. function done(value) {
  242. if (finished) return;
  243. finished = true;
  244. document.removeEventListener("keydown", onKey, true);
  245. overlay.remove();
  246. if (opener && opener.focus && document.contains(opener)) opener.focus({ preventScroll: true });
  247. resolve(value === undefined ? null : value);
  248. }
  249. function onKey(e) {
  250. if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); done(null); }
  251. if (e.key === "Enter" && opts.enterValue !== undefined && e.target.tagName === "INPUT") {
  252. e.preventDefault();
  253. done(opts.enterValue);
  254. }
  255. }
  256. if (opts.ref) opts.ref.close = function () { done(null); };
  257. closeBtn.addEventListener("click", function () { done(null); });
  258. overlay.addEventListener("mousedown", function (e) { if (e.target === overlay) done(null); });
  259. document.addEventListener("keydown", onKey, true);
  260. document.body.appendChild(overlay);
  261. ​
  262. var focus = modal.querySelector("input, textarea, select") || actions.querySelector(".orb-btn-primary, .orb-btn-danger") || closeBtn;
  263. setTimeout(function () { focus.focus(); if (opts.onOpen) opts.onOpen(modal); }, 0);
  264. });
  265. };
  266. ​
  267. ui.prompt = function (title, label, value, okLabel) {
  268. var input = util.el("input", { class: "orb-input orb-mono", type: "text", spellcheck: "false", autocomplete: "off", value: value || "" });
  269. var body = util.el("div", {}, [util.el("label", { class: "orb-label" }, label), input]);
  270. return ui.dialog({
  271. title: title,
  272. body: body,
  273. size: "sm",
  274. actions: [
  275. { label: A.t("common.cancel"), value: null, kind: "ghost" },
  276. { label: okLabel || A.t("common.ok"), value: "ok", kind: "primary" }
  277. ],
  278. enterValue: "ok",
  279. onOpen: function () {
  280. var v = input.value, dot = v.lastIndexOf(".");
  281. input.setSelectionRange(0, dot > 0 ? dot : v.length);
  282. }
  283. }).then(function (r) {
  284. var v = input.value.trim();
  285. return r === "ok" && v ? v : null;
  286. });
  287. };
  288. ​
  289. ui.confirm = function (title, message, okLabel, danger) {
  290. return ui.dialog({
  291. title: title,
  292. body: util.el("p", { class: "ath-dialog-text" }, message),
  293. size: "sm",
  294. actions: [
  295. { label: A.t("common.cancel"), value: false, kind: "ghost" },
  296. { label: okLabel || A.t("common.ok"), value: true, kind: danger ? "danger" : "primary" }
  297. ]
  298. }).then(function (r) { return r === true; });
  299. };
  300. ​
  301. ui.contextMenu = function (x, y, items) {
  302. ui.closeContextMenu();
  303. var menu = util.el("div", { class: "orb-dropdown-menu is-open ath-context-menu", role: "menu" });
  304. items.forEach(function (it) {
  305. if (it === "-") return menu.appendChild(util.el("div", { class: "orb-dropdown-divider" }));
  306. menu.appendChild(util.el("button", {
  307. type: "button",
  308. role: "menuitem",
  309. class: "orb-dropdown-item" + (it.danger ? " is-danger" : ""),
  310. html: (it.icon ? A.icon(it.icon) : "") + "<span>" + util.esc(it.label) + "</span>" +
  311. (it.kbd ? '<span class="orb-kbd">' + util.esc(it.kbd) + "</span>" : ""),
  312. onclick: function () { ui.closeContextMenu(); it.action(); }
  313. }));
  314. });
  315. document.body.appendChild(menu);
  316. menu.style.maxHeight = Math.max(120, window.innerHeight - 16) + "px";
  317. menu.style.overflowY = "auto";
  318. var w = menu.offsetWidth, h = menu.offsetHeight;
  319. menu.style.left = Math.max(8, Math.min(x, window.innerWidth - w - 8)) + "px";
  320. menu.style.top = Math.max(8, Math.min(y, window.innerHeight - h - 8)) + "px";
  321. var first = menu.querySelector("button");
  322. if (first) first.focus();
  323. function outside(e) { if (!menu.contains(e.target)) ui.closeContextMenu(); }
  324. function keys(e) {
  325. if (e.key === "Escape") { e.stopPropagation(); ui.closeContextMenu(); }
  326. if (e.key === "ArrowDown" || e.key === "ArrowUp") {
  327. e.preventDefault();
  328. var list = [].slice.call(menu.querySelectorAll("button"));
  329. var i = list.indexOf(document.activeElement);
  330. list[(i + (e.key === "ArrowDown" ? 1 : -1) + list.length) % list.length].focus();
  331. }
  332. }
  333. setTimeout(function () {
  334. document.addEventListener("mousedown", outside, true);
  335. document.addEventListener("keydown", keys, true);
  336. window.addEventListener("blur", ui.closeContextMenu);
  337. }, 0);
  338. ui._menuCleanup = function () {
  339. document.removeEventListener("mousedown", outside, true);
  340. document.removeEventListener("keydown", keys, true);
  341. window.removeEventListener("blur", ui.closeContextMenu);
  342. menu.remove();
  343. };
  344. };
  345. ui.copyText = function (text) {
  346. function legacy() {
  347. var ta = util.el("textarea", { style: "position:fixed;top:0;left:0;opacity:0" });
  348. ta.value = text;
  349. document.body.appendChild(ta);
  350. ta.select();
  351. var ok = false;
  352. try { ok = document.execCommand("copy"); } catch (e) { }
  353. ta.remove();
  354. if (!ok) throw new Error("clipboard");
  355. }
  356. if (navigator.clipboard && navigator.clipboard.writeText) {
  357. return navigator.clipboard.writeText(text).catch(legacy);
  358. }
  359. return Promise.resolve().then(legacy);
  360. };
  361. ​
  362. ui.closeContextMenu = function () {
  363. if (ui._menuCleanup) { var c = ui._menuCleanup; ui._menuCleanup = null; c(); }
  364. };
  365. })();
  366. ​