From a45b08f881826798237e1e73713a85bf84d1038a Mon Sep 17 00:00:00 2001 From: fsimonjetz Date: Wed, 31 Jul 2024 13:25:46 +0200 Subject: [PATCH] Refactor LineAccumulator (#501) * move isInLineGroup to class scope * move showMeter to class scope * use showMeter class attribute * move showIpa to class scope --- src/transliteration/domain/columns.ts | 5 +--- src/transliteration/ui/LineAccumulator.tsx | 31 +++++++++------------- src/transliteration/ui/line-tokens.tsx | 3 --- 3 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/transliteration/domain/columns.ts b/src/transliteration/domain/columns.ts index 263811ddc..9ada39220 100644 --- a/src/transliteration/domain/columns.ts +++ b/src/transliteration/domain/columns.ts @@ -59,9 +59,6 @@ export function lineAccFromColumns({ acc.addColumnToken( token, index, - isInLineGroup, - showMeter, - showIpa, updatePhoneticPropsContext(column.content, index, phoneticProps), _.isEmpty(_.intersection(token.uniqueLemma, highlightLemmas)) ? [] @@ -72,7 +69,7 @@ export function lineAccFromColumns({ acc ) return acc - }, new LineAccumulator()) + }, new LineAccumulator(isInLineGroup, showMeter, showIpa)) } export function numberOfColumns(columns: readonly TextLineColumn[]): number { diff --git a/src/transliteration/ui/LineAccumulator.tsx b/src/transliteration/ui/LineAccumulator.tsx index 10a16b6ec..2a27b567d 100644 --- a/src/transliteration/ui/LineAccumulator.tsx +++ b/src/transliteration/ui/LineAccumulator.tsx @@ -56,8 +56,17 @@ export class LineAccumulator { private enclosureOpened = false private protocol: Protocol | null = null private isFirstWord = true + private isInLineGroup = false + private showMeter = false + private showIpa = false lemmas: string[] = [] + constructor(isInLineGroup?: boolean, showMeter?: boolean, showIpa?: boolean) { + this.isInLineGroup = isInLineGroup || false + this.showMeter = showMeter || false + this.showIpa = showIpa || false + } + getColumns(maxColumns: number): React.ReactNode[] { return this.columns.map((column: ColumnData, index: number) => ( @@ -87,9 +96,6 @@ export class LineAccumulator { pushToken( token: Token, index: number, - isInLineGroup = false, - showMeter = false, - showIpa = false, phoneticProps?: PhoneticProps, bemModifiers: string[] = [] ): void { @@ -100,7 +106,7 @@ export class LineAccumulator { this.pushSeparator() } - const DisplayTokenComponent = isInLineGroup + const DisplayTokenComponent = this.isInLineGroup ? DisplayLineGroupToken : DisplayToken @@ -110,8 +116,8 @@ export class LineAccumulator { token={token} bemModifiers={[...this.bemModifiers, ...bemModifiers]} Wrapper={this.inGloss && !isEnclosure(token) ? GlossWrapper : undefined} - showMeter={showMeter} - showIpa={showIpa} + showMeter={this.showMeter} + showIpa={this.showIpa} phoneticProps={phoneticProps} /> ) @@ -139,9 +145,6 @@ export class LineAccumulator { addColumnToken( token: Token, index: number, - isInLineGroup?: boolean, - showMeter?: boolean, - showIpa?: boolean, phoneticProps?: PhoneticProps, bemModifiers: string[] = [] ): void { @@ -158,15 +161,7 @@ export class LineAccumulator { case 'Column': throw new Error('Unexpected column token.') default: - this.pushToken( - token, - index, - isInLineGroup, - showMeter, - showIpa, - phoneticProps, - bemModifiers - ) + this.pushToken(token, index, phoneticProps, bemModifiers) this.pushLemma(token.uniqueLemma) this.isFirstWord = false } diff --git a/src/transliteration/ui/line-tokens.tsx b/src/transliteration/ui/line-tokens.tsx index 2699c216d..9a5e37f18 100644 --- a/src/transliteration/ui/line-tokens.tsx +++ b/src/transliteration/ui/line-tokens.tsx @@ -28,9 +28,6 @@ export function LineTokens({ acc.addColumnToken( token, index, - false, - false, - false, {}, highlightTokens.includes(index) ? ['highlight'] : [] )