Add paginated outputText

This commit is contained in:
Alfred Melch 2019-12-22 19:24:29 +01:00
parent 797ef6a1fa
commit f87acd5514
5 changed files with 72 additions and 28 deletions

View File

@ -30,7 +30,9 @@ export const RsvpReader = () => {
<Options></Options>
<TotalTime />
</div>
<div className={styles.item}>{/* <TextOutput /> */}</div>
<div className={styles.item}>
<TextOutput />
</div>
</div>
)
}

View File

@ -1,5 +1,5 @@
.current {
text-decoration: underline;
background-color: yellow;
}
.sentenceBeginning {

View File

@ -5,38 +5,48 @@ import classNames from 'classnames'
import { getNextSmallerNumber } from '../lib/array-util.js'
import {
selectParsedText,
selectCurrentSegmentIndex
selectCurrentSegmentIndex,
selectDisplayMode,
selectSegmentWindow
} from '../store/selectors.js'
import styles from './TextOutput.css'
export const TextOutput = () => {
const { segments, sentences, words } = useSelector(selectParsedText)
const { segments, offset } = useSelector(selectSegmentWindow)
const { sentences, words } = useSelector(selectParsedText)
const curSegment = useSelector(selectCurrentSegmentIndex)
const mode = useSelector(selectDisplayMode)
return (
<div>
{segments.map((segment, idx) => {
const isCurrent = curSegment === idx
const isWordStart = words.includes(idx)
const wordBeginning = isWordStart
? idx
: getNextSmallerNumber(idx, words)
const isSentenceStart = sentences.includes(wordBeginning)
<>
<div>
{mode}, {offset}
</div>
<div>
{segments.map((segment, idx) => {
idx = idx + offset
const isCurrent = curSegment === idx
const isWordStart = words.includes(idx)
const wordBeginning = isWordStart
? idx
: getNextSmallerNumber(idx, words)
const isSentenceStart = sentences.includes(wordBeginning)
return (
<span key={idx}>
{isWordStart && ' '}
<span
className={classNames({
[styles.current]: isCurrent,
[styles.sentenceBeginning]: isSentenceStart
})}
>
{segment}
return (
<span key={idx}>
{isWordStart && ' '}
<span
className={classNames({
[styles.current]: isCurrent,
[styles.sentenceBeginning]: isSentenceStart
})}
>
{segment}
</span>
</span>
</span>
)
})}
</div>
)
})}
</div>
</>
)
}

View File

@ -15,7 +15,15 @@ export const initialState = {
wpm: 300,
offset: -15,
running: false,
lang: 'en'
lang: 'en',
textDisplay: {
mode: 'pagination',
options: {
pagination: { pageLength: 50 },
segments: { prev: 9, next: 40 },
sentences: { prev: 1, next: 8 }
}
}
}
const reducer = {

View File

@ -11,9 +11,10 @@ const notNull = val => val != null
// parsedText selectors
export const selectMaxLength = state => state.maxLength
export const selectOriginalText = state => state.originalText
export const selectParsedText = createSelector(
state => state.originalText,
selectOriginalText,
selectMaxLength,
(originalText, maxLength) => parseText(originalText, maxLength)
)
@ -166,3 +167,26 @@ export const selectTotalTime = createSelector(selectIntervals, intervals =>
// player
export const selectRunning = state => state.running
// text display
export const selectDisplayMode = state => state.textDisplay.mode
export const selectDisplayOptions = state => state.textDisplay.options
export const selectCurrentDisplayOptions = createSelector(
selectDisplayMode,
selectDisplayOptions,
(mode, options) => options[mode]
)
export const selectSegmentWindow = createSelector(
selectDisplayMode,
selectCurrentDisplayOptions,
selectCurrentSegmentIndex,
selectSegments,
(mode, options, idx, segments) => {
const page = Math.floor(idx / options.pageLength)
const start = page * options.pageLength
const end = page * options.pageLength + options.pageLength
return { offset: start, segments: segments.slice(start, end) }
}
)