|
@@ -10,6 +10,7 @@ import { loadMathJax } from '../../../mathjax/load-mathjax'
|
|
|
import { typesetNodeIntoElement } from '../../extensions/visual/utils/typeset-content'
|
|
import { typesetNodeIntoElement } from '../../extensions/visual/utils/typeset-content'
|
|
|
import { parser } from '../../lezer-latex/latex.mjs'
|
|
import { parser } from '../../lezer-latex/latex.mjs'
|
|
|
import { useTableContext } from './contexts/table-context'
|
|
import { useTableContext } from './contexts/table-context'
|
|
|
|
|
+import { CellInput, CellInputRef } from './cell-input'
|
|
|
|
|
|
|
|
export const Cell: FC<{
|
|
export const Cell: FC<{
|
|
|
cellData: CellData
|
|
cellData: CellData
|
|
@@ -38,7 +39,7 @@ export const Cell: FC<{
|
|
|
startEditing,
|
|
startEditing,
|
|
|
commitCellData,
|
|
commitCellData,
|
|
|
} = useEditingContext()
|
|
} = useEditingContext()
|
|
|
- const inputRef = useRef<HTMLInputElement>(null)
|
|
|
|
|
|
|
+ const inputRef = useRef<CellInputRef>(null)
|
|
|
|
|
|
|
|
const editing =
|
|
const editing =
|
|
|
editingCellData?.rowIndex === rowIndex &&
|
|
editingCellData?.rowIndex === rowIndex &&
|
|
@@ -48,6 +49,9 @@ export const Cell: FC<{
|
|
|
|
|
|
|
|
const onMouseDown: MouseEventHandler = useCallback(
|
|
const onMouseDown: MouseEventHandler = useCallback(
|
|
|
event => {
|
|
event => {
|
|
|
|
|
+ if (editing) {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
if (event.button !== 0) {
|
|
if (event.button !== 0) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
@@ -65,7 +69,7 @@ export const Cell: FC<{
|
|
|
)
|
|
)
|
|
|
})
|
|
})
|
|
|
},
|
|
},
|
|
|
- [setDragging, columnIndex, rowIndex, setSelection, table]
|
|
|
|
|
|
|
+ [setDragging, columnIndex, rowIndex, setSelection, table, editing]
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
const onMouseUp = useCallback(() => {
|
|
const onMouseUp = useCallback(() => {
|
|
@@ -117,19 +121,19 @@ export const Cell: FC<{
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
useEffect(() => {
|
|
|
- if (editing) {
|
|
|
|
|
- inputRef.current?.focus()
|
|
|
|
|
|
|
+ if (editing && inputRef.current) {
|
|
|
|
|
+ inputRef.current.focus()
|
|
|
}
|
|
}
|
|
|
- }, [editing])
|
|
|
|
|
|
|
+ }, [editing, cellData.content.length])
|
|
|
|
|
|
|
|
- const filterInput = (input: string) => {
|
|
|
|
|
|
|
+ const filterInput = useCallback((input: string) => {
|
|
|
// TODO: Are there situations where we don't want to filter the input?
|
|
// TODO: Are there situations where we don't want to filter the input?
|
|
|
return input
|
|
return input
|
|
|
.replaceAll(/(^&|[^\\]&)/g, match =>
|
|
.replaceAll(/(^&|[^\\]&)/g, match =>
|
|
|
match.length === 1 ? '\\&' : `${match[0]}\\&`
|
|
match.length === 1 ? '\\&' : `${match[0]}\\&`
|
|
|
)
|
|
)
|
|
|
.replaceAll('\\\\', '')
|
|
.replaceAll('\\\\', '')
|
|
|
- }
|
|
|
|
|
|
|
+ }, [])
|
|
|
|
|
|
|
|
const isFocused =
|
|
const isFocused =
|
|
|
selection?.to.row === rowIndex &&
|
|
selection?.to.row === rowIndex &&
|
|
@@ -161,18 +165,22 @@ export const Cell: FC<{
|
|
|
}
|
|
}
|
|
|
}, [cellData.content, editing])
|
|
}, [cellData.content, editing])
|
|
|
|
|
|
|
|
|
|
+ const onInput = useCallback(
|
|
|
|
|
+ e => {
|
|
|
|
|
+ update(filterInput(e.target.value))
|
|
|
|
|
+ },
|
|
|
|
|
+ [update, filterInput]
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
let body = <div ref={renderDiv} />
|
|
let body = <div ref={renderDiv} />
|
|
|
if (editing) {
|
|
if (editing) {
|
|
|
body = (
|
|
body = (
|
|
|
- <input
|
|
|
|
|
|
|
+ <CellInput
|
|
|
className="table-generator-cell-input"
|
|
className="table-generator-cell-input"
|
|
|
- ref={inputRef}
|
|
|
|
|
value={editingCellData.content}
|
|
value={editingCellData.content}
|
|
|
onBlur={commitCellData}
|
|
onBlur={commitCellData}
|
|
|
- style={{ width: `inherit` }}
|
|
|
|
|
- onChange={e => {
|
|
|
|
|
- update(filterInput(e.target.value))
|
|
|
|
|
- }}
|
|
|
|
|
|
|
+ onInput={onInput}
|
|
|
|
|
+ ref={inputRef}
|
|
|
/>
|
|
/>
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|