|
| 1 | +import { IRectpackrConfig, IRectpackr, rectpackr } from './core'; |
| 2 | + |
| 3 | +/* ------------------------------------------------------------------------- */ |
| 4 | +/* -------------------------- // Helper functions -------------------------- */ |
| 5 | +/* ------------------------------------------------------------------------- */ |
| 6 | + |
| 7 | +const parseConfig = (config: { |
| 8 | + positioning: string | undefined; |
| 9 | + 'x-direction': string | undefined; |
| 10 | + 'y-direction': string | undefined; |
| 11 | +}): IRectpackrConfig => ({ |
| 12 | + positioning: |
| 13 | + (config.positioning?.trim() ?? '') === 'offset' ? 'offset' : 'transform', |
| 14 | + 'x-direction': |
| 15 | + (config['x-direction']?.trim() ?? '') === 'rtl' ? 'rtl' : 'ltr', |
| 16 | + 'y-direction': |
| 17 | + (config['y-direction']?.trim() ?? '') === 'btt' ? 'btt' : 'ttb', |
| 18 | +}); |
| 19 | + |
| 20 | +const getStyleTextContent = (config: IRectpackrConfig) => { |
| 21 | + const insetValue = { |
| 22 | + ltr: { ttb: '0 auto auto 0', btt: 'auto auto 0 0' }, |
| 23 | + rtl: { ttb: '0 0 auto auto', btt: 'auto 0 0 auto' }, |
| 24 | + }[config['x-direction']][config['y-direction']]; |
| 25 | + |
| 26 | + return ` |
| 27 | + slot{ box-sizing: border-box; position:relative; display:block; width:100% } |
| 28 | + ::slotted(:not([slot])){ position:absolute; inset:${insetValue} }`; |
| 29 | +}; |
| 30 | + |
| 31 | +/* ------------------------------------------------------------------------- */ |
| 32 | +/* -------------------------- Helper functions // -------------------------- */ |
| 33 | +/* ------------------------------------------------------------------------- */ |
| 34 | + |
| 35 | +export class RectpackrLayout extends HTMLElement { |
| 36 | + #obj: IRectpackr | undefined = undefined; |
| 37 | + |
| 38 | + static get observedAttributes() { |
| 39 | + return ['positioning', 'x-direction', 'y-direction']; |
| 40 | + } |
| 41 | + |
| 42 | + #clear() { |
| 43 | + if (this.#obj) { |
| 44 | + rectpackr.clear(this.#obj); |
| 45 | + this.#obj = undefined; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + #render() { |
| 50 | + const config = parseConfig({ |
| 51 | + positioning: this.getAttribute('positioning') ?? undefined, |
| 52 | + 'x-direction': this.getAttribute('x-direction') ?? undefined, |
| 53 | + 'y-direction': this.getAttribute('y-direction') ?? undefined, |
| 54 | + }); |
| 55 | + |
| 56 | + if (this.shadowRoot) { |
| 57 | + const slot = this.shadowRoot.querySelector('slot')!; |
| 58 | + const style = this.shadowRoot.querySelector('style')!; |
| 59 | + style.textContent = getStyleTextContent(config); |
| 60 | + this.#obj = rectpackr.create(slot, this, config); |
| 61 | + } else { |
| 62 | + const shadowRoot = this.attachShadow({ mode: 'open' }); |
| 63 | + const slot = document.createElement('slot'); |
| 64 | + const style = document.createElement('style'); |
| 65 | + style.textContent = getStyleTextContent(config); |
| 66 | + shadowRoot.appendChild(style); |
| 67 | + shadowRoot.appendChild(slot); |
| 68 | + this.#obj = rectpackr.create(slot, this, config); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + attributeChangedCallback() { |
| 73 | + if (!this.shadowRoot) { |
| 74 | + return; |
| 75 | + } |
| 76 | + this.#clear(); |
| 77 | + this.#render(); |
| 78 | + } |
| 79 | + |
| 80 | + connectedCallback() { |
| 81 | + this.#render(); |
| 82 | + } |
| 83 | + |
| 84 | + disconnectedCallback() { |
| 85 | + this.#clear(); |
| 86 | + } |
| 87 | +} |
0 commit comments