Skip to content

Commit

Permalink
Merge pull request #302 from ajthinking/customizable-ondrop
Browse files Browse the repository at this point in the history
Customizable ondrop
  • Loading branch information
ajthinking authored Sep 22, 2024
2 parents 25d1d2c + 72c57ca commit 04899da
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 97 deletions.
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@data-story/core",
"version": "0.0.92",
"version": "0.0.93",
"main": "dist/main/index.js",
"type": "commonjs",
"types": "dist/main/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@data-story/nodejs",
"version": "0.0.92",
"version": "0.0.93",
"main": "dist/index.js",
"type": "commonjs",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@data-story/ui",
"version": "0.0.92",
"version": "0.0.93",
"main": "./dist/bundle.js",
"types": "./dist/src/index.d.ts",
"exports": {
Expand Down
19 changes: 13 additions & 6 deletions packages/ui/src/components/DataStory/DataStoryCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import InputNodeComponent from '../Node/InputNodeComponent';
import TableNodeComponent from '../Node/TableNodeComponent';
import { DataStoryCanvasProps, StoreInitOptions, StoreSchema } from './types';
import OutputNodeComponent from '../Node/OutputNodeComponent';
import { onDragOver, onDrop } from './onDrop';
import { onDropDefault } from './onDropDefault';
import type { NodeTypes } from '@xyflow/react/dist/esm/types';
import { useSelectedNodeSettings } from './Form/useSelectedNodeSettings';
import { HotkeyManager, useHotkeys } from './useHotkeys';
Expand Down Expand Up @@ -50,6 +50,7 @@ const Flow = ({
selectedNodeData,
selectedNode,
onSave,
onDrop,
client,
onChange,
}: DataStoryCanvasProps) => {
Expand Down Expand Up @@ -155,11 +156,17 @@ const Flow = ({
fitViewOptions={{
padding: 0.25,
}}
onDragOver={useCallback(onDragOver, [])}
onDrop={useCallback(
(e) => onDrop(e, addNodeFromDescription),
[addNodeFromDescription]
)}
onDragOver={useCallback((event) => {
event.preventDefault();
event.dataTransfer.dropEffect = 'move';
}, [])}
onDrop={
useCallback((event) => {
const handler = onDrop || onDropDefault;

handler(event, addNodeFromDescription)
}, [addNodeFromDescription]
)}
>
<DataStoryControls
onSave={onSave}
Expand Down
88 changes: 0 additions & 88 deletions packages/ui/src/components/DataStory/onDrop.ts

This file was deleted.

89 changes: 89 additions & 0 deletions packages/ui/src/components/DataStory/onDropDefault.ts
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);
}
1 change: 1 addition & 0 deletions packages/ui/src/components/DataStory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type DataStoryProps = {
observers?: DataStoryObservers;
onInitialize?: DataStoryCallback;
hideSidebar?: boolean;
onDrop?: (event: any, addNodeFromDescription: any) => void;
/**
* hideActivityBar: true (hide all activity bars)
* hideActivityBar: ['node', 'diagram'] (hide node and diagram activity bars)
Expand Down

0 comments on commit 04899da

Please sign in to comment.