From f87acd55143c2e82421725eab2c8e90f26ccd4a2 Mon Sep 17 00:00:00 2001 From: Alfred Melch Date: Sun, 22 Dec 2019 19:24:29 +0100 Subject: [PATCH] Add paginated outputText --- src/components/RsvpReader.js | 4 ++- src/components/TextOutput.css | 2 +- src/components/TextOutput.js | 58 ++++++++++++++++++++--------------- src/store/reducer.js | 10 +++++- src/store/selectors.js | 26 +++++++++++++++- 5 files changed, 72 insertions(+), 28 deletions(-) diff --git a/src/components/RsvpReader.js b/src/components/RsvpReader.js index da981f3..28694e6 100644 --- a/src/components/RsvpReader.js +++ b/src/components/RsvpReader.js @@ -30,7 +30,9 @@ export const RsvpReader = () => { -
{/* */}
+
+ +
) } diff --git a/src/components/TextOutput.css b/src/components/TextOutput.css index f483f55..dbc42f8 100644 --- a/src/components/TextOutput.css +++ b/src/components/TextOutput.css @@ -1,5 +1,5 @@ .current { - text-decoration: underline; + background-color: yellow; } .sentenceBeginning { diff --git a/src/components/TextOutput.js b/src/components/TextOutput.js index fcf6779..94d4d06 100644 --- a/src/components/TextOutput.js +++ b/src/components/TextOutput.js @@ -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 ( -
- {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) + <> +
+ {mode}, {offset} +
+
+ {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 ( - - {isWordStart && ' '} - - {segment} + return ( + + {isWordStart && ' '} + + {segment} + - - ) - })} -
+ ) + })} +
+ ) } diff --git a/src/store/reducer.js b/src/store/reducer.js index f913c0e..f2d9bab 100644 --- a/src/store/reducer.js +++ b/src/store/reducer.js @@ -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 = { diff --git a/src/store/selectors.js b/src/store/selectors.js index cb8fc89..92d560d 100644 --- a/src/store/selectors.js +++ b/src/store/selectors.js @@ -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) } + } +)