diff --git a/components/rsvp-controls.js b/components/rsvp-controls.js index e69de29..e7cef29 100644 --- a/components/rsvp-controls.js +++ b/components/rsvp-controls.js @@ -0,0 +1,54 @@ +// class RSVPControlButton extends HTMLElement { +// constructor() { +// super() +// const shadow = this.attachShadow({mode: 'open'}) +// const button = document.createElement() +// } +// } + +class RSVPControls extends HTMLElement { + constructor() { + super() + const shadow = this.attachShadow({ mode: 'open' }) + // const shadow = document.createElement('div') + shadow.innerHTML = ` + + + + + + + ` + this._root = shadow + // this.appendChild(shadow) + } + + connectedCallback() { + for (let button of this._root.querySelectorAll('button')) { + button.addEventListener('click', e => { + let action = e.target.getAttribute('action') + let evt = new CustomEvent('control-event', { detail: { action } }) + this.dispatchEvent(evt) + }) + } + } + + updateUI(chapter, player) { + this.getButton('prevSentence').disabled = !chapter.hasPrevSentence() + this.getButton('prevWord').disabled = !chapter.hasPrevWord() + this.getButton('nextWord').disabled = !chapter.hasNextWord() + this.getButton('nextSentence').disabled = !chapter.hasNextSentence() + this.getButton('playpause').innerText = player.playing ? 'pause' : 'start' + } + + getButton(name) { + for (let btn of this._root.querySelectorAll('button')) { + if (btn.getAttribute('action') === name) { + return btn + } + } + return null + } +} + +window.customElements.define('rsvp-controls', RSVPControls) diff --git a/components/rsvp-reader.js b/components/rsvp-reader.js index e69de29..c80f95c 100644 --- a/components/rsvp-reader.js +++ b/components/rsvp-reader.js @@ -0,0 +1,76 @@ +import './rsvp-word.js' +import './rsvp-controls.js' + +import { Chapter } from '../src/Chapter.js' +import { Player } from '../src/Player.js' + +class RSVPReader extends HTMLElement { + constructor() { + super() + const shadow = this.attachShadow({ mode: 'open' }) + const word = document.createElement('rsvp-word') + const controls = document.createElement('rsvp-controls') + + shadow.appendChild(word) + shadow.appendChild(controls) + + this._root = shadow + this._rsvpWord = word + this._rsvpControls = controls + + this.player = new Player() + this.player.subscribe('main', this.tick.bind(this)) + this.setText('Sample Text. Easy there.') + + this._rsvpControls.addEventListener( + 'control-event', + this.handleControlEvent.bind(this) + ) + } + + setText(text) { + this.chapter = new Chapter(text, 10) + this.updateChildrenUI() + } + + tick() { + if (!this.chapter.hasNext()) { + this.player.stop() + } else { + this.chapter.next() + } + this.updateChildrenUI() + } + + updateChildrenUI() { + this._rsvpWord.setAttribute('word', this.chapter.currentSegment) + this._rsvpControls.updateUI(this.chapter, this.player) + } + + handleControlEvent(e) { + switch (e.detail.action) { + case 'prevSentence': + this.chapter.prevSentence() + break + case 'nextSentence': + this.chapter.nextSentence() + break + case 'prevWord': + this.chapter.prevWord() + break + case 'nextWord': + this.chapter.nextWord() + break + case 'playpause': + this.player.toggle() + break + case 'stop': + this.player.stop() + this.chapter.reset() + break + } + this.updateChildrenUI() + } +} + +window.customElements.define('rsvp-reader', RSVPReader) diff --git a/index.html b/index.html index 7d5d04e..b38c1e6 100644 --- a/index.html +++ b/index.html @@ -1,47 +1,24 @@ -
+ +