This repository has been archived on 2024-10-26. You can view files and clone it, but cannot push or open issues or pull requests.
staxman-old/static/scripts/components/editor/script.ts

103 lines
2.5 KiB
TypeScript
Raw Normal View History

2023-11-30 00:57:45 +00:00
import "/static/vendor/ace/ace.js";
2023-11-30 09:14:32 +00:00
import { $el } from "/static/vendor/domutil/domutil.js";
2023-11-30 00:57:45 +00:00
class Editor extends HTMLElement {
2023-11-30 09:14:32 +00:00
private editor: AceAjax.Editor;
private tabEl: HTMLDivElement;
private files: Record<string, AceAjax.IEditSession>;
private root: ShadowRoot;
2023-11-30 09:29:30 +00:00
private addTabButton: HTMLDivElement;
2023-11-30 00:57:45 +00:00
constructor() {
super();
this.files = {};
2023-11-30 09:14:32 +00:00
this.root = this.attachShadow({ mode: "open" });
2023-11-30 00:57:45 +00:00
}
connectedCallback() {
2023-11-30 09:29:30 +00:00
this.addTabButton = $el("div", { className: "tab" }, "+");
this.tabEl = $el("div", { className: "tab-container" }, this.addTabButton);
2023-11-30 09:14:32 +00:00
this.root.append(this.tabEl);
2023-11-30 00:57:45 +00:00
const style = $el("style");
style.textContent = `
.tab-container {
display: flex;
2023-11-30 09:29:30 +00:00
margin-bottom: -2px;
2023-11-30 00:57:45 +00:00
& .tab {
border: 2px solid var(--table-border-color);
padding: 0.5ch 0.8ch;
background-color: var(--button-bg);
cursor: pointer;
&.active {
background-color: var(--table-border-color);
}
&:not(:first-child) {
margin-left: -2px;
}
}
}
`;
const slot = $el("slot", { name: "body" });
2023-11-30 09:14:32 +00:00
this.root.append(style, slot);
2023-11-30 00:57:45 +00:00
const editorEl = $el("div", { slot: "body" });
this.append(editorEl);
// Create editor
ace.config.set('basePath', '/static/vendor/ace')
this.editor = ace.edit(editorEl);
// todo make this stuff customizable??
this.editor.setTheme("ace/theme/dracula");
this.editor.setOptions({
fontFamily: "Iosevka Web",
fontSize: "12pt",
});
this.editor.getSession().setUseWrapMode(true);
this.editor.setKeyboardHandler("ace/keyboard/vim");
}
/**
* Add a file editing session
2023-11-30 09:14:32 +00:00
* @param name File name
* @param content File contents
2023-11-30 00:57:45 +00:00
*/
2023-11-30 09:14:32 +00:00
addFile(name: string, content: string) {
2023-11-30 00:57:45 +00:00
// Add to session list
this.files[name] = ace.createEditSession(content);
// TODO replace this with auto-detection
this.files[name].setMode("ace/mode/nix");
// Create tab and set as active
const tab = this.#addTab(name);
tab.click();
}
2023-11-30 09:29:30 +00:00
setCurrent(name: string) {
this.editor.setSession(this.files[name]);
this.tabEl.querySelectorAll<HTMLDivElement>(".tab").forEach(el => {
if (el.dataset.name === name) el.classList.add("active");
else el.classList.remove("active");
});
}
2023-11-30 00:57:45 +00:00
/**
* Create a new tab
2023-11-30 09:14:32 +00:00
* @param name Tab name
2023-11-30 00:57:45 +00:00
* @returns Tab element
*/
2023-11-30 09:14:32 +00:00
#addTab(name: string) {
2023-11-30 00:57:45 +00:00
const tab = $el("div", {
className: "tab",
2023-11-30 09:29:30 +00:00
dataset: { name },
"@click": () => this.setCurrent(name)
2023-11-30 00:57:45 +00:00
}, name);
2023-11-30 09:29:30 +00:00
this.tabEl.insertBefore(tab, this.addTabButton);
2023-11-30 00:57:45 +00:00
return tab;
}
}
customElements.define("file-editor", Editor);