-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsegment.tsx
More file actions
340 lines (312 loc) · 9.01 KB
/
segment.tsx
File metadata and controls
340 lines (312 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import { Tooltip } from "@reach/tooltip"
import "@reach/tooltip/styles.css"
import cx from "classnames"
import React from "react"
import { MdCircle, MdInfoOutline } from "react-icons/md/index"
import * as Dailp from "src/graphql/dailp"
import { DocumentContents } from "src/pages/documents/document.page"
import { std } from "src/style/utils.css"
import { CleanButton } from "./components"
import { useForm } from "./edit-word-form-context"
import { Environment, deploymentEnvironment } from "./env"
import { PanelDetails } from "./panel-layout"
import * as css from "./segment.css"
import { BasicMorphemeSegment, LevelOfDetail } from "./types"
type TranslatedPage = NonNullable<DocumentContents["translatedPages"]>[0]
export type TranslatedParagraph = TranslatedPage["paragraphs"][0]
type Segment = TranslatedParagraph["source"][0]
interface Props {
segment: Segment
onOpenDetails: (morpheme: BasicMorphemeSegment) => void
levelOfDetail: LevelOfDetail
cherokeeRepresentation: Dailp.CherokeeOrthography
pageImages: readonly string[]
wordPanelDetails: PanelDetails
}
export const DocumentPage = (
p: Omit<Props, "segment"> & { segment: TranslatedPage }
) => (
<>
{p.segment.pageNumber !== "1" ? (
<div
id={`document-page-${p.segment.pageNumber}`}
className={css.pageBreak}
aria-label={`Start of page ${p.segment.pageNumber}`}
>
Page {p.segment.pageNumber}
</div>
) : null}
{p.segment.paragraphs.map((paragraph, i) => (
<DocumentParagraph key={i} {...p} segment={paragraph} />
))}
</>
)
export const DocumentParagraph = (
p: Omit<Props, "segment"> & { segment: TranslatedParagraph }
) => {
const children =
p.segment.source?.map(function (seg, i) {
return (
<Segment
key={i}
segment={seg as Segment}
onOpenDetails={p.onOpenDetails}
levelOfDetail={p.levelOfDetail}
cherokeeRepresentation={p.cherokeeRepresentation}
pageImages={p.pageImages}
wordPanelDetails={p.wordPanelDetails}
/>
)
}) ?? null
const blockStyle =
p.levelOfDetail > LevelOfDetail.Story
? css.documentBlock.wordByWord
: css.documentBlock.story
const annotationStyle =
p.levelOfDetail > LevelOfDetail.Pronunciation
? css.annotationSection.wordParts
: css.annotationSection.story
let isSelected = false
if (p.wordPanelDetails.currContents?.__typename === "DocumentParagraph") {
isSelected = p.wordPanelDetails.currContents.source === p.segment.source
}
const detailButton = (
<CleanButton
title="View paragraph details"
onClick={() => {
p.wordPanelDetails.setCurrContents(p.segment)
}}
>
{isSelected ? (
<MdCircle size={20} className={css.linkSvg} />
) : (
<MdInfoOutline size={20} className={css.linkSvg} />
)}
</CleanButton>
)
let wordCSS = css.wordGroup
if (isSelected) {
wordCSS = cx(wordCSS, css.selectedWord)
}
return (
<section className={blockStyle}>
<div className={wordCSS}>
<div className={annotationStyle}>
{children}
{deploymentEnvironment !== Environment.Production &&
p.levelOfDetail === LevelOfDetail.Story &&
detailButton}
</div>
<p className={css.inlineBlock}>
{p.segment.translation ?? null}
{deploymentEnvironment !== Environment.Production &&
p.levelOfDetail >= LevelOfDetail.Pronunciation &&
detailButton}
</p>
{/*<SegmentAudio/>*/}
</div>
</section>
)
}
/** Displays one segment of the document, which may be a word, block, or phrase. */
export const Segment = (p: Props) => {
const segment = p.segment
if (segment.__typename === "AnnotatedForm") {
return <AnnotatedForm {...p} segment={segment} />
} else {
return null
}
}
export const AnnotatedForm = (
p: Props & {
segment: Dailp.FormFieldsFragment
}
) => {
if (!p.segment.source) {
return null
}
const { form } = useForm()
const showAnything = p.levelOfDetail > LevelOfDetail.Story
if (showAnything) {
const showSegments = p.levelOfDetail >= LevelOfDetail.Segmentation
const translation = p.segment.englishGloss.join(", ")
const isSelected =
(p.wordPanelDetails.currContents as Dailp.FormFieldsFragment)?.id ===
p.segment.id
let wordCSS = showSegments ? css.wordGroup : css.wordGroupInline
if (isSelected) {
wordCSS = cx(wordCSS, css.selectedWord)
p.wordPanelDetails.setCurrContents(
p.segment
) /* This makes sure the word panel updates for changes to the word panel*/
}
return (
<div className={wordCSS} id={`w${p.segment.index}`}>
<div className={css.syllabaryLayer} lang="chr">
{p.segment.source}
<CleanButton
title={`View word details for '${p.segment.source}'`}
onClick={() => {
p.wordPanelDetails.setCurrContents(p.segment)
form.update("word", p.segment)
}}
>
{isSelected ? (
<MdCircle size={20} className={css.linkSvg} />
) : (
<MdInfoOutline size={20} className={css.linkSvg} />
)}
</CleanButton>
</div>
{p.segment.romanizedSource ? (
<>
<div>{p.segment.romanizedSource}</div>
<div>
{p.segment.phonemic &&
p.levelOfDetail >= LevelOfDetail.Pronunciation && <div />}
</div>
</>
) : (
<FillerLine />
)}
{showSegments ? (
<MorphemicSegmentation
segments={p.segment.segments}
onOpenDetails={p.onOpenDetails}
levelOfDetail={p.levelOfDetail}
cherokeeRepresentation={p.cherokeeRepresentation}
/>
) : null}
{translation.length ? (
<div>‘{translation}’</div>
) : (
<FillerLine />
)}
</div>
)
} else {
return (
<>
<span
className={css.plainSyllabary}
id={`w${p.segment.index}`}
lang="chr"
>
{p.segment.source}
</span>{" "}
</>
)
}
}
const WithTooltip = (p: {
hint: string
children: any
"aria-label"?: string
}) => (
<Tooltip
className={std.tooltip}
label={p.hint || ""}
aria-label={p["aria-label"]}
>
{p.children}
</Tooltip>
)
const FillerLine = () => (
<div className={css.lineBox}>
<hr className={css.fillerLine} />
</div>
)
/**
* Displays the break-down of a word into its units of meaning and the English
* glosses for each morpheme.
*/
export const MorphemicSegmentation = (p: {
segments: Dailp.FormFieldsFragment["segments"]
cherokeeRepresentation: Dailp.CherokeeOrthography
onOpenDetails: Props["onOpenDetails"]
levelOfDetail: LevelOfDetail
}) => {
// If there is no segmentation, return a hard break for the
// morphemic segmentation and morpheme gloss layers.
if (!p.segments?.length) {
return (
<>
<FillerLine />
<FillerLine />
</>
)
}
// Adapt the segment shape to the chosen experience level.
let segmentation = ""
for (const seg of p.segments) {
if (
segmentation.length > 0 &&
seg.role &&
seg.role !== Dailp.WordSegmentRole.Modifier
) {
segmentation += seg.previousSeparator
}
segmentation += seg.morpheme
}
return (
<>
<i>{segmentation}</i>
<div>
{p.segments.map(function (segment, i) {
return (
<React.Fragment key={i}>
{i > 0 && segment.previousSeparator}
<MorphemeSegment
segment={segment}
cherokeeRepresentation={p.cherokeeRepresentation}
onOpenDetails={p.onOpenDetails}
/>
</React.Fragment>
)
})}
</div>
</>
)
}
/** One morpheme that can be clicked to see further details. */
const MorphemeSegment = (p: {
segment: BasicMorphemeSegment
cherokeeRepresentation: Dailp.CherokeeOrthography
onOpenDetails: Props["onOpenDetails"]
}) => {
const matchingTag = p.segment.matchingTag
const gloss = matchingTag?.tag || p.segment.gloss
// Display functional tags in small-caps, per interlinear typesetting practice.
if (matchingTag && matchingTag.title) {
return (
<WithTooltip hint={matchingTag.title}>
<button
className={css.morphemeButton.functional}
onClick={() => p.onOpenDetails(p.segment)}
>
{gloss}
</button>
</WithTooltip>
)
} else if (gloss === "?") {
return (
<WithTooltip hint="Unanalyzed or unfamiliar segment">
<button
className={css.morphemeButton.lexical}
onClick={() => p.onOpenDetails(p.segment)}
>
{gloss}
</button>
</WithTooltip>
)
} else {
return (
<button
className={css.morphemeButton.lexical}
onClick={() => p.onOpenDetails(p.segment)}
>
{gloss}
</button>
)
}
}