Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/#132 Implement open diagram from local disk #147

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/file-input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './open-file';
13 changes: 13 additions & 0 deletions src/common/file-input/open-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type OnFileSelectedCallback = (file: File) => void;

export const FileInput = (onFileSelected: OnFileSelectedCallback) => {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.accept = '.mml';
fileInput.addEventListener('change', event => {
const target = event.target as HTMLInputElement;
const file = target.files?.[0];
if (file) onFileSelected(file);
});
fileInput.click();
};
27 changes: 23 additions & 4 deletions src/pods/toolbar/components/open-button/open-button.component.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import React from 'react';
import { OpenIcon } from '@/common/components/icons/open-icon.component';
import { ToolbarButton } from '@/pods/toolbar/components/toolbar-button';
import { useCanvasSchemaContext } from '@/core/providers';
import { FileInput, OnFileSelectedCallback } from '@/common/file-input';
import classes from '@/pods/toolbar/toolbar.pod.module.css';

export const OpenButton = () => {
const handleOpenButtonClick = () => {
console.log('Open button clicked');
export const OpenButton: React.FC = () => {
const { loadSchema } = useCanvasSchemaContext();

const handleFileRead = (fileContent: string) => {
try {
const parsedData = JSON.parse(fileContent);
loadSchema(parsedData);
} catch (error) {
throw new Error('Error opening file' + error);
}
};

const handleOpenButtonClick: OnFileSelectedCallback = (file: File) => {
const reader = new FileReader();
reader.onload = e => {
const fileContent = e.target?.result as string;
if (fileContent) handleFileRead(fileContent);
};
reader.readAsText(file);
};

return (
<ToolbarButton
icon={<OpenIcon />}
label={'Open'}
onClick={handleOpenButtonClick}
onClick={() => FileInput(handleOpenButtonClick)}
className={classes.button}
/>
);
Expand Down
Loading