Skip to content

Commit

Permalink
Added Cuneiform Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Vas9ka committed Jun 12, 2024
1 parent 967872b commit ef9a75e
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
textarea.form-control.assurbanipal
font-size: 150%
font-family: Assurbanipal, Junicode, serif !important
textarea.form-control.neo-babylonian
font-size: 150%
font-family: Neo-Babylonian,'Adobe Blank', Junicode, serif !important
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React from 'react'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import CuneiformConverterForm from 'chronology/ui/CuneiformConverter/CuneiformConverterForm'
import SignService from 'signs/application/SignService'

jest.mock('signs/application/SignService')
const signServiceMock = new (SignService as jest.Mock<
jest.Mocked<SignService>
>)()

describe('CuneiformConverterForm', () => {
beforeEach(() => {
Object.defineProperty(window.navigator, 'clipboard', {
value: {
writeText: jest.fn().mockResolvedValue(true),
},
writable: true,
})
})

afterEach(() => {
jest.resetAllMocks()
})

it('renders form, options, and scenario panel correctly', () => {
render(<CuneiformConverterForm signService={signServiceMock} />)
expect(screen.getByLabelText('Text to Convert')).toBeInTheDocument()
expect(screen.getByLabelText('Select Font')).toBeInTheDocument()
expect(screen.getByLabelText('Text to Convert')).toBeInTheDocument()
})
it('handles input change', () => {
render(<CuneiformConverterForm signService={signServiceMock} />)
const inputTextArea = screen.getByLabelText('Text to Convert')
fireEvent.change(inputTextArea, { target: { value: 'test text' } })
expect(inputTextArea).toHaveValue('test text')
})
it('handles font change', () => {
render(<CuneiformConverterForm signService={signServiceMock} />)
const fontSelector = screen.getByLabelText('Select Font')
fireEvent.change(fontSelector, { target: { value: 'Neo-Babylonian' } })
expect(fontSelector).toHaveValue('Neo-Babylonian')
})
it('converts text correctly', async () => {
signServiceMock.getUnicodeFromAtf.mockResolvedValueOnce([
{ unicode: [73979] },
])
render(<CuneiformConverterForm signService={signServiceMock} />)

const inputTextArea = screen.getByLabelText('Text to Convert')
fireEvent.change(inputTextArea, { target: { value: 'test text' } })

const convertButton = screen.getByText('Convert')
fireEvent.click(convertButton)

await waitFor(() => {
expect(screen.getByLabelText('Converted Text')).toHaveValue('𒃻')
})
})
})
106 changes: 106 additions & 0 deletions src/chronology/ui/CuneiformConverter/CuneiformConverterForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React, { useState } from 'react'
import { Form, Button } from 'react-bootstrap'
import Bluebird from 'bluebird'
import SignService from 'signs/application/SignService'
import replaceTransliteration from 'fragmentarium/domain/replaceTransliteration'
import { displayUnicode } from 'signs/ui/search/SignsSearch'
import './CuneiformConverterForm.sass'

function CuneiformConverterForm({
signService,
}: {
signService: SignService
}): JSX.Element {
const [content, setContent] = useState('')
const [convertedContent, setConvertedContent] = useState('')
const [selectedFont, setSelectedFont] = useState('Assurbanipal')

const handleChange = (event) => {
setContent(event.target.value)
}

const handleConvert = () => {
const lines = content.split('\n')
const replacedLines = lines.map((line) => replaceTransliteration(line))

Promise.all(
replacedLines
.filter((line) => line.trim() !== '')
.map((line) => query(line))
)
.then((results) => {
const convertedText = results
.map((result) =>
result
.map((entry) =>
entry.unicode[0] === 9999 ? ' ' : displayUnicode(entry.unicode)
)
.join('')
)
.join('\n')

setConvertedContent(convertedText)
})
.catch((error) => {
console.error('Query Error:', error)
})
}

const query = (content) => {
return Bluebird.resolve(signService.getUnicodeFromAtf(content))
}

const handleKeyDown = (event) => {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault()
handleConvert()
}
}

const handleFontChange = (event) => {
setSelectedFont(event.target.value)
}

return (
<>
<Form.Label htmlFor="inputText">Text to Convert</Form.Label>
<Form.Control
as="textarea"
id="inputText"
aria-describedby="textHelpBlock"
value={content}
onChange={handleChange}
onKeyDown={handleKeyDown}
/>
<Form.Text id="textHelpBlock" muted>
Enter the text you want to convert to Unicode.
</Form.Text>
<Form.Label htmlFor="fontSelector">Select Font</Form.Label>
<select
id="fontSelector"
className="form-select"
value={selectedFont}
onChange={handleFontChange}
>
<option value="Assurbanipal">Neo-Assyrian</option>
<option value="Neo-Babylonian">Neo-Babylonian</option>
</select>
<Button onClick={handleConvert}>Convert</Button>
<br></br>
<Form.Label htmlFor="outputText">Converted Text</Form.Label>
<Form.Control
as="textarea"
id="outputText"
className={`${selectedFont.toLowerCase().replace(/\s/g, '-')}`}
aria-describedby="outputHelpBlock"
value={convertedContent}
readOnly
/>
<Form.Text id="outputHelpBlock" muted>
This is the converted Unicode text.
</Form.Text>
</>
)
}

export default CuneiformConverterForm
16 changes: 15 additions & 1 deletion src/router/toolsRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,23 @@ import ListOfKings from 'chronology/ui/Kings/BrinkmanKingsTable'
import _ from 'lodash'
import 'about/ui/about.sass'
import NotFoundPage from 'NotFoundPage'
import CuneiformConverterForm from 'chronology/ui/CuneiformConverter/CuneiformConverterForm'
import SignService from 'signs/application/SignService'

const tabIds = ['date-converter', 'list-of-kings'] as const
const tabIds = [
'date-converter',
'list-of-kings',
'cuneiform-converter',
] as const
type TabId = typeof tabIds[number]

const Tools = ({
markupService,
signService,
activeTab,
}: {
markupService: MarkupService
signService: SignService
activeTab: TabId
}): JSX.Element => {
const history = useHistory()
Expand Down Expand Up @@ -57,16 +65,21 @@ const Tools = ({
<Tab eventKey="list-of-kings" title="List of kings">
{ListOfKings()}
</Tab>
<Tab eventKey="cuneiform-converter" title="Cuneiform converter">
<CuneiformConverterForm signService={signService} />
</Tab>
</Tabs>
</AppContent>
)
}

export default function ToolsRoutes({
sitemap,
signService,
markupService,
}: {
sitemap: boolean
signService: SignService
markupService: MarkupService
}): JSX.Element[] {
return [
Expand All @@ -81,6 +94,7 @@ export default function ToolsRoutes({
>
<Tools
markupService={markupService}
signService={signService}
activeTab={props.match.params.id as TabId}
/>
</HeadTagsService>
Expand Down
5 changes: 4 additions & 1 deletion src/signs/application/SignService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Bluebird from 'bluebird'
import Sign, { OrderedSign, SignQuery } from 'signs/domain/Sign'
import Sign, { OrderedSign, SignQuery, UnicodeAtf } from 'signs/domain/Sign'
import SignRepository from 'signs/infrastructure/SignRepository'
import { AnnotationToken } from 'fragmentarium/domain/annotation-token'

Expand Down Expand Up @@ -39,4 +39,7 @@ export default class SignService {
): Bluebird<[OrderedSign[]]> {
return this.signsRepository.findSignsByOrder(signName, sortEra)
}
getUnicodeFromAtf(text: string): Bluebird<UnicodeAtf[]> {
return this.signsRepository.getUnicodeFromAtf(text)
}
}
3 changes: 3 additions & 0 deletions src/signs/domain/Sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export interface OrderedSign {
readonly unicode: readonly number[]
readonly mzl: string | null
}
export interface UnicodeAtf {
readonly unicode: number[]
}
export interface Fossey {
page: number
number: number
Expand Down
7 changes: 6 additions & 1 deletion src/signs/infrastructure/SignRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import ApiClient from 'http/ApiClient'
import Promise from 'bluebird'
import Sign, { OrderedSign, SignQuery } from 'signs/domain/Sign'
import Sign, { OrderedSign, SignQuery, UnicodeAtf } from 'signs/domain/Sign'
import { stringify } from 'query-string'
import { AnnotationToken } from 'fragmentarium/domain/annotation-token'
import { AnnotationTokenType } from 'fragmentarium/domain/annotation'
Expand Down Expand Up @@ -89,6 +89,11 @@ class SignRepository {
false
)
}
getUnicodeFromAtf(text: string): Promise<UnicodeAtf[]> {
return this.apiClient.fetchJson(
`/signs/transliteration/${encodeURIComponent(text)}`
)
}
}

export default SignRepository
2 changes: 1 addition & 1 deletion src/signs/ui/search/SignsSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function sortSigns(signs: Sign[]): Sign[] {
)
}

function displayUnicode(unicode: readonly number[]): string {
export function displayUnicode(unicode: readonly number[]): string {
return unicode.map((unicode) => String.fromCodePoint(unicode)).join('')
}

Expand Down

0 comments on commit ef9a75e

Please sign in to comment.