|
| 1 | +import { LitElement, html, css } from "lit"; |
| 2 | +import { customElement, property } from "lit/decorators.js"; |
| 3 | +import { view, tags, render } from "@commontools/common-ui"; |
| 4 | +import { isGem, Gem, ID } from "../recipe.js"; |
| 5 | +const { binding } = view; |
| 6 | +const { include } = tags; |
| 7 | + |
| 8 | +@customElement("common-window-manager") |
| 9 | +export class CommonWindowManager extends LitElement { |
| 10 | + static override styles = css` |
| 11 | + :host { |
| 12 | + display: flex; |
| 13 | + overflow-x: auto; |
| 14 | + width: 100%; |
| 15 | + padding: 20px 0; /* Add vertical padding */ |
| 16 | + } |
| 17 | + .window { |
| 18 | + flex: 0 0 auto; |
| 19 | + width: 300px; |
| 20 | + margin-left: 20px; |
| 21 | + padding: 10px; |
| 22 | + border: 1px solid #e0e0e0; |
| 23 | + border-radius: 8px; |
| 24 | + background-color: rgba(255, 255, 255, 0.8); |
| 25 | + backdrop-filter: blur(10px); |
| 26 | + box-shadow: |
| 27 | + 0 10px 20px rgba(0, 0, 0, 0.1), |
| 28 | + 0 6px 6px rgba(0, 0, 0, 0.1), |
| 29 | + 0 0 0 1px rgba(0, 0, 0, 0.05); |
| 30 | + transition: all 0.3s ease; |
| 31 | + } |
| 32 | + .close-button { |
| 33 | + position: absolute; |
| 34 | + top: 8px; |
| 35 | + right: 8px; |
| 36 | + width: 16px; |
| 37 | + height: 16px; |
| 38 | + border-radius: 50%; |
| 39 | + background-color: rgba(0, 0, 0, 0.1); |
| 40 | + border: none; |
| 41 | + cursor: pointer; |
| 42 | + display: flex; |
| 43 | + align-items: center; |
| 44 | + justify-content: center; |
| 45 | + font-size: 12px; |
| 46 | + color: rgba(0, 0, 0, 0.4); |
| 47 | + font-weight: bold; |
| 48 | + transition: all 0.2s ease; |
| 49 | + } |
| 50 | + .close-button:hover { |
| 51 | + background-color: rgba(0, 0, 0, 0.15); |
| 52 | + color: rgba(0, 0, 0, 0.6); |
| 53 | + } |
| 54 | + `; |
| 55 | + |
| 56 | + @property({ type: Array }) |
| 57 | + sagas: Gem[] = []; |
| 58 | + |
| 59 | + private renderedSagas: { [key: string]: HTMLElement } = {}; |
| 60 | + |
| 61 | + override render() { |
| 62 | + return html` |
| 63 | + ${this.sagas.map((saga) => { |
| 64 | + if (!this.renderedSagas[saga[ID]]) |
| 65 | + this.renderedSagas[saga[ID]] = render.render( |
| 66 | + include({ content: binding("UI") }), |
| 67 | + { |
| 68 | + UI: saga.UI, |
| 69 | + } |
| 70 | + ) as HTMLElement; |
| 71 | +
|
| 72 | + return html` |
| 73 | + <div class="window" id="${saga[ID]}"> |
| 74 | + <button class="close-button" @click="${this.onClose}">×</button> |
| 75 | + <common-screen-element> |
| 76 | + ${this.renderedSagas[saga[ID]]} |
| 77 | + </common-screen-element> |
| 78 | + </div> |
| 79 | + `; |
| 80 | + })} |
| 81 | + `; |
| 82 | + } |
| 83 | + |
| 84 | + openSaga(saga: Gem) { |
| 85 | + this.sagas = [...this.sagas, saga]; |
| 86 | + this.updateComplete.then(() => { |
| 87 | + const newWindow = this.renderRoot.querySelector(".window:last-child"); |
| 88 | + if (newWindow) { |
| 89 | + newWindow.scrollIntoView({ |
| 90 | + behavior: "smooth", |
| 91 | + block: "nearest", |
| 92 | + inline: "start", |
| 93 | + }); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + onClose(e: Event) { |
| 99 | + const id = (e.currentTarget as HTMLElement).parentElement?.id; |
| 100 | + if (id) { |
| 101 | + this.sagas = this.sagas.filter((saga) => saga[ID] + "" !== id); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + override connectedCallback() { |
| 106 | + super.connectedCallback(); |
| 107 | + this.addEventListener("open-saga", this.handleAddWindow); |
| 108 | + } |
| 109 | + |
| 110 | + override disconnectedCallback() { |
| 111 | + super.disconnectedCallback(); |
| 112 | + this.removeEventListener("open-saga", this.handleAddWindow); |
| 113 | + } |
| 114 | + |
| 115 | + private handleAddWindow(e: Event) { |
| 116 | + const saga = (e as CustomEvent).detail.saga; |
| 117 | + if (isGem(saga)) { |
| 118 | + this.openSaga(saga); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments