Skip to content

Commit

Permalink
Add equation keyboard under input
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Gill97 committed Jan 9, 2024
1 parent 36e440f commit c40a5e7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/components/InputProblem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,14 @@ export const InputProblem = ({
{
comparator.toLowerCase() === 'math'
? (
<div className='os-mathfield-container'>

Check warning on line 190 in src/components/InputProblem.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/InputProblem.tsx#L190

Added line #L190 was not covered by tests
<Field
name="response"
disabled={inputDisabled || isSubmitting}
as={Mathfield}
onInput={(e: React.ChangeEvent<MathfieldElement>): void => { clearFeedback(); void setFieldValue('response', e.target.value) }}
className={buildClassName(userResponseCorrect, inputDisabled || isSubmitting, errors.response)} />
</div>
)
: (
<Field
Expand Down
27 changes: 23 additions & 4 deletions src/components/Mathfield.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,38 @@ interface MathfieldProps {

export const Mathfield = ({ className, disabled, onInput }: MathfieldProps): JSX.Element => {
const mathfieldRef = useRef<MathfieldElement>(null)
const mathKeyboardRef = useRef<HTMLDivElement>(null)

Check warning on line 23 in src/components/Mathfield.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Mathfield.tsx#L23

Added line #L23 was not covered by tests

useEffect(() => {
const mathFieldCurrent = mathfieldRef.current
if ((mathFieldCurrent === null)) {
const maybeMathKeyboard = mathKeyboardRef.current

Check warning on line 27 in src/components/Mathfield.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Mathfield.tsx#L27

Added line #L27 was not covered by tests

if ((mathFieldCurrent === null) || (maybeMathKeyboard === null)) {
return
}

mathFieldCurrent.mathVirtualKeyboardPolicy = 'manual'
mathFieldCurrent.minFontScale = 0.7
mathFieldCurrent.addEventListener('focusin', (ev) => {
window.mathVirtualKeyboard.container = maybeMathKeyboard
const r = mathFieldCurrent.getBoundingClientRect()
maybeMathKeyboard.style.display = 'block'
const w = maybeMathKeyboard.offsetWidth
maybeMathKeyboard.style.top = `${r.bottom + 16}px`
maybeMathKeyboard.style.left = `${r.left + r.width / 2 - w / 2}px`
maybeMathKeyboard.style.height = '300px'
maybeMathKeyboard.style.width = '100%'

Check warning on line 43 in src/components/Mathfield.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Mathfield.tsx#L36-L43

Added lines #L36 - L43 were not covered by tests
window.mathVirtualKeyboard.show()
})

mathFieldCurrent.addEventListener('focusout', (ev) => {
window.mathVirtualKeyboard.hide()
})
const handleFocusOut = (ev: FocusEvent): void => {

Check warning on line 47 in src/components/Mathfield.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Mathfield.tsx#L47

Added line #L47 was not covered by tests
if (ev.relatedTarget === null) {
maybeMathKeyboard.style.display = 'none'
window.mathVirtualKeyboard.hide()

Check warning on line 50 in src/components/Mathfield.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Mathfield.tsx#L49-L50

Added lines #L49 - L50 were not covered by tests
}
}

mathFieldCurrent.addEventListener('focusout', handleFocusOut)

Check warning on line 54 in src/components/Mathfield.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Mathfield.tsx#L54

Added line #L54 was not covered by tests
}, [])

useEffect(() => {
Expand Down Expand Up @@ -65,6 +83,7 @@ export const Mathfield = ({ className, disabled, onInput }: MathfieldProps): JSX
onInput={onInput}
tabIndex={disabled ? '-1' : '0'}
/>
<div ref={mathKeyboardRef}></div>
</>
)
}
5 changes: 5 additions & 0 deletions src/styles/content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,8 @@ $arrow-svg: url("data:image/svg+xml,%3Csvg width='16' height='9' viewBox='0 0 16
font-weight: 700;
font-size: 1.25rem;
}

.os-mathfield-container {
flex-direction: column;
width: 100%;
}

0 comments on commit c40a5e7

Please sign in to comment.