UNPKG

@dodona/papyros

Version:

Scratchpad for multiple programming languages in the browser.

215 lines (208 loc) 7.31 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { customElement, property, state } from "lit/decorators.js"; import { PapyrosElement } from "./PapyrosElement"; import { css, html } from "lit"; import { createRef, ref } from "lit/directives/ref.js"; import { inlineInputStyles, tabButtonStyles } from "./shared-styles"; import { isValidFileName } from "../../util/Util"; let EditorTab = class EditorTab extends PapyrosElement { constructor() { super(...arguments); this.renaming = false; this.invalid = false; this.renameInputRef = createRef(); } static get styles() { return css ` :host { display: flex; } ${tabButtonStyles} .close-btn, .rename-btn { display: none; align-items: center; justify-content: center; width: 1rem; height: 1rem; border-radius: 50%; font-size: 0.75rem; line-height: 1; padding: 0; background: transparent; border: none; cursor: pointer; color: inherit; opacity: 0.6; } .rename-btn svg { width: 0.75rem; height: 0.75rem; } button.active .rename-btn, button.active .close-btn { display: inline-flex; } .close-btn:hover { opacity: 1; background-color: var(--md-sys-color-error); color: var(--md-sys-color-on-error); } .rename-btn:hover { opacity: 1; background-color: var(--md-sys-color-primary); color: var(--md-sys-color-on-primary); } ${inlineInputStyles} `; } setTab() { this.papyros.io.activeEditorTab = this.file.name; } closeFile(e) { e.stopPropagation(); if (!confirm(this.t("Papyros.close_file_confirm"))) return; this.papyros.io.removeFile(this.file.name); void this.papyros.runner.deleteFile(this.file.name); } startRenaming() { this.renaming = true; this.invalid = false; } confirmRename() { var _a, _b; if (!this.renaming) return; const oldName = this.file.name; const newName = (_b = (_a = this.renameInputRef.value) === null || _a === void 0 ? void 0 : _a.value.trim()) !== null && _b !== void 0 ? _b : ""; if (newName === oldName) { this.renaming = false; return; } if (!this.papyros.io.renameFile(oldName, newName)) { return; } this.renaming = false; void this.papyros.runner.renameFile(oldName, newName); } cancelRename() { this.renaming = false; } onRenameInput() { var _a, _b; const value = (_b = (_a = this.renameInputRef.value) === null || _a === void 0 ? void 0 : _a.value.trim()) !== null && _b !== void 0 ? _b : ""; this.invalid = !isValidFileName(value) || (value !== this.file.name && this.papyros.io.files.some((f) => f.name === value)); } onRenameKeydown(e) { if (e.key === "Enter") { e.preventDefault(); this.confirmRename(); } else if (e.key === "Escape") { this.cancelRename(); } } onDblClick() { if (!this.papyros.debugger.active) this.startRenaming(); } onAuxClick(e) { if (!this.papyros.debugger.active && e.button === 1) this.closeFile(e); } onRenameClick(e) { e.stopPropagation(); this.startRenaming(); } onRenameBtnKeydown(e) { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); e.stopPropagation(); this.startRenaming(); } } onCloseBtnKeydown(e) { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); this.closeFile(e); } } updated() { if (this.renaming) { const input = this.renameInputRef.value; if (input) { input.focus(); input.select(); } } } render() { const active = this.papyros.io.activeEditorTab === this.file.name; const debugActive = this.papyros.debugger.active; if (!debugActive && this.renaming) { return html `<input ${ref(this.renameInputRef)} class=${this.invalid ? "inline-input invalid" : "inline-input"} .value=${this.file.name} @input=${this.onRenameInput} @keydown=${this.onRenameKeydown} @blur=${this.confirmRename} />`; } return html ` <button class=${active ? "active" : ""} @click=${this.setTab} @dblclick=${this.onDblClick} @auxclick=${this.onAuxClick} > ${this.file.name} ${debugActive ? "" : html `<span class="rename-btn" role="button" tabindex="0" title=${this.t("Papyros.rename_file_tab")} aria-label=${this.t("Papyros.rename_file_tab")} @click=${this.onRenameClick} @keydown=${this.onRenameBtnKeydown} > ${this.papyros.constants.icons.edit} </span> <span class="close-btn" role="button" tabindex="0" title=${this.t("Papyros.close_file_tab")} aria-label=${this.t("Papyros.close_file_tab")} @click=${this.closeFile} @keydown=${this.onCloseBtnKeydown} >×</span >`} </button> `; } }; __decorate([ property({ attribute: false }) ], EditorTab.prototype, "file", void 0); __decorate([ state() ], EditorTab.prototype, "renaming", void 0); __decorate([ state() ], EditorTab.prototype, "invalid", void 0); EditorTab = __decorate([ customElement("p-editor-tab") ], EditorTab); export { EditorTab }; //# sourceMappingURL=EditorTab.js.map