Skip to content

Commit 4fe015d

Browse files
committed
Added create file option
1 parent 75dfecf commit 4fe015d

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

src/component/modals/FileEdit.jsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useState, useEffect } from 'react';
22
import Editor from 'react-simple-code-editor';
33
import { highlight, languages } from 'prismjs/components/prism-core';
4+
import { saveAs } from 'file-saver';
45
import Modal from './ParentModal';
56
import './file-edit.css';
67
import 'prismjs/components/prism-clike';
@@ -11,6 +12,14 @@ import { actionType as T } from '../../reducer';
1112
const FileEditModal = ({ superState, dispatcher }) => {
1213
const [codeStuff, setCodeStuff] = useState('');
1314
const [fileName, setFileName] = useState('');
15+
const [dirButton, setDirButton] = useState(false);
16+
17+
useEffect(() => {
18+
if (navigator.userAgent.indexOf('Edg') !== -1 || navigator.userAgent.indexOf('Chrome') !== -1) {
19+
setDirButton(true);
20+
}
21+
}, []);
22+
1423
const close = () => dispatcher({ type: T.EDIT_TEXTFILE, payload: { show: false } });
1524
// TODO - Save file
1625
async function submit() {
@@ -25,6 +34,23 @@ const FileEditModal = ({ superState, dispatcher }) => {
2534
dispatcher({ type: T.EDIT_TEXTFILE, payload: { show: false } });
2635
}
2736

37+
async function saveAsSubmit() {
38+
const handle = await window.showSaveFilePicker();
39+
const stream = await handle.createWritable();
40+
await stream.write(codeStuff);
41+
await stream.close();
42+
// dispatcher({ type: T.EDIT_TEXTFILE, payload: { show: false } });
43+
}
44+
45+
async function saveSubmit() {
46+
// eslint-disable-next-line no-alert
47+
const newFileName = prompt('Filename:');
48+
const bytes = new TextEncoder().encode(codeStuff);
49+
const blob = new Blob([bytes], { type: 'application/json;charset=utf-8' });
50+
saveAs(blob, newFileName);
51+
// dispatcher({ type: T.EDIT_TEXTFILE, payload: { show: false } });
52+
}
53+
2854
useEffect(() => {
2955
if (superState.fileObj) {
3056
setFileName(superState.fileObj.name);
@@ -44,7 +70,16 @@ const FileEditModal = ({ superState, dispatcher }) => {
4470
>
4571
<div className="File Edit Container">
4672
<div className="footer">
47-
<button type="submit" className="btn btn-primary" onClick={submit}>Save</button>
73+
{fileName
74+
&& (
75+
<button type="submit" className="btn btn-primary" onClick={submit}>Save</button>
76+
)}
77+
{dirButton && (
78+
<button type="submit" className="btn btn-primary" onClick={saveAsSubmit}>Save As</button>
79+
)}
80+
{!dirButton && (
81+
<button type="submit" className="btn btn-primary" onClick={saveSubmit}>Save As</button>
82+
)}
4883
</div>
4984
<div
5085
style={{

src/toolbarActions/toolbarFunctions.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ const readTextFile = (state, setState, file, fileHandle) => {
123123
}
124124
};
125125

126+
const createFile = (state, setState) => {
127+
setState({
128+
type: T.EDIT_TEXTFILE,
129+
payload: { show: true },
130+
});
131+
};
132+
126133
const newProject = (state, setState) => {
127134
setState({ type: T.NEW_GRAPH });
128135
};
@@ -167,7 +174,7 @@ const toggleServer = (state, dispatcher) => {
167174

168175
export {
169176
createNode, editElement, deleteElem, downloadImg, saveAction, saveGraphMLFile,
170-
readFile, readTextFile, newProject, clearAll, editDetails, undo, redo,
177+
createFile, readFile, readTextFile, newProject, clearAll, editDetails, undo, redo,
171178
openShareModal, openSettingModal, viewHistory,
172179
toggleServer,
173180
};

src/toolbarActions/toolbarList.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111

1212
import {
1313
createNode, editElement, deleteElem, downloadImg, saveAction, saveGraphMLFile,
14-
readFile, clearAll, undo, redo, openShareModal, viewHistory,
14+
createFile, readFile, clearAll, undo, redo, openShareModal, viewHistory,
1515
toggleServer,
1616
// openSettingModal,
1717
} from './toolbarFunctions';
@@ -27,6 +27,14 @@ const toolbarList = (state, dispatcher) => [
2727
hotkey: 'Ctrl+G',
2828
},
2929
{ type: 'vsep' },
30+
{
31+
type: 'action',
32+
text: 'Create',
33+
icon: FaFileImport,
34+
action: createFile,
35+
active: true,
36+
visibility: true,
37+
},
3038
{
3139
type: 'file-upload',
3240
text: 'Open',

0 commit comments

Comments
 (0)