-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into ajf/formula-array-literals
- Loading branch information
Showing
18 changed files
with
352 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { atom } from 'recoil'; | ||
|
||
export const showCSVImportHelpAtom = atom({ | ||
key: 'showCSVImportHelpState', // unique ID (with respect to other atoms/selectors) | ||
default: false, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Coordinate } from '../../../gridGL/types/size'; | ||
import { SheetController } from '../../controller/sheetController'; | ||
// import { Cell } from '../../sheet/gridTypes'; | ||
// import { updateCellAndDCells } from '../updateCellAndDCells'; | ||
import * as Papa from 'papaparse'; | ||
import { updateCellAndDCells } from '../updateCellAndDCells'; | ||
import { Cell } from '../../sheet/gridTypes'; | ||
|
||
export const InsertCSV = (props: { | ||
file: File; | ||
insertAtCellLocation: Coordinate; | ||
sheetController: SheetController; | ||
reportError: (error: string) => void; | ||
}) => { | ||
const { file } = props; | ||
props.sheetController.start_transaction(); | ||
|
||
let rowIndex = 0; | ||
Papa.parse(file, { | ||
error: function (error, File) { | ||
props.reportError(error.name + ': ' + error.message); | ||
}, | ||
complete: function () { | ||
props.sheetController.end_transaction(); | ||
}, | ||
step: function (row) { | ||
const cellsToInsert: Cell[] = []; | ||
|
||
//@ts-expect-error | ||
row.data.forEach((text: string, cellIndex: number) => { | ||
cellsToInsert.push({ | ||
value: text.trim(), | ||
type: 'TEXT', | ||
x: props.insertAtCellLocation.x + cellIndex, | ||
y: props.insertAtCellLocation.y + rowIndex, | ||
last_modified: new Date().toISOString(), | ||
}); | ||
}); | ||
rowIndex++; | ||
|
||
updateCellAndDCells({ | ||
starting_cells: cellsToInsert, | ||
sheetController: props.sheetController, | ||
create_transaction: false, | ||
}); | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { useState, DragEvent, useRef } from 'react'; | ||
import { useRecoilState } from 'recoil'; | ||
import { InsertCSV } from '../../grid/actions/insertData/insertCSV'; | ||
import { SheetController } from '../../grid/controller/sheetController'; | ||
import { PixiApp } from '../../gridGL/pixiApp/PixiApp'; | ||
import { Coordinate } from '../../gridGL/types/size'; | ||
import { gridInteractionStateAtom } from '../../atoms/gridInteractionStateAtom'; | ||
import debounce from 'lodash.debounce'; | ||
import { QuadraticSnackBar } from './QuadraticSnackBar'; | ||
|
||
interface Props { | ||
sheetController: SheetController; | ||
app: PixiApp; | ||
} | ||
|
||
export const FileUploadWrapper = (props: React.PropsWithChildren<Props>) => { | ||
const { app } = props; | ||
// drag state | ||
const [dragActive, setDragActive] = useState(false); | ||
const divRef = useRef<HTMLDivElement>(null); | ||
const [interactionState, setInteractionState] = useRecoilState(gridInteractionStateAtom); | ||
|
||
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined); | ||
|
||
const moveCursor = debounce((e: DragEvent<HTMLDivElement>): void => { | ||
const clientBoudingRect = divRef?.current?.getBoundingClientRect(); | ||
const world = app.viewport.toWorld( | ||
e.pageX - (clientBoudingRect?.left || 0), | ||
e.pageY - (clientBoudingRect?.top || 0) | ||
); | ||
const { column, row } = props.sheetController.sheet.gridOffsets.getRowColumnFromWorld(world.x, world.y); | ||
setInteractionState({ | ||
...interactionState, | ||
cursorPosition: { x: column, y: row }, | ||
keyboardMovePosition: { x: column, y: row }, | ||
multiCursorPosition: { | ||
originPosition: { x: column, y: row }, | ||
terminalPosition: { x: column, y: row }, | ||
}, | ||
}); | ||
}, 100); | ||
|
||
// handle drag events | ||
const handleDrag = function (e: DragEvent<HTMLDivElement>) { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
if (e.type === 'dragenter' || e.type === 'dragover') { | ||
setDragActive(true); | ||
|
||
moveCursor(e); | ||
} else if (e.type === 'dragleave') { | ||
setDragActive(false); | ||
} | ||
}; | ||
|
||
// triggers when file is dropped | ||
const handleDrop = async (e: DragEvent<HTMLDivElement>) => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
setDragActive(false); | ||
|
||
if (e.dataTransfer.files && e.dataTransfer.files[0]) { | ||
const file = e.dataTransfer.files[0]; | ||
if (file.type === 'text/csv' || file.type === 'text/tab-separated-values') { | ||
const clientBoudingRect = divRef?.current?.getBoundingClientRect(); | ||
const world = app.viewport.toWorld( | ||
e.pageX - (clientBoudingRect?.left || 0), | ||
e.pageY - (clientBoudingRect?.top || 0) | ||
); | ||
const { column, row } = props.sheetController.sheet.gridOffsets.getRowColumnFromWorld(world.x, world.y); | ||
|
||
InsertCSV({ | ||
sheetController: props.sheetController, | ||
file: file, | ||
insertAtCellLocation: { x: column, y: row } as Coordinate, | ||
reportError: setErrorMessage, | ||
}); | ||
} else { | ||
setErrorMessage('File type not supported. Please upload a CSV file.'); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<div ref={divRef} onDragEnter={handleDrag} style={{ flex: 1 }}> | ||
{props.children} | ||
{dragActive && ( | ||
<div | ||
id="drag-file-element" | ||
onDragEnter={handleDrag} | ||
onDragLeave={handleDrag} | ||
onDragOver={handleDrag} | ||
onDrop={handleDrop} | ||
style={{ | ||
position: 'absolute', | ||
width: '100%', | ||
height: '100%', | ||
top: '0px', | ||
right: '0px', | ||
bottom: '0px', | ||
left: '0px', | ||
opacity: '0', | ||
}} | ||
></div> | ||
)} | ||
<QuadraticSnackBar | ||
open={errorMessage !== undefined} | ||
onClose={() => { | ||
setErrorMessage(undefined); | ||
}} | ||
message={errorMessage} | ||
/> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Fade, Snackbar, SnackbarProps } from '@mui/material'; | ||
|
||
export const QuadraticSnackBar = (props: SnackbarProps) => { | ||
return ( | ||
<Snackbar | ||
anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }} | ||
autoHideDuration={4000} | ||
TransitionComponent={Fade} | ||
{...props} | ||
/> | ||
); | ||
}; |
Oops, something went wrong.