Skip to content

Commit

Permalink
Merge pull request #1619 from concord-consortium/188415967-preserve-f…
Browse files Browse the repository at this point in the history
…ilename-on-drag

use new cfm openLocalFileWithConfirmation
  • Loading branch information
scytacki authored Nov 15, 2024
2 parents 0528b1f + b2424aa commit 7a3eced
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 36 deletions.
15 changes: 8 additions & 7 deletions v3/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@
"@codemirror/commands": "^6.7.1",
"@codemirror/language": "^6.10.3",
"@codemirror/view": "^6.34.1",
"@concord-consortium/cloud-file-manager": "^2.0.0-pre.6",
"@concord-consortium/cloud-file-manager": "^2.0.0-pre.7",
"@concord-consortium/mobx-state-tree": "^6.0.0-cc.1",
"@concord-consortium/slate-editor": "^0.10.1",
"@dnd-kit/core": "^6.1.0",
Expand Down
10 changes: 4 additions & 6 deletions v3/src/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { CodapDndContext } from "../lib/dnd-kit/codap-dnd-context"
import { Container } from "./container/container"
import { ToolShelf } from "./tool-shelf/tool-shelf"
import { kCodapAppElementId } from "./constants"
import { ICodapV2DocumentJson } from "../v2/codap-v2-types"
import { MenuBar, kMenuBarElementId } from "./menu-bar/menu-bar"
import { useCloudFileManager } from "../lib/use-cloud-file-manager"
import { Logger } from "../lib/logger"
Expand All @@ -13,7 +12,6 @@ import { addDefaultComponents } from "../models/codap/add-default-content"
import {gDataBroker} from "../models/data/data-broker"
import {IDataSet} from "../models/data/data-set"
import { dataContextCountChangedNotification } from "../models/data/data-set-notifications"
import { IDocumentModelSnapshot } from "../models/document/document"
import { IImportDataSetOptions } from "../models/document/document-content"
import { ISharedDataSet } from "../models/shared/shared-data-set"
import { getSharedModelManager } from "../models/tiles/tile-environment"
Expand All @@ -37,7 +35,7 @@ registerTileTypes([])
export const App = observer(function App() {
useKeyStates()

useCloudFileManager({
const cfm = useCloudFileManager({
appOrMenuElemId: kMenuBarElementId
})

Expand All @@ -57,9 +55,9 @@ export const App = observer(function App() {
sharedData?.dataSet.completeSnapshot()
}, [])

const handleImportDocument = useCallback((document: IDocumentModelSnapshot | ICodapV2DocumentJson) => {
appState.setDocument(document)
}, [])
const handleImportDocument = useCallback((file: File) => {
cfm?.client.openLocalFileWithConfirmation(file)
}, [cfm])

const handleUrlDrop = useCallback((url: string) => {
const tile = appState.document.content?.createOrShowTile(kWebViewTileType)
Expand Down
24 changes: 2 additions & 22 deletions v3/src/hooks/use-drop-handler.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
import { useEffect, useRef } from "react"
import { IDataSet } from "../models/data/data-set"
import { IDocumentModelSnapshot } from "../models/document/document"
import { convertParsedCsvToDataSet, CsvParseResult, importCsvFile } from "../utilities/csv-import"
import { safeJsonParse } from "../utilities/js-utils"
import { ICodapV2DocumentJson } from "../v2/codap-v2-types"

function importCodapDocument(
file: File | null,
onComplete: (document: IDocumentModelSnapshot | ICodapV2DocumentJson) => void
) {
const reader = new FileReader()
reader.onload = () => {
const document = reader.result &&
safeJsonParse<IDocumentModelSnapshot | ICodapV2DocumentJson>(reader.result as string)
document && onComplete(document)
}
file && reader.readAsText(file)
}

export interface IDropHandler {
selector: string
onImportDataSet?: (data: IDataSet) => void
onImportDocument?: (document: IDocumentModelSnapshot | ICodapV2DocumentJson) => void
onImportDocument?: (file: File) => void
onHandleUrlDrop?: (url: string) => void
}
export const useDropHandler = ({
Expand All @@ -39,10 +23,6 @@ export const useDropHandler = ({

function dropHandler(event: DragEvent) {

function onCompleteCodapImport(document: IDocumentModelSnapshot | ICodapV2DocumentJson) {
onImportDocument?.(document)
}

function onCompleteCsvImport(results: CsvParseResult, aFile: any) {
const ds = convertParsedCsvToDataSet(results, aFile.name)
onImportDataSet?.(ds)
Expand All @@ -62,7 +42,7 @@ export const useDropHandler = ({
switch (extension) {
case "codap":
case "codap3":
importCodapDocument(file, onCompleteCodapImport)
file && onImportDocument?.(file)
break
case "csv":
importCsvFile(file, onCompleteCsvImport)
Expand Down

0 comments on commit 7a3eced

Please sign in to comment.