-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPropositionalLogic.component.tsx
More file actions
204 lines (182 loc) · 6.34 KB
/
PropositionalLogic.component.tsx
File metadata and controls
204 lines (182 loc) · 6.34 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
import Stack from '@mui/material/Stack'
import Box from '@mui/system/Box'
import React, { useCallback, useEffect, useRef, useState } from 'react'
import {
OmniInputResponsArea,
OmniInputResponsAreaProps,
} from '@components/OmniInput/OmniInputResponseArea.component'
import { ResponseAreaOmniInputContainer } from '@modules/shared/components/ResponseArea/ResponseAreaOmniInputContainer.component'
import { BaseResponseAreaProps } from '../base-props.type'
import { PropositionalLogicAnswerSchema } from './PropositionalLogic.schema'
import { PropositionalLogicSymbolKeyboard } from './PropositionalLogicSymbolKeyboard.component'
import { TruthTableSection } from './TruthTableSection.component'
type PropositionalLogicProps = Omit<
BaseResponseAreaProps,
'handleChange' | 'answer'
> & {
handleChange: (answer: PropositionalLogicAnswerSchema) => void
answer: PropositionalLogicAnswerSchema | undefined
allowDraw: boolean
allowScan: boolean
enableRefinement: boolean
allowTruthTable?: boolean
}
export const PropositionalLogic: React.FC<PropositionalLogicProps> = ({
handleChange,
handleSubmit,
answer,
allowDraw,
allowScan,
allowTruthTable = false,
hasPreview,
enableRefinement,
feedback,
typesafeErrorMessage,
checkIsLoading,
preResponseText,
postResponseText,
responsePreviewParams,
displayMode,
}) => {
// Normalize answer to object shape { formula, truthTable }
const answerObject = answer ?? { formula: '', truthTable: undefined }
const currentFormula = answerObject.formula ?? ''
// Remount OmniInput when symbol button is clicked so it shows updated value (it only reads defaultValue on mount)
const [formulaKey, setFormulaKey] = useState(0)
const [displayAnswer, setDisplayAnswer] = useState(currentFormula)
// Sync displayAnswer with answer prop when it changes
useEffect(() => {
setDisplayAnswer(currentFormula)
}, [currentFormula])
const omniInputContainerRef = useRef<HTMLDivElement | null>(null)
const cursorRef = useRef({ start: 0, end: 0 })
const pendingCursorRef = useRef<number | null>(null)
const submitAnswer = useCallback(
(formula: string, truthTable: PropositionalLogicAnswerSchema['truthTable']) => {
handleChange({
formula,
truthTable: allowTruthTable ? truthTable : undefined,
})
},
[handleChange, allowTruthTable],
)
const onFormulaChange = useCallback<OmniInputResponsAreaProps['handleChange']>(
(newFormula) => {
setDisplayAnswer(newFormula)
submitAnswer(newFormula, answerObject.truthTable)
},
[answerObject.truthTable, submitAnswer],
)
const insertSymbol = useCallback(
(symbol: string) => {
const { start, end } = cursorRef.current
const newValue =
displayAnswer.slice(0, start) + symbol + displayAnswer.slice(end)
setDisplayAnswer(newValue)
submitAnswer(newValue, answerObject.truthTable)
pendingCursorRef.current = start + symbol.length
setFormulaKey(k => k + 1)
},
[displayAnswer, answerObject.truthTable, submitAnswer],
)
// Attach cursor-tracking listeners to OmniInput's textarea (found via DOM)
useEffect(() => {
const container = omniInputContainerRef.current
if (!container) return
let timeoutId: ReturnType<typeof setTimeout>
let cleanup: (() => void) | undefined
const tryAttach = () => {
const textarea = container.querySelector('textarea')
if (textarea) {
const updateCursor = () => {
cursorRef.current = {
start: textarea.selectionStart ?? 0,
end: textarea.selectionEnd ?? 0,
}
}
const events = ['select', 'keyup', 'mouseup', 'blur', 'focus', 'input'] as const
events.forEach(ev => textarea.addEventListener(ev, updateCursor))
cleanup = () => {
events.forEach(ev => textarea.removeEventListener(ev, updateCursor))
}
return
}
timeoutId = setTimeout(tryAttach, 50)
}
tryAttach()
return () => {
clearTimeout(timeoutId)
cleanup?.()
}
}, [formulaKey])
// Restore cursor position after symbol insert (OmniInput remounts)
useEffect(() => {
const pos = pendingCursorRef.current
if (pos === null) return
const container = omniInputContainerRef.current
if (!container) return
const tryRestore = () => {
const textarea = container.querySelector('textarea')
if (textarea) {
pendingCursorRef.current = null
textarea.focus()
textarea.setSelectionRange(pos, pos)
return true
}
return false
}
if (!tryRestore()) {
const id = setTimeout(() => tryRestore(), 0)
return () => clearTimeout(id)
}
}, [displayAnswer, formulaKey])
const onTruthTableChange = useCallback(
(truthTable: PropositionalLogicAnswerSchema['truthTable']) => {
if (!truthTable) return
submitAnswer(displayAnswer, truthTable)
},
[displayAnswer, submitAnswer],
)
const onRemoveTruthTable = useCallback(() => {
submitAnswer(displayAnswer, undefined)
}, [displayAnswer, submitAnswer])
return (
<ResponseAreaOmniInputContainer
preResponseText={preResponseText}
postResponseText={postResponseText}>
<Stack spacing={1}>
<PropositionalLogicSymbolKeyboard onInsert={insertSymbol} />
<Box ref={omniInputContainerRef}>
<OmniInputResponsArea
key={formulaKey}
handleChange={onFormulaChange}
handleSubmit={handleSubmit}
answer={displayAnswer}
processingMode="markdown"
allowDraw={allowDraw}
allowScan={allowScan}
hasPreview={hasPreview}
enableRefinement={false}
feedback={feedback}
typesafeErrorMessage={typesafeErrorMessage}
checkIsLoading={checkIsLoading}
responsePreviewParams={responsePreviewParams}
displayMode={displayMode}
/>
</Box>
{allowTruthTable && (
<TruthTableSection
formula={displayAnswer}
truthTable={answerObject.truthTable ?? undefined}
onTruthTableChange={onTruthTableChange}
onRemoveTruthTable={onRemoveTruthTable}
allowDraw={allowDraw}
allowScan={allowScan}
processingMode="markdown"
/>
)}
</Stack>
</ResponseAreaOmniInputContainer>
)
}
export const HMR = true