-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #302 from ajthinking/customizable-ondrop
Customizable ondrop
- Loading branch information
Showing
7 changed files
with
106 additions
and
97 deletions.
There are no files selected for viewing
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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
NodeDescription, | ||
jsonEvaluation, | ||
hjsonEvaluation, | ||
jsExpressionEvaluation, | ||
jsFunctionEvaluation, | ||
json_ | ||
} from '@data-story/core'; | ||
|
||
export const fileDropper = { | ||
canHandle: (event) => { | ||
return event.dataTransfer.files.length > 0; | ||
}, | ||
|
||
handle: (event, addNodeFromDescription) => { | ||
console.log('HANDLING IN FILE DROPPER') | ||
const files = event.dataTransfer.files; | ||
|
||
const file = files[0]; | ||
if (file.type !== 'application/json') { | ||
console.error('You dragged and dropped a FILE TYPE not supported yet.'); | ||
return | ||
} | ||
|
||
const reader = new FileReader(); | ||
reader.onload = (event) => { | ||
try { | ||
// Parse the JSON content | ||
const jsonFileContent = JSON.parse(event?.target?.result as string); | ||
|
||
// This is a hack, consider the following: | ||
// The UI now hardcodes the Create node details | ||
// We don't know if the server even provides a Create node | ||
// Furthermore, the definition of the Create node might change | ||
// Consider having datastory register DnD-ables? | ||
const description: NodeDescription = { | ||
name: 'Create', | ||
label: file.name, | ||
inputs: [], | ||
outputs: [{ | ||
name: 'output', | ||
schema: {} | ||
}], | ||
params: [ | ||
json_({ | ||
name: 'data', | ||
help: 'You may use json, hson js function or expression', | ||
value: JSON.stringify(jsonFileContent, null, 2), | ||
evaluations: [ | ||
{ ...jsonEvaluation, selected: true }, | ||
hjsonEvaluation, | ||
jsFunctionEvaluation, | ||
jsExpressionEvaluation, | ||
] | ||
}) | ||
], | ||
} | ||
|
||
addNodeFromDescription(description); | ||
} catch (error) { | ||
console.error('Error parsing JSON:', error); | ||
} | ||
}; | ||
|
||
reader.onerror = (error) => { | ||
console.error('Error reading file:', error); | ||
}; | ||
|
||
// Read the file content | ||
reader.readAsText(file); | ||
}, | ||
} | ||
|
||
export const onDropDefault = (event, addNodeFromDescription) => { | ||
event.preventDefault(); | ||
|
||
const handlers = [ | ||
fileDropper, | ||
] | ||
|
||
const handler = handlers.find(h => h.canHandle(event)); | ||
if(!handler) { | ||
console.error('You dragged and dropped something not supported yet!'); | ||
console.log(event) | ||
return; | ||
} | ||
|
||
handler.handle(event, addNodeFromDescription); | ||
} |
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