v1.5.0
AtheraStudio Code
- (function () {
- "use strict";
-
- var A = window.Athera;
- var util = A.util;
- var t = function (k, p) { return A.t(k, p); };
-
- var LINE_MAX = 5000;
- var INDEX = ["index.html", "index.php", "index.htm"];
-
- var PATTERNS = [
- { re: /\b(?:href|src|action|formaction|poster|data-src|data-href|xlink:href)\s*=\s*(["'])([^"'\n]*)\1/gi, group: 2 },
- { re: /\bsrcset\s*=\s*(["'])([^"'\n]*)\1/gi, group: 2, srcset: true },
- { re: /\burl\(\s*(["']?)([^"')\s]+)\1\s*\)/gi, group: 2 },
- { re: /@import\s+(["'])([^"'\n]+)\1/gi, group: 2 },
- { re: /\b(?:include|require)(?:_once)?\s*\(?\s*(__DIR__\s*\.\s*)?(["'])([^"'\n]+)\2/gi, group: 3, php: true },
- { re: /\bfrom\s*(["'])(\.{0,2}\/[^"'\n]+)\1/g, group: 2 },
- { re: /\bimport\s*\(?\s*(["'])(\.{0,2}\/[^"'\n]+)\1/g, group: 2 },
- { re: /\]\(([^()\s]+)\)/g, group: 1 },
- { re: /\b(?:https?:\/\/|mailto:)[^\s"'<>()\[\]{}`\\]+/gi, group: 0, bare: true }
- ];
-
- function find(state, pos) {
- var line = state.doc.lineAt(pos);
- if (line.length > LINE_MAX) return null;
- var text = line.text, off = pos - line.from;
- for (var i = 0; i < PATTERNS.length; i++) {
- var p = PATTERNS[i];
- p.re.lastIndex = 0;
- var m;
- while ((m = p.re.exec(text))) {
- if (!m[0]) { p.re.lastIndex++; continue; }
- var value = m[p.group];
- if (value == null) continue;
- if (p.bare) value = value.replace(/[.,;:!?]+$/, "");
- var start = p.group === 0 ? m.index : m.index + m[0].lastIndexOf(value);
- var end = start + value.length;
- if (off < start || off > end || !value) continue;
- if (p.srcset) {
- var seg = segmentAt(value, off - start);
- if (!seg) continue;
- start += seg.start;
- end = start + seg.text.length;
- value = seg.text;
- }
- return { from: line.from + start, to: line.from + end, value: value, phpDir: !!(p.php && m[1]) };
- }
- }
- return null;
- }
-
- function segmentAt(value, at) {
- var re = /[^\s,][^\s,]*/g, m;
- while ((m = re.exec(value))) {
- if (at >= m.index && at <= m.index + m[0].length && !/^\d+(\.\d+)?[wx]$/.test(m[0])) return { start: m.index, text: m[0] };
- }
- return null;
- }
-
- function classify(link) {
- var v = String(link.value || "").trim();
- if (!v || /[<>{}$`]|\?>|\{\{/.test(v) || /^(javascript|data|blob|about|vbscript):/i.test(v)) return null;
- if (/^\/\//.test(v)) return { type: "url", url: "https:" + v };
- if (/^https?:\/\//i.test(v)) return { type: "url", url: v };
- if (/^(mailto|tel):/i.test(v)) return { type: "url", url: v };
- if (/^[a-z][a-z0-9+.\-]*:/i.test(v)) return null;
- if (v.charAt(0) === "#") {
- var id = v.slice(1);
- try { id = decodeURIComponent(id); } catch (e) { }
- return id ? { type: "anchor", id: id } : null;
- }
- var hash = "";
- var h = v.indexOf("#");
- if (h >= 0) {
- hash = v.slice(h + 1);
- try { hash = decodeURIComponent(hash); } catch (e) { }
- }
- if (link.phpDir) v = v.replace(/^\/+/, "");
- return { type: "file", ref: v, hash: hash };
- }
-
- function at(state, pos) {
- var link = find(state, pos);
- if (!link) return null;
- var target = classify(link);
- return target ? Object.assign(link, target) : null;
- }
-
- function hostLabel(url) {
- if (/^mailto:/i.test(url)) return url.slice(7).split("?")[0];
- if (/^tel:/i.test(url)) return url.slice(4);
- try { return new URL(url).hostname; } catch (e) { return url; }
- }
-
- async function openUrl(url) {
- var body = util.el("div", { class: "ath-link-confirm" }, [
- util.el("div", { class: "ath-link-host" }, hostLabel(url)),
- util.el("div", { class: "ath-link-full" }, url)
- ]);
- var yes = await A.ui.dialog({
- title: t("links.url_title"),
- size: "sm",
- body: body,
- actions: [
- { label: t("links.no"), value: false, kind: "secondary" },
- { label: t("links.yes"), value: true, kind: "primary" }
- ]
- });
- if (yes !== true) return;
- if (window.atheraDesktop && typeof window.atheraDesktop.openExternal === "function") return window.atheraDesktop.openExternal(url);
- window.open(url, "_blank", "noopener,noreferrer");
- }
-
- function jumpToId(id) {
- var view = A.editor.view;
- var text = view.state.doc.toString();
- var esc = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
- var m = new RegExp("\\b(?:id|name)\\s*=\\s*([\"']?)" + esc + "\\1(?=[\\s>/])", "i").exec(text);
- if (!m) {
- A.ui.toast(t("links.anchor_missing", { id: id }), "warning");
- return false;
- }
- var pos = m.index + m[0].lastIndexOf(id);
- view.dispatch({ selection: { anchor: pos, head: pos + id.length }, scrollIntoView: true });
- A.editor.focus();
- return true;
- }
-
- async function kindOf(path) {
- var p = A.fs.provider;
- try { await p.readFile(path); return "file"; } catch (e) { }
- try { return (await p.exists(path)) ? "dir" : null; } catch (e) { return null; }
- }
-
- async function missingDir(path) {
- var dir = util.dirname(path), top = "";
- while (dir && !(await A.fs.provider.exists(dir))) {
- top = dir;
- dir = util.dirname(dir);
- }
- return top;
- }
-
- async function openPath(path, hash) {
- var tab = await A.tabs.openPath(path);
- if (tab && hash && tab.kind === "text") setTimeout(function () { jumpToId(hash); }, 30);
- return tab;
- }
-
- async function createAndOpen(path, hash) {
- if (A.fs.isBinary(path)) {
- A.ui.toast(t("links.missing", { path: path }), "warning");
- return;
- }
- var dir = await missingDir(path);
- var body = util.el("div", {}, [
- util.el("p", { class: "ath-dialog-text" }, t("links.create_text", { path: path })),
- dir ? util.el("p", { class: "ath-dialog-text ath-link-dirs" }, t("links.create_dirs", { dir: dir })) : null
- ]);
- var go = await A.ui.dialog({
- title: t("links.create_title"),
- size: "sm",
- body: body,
- actions: [
- { label: A.t("common.cancel"), value: false, kind: "secondary" },
- { label: t("links.create"), value: true, kind: "primary" }
- ]
- });
- if (go !== true) return;
- try {
- await A.fs.provider.createFile(path, A.templates ? A.templates.forFile(path) : "");
- } catch (e) {
- A.ui.toast(e.message, "danger");
- return;
- }
- if (A.explorer) {
- await A.explorer.refresh();
- A.explorer.reveal(path).catch(function () { });
- }
- A.ui.toast(t("links.created", { path: path }), "success");
- await openPath(path, hash);
- }
-
- async function openFile(target, tab) {
- if (!A.fs.provider || !tab || tab.path == null) {
- A.ui.toast(t("links.no_folder"), "info");
- return;
- }
- var base = util.dirname(tab.path);
- var path = target.ref === "/" ? "" : util.resolve(base, target.ref);
- if (path == null) return;
- var kind = path ? await kindOf(path) : "dir";
- if (kind === "file") return openPath(path, target.hash);
- if (!kind && /\/$/.test(target.ref.split(/[?#]/)[0])) kind = "dir";
- var candidates = [];
- if (kind === "dir") candidates = INDEX.map(function (n) { return util.join(path, n); });
- else if (!util.ext(path)) candidates = [path + ".html", path + ".php", util.join(path, "index.html"), util.join(path, "index.php")];
- for (var i = 0; i < candidates.length; i++) {
- if ((await kindOf(candidates[i])) === "file") return openPath(candidates[i], target.hash);
- }
- var create = path;
- if (kind === "dir") create = util.join(path, /\.php$/i.test(tab.path) ? "index.php" : "index.html");
- else if (!util.ext(path)) create = path + (/\.php$/i.test(tab.path) ? ".php" : ".html");
- return createAndOpen(create, target.hash);
- }
-
- async function follow(view, pos) {
- var link = at(view.state, pos);
- if (!link) return false;
- var tab = A.tabs.active;
- if (link.type === "url") await openUrl(link.url);
- else if (link.type === "anchor") jumpToId(link.id);
- else await openFile(link, tab);
- return true;
- }
-
- function run(promise) {
- Promise.resolve(promise).catch(function (e) {
- console.error(e);
- A.ui.toast(e && e.message ? e.message : String(e), "danger");
- });
- }
-
- var setHover = CM.StateEffect.define();
-
- var hoverField = CM.StateField.define({
- create: function () { return CM.Decoration.none; },
- update: function (deco, tr) {
- deco = deco.map(tr.changes);
- tr.effects.forEach(function (e) {
- if (!e.is(setHover)) return;
- deco = e.value
- ? CM.Decoration.set([CM.Decoration.mark({ class: "ath-link-hover", attributes: { title: e.value.title } }).range(e.value.from, e.value.to)])
- : CM.Decoration.none;
- });
- return deco;
- },
- provide: function (f) { return CM.EditorView.decorations.from(f); }
- });
-
- function isMod(e) {
- return (e.ctrlKey || e.metaKey) && !e.altKey && !e.shiftKey;
- }
-
- var hoverPlugin = CM.ViewPlugin.fromClass(class {
- constructor(view) {
- var self = this;
- this.view = view;
- this.last = null;
- this.inside = false;
- this.x = 0;
- this.y = 0;
- this.mod = false;
- this.onMove = function (e) {
- self.inside = true;
- self.x = e.clientX;
- self.y = e.clientY;
- self.mod = isMod(e);
- self.check();
- };
- this.onLeave = function () {
- self.inside = false;
- self.mod = false;
- self.set(null);
- };
- this.onKey = function (e) {
- var m = isMod(e);
- if (m === self.mod) return;
- self.mod = m;
- if (self.inside) self.check();
- };
- view.dom.addEventListener("mousemove", this.onMove);
- view.dom.addEventListener("mouseleave", this.onLeave);
- window.addEventListener("keydown", this.onKey, true);
- window.addEventListener("keyup", this.onKey, true);
- window.addEventListener("blur", this.onLeave);
- }
- update(u) {
- if (u.docChanged && this.last) this.set(null);
- }
- check() {
- if (!this.mod) return this.set(null);
- var pos = this.view.posAtCoords({ x: this.x, y: this.y });
- var link = pos == null ? null : at(this.view.state, pos);
- this.set(link ? { from: link.from, to: link.to, title: t(link.type === "url" ? "links.hover_url" : "links.hover_file") } : null);
- }
- set(r) {
- var a = this.last;
- if ((!r && !a) || (r && a && r.from === a.from && r.to === a.to)) return;
- this.last = r;
- var view = this.view;
- Promise.resolve().then(function () { view.dispatch({ effects: setHover.of(r) }); });
- }
- destroy() {
- this.view.dom.removeEventListener("mousemove", this.onMove);
- this.view.dom.removeEventListener("mouseleave", this.onLeave);
- window.removeEventListener("keydown", this.onKey, true);
- window.removeEventListener("keyup", this.onKey, true);
- window.removeEventListener("blur", this.onLeave);
- }
- });
-
- var clickHandler = CM.Prec.highest(CM.EditorView.domEventHandlers({
- mousedown: function (e, view) {
- if (e.button !== 0 || !isMod(e)) return false;
- var pos = view.posAtCoords({ x: e.clientX, y: e.clientY });
- if (pos == null || !at(view.state, pos)) return false;
- e.preventDefault();
- run(follow(view, pos));
- return true;
- }
- }));
-
- A.links = {
- at: at,
- follow: follow,
- run: run,
- extension: function () {
- return [hoverField, hoverPlugin, clickHandler];
- },
- atCursor: function () {
- var view = A.editor.view;
- return view ? at(view.state, view.state.selection.main.head) : null;
- },
- followCursor: function () {
- var view = A.editor.view;
- if (!view) return;
- var pos = view.state.selection.main.head;
- if (!at(view.state, pos)) return A.ui.toast(t("links.none"), "info");
- run(follow(view, pos));
- }
- };
- })();
-