v1.5.0
AtheraStudio Code
- (function () {
- "use strict";
-
- var A = window.Athera;
- var util = A.util;
- var t = CM.tags;
-
- var LANGS = {
- html: { label: "HTML", make: function () { return CM.html({ autoCloseTags: true }); } },
- php: { label: "PHP", make: function () { return CM.php(); } },
- css: { label: "CSS", make: function () { return CM.css(); } },
- js: { label: "JavaScript", make: function () { return CM.javascript(); } },
- jsx: { label: "JavaScript JSX", make: function () { return CM.javascript({ jsx: true }); } },
- ts: { label: "TypeScript", make: function () { return CM.javascript({ typescript: true }); } },
- tsx: { label: "TypeScript JSX", make: function () { return CM.javascript({ typescript: true, jsx: true }); } },
- json: { label: "JSON", make: function () { return [CM.json(), CM.linter(CM.jsonParseLinter())]; } },
- jsonc: { label: "JSON with Comments", make: function () { return CM.javascript(); } },
- md: { label: "Markdown", make: function () { return CM.markdown(); } },
- svg: { label: "SVG", make: function () { return CM.html(); } },
- text: { label: null, make: function () { return []; } }
- };
- var EXT_LANG = {
- html: "html", htm: "html", xhtml: "html", svg: "svg", xml: "html",
- php: "php", phtml: "php",
- css: "css", scss: "css", less: "css",
- js: "js", mjs: "js", cjs: "js", jsx: "jsx", ts: "ts", mts: "ts", tsx: "tsx",
- json: "json", jsonc: "jsonc", webmanifest: "json",
- md: "md", markdown: "md"
- };
-
- function langId(name) {
- return EXT_LANG[util.ext(name)] || "text";
- }
-
- var highlight = CM.HighlightStyle.define([
- { tag: [t.keyword, t.controlKeyword, t.operatorKeyword, t.definitionKeyword, t.moduleKeyword, t.modifier, t.self], color: "var(--ath-syn-keyword)" },
- { tag: [t.string, t.special(t.string), t.attributeValue, t.character], color: "var(--ath-syn-string)" },
- { tag: [t.number, t.bool, t.null, t.atom, t.unit], color: "var(--ath-syn-number)" },
- { tag: [t.comment, t.lineComment, t.blockComment, t.docComment], color: "var(--ath-syn-comment)", fontStyle: "italic" },
- { tag: [t.tagName, t.standard(t.tagName)], color: "var(--ath-syn-tag)" },
- { tag: t.attributeName, color: "var(--ath-syn-attr)" },
- { tag: [t.propertyName, t.definition(t.propertyName)], color: "var(--ath-syn-property)" },
- { tag: [t.function(t.variableName), t.function(t.propertyName), t.macroName], color: "var(--ath-syn-function)" },
- { tag: [t.typeName, t.className, t.namespace], color: "var(--ath-syn-type)" },
- { tag: [t.definition(t.variableName), t.special(t.variableName)], color: "var(--ath-syn-variable)" },
- { tag: [t.operator, t.punctuation, t.separator, t.bracket, t.angleBracket, t.derefOperator], color: "var(--ath-syn-punct)" },
- { tag: [t.meta, t.processingInstruction, t.documentMeta], color: "var(--ath-syn-meta)" },
- { tag: t.regexp, color: "var(--ath-syn-regexp)" },
- { tag: [t.color, t.constant(t.name), t.labelName], color: "var(--ath-syn-number)" },
- { tag: t.heading, fontWeight: "700", color: "var(--ath-syn-keyword)" },
- { tag: t.strong, fontWeight: "700" },
- { tag: t.emphasis, fontStyle: "italic" },
- { tag: t.link, color: "var(--ath-syn-function)", textDecoration: "underline" },
- { tag: t.url, color: "var(--ath-syn-string)" },
- { tag: t.invalid, color: "var(--orb-danger)" }
- ]);
-
- var theme = CM.EditorView.theme({
- "&": { height: "100%", backgroundColor: "var(--ath-editor-bg)", color: "var(--orb-text)", fontSize: "var(--ath-font-size)" },
- "&.cm-focused": { outline: "none" },
- ".cm-scroller": { fontFamily: "var(--ath-font-code)", lineHeight: "var(--ath-line-height, 1.65)" },
- ".cm-content": { caretColor: "var(--orb-primary)", padding: "10px 0 40vh" },
- ".cm-cursor, .cm-dropCursor": { borderLeftColor: "var(--orb-primary)", borderLeftWidth: "2px" },
- "&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection":
- { backgroundColor: "var(--ath-selection) !important" },
- ".cm-activeLine": { backgroundColor: "var(--ath-active-line)" },
- ".cm-gutters": { backgroundColor: "var(--ath-editor-bg)", color: "var(--orb-text-subtle)", border: "none", paddingLeft: "6px" },
- ".cm-activeLineGutter": { backgroundColor: "transparent", color: "var(--orb-text)" },
- ".cm-lineNumbers .cm-gutterElement": { padding: "0 10px 0 6px", minWidth: "36px" },
- ".cm-foldGutter .cm-gutterElement": { color: "var(--orb-text-subtle)", cursor: "pointer", padding: "0 4px" },
- ".cm-foldPlaceholder": { backgroundColor: "var(--orb-primary-dim)", border: "1px solid var(--orb-primary-border)", color: "var(--orb-primary-text)", borderRadius: "4px", padding: "0 6px" },
- ".cm-matchingBracket, &.cm-focused .cm-matchingBracket": { backgroundColor: "var(--orb-primary-dim-strong)", outline: "1px solid var(--orb-primary-border)", color: "inherit" },
- ".cm-nonmatchingBracket": { color: "var(--orb-danger)" },
- ".cm-selectionMatch": { backgroundColor: "var(--ath-selection-match)" },
- ".cm-searchMatch": { backgroundColor: "color-mix(in srgb, var(--orb-warning) 30%, transparent)", outline: "1px solid color-mix(in srgb, var(--orb-warning) 60%, transparent)" },
- ".cm-searchMatch.cm-searchMatch-selected": { backgroundColor: "color-mix(in srgb, var(--orb-warning) 55%, transparent)" },
- ".cm-tooltip": { backgroundColor: "var(--orb-surface)", color: "var(--orb-text)", border: "1px solid var(--orb-border)", borderRadius: "10px", boxShadow: "var(--orb-shadow-lg)", overflow: "hidden" },
- ".cm-tooltip-autocomplete > ul": { fontFamily: "var(--ath-font-code)", fontSize: "13px", maxHeight: "18em" },
- ".cm-tooltip-autocomplete > ul > li": { padding: "3px 10px 3px 6px", lineHeight: "1.5" },
- ".cm-tooltip-autocomplete > ul > li[aria-selected]": { backgroundColor: "var(--orb-primary-dim-strong)", color: "var(--orb-text)" },
- ".cm-completionIcon": { opacity: "0.75", width: "1.2em", paddingRight: "8px" },
- ".cm-completionDetail": { color: "var(--orb-text-muted)", fontStyle: "normal", marginLeft: "10px" },
- ".cm-completionMatchedText": { color: "var(--orb-primary-text)", textDecoration: "none", fontWeight: "700" },
- ".cm-completionInfo": { padding: "8px 12px", maxWidth: "380px", fontFamily: "var(--orb-font-sans)", fontSize: "13px" },
- ".cm-panels": { backgroundColor: "var(--orb-surface-alt)", color: "var(--orb-text)" },
- ".cm-panels.cm-panels-bottom": { borderTop: "1px solid var(--orb-border)" },
- ".cm-panels.cm-panels-top": { borderBottom: "1px solid var(--orb-border)" },
- ".cm-search": { fontFamily: "var(--orb-font-sans)", fontSize: "13px", padding: "8px 12px" },
- ".cm-search input, .cm-search button, .cm-search label": { fontSize: "13px" },
- ".cm-textfield": { backgroundColor: "var(--orb-bg)", border: "1px solid var(--orb-border-strong)", borderRadius: "6px", color: "var(--orb-text)", padding: "3px 8px" },
- ".cm-button": { backgroundImage: "none", backgroundColor: "var(--orb-surface)", border: "1px solid var(--orb-border-strong)", borderRadius: "6px", color: "var(--orb-text)", padding: "3px 10px" },
- ".cm-diagnostic": { fontFamily: "var(--orb-font-sans)", fontSize: "13px", padding: "6px 10px" },
- ".cm-diagnostic-error": { borderLeft: "3px solid var(--orb-danger)" },
- ".cm-diagnostic-warning": { borderLeft: "3px solid var(--orb-warning)" },
- ".cm-lintRange-error": { backgroundImage: "none", textDecoration: "underline wavy var(--orb-danger)", textUnderlineOffset: "3px" },
- ".cm-lintRange-warning": { backgroundImage: "none", textDecoration: "underline wavy var(--orb-warning)", textUnderlineOffset: "3px" },
- ".cm-lintRange-info": { backgroundImage: "none", textDecoration: "underline dotted var(--orb-info)", textUnderlineOffset: "3px" },
- ".cm-diagnostic-info": { borderLeft: "3px solid var(--orb-info)" },
- ".cm-diagnosticAction": { backgroundColor: "var(--orb-primary)", color: "var(--orb-primary-ink)", border: "0", borderRadius: "6px", padding: "2px 10px", marginLeft: "10px", font: "inherit", fontWeight: "600", cursor: "pointer" },
- ".cm-panel.cm-panel-lint ul [aria-selected]": { backgroundColor: "var(--orb-primary-dim-strong)", color: "var(--orb-text)" },
- ".cm-panel.cm-panel-lint ul": { fontFamily: "var(--orb-font-sans)", fontSize: "13px" },
- ".emmet-tracker": { textDecoration: "underline 1px dotted var(--orb-primary)", textUnderlineOffset: "3px" },
- ".emmet-preview": { fontSize: "12px", padding: "6px 10px", fontFamily: "var(--ath-font-code)" },
- ".emmet-preview .cm-editor": { backgroundColor: "transparent" },
- ".emmet-preview_error": { color: "var(--orb-danger)" },
- ".emmet-wrap-with-abbreviation__content": { backgroundColor: "var(--orb-surface)", border: "1px solid var(--orb-border-strong)", borderTop: "0", boxShadow: "var(--orb-shadow-lg)", padding: "8px" },
- ".emmet-wrap-with-abbreviation__content input": { backgroundColor: "var(--orb-bg)", color: "var(--orb-text)", border: "1px solid var(--orb-border-strong)", borderRadius: "6px", padding: "6px 10px", font: "inherit", outline: "none" },
- ".cm-completionIcon-snippet::after": { content: "'⧉'" },
- ".cm-tooltip.cm-completionInfo": { padding: "10px 12px" }
- });
-
- function createMultilineSearchPanel(view) {
- var dom = util.el("div", { class: "ath-cm-search-panel" });
- var search = util.el("textarea", {
- class: "orb-input ath-cm-search-field",
- rows: "1",
- "main-field": "",
- "aria-label": A.t("search.editor_find"),
- placeholder: A.t("search.editor_find"),
- spellcheck: "false"
- });
- var replace = util.el("textarea", {
- class: "orb-input ath-cm-search-field",
- rows: "1",
- "aria-label": A.t("search.editor_replace"),
- placeholder: A.t("search.editor_replace"),
- spellcheck: "false"
- });
- var replaceRow = util.el("div", { class: "ath-cm-search-replace", hidden: true });
- var caseSensitive = false, wholeWord = false, regexp = false;
- var options = [];
-
- function refreshQuery() {
- view.dispatch({
- effects: CM.setSearchQuery.of(new CM.SearchQuery({
- search: search.value,
- replace: replace.value,
- caseSensitive: caseSensitive,
- wholeWord: wholeWord,
- regexp: regexp
- }))
- });
- }
-
- function action(label, title, run, className) {
- var button = util.el("button", {
- type: "button",
- class: className || "ath-cm-search-button",
- title: title,
- "aria-label": title
- }, label);
- button.addEventListener("click", run);
- return button;
- }
-
- function resizeField(field) {
- field.style.height = "auto";
- field.style.height = Math.min(field.scrollHeight, 84) + "px";
- }
-
- function option(label, title, key) {
- var button = action(label, title, function () {
- if (key === "caseSensitive") caseSensitive = !caseSensitive;
- else if (key === "wholeWord") wholeWord = !wholeWord;
- else regexp = !regexp;
- button.setAttribute("aria-pressed", button.getAttribute("aria-pressed") !== "true" ? "true" : "false");
- refreshQuery();
- }, "ath-cm-search-button ath-cm-search-option");
- button.setAttribute("aria-pressed", "false");
- options.push({ key: key, button: button });
- return button;
- }
-
- var controls = util.el("div", { class: "ath-cm-search-controls" }, [
- option("Aa", A.t("search.case"), "caseSensitive"),
- option("ab", A.t("search.word"), "wholeWord"),
- option(".*", A.t("search.regex"), "regexp"),
- action("↕", A.t("search.toggle_replace"), function () {
- replaceRow.hidden = !replaceRow.hidden;
- }),
- action("‹", A.t("search.editor_previous"), function () { CM.findPrevious(view); }),
- action("›", A.t("search.editor_next"), function () { CM.findNext(view); }),
- action("×", A.t("search.editor_close"), function () { CM.closeSearchPanel(view); })
- ]);
- var searchRow = util.el("div", { class: "ath-cm-search-row" }, [search, controls]);
- replaceRow.appendChild(replace);
- replaceRow.appendChild(action(A.t("search.replace_one"), A.t("search.replace_one"), function () { CM.replaceNext(view); }));
- replaceRow.appendChild(action(A.t("search.replace_all"), A.t("search.replace_all"), function () { CM.replaceAll(view); }));
- dom.appendChild(searchRow);
- dom.appendChild(replaceRow);
-
- search.addEventListener("input", function () {
- resizeField(search);
- refreshQuery();
- });
- replace.addEventListener("input", function () {
- resizeField(replace);
- refreshQuery();
- });
- [search, replace].forEach(function (field) {
- field.addEventListener("keydown", function (event) {
- if (event.key === "Escape") {
- event.preventDefault();
- CM.closeSearchPanel(view);
- } else if (event.key === "Enter" && (event.ctrlKey || event.metaKey)) {
- event.preventDefault();
- if (event.shiftKey) CM.findPrevious(view);
- else CM.findNext(view);
- }
- });
- });
-
- function updateFromState() {
- var query = CM.getSearchQuery(view.state);
- if (!query) return;
- if (search.value !== query.search) search.value = query.search;
- if (replace.value !== query.replace) replace.value = query.replace;
- resizeField(search);
- resizeField(replace);
- caseSensitive = query.caseSensitive;
- wholeWord = query.wholeWord;
- regexp = query.regexp;
- options.forEach(function (item) {
- var enabled = item.key === "caseSensitive" ? caseSensitive : item.key === "wholeWord" ? wholeWord : regexp;
- item.button.setAttribute("aria-pressed", enabled ? "true" : "false");
- });
- }
-
- updateFromState();
- return { dom: dom, top: true, update: updateFromState };
- }
-
- var langComp = new CM.Compartment();
- var optComp = new CM.Compartment();
- var phraseComp = new CM.Compartment();
- var S = A.settings;
-
- var langCache = {};
- function langExt(name) {
- var id = langId(name), ext = util.ext(name);
- var swatches = S.get("colorSwatches") && A.colors.supports(id);
- var key = id + ":" + ext + ":" + S.get("emmet") + ":" + (swatches ? "c" : "");
- if (!langCache[key]) {
- langCache[key] = [LANGS[id].make(), A.intellisense.forLanguage(id, ext)];
- if (swatches) langCache[key].push(A.colors.extension(id));
- }
- return langCache[key];
- }
-
- var lastCounts = "";
- function countDiagnostics(state) {
- var c = { error: 0, warning: 0, info: 0 };
- CM.forEachDiagnostic(state, function (d) { c[d.severity] = (c[d.severity] || 0) + 1; });
- return c;
- }
-
- function onUpdate(u) {
- if (u.docChanged) A.emit("doc-change", u.state);
- if (u.docChanged || u.selectionSet) A.emit("cursor", u.state);
- var c = countDiagnostics(u.state), key = c.error + ":" + c.warning + ":" + c.info;
- if (key !== lastCounts) {
- lastCounts = key;
- A.emit("diagnostics", c);
- }
- }
-
- var minimapExt = CM.ViewPlugin.fromClass(class {
- constructor(view) {
- this.view = view;
- this.host = view.dom.parentElement;
- this.el = document.createElement("div");
- this.el.className = "ath-minimap";
- this.el.tabIndex = 0;
- this.el.setAttribute("role", "scrollbar");
- this.el.setAttribute("aria-orientation", "vertical");
- this.el.setAttribute("aria-label", A.t("settings.minimap"));
- if (!view.contentDOM.id) view.contentDOM.id = "ath-editor-content";
- this.el.setAttribute("aria-controls", view.contentDOM.id);
- this.canvas = document.createElement("canvas");
- this.viewport = document.createElement("div");
- this.viewport.className = "ath-minimap-viewport";
- this.markers = document.createElement("div");
- this.markers.className = "ath-minimap-markers";
- this.el.appendChild(this.canvas);
- this.el.appendChild(this.viewport);
- this.el.appendChild(this.markers);
- this.host.classList.add("has-minimap");
- this.host.appendChild(this.el);
- this.dragOffset = 0;
- this.renderFrame = 0;
- this.documentDirty = true;
- this.selectionDirty = true;
- this.onScroll = this.renderViewport.bind(this);
- this.onResize = function () { this.scheduleRender(true, true); }.bind(this);
- this.onPointerDown = this.pointerDown.bind(this);
- this.onPointerMove = this.pointerMove.bind(this);
- this.onPointerEnd = this.pointerEnd.bind(this);
- this.onKeyDown = this.keyDown.bind(this);
- view.scrollDOM.addEventListener("scroll", this.onScroll, { passive: true });
- this.el.addEventListener("pointerdown", this.onPointerDown);
- this.el.addEventListener("pointermove", this.onPointerMove);
- this.el.addEventListener("pointerup", this.onPointerEnd);
- this.el.addEventListener("pointercancel", this.onPointerEnd);
- this.el.addEventListener("keydown", this.onKeyDown);
- this.resizeObserver = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(this.onResize);
- if (this.resizeObserver) {
- this.resizeObserver.observe(view.scrollDOM);
- this.resizeObserver.observe(this.el);
- }
- else window.addEventListener("resize", this.onResize);
- this.scheduleRender();
- }
-
- update(update) {
- var documentChanged = update.docChanged || update.geometryChanged;
- if (documentChanged || update.selectionSet) this.scheduleRender(documentChanged, documentChanged || update.selectionSet);
- }
-
- scheduleRender(documentChanged, selectionChanged) {
- if (documentChanged) this.documentDirty = true;
- if (selectionChanged) this.selectionDirty = true;
- if (this.renderFrame) return;
- this.renderFrame = requestAnimationFrame(function () {
- this.renderFrame = 0;
- var rect = this.canvas.getBoundingClientRect();
- if (!rect.width || !rect.height) return;
- var contentHeight = this.view.contentHeight;
- if (this.documentDirty) {
- this.renderDocument(rect, contentHeight);
- this.documentDirty = false;
- }
- if (this.selectionDirty) {
- this.renderSelection(rect, contentHeight);
- this.selectionDirty = false;
- }
- this.renderViewport();
- }.bind(this));
- }
-
- destroy() {
- if (this.renderFrame) cancelAnimationFrame(this.renderFrame);
- this.view.scrollDOM.removeEventListener("scroll", this.onScroll);
- this.el.removeEventListener("pointerdown", this.onPointerDown);
- this.el.removeEventListener("pointermove", this.onPointerMove);
- this.el.removeEventListener("pointerup", this.onPointerEnd);
- this.el.removeEventListener("pointercancel", this.onPointerEnd);
- this.el.removeEventListener("keydown", this.onKeyDown);
- if (this.resizeObserver) this.resizeObserver.disconnect();
- else window.removeEventListener("resize", this.onResize);
- this.el.remove();
- this.host.classList.remove("has-minimap");
- }
-
- renderDocument(rect, contentHeight) {
- var dpr = window.devicePixelRatio || 1;
- var width = Math.round(rect.width * dpr), height = Math.round(rect.height * dpr);
- if (this.canvas.width !== width || this.canvas.height !== height) {
- this.canvas.width = width;
- this.canvas.height = height;
- }
- var ctx = this.canvas.getContext("2d");
- ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
- ctx.clearRect(0, 0, rect.width, rect.height);
- if (!contentHeight) return;
- var sampleHeight = 1;
- var codeColor = getComputedStyle(this.el).getPropertyValue("--ath-minimap-code").trim();
- var codeFont = getComputedStyle(this.view.contentDOM).fontFamily;
- var lastFrom = -1, lastMapY = -1;
- for (var y = 0; y < rect.height; y += sampleHeight) {
- var block = this.view.lineBlockAtHeight((y + sampleHeight / 2) / rect.height * contentHeight);
- if (block.from === lastFrom) continue;
- lastFrom = block.from;
- var mapY = Math.max(0, Math.min(rect.height - 1, Math.round(block.top / contentHeight * rect.height)));
- if (mapY === lastMapY) continue;
- lastMapY = mapY;
- var text = this.view.state.doc.lineAt(block.from).text;
- if (!text.trim()) continue;
- var indent = (text.match(/^\s*/) || [""])[0].length;
- var x = 10 + Math.min(24, indent * 1.25);
- var lineHeight = block.height / contentHeight * rect.height;
- var fontSize = Math.max(0.65, Math.min(3, lineHeight * 0.72));
- ctx.fillStyle = codeColor;
- ctx.globalAlpha = text.trimStart().charAt(0) === "<" ? 0.78 : 1;
- ctx.font = fontSize + "px " + codeFont;
- ctx.textBaseline = "top";
- ctx.fillText(text.slice(0, 500), x, mapY, rect.width - x - 8);
- }
- }
-
- renderSelection(rect, contentHeight) {
- this.markers.replaceChildren();
- if (!contentHeight) return;
- var view = this.view;
- var scale = rect.height / contentHeight;
- var selection = view.state.selection;
- selection.ranges.forEach(function (range) {
- if (range.empty) return;
- var from = view.lineBlockAt(range.from);
- var to = view.lineBlockAt(range.to);
- var top = Math.max(0, Math.min(from.top, to.top));
- var bottom = Math.min(contentHeight, Math.max(from.top + from.height, to.top + to.height));
- if (bottom > top) {
- var marker = document.createElement("div");
- marker.className = "ath-minimap-selection";
- marker.style.top = top * scale + "px";
- marker.style.height = Math.max(0.75, (bottom - top) * scale) + "px";
- this.markers.appendChild(marker);
- }
- }, this);
- var cursor = selection.main;
- var cursorBlock = view.lineBlockAt(cursor.head);
- var cursorY = Math.max(0, Math.min(contentHeight, cursorBlock.top + cursorBlock.height / 2)) * scale;
- var caret = document.createElement("div");
- caret.className = "ath-minimap-cursor";
- caret.style.top = Math.max(0, Math.min(rect.height - 1, cursorY - 0.5)) + "px";
- this.markers.appendChild(caret);
- }
-
- renderViewport() {
- var rect = this.canvas.getBoundingClientRect();
- if (!rect.width || !rect.height) return;
- var scroll = this.view.scrollDOM;
- var contentHeight = this.view.contentHeight;
- var maxScroll = Math.max(0, contentHeight - scroll.clientHeight);
- var top = maxScroll ? scroll.scrollTop / contentHeight * rect.height : 0;
- var visible = maxScroll ? scroll.clientHeight / contentHeight * rect.height : rect.height;
- this.viewport.hidden = maxScroll <= 1;
- this.viewport.style.top = Math.min(top, rect.height - visible) + "px";
- this.viewport.style.height = Math.min(visible, rect.height) + "px";
- this.el.setAttribute("aria-valuemin", "0");
- this.el.setAttribute("aria-valuemax", "100");
- this.el.setAttribute("aria-valuenow", String(maxScroll ? Math.round(scroll.scrollTop / maxScroll * 100) : 0));
- }
-
- scrollFromPointer(event, behavior) {
- var rect = this.canvas.getBoundingClientRect();
- var scroll = this.view.scrollDOM;
- var contentHeight = this.view.contentHeight;
- var maxScroll = Math.max(0, contentHeight - scroll.clientHeight);
- if (!maxScroll) return;
- var visible = scroll.clientHeight / contentHeight * rect.height;
- var mapTop = Math.max(0, Math.min(rect.height - visible, event.clientY - rect.top - this.dragOffset));
- this.view.scrollDOM.scrollTo({ top: mapTop / rect.height * contentHeight, behavior: behavior });
- }
-
- pointerDown(event) {
- if (event.button !== 0) return;
- event.preventDefault();
- this.el.focus();
- this.el.setPointerCapture(event.pointerId);
- var viewportRect = this.viewport.getBoundingClientRect();
- this.dragOffset = event.clientY >= viewportRect.top && event.clientY <= viewportRect.bottom
- ? event.clientY - viewportRect.top
- : viewportRect.height / 2;
- this.el.classList.add("is-dragging");
- this.scrollFromPointer(event, "smooth");
- }
-
- pointerMove(event) {
- if (this.el.hasPointerCapture(event.pointerId)) this.scrollFromPointer(event, "instant");
- }
-
- pointerEnd() {
- this.el.classList.remove("is-dragging");
- }
-
- keyDown(event) {
- var scroll = this.view.scrollDOM, step = 40;
- if (event.key === "ArrowUp") scroll.scrollTop -= step;
- else if (event.key === "ArrowDown") scroll.scrollTop += step;
- else if (event.key === "PageUp") scroll.scrollTop -= scroll.clientHeight;
- else if (event.key === "PageDown") scroll.scrollTop += scroll.clientHeight;
- else if (event.key === "Home") scroll.scrollTop = 0;
- else if (event.key === "End") scroll.scrollTop = scroll.scrollHeight;
- else return;
- event.preventDefault();
- }
- });
-
- var baseExtensions = [
- CM.highlightSpecialChars(),
- CM.history(),
- CM.drawSelection(),
- CM.dropCursor(),
- CM.EditorState.allowMultipleSelections.of(true),
- CM.indentOnInput(),
- CM.syntaxHighlighting(highlight),
- CM.bracketMatching(),
- CM.rectangularSelection(),
- CM.crosshairCursor(),
- CM.lintGutter(),
- CM.search({ top: true, createPanel: createMultilineSearchPanel }),
- A.intellisense.base(),
- CM.Prec.highest(CM.keymap.of([{ key: "Tab", run: CM.acceptCompletion }])),
- CM.keymap.of([].concat(
- CM.closeBracketsKeymap, CM.defaultKeymap, CM.searchKeymap, CM.historyKeymap,
- CM.foldKeymap, CM.completionKeymap, CM.lintKeymap, [CM.indentWithTab]
- )),
- theme,
- CM.EditorView.domEventHandlers({
- drop: function (e, view) { return !!A.images && A.images.onDrop(e, view); },
- paste: function (e, view) { return !!A.images && A.images.onPaste(e, view); }
- }),
- CM.EditorView.updateListener.of(onUpdate)
- ];
-
- function optExt() {
- var o = S.all();
- var ext = [
- CM.EditorState.tabSize.of(o.tabSize),
- CM.indentUnit.of(o.insertSpaces ? " ".repeat(o.tabSize) : " "),
- CM.autocompletion({ activateOnTyping: o.autocomplete, icons: true })
- ];
- if (o.lineNumbers) ext.push(CM.lineNumbers(), CM.highlightActiveLineGutter());
- if (o.fold) ext.push(CM.foldGutter({ openText: "⌄", closedText: "›" }));
- if (o.activeLine) ext.push(CM.highlightActiveLine());
- if (o.closeBrackets) ext.push(CM.closeBrackets());
- if (o.matchHighlight) ext.push(CM.highlightSelectionMatches());
- if (o.wrap) ext.push(CM.EditorView.lineWrapping);
- if (o.minimap) ext.push(minimapExt);
- return ext;
- }
-
- function phraseExt() {
- return CM.EditorState.phrases.of(A.i18n.phrases());
- }
-
- var editor = (A.editor = {
- view: null,
- langId: langId,
-
- langLabel: function (name) {
- return LANGS[langId(name)].label || A.t("status.plain_text");
- },
-
- init: function (parent) {
- editor.view = new CM.EditorView({ state: editor.createState("", "untitled.txt"), parent: parent });
- A.on("lang", function () {
- editor.view.dispatch({ effects: phraseComp.reconfigure(phraseExt()) });
- });
- A.on("settings", function (keys) {
- var effects = [optComp.reconfigure(optExt())];
- if ((keys.indexOf("emmet") >= 0 || keys.indexOf("colorSwatches") >= 0) && A.tabs && A.tabs.active) effects.push(langComp.reconfigure(langExt(A.tabs.active.name)));
- editor.view.dispatch({ effects: effects });
- if (keys.indexOf("lintHints") >= 0) CM.forceLinting(editor.view);
- if (keys.some(function (k) { return /font|lineHeight/i.test(k); })) editor.view.requestMeasure();
- });
- if (document.fonts) {
- document.fonts.addEventListener("loadingdone", function () { editor.view.requestMeasure(); });
- }
- },
-
- createState: function (doc, name) {
- return CM.EditorState.create({
- doc: doc,
- extensions: [
- A.aiGhost ? A.aiGhost.extension() : [],
- A.links ? A.links.extension() : [],
- baseExtensions,
- optComp.of(optExt()),
- phraseComp.of(phraseExt()),
- langComp.of(langExt(name))
- ]
- });
- },
-
- setLanguage: function (state, name) {
- return state.update({ effects: langComp.reconfigure(langExt(name)) }).state;
- },
-
- syncState: function (state, name) {
- var effects = [optComp.reconfigure(optExt()), phraseComp.reconfigure(phraseExt())];
- if (name) effects.push(langComp.reconfigure(langExt(name)));
- return state.update({ effects: effects }).state;
- },
-
- diagnostics: function () {
- return countDiagnostics(editor.view.state);
- },
-
- toggleWrap: function () {
- S.set("wrap", !S.get("wrap"));
- return S.get("wrap");
- },
-
- zoom: function (delta) {
- S.set("fontSize", delta === 0 ? S.defaults.fontSize : Math.max(10, Math.min(28, S.get("fontSize") + delta)));
- },
-
- focus: function () {
- if (editor.view) editor.view.focus();
- },
-
- openSearch: function () {
- CM.openSearchPanel(editor.view);
- },
-
- run: function (command) {
- editor.view.focus();
- return command(editor.view);
- },
-
- goTo: function (line, col) {
- var view = editor.view, doc = view.state.doc;
- var l = doc.line(Math.max(1, Math.min(doc.lines, line)));
- var pos = Math.min(l.from + Math.max(0, (col || 1) - 1), l.to);
- view.dispatch({ selection: { anchor: pos }, effects: CM.EditorView.scrollIntoView(pos, { y: "center" }) });
- view.focus();
- },
-
- select: function (line, col, length) {
- var view = editor.view, doc = view.state.doc;
- var l = doc.line(Math.max(1, Math.min(doc.lines, line)));
- var from = Math.min(l.from + Math.max(0, (col || 1) - 1), l.to);
- var to = Math.min(from + (length || 0), doc.length);
- view.dispatch({ selection: { anchor: from, head: to }, effects: CM.EditorView.scrollIntoView(from, { y: "center" }) });
- view.focus();
- },
-
- selectOccurrences: function () {
- var view = editor.view, r = view.state.selection.main;
- if (r.empty) {
- var w = view.state.wordAt(r.head);
- if (!w) return false;
- view.dispatch({ selection: { anchor: w.from, head: w.to } });
- }
- view.focus();
- return CM.selectSelectionMatches(view);
- },
-
- copy: async function (cut) {
- var view = editor.view, st = view.state;
- var ranges = st.selection.ranges.filter(function (r) { return !r.empty; });
- var text, changes;
- if (ranges.length) {
- text = ranges.map(function (r) { return st.sliceDoc(r.from, r.to); }).join(st.lineBreak);
- changes = ranges.map(function (r) { return { from: r.from, to: r.to }; });
- } else {
- var line = st.doc.lineAt(st.selection.main.head);
- text = line.text + st.lineBreak;
- changes = { from: line.from, to: Math.min(line.to + 1, st.doc.length) };
- }
- await A.ui.copyText(text);
- if (cut) view.dispatch({ changes: changes, userEvent: "delete.cut" });
- view.focus();
- },
-
- paste: async function () {
- var view = editor.view;
- var text = await navigator.clipboard.readText();
- view.dispatch(view.state.replaceSelection(text), { userEvent: "input.paste", scrollIntoView: true });
- view.focus();
- },
-
- simulateKey: function (key, code, mods) {
- var ev = new KeyboardEvent("keydown", Object.assign({ key: key, code: code, bubbles: true, cancelable: true }, mods || {}));
- editor.view.focus();
- editor.view.contentDOM.dispatchEvent(ev);
- }
- });
- })();
-