rsvp-reader/components/rsvp-word.js

54 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-10-18 06:51:50 +02:00
import { pivotize } from '../src/textProcessing/pivotize.js'
2019-12-08 09:16:12 +01:00
import { RSVPComponent } from './rsvp-component.js'
2019-10-18 06:51:50 +02:00
2019-12-08 09:16:12 +01:00
class RSVPWord extends RSVPComponent {
2019-10-18 06:51:50 +02:00
constructor() {
super()
const style = document.createElement('style')
const word = document.createElement('div')
const prefix = document.createElement('span')
const pivot = document.createElement('span')
const suffix = document.createElement('span')
word.setAttribute('class', 'word')
prefix.setAttribute('class', 'prefix')
pivot.setAttribute('class', 'pivot')
suffix.setAttribute('class', 'suffix')
style.textContent =
2019-10-18 16:07:38 +02:00
'.word{display:flex; font-size: 2.4em;}.pivot{color:red}.prefix{flex:1}.suffix{flex:1}.prefix{text-align:right}'
2019-10-18 06:51:50 +02:00
word.appendChild(prefix)
word.appendChild(pivot)
word.appendChild(suffix)
2019-12-08 09:16:12 +01:00
this._root.appendChild(style)
this._root.appendChild(word)
2019-10-18 06:51:50 +02:00
this.wordParts = { prefix, pivot, suffix }
}
2019-12-08 09:16:12 +01:00
update({ chapter }) {
const [prefix, pivot, suffix] = pivotize(chapter.currentSegment)
this.wordParts.prefix.innerText = prefix
this.wordParts.pivot.innerText = pivot
this.wordParts.suffix.innerText = suffix
2019-10-18 06:51:50 +02:00
}
static get observedAttributes() {
return ['word']
}
attributeChangedCallback() {
this.updateDisplay()
}
updateDisplay() {
const [prefix, pivot, suffix] = pivotize(this.getAttribute('word') || '')
this.wordParts.prefix.innerText = prefix
this.wordParts.pivot.innerText = pivot
this.wordParts.suffix.innerText = suffix
}
}
window.customElements.define('rsvp-word', RSVPWord)