-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tabular annotation tool prototype
- Loading branch information
1 parent
696a10e
commit 997ea7d
Showing
3 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/fragmentarium/ui/fragment/TabularAnnotationTool.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { Fragment } from 'fragmentarium/domain/fragment' | ||
import React, { Component } from 'react' | ||
import { isTextLine } from 'transliteration/domain/type-guards' | ||
// import DisplayToken from 'transliteration/ui/DisplayToken' | ||
import { Token } from 'transliteration/domain/token' | ||
import _ from 'lodash' | ||
import { TextLine } from 'transliteration/domain/text-line' | ||
import { Table } from 'react-bootstrap' | ||
import lineNumberToString from 'transliteration/domain/lineNumberToString' | ||
import './TokenAnnotationTool.sass' | ||
import DisplayToken from 'transliteration/ui/DisplayToken' | ||
import { LineNumber, LineNumberRange } from 'transliteration/domain/line-number' | ||
import { LemmaSearchForm } from '../LemmaSearchForm' | ||
import WordService from 'dictionary/application/WordService' | ||
|
||
type Props = { | ||
fragment: Fragment | ||
wordService: WordService | ||
onSave(fragment: Fragment): void | ||
} | ||
|
||
type AnnotationRow = { | ||
lineNumber: LineNumber | LineNumberRange | ||
lineIndex: number | ||
token: Token | ||
newUniqueLemma: string[] | ||
tokenIndex: number | ||
uniqueId: string | ||
} | ||
|
||
type AnnotationTable = AnnotationRow[] | ||
|
||
export default class TokenAnnotationTool extends Component<Props, unknown> { | ||
private annotationTable: AnnotationTable | ||
fragment: Fragment | ||
|
||
constructor(props: Props) { | ||
super(props) | ||
this.fragment = props.fragment | ||
this.annotationTable = this.createAnnotationTable() | ||
} | ||
|
||
createAnnotationTable(): AnnotationTable { | ||
const lines = this.props.fragment.text.allLines | ||
|
||
return lines | ||
.map((line, lineIndex) => ({ line, lineIndex })) | ||
.filter((indexedLine) => isTextLine(indexedLine.line)) | ||
.flatMap((indexedLine) => { | ||
const line = indexedLine.line as TextLine | ||
return line.content.map((token, tokenIndex) => ({ | ||
lineNumber: line.lineNumber, | ||
token, | ||
tokenIndex, | ||
uniqueId: _.uniqueId(), | ||
lineIndex: indexedLine.lineIndex, | ||
newUniqueLemma: [], | ||
})) | ||
}) | ||
} | ||
|
||
LemmaEditor({ | ||
row, | ||
wordService, | ||
}: { | ||
row: AnnotationRow | ||
wordService: WordService | ||
}): JSX.Element { | ||
const lemmas = row.token.uniqueLemma || [] | ||
return !row.token.lemmatizable ? ( | ||
<></> | ||
) : ( | ||
<LemmaSearchForm | ||
wordService={wordService} | ||
lemmas={lemmas.join('+') || ''} | ||
onChange={() => () => console.log('something')} | ||
placeholder="Add lemma..." | ||
/> | ||
) | ||
} | ||
|
||
render(): JSX.Element { | ||
let lastLineNumber = '' | ||
return ( | ||
<Table | ||
bordered | ||
size={'sm'} | ||
className={'annotation-tool__table-annotator'} | ||
> | ||
<thead> | ||
<tr> | ||
<td>Line</td> | ||
<td>Token</td> | ||
<td>Lemma</td> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{this.annotationTable.map((row, index) => { | ||
const lineNumber = lineNumberToString(row.lineNumber) | ||
const displayRow = ( | ||
<tr key={index}> | ||
<td> | ||
{lineNumber !== lastLineNumber && | ||
`(${lineNumberToString(row.lineNumber)})`} | ||
</td> | ||
<td> | ||
<DisplayToken token={row.token} isInPopover={true} /> | ||
</td> | ||
<td> | ||
<this.LemmaEditor | ||
row={row} | ||
wordService={this.props.wordService} | ||
/> | ||
</td> | ||
</tr> | ||
) | ||
lastLineNumber = lineNumber | ||
return displayRow | ||
})} | ||
</tbody> | ||
</Table> | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters