|
| 1 | +import { Application, Controller } from 'https://unpkg.com/@hotwired/stimulus/dist/stimulus.js'; |
| 2 | + |
| 3 | +const application = Application.start(); |
| 4 | +application.debug = true; |
| 5 | + |
| 6 | +class ScrollObserverController extends Controller { |
| 7 | + initialize() { |
| 8 | + if (typeof IntersectionObserver !== 'function') { |
| 9 | + this.observer = null; |
| 10 | + return; |
| 11 | + } |
| 12 | + |
| 13 | + this.observer = new IntersectionObserver((entries) => { |
| 14 | + entries.forEach((entry) => { |
| 15 | + const eventName = entry.isIntersecting ? 'enter' : 'leave'; |
| 16 | + this.dispatch(eventName, { prefix: 'scroll' }); |
| 17 | + }); |
| 18 | + }, { |
| 19 | + root: null, |
| 20 | + threshold: 0, |
| 21 | + rootMargin: '-40% 0px -55% 0px', |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + connect() { |
| 26 | + this.observer?.observe(this.element); |
| 27 | + } |
| 28 | + |
| 29 | + disconnect() { |
| 30 | + this.observer?.disconnect(); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +class CopyController extends Controller { |
| 35 | + static targets = ['text', 'button']; |
| 36 | + |
| 37 | + connect() { |
| 38 | + this.resetTimer = null; |
| 39 | + } |
| 40 | + |
| 41 | + disconnect() { |
| 42 | + if (this.resetTimer) { |
| 43 | + clearTimeout(this.resetTimer); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + async copy(event) { |
| 48 | + event?.preventDefault?.(); |
| 49 | + if (!navigator.clipboard || !this.hasTextTarget || !this.hasButtonTarget) return; |
| 50 | + |
| 51 | + const text = this.textTarget.textContent?.trim(); |
| 52 | + if (!text) return; |
| 53 | + |
| 54 | + try { |
| 55 | + await navigator.clipboard.writeText(text); |
| 56 | + this.showFeedback(); |
| 57 | + } catch (error) { |
| 58 | + console.error('Unable to copy command', error); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + showFeedback() { |
| 63 | + this.buttonTarget.classList.add('copied'); |
| 64 | + clearTimeout(this.resetTimer); |
| 65 | + this.resetTimer = window.setTimeout(() => { |
| 66 | + this.buttonTarget.classList.remove('copied'); |
| 67 | + this.resetTimer = null; |
| 68 | + }, 1500); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class NavigationController extends Controller { |
| 73 | + static targets = ['navButton', 'downLink']; |
| 74 | + |
| 75 | + connect() { |
| 76 | + this.currentButton = null; |
| 77 | + } |
| 78 | + |
| 79 | + to(event) { |
| 80 | + const candidate = this.resolveButton(event); |
| 81 | + if (!candidate) return; |
| 82 | + |
| 83 | + if (event?.type === 'scroll:enter') { |
| 84 | + this.setCurrentButton(candidate); |
| 85 | + } |
| 86 | + // clicks fall through and rely on scroll observer to update active state |
| 87 | + } |
| 88 | + |
| 89 | + resolveButton(event) { |
| 90 | + const number = event?.params?.number ?? |
| 91 | + event?.currentTarget?.dataset?.navigationNumberParam ?? |
| 92 | + event?.target?.dataset?.navigationNumberParam; |
| 93 | + if (number !== undefined) { |
| 94 | + return this.findButtonByNumber(number); |
| 95 | + } |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + setCurrentButton(button) { |
| 100 | + if (!button) { |
| 101 | + this.updateDownLink(null); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + if (button === this.currentButton) { |
| 106 | + this.updateDownLink(button); |
| 107 | + this.notifyStage(button); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + this.toggleButton(this.currentButton, false); |
| 112 | + this.toggleButton(button, true); |
| 113 | + this.currentButton = button; |
| 114 | + this.updateDownLink(button); |
| 115 | + this.notifyStage(button); |
| 116 | + } |
| 117 | + |
| 118 | + notifyStage(button) { |
| 119 | + const scene = button?.dataset.stageScene; |
| 120 | + if (!scene) return; |
| 121 | + window.dispatchEvent(new CustomEvent('stage:change', { detail: { scene } })); |
| 122 | + } |
| 123 | + |
| 124 | + toggleButton(button, isActive) { |
| 125 | + if (!button) return; |
| 126 | + if (isActive) { |
| 127 | + button.setAttribute('aria-current', 'page'); |
| 128 | + } else { |
| 129 | + button.removeAttribute('aria-current'); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + findButtonByNumber(number) { |
| 134 | + if (number === undefined || number === null) return null; |
| 135 | + const lookup = Number(number); |
| 136 | + if (Number.isNaN(lookup)) return null; |
| 137 | + return this.navButtonTargets.find( |
| 138 | + (button) => Number(button.dataset.navigationNumberParam) === lookup, |
| 139 | + ) || null; |
| 140 | + } |
| 141 | + |
| 142 | + updateDownLink(currentButton) { |
| 143 | + if (!this.hasDownLinkTarget) return; |
| 144 | + if (!currentButton) { |
| 145 | + this.downLinkTarget.removeAttribute('href'); |
| 146 | + this.downLinkTarget.setAttribute('aria-disabled', 'true'); |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + const currentIndex = this.navButtonTargets.indexOf(currentButton); |
| 151 | + if (currentIndex === -1) return; |
| 152 | + const nextButton = this.navButtonTargets[currentIndex + 1] || null; |
| 153 | + if (nextButton) { |
| 154 | + this.downLinkTarget.setAttribute('href', nextButton.getAttribute('href') || '#'); |
| 155 | + this.downLinkTarget.removeAttribute('aria-disabled'); |
| 156 | + } else { |
| 157 | + this.downLinkTarget.removeAttribute('href'); |
| 158 | + this.downLinkTarget.setAttribute('aria-disabled', 'true'); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +class StageController extends Controller { |
| 164 | + static targets = ['scene']; |
| 165 | + |
| 166 | + connect() { |
| 167 | + this.handleChange = (event) => this.show(event.detail?.scene); |
| 168 | + window.addEventListener('stage:change', this.handleChange); |
| 169 | + this.typingInterval = null; |
| 170 | + const initialScene = this.sceneTargets[0]?.dataset.sceneId; |
| 171 | + if (initialScene) { |
| 172 | + this.show(initialScene); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + disconnect() { |
| 177 | + window.removeEventListener('stage:change', this.handleChange); |
| 178 | + this.stopTyping(); |
| 179 | + } |
| 180 | + |
| 181 | + show(sceneId) { |
| 182 | + if (!sceneId) return; |
| 183 | + this.stopTyping(); |
| 184 | + |
| 185 | + let matched = false; |
| 186 | + this.sceneTargets.forEach((scene) => { |
| 187 | + const isActive = scene.dataset.sceneId === sceneId; |
| 188 | + scene.classList.toggle('stage-scene--active', isActive); |
| 189 | + if (isActive) { |
| 190 | + matched = true; |
| 191 | + if (sceneId === 'interact') { |
| 192 | + this.startTyping(scene); |
| 193 | + } |
| 194 | + } |
| 195 | + }); |
| 196 | + if (!matched && this.sceneTargets.length) { |
| 197 | + const fallback = this.sceneTargets[0]; |
| 198 | + fallback.classList.add('stage-scene--active'); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + startTyping(scene) { |
| 203 | + const textEl = scene.querySelector('.stage-typed-text'); |
| 204 | + if (!textEl) return; |
| 205 | + |
| 206 | + const fullText = '••••'; |
| 207 | + let currentIndex = 0; |
| 208 | + |
| 209 | + const typeChar = () => { |
| 210 | + if (currentIndex <= fullText.length) { |
| 211 | + textEl.textContent = fullText.substring(0, currentIndex); |
| 212 | + currentIndex++; |
| 213 | + } else { |
| 214 | + // Pause then restart |
| 215 | + setTimeout(() => { |
| 216 | + currentIndex = 0; |
| 217 | + textEl.textContent = ''; |
| 218 | + }, 1000); |
| 219 | + } |
| 220 | + }; |
| 221 | + |
| 222 | + // Start typing after a short delay |
| 223 | + setTimeout(() => { |
| 224 | + typeChar(); |
| 225 | + this.typingInterval = setInterval(typeChar, 150); |
| 226 | + }, 300); |
| 227 | + } |
| 228 | + |
| 229 | + stopTyping() { |
| 230 | + if (this.typingInterval) { |
| 231 | + clearInterval(this.typingInterval); |
| 232 | + this.typingInterval = null; |
| 233 | + } |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +application.register('scroll-observer', ScrollObserverController); |
| 238 | +application.register('copy', CopyController); |
| 239 | +application.register('navigation', NavigationController); |
| 240 | +application.register('stage', StageController); |
0 commit comments