Skip to content

Commit 1a3ec83

Browse files
Upload and Download Process
1 parent 7be2b43 commit 1a3ec83

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

examples/main.jsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import RemoveIcon from '@material-ui/icons/Remove';
3232
import DownloadIcon from '@material-ui/icons/CloudDownload';
3333
import ZoomInIcon from '@material-ui/icons/ZoomIn';
3434
import ZoomOutIcon from '@material-ui/icons/ZoomOut';
35+
import UploadIcon from '@material-ui/icons/Backup';
36+
3537
import dataJson from './data.json';
3638
import dataJsonControlled from './data.json.controlled';
3739
import {SketchField, Tools} from '../src';
@@ -91,7 +93,7 @@ const styles = {
9193
},
9294
card: {
9395
margin: '10px 10px 5px 0'
94-
}
96+
},
9597
};
9698

9799
/**
@@ -111,6 +113,9 @@ function eventFire(el, etype) {
111113
}
112114
}
113115

116+
117+
118+
114119
class SketchFieldDemo extends React.Component {
115120

116121
constructor(props) {
@@ -124,7 +129,7 @@ class SketchFieldDemo extends React.Component {
124129
shadowWidth: 0,
125130
shadowOffset: 0,
126131
tool: Tools.Select,
127-
enableRemoveSelected: false,
132+
enableRemoveSelected: true,
128133
fillWithColor: false,
129134
fillWithBackgroundColor: false,
130135
drawings: [],
@@ -200,9 +205,6 @@ class SketchFieldDemo extends React.Component {
200205
this._sketch.download();
201206
};
202207

203-
// Start
204-
//
205-
// Process
206208

207209
_getHash = (obj, obArray) => {
208210

@@ -341,7 +343,7 @@ class SketchFieldDemo extends React.Component {
341343
let { stretched, stretchedX, stretchedY, originX, originY } = this.state;
342344
reader.addEventListener(
343345
'load',
344-
() =>sketch.setBackgroundFromDataUrl(reader.result, {
346+
() => sketch.setBackgroundFromDataUrl(reader.result, {
345347
stretched: stretched,
346348
stretchedX: stretchedX,
347349
stretchedY: stretchedY,
@@ -357,6 +359,20 @@ class SketchFieldDemo extends React.Component {
357359

358360
_addText = () => this._sketch.addText(this.state.text);
359361

362+
_readFile = (event) => {
363+
let sketch = this._sketch;
364+
let file = event.target.files[0]
365+
if(file){
366+
let reader = new FileReader();
367+
reader.onload = function (e) {
368+
let data = e.target.result;
369+
console.log(data);
370+
sketch.fromJSON(data);
371+
}
372+
reader.readAsText(file);
373+
}
374+
}
375+
360376
componentDidMount = () => {
361377
(function(console) {
362378
console.save = function(data, filename) {
@@ -378,8 +394,11 @@ class SketchFieldDemo extends React.Component {
378394
a.dispatchEvent(e);
379395
};
380396
})(console);
397+
381398
};
382399

400+
401+
383402
render = () => {
384403
let { controlledValue } = this.state;
385404
const theme = createMuiTheme({
@@ -430,12 +449,23 @@ class SketchFieldDemo extends React.Component {
430449
onClick={this._save}>
431450
<SaveIcon/>
432451
</IconButton>
452+
<Button
453+
style={{background: 'none', border: 'none', color: '#607d8b', boxShadow: 'none'}}
454+
variant="contained"
455+
component="label"
456+
title="Upload" >
457+
<UploadIcon />
458+
<input type="file"
459+
onChange={(e) => this._readFile(e)}
460+
style={{ display: "none" }} />
461+
</Button>
433462
<IconButton
434463
color="primary"
435464
title="Download"
436465
onClick={this._download}>
437466
<DownloadIcon/>
438467
</IconButton>
468+
439469
<IconButton
440470
color="primary"
441471
title="Delete All"

src/history.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,20 @@ class History {
8282
* @returns the new current value after the redo operation, or null if no redo operation was possible
8383
*/
8484
redo() {
85+
console.log('heres')
8586
try {
87+
console.log('checking is here')
88+
console.log(this.redoList)
8689
if (this.redoList.length > 0) {
90+
8791
if (this.current) this.undoList.push(this.current);
8892
this.current = this.redoList.pop();
8993
return this.current;
9094
}
95+
9196
return null;
9297
} finally {
93-
this.print();
98+
//this.print();
9499
}
95100
}
96101

@@ -128,15 +133,22 @@ class History {
128133
*
129134
*/
130135
saveToFile() {
131-
var allElement = this.undoList;
132-
if(this.current){
133-
if(this.redoList.length == 0){
134-
allElement = this.current
136+
let dataArray = [];
137+
138+
if(this.current){
139+
if(this.undoList.length == 0){
140+
allElement = this.current[0]
141+
dataArray.push(this.current[0])
142+
} else {
143+
for(let i = 0; i < this.undoList.length; i++){
144+
dataArray.push(this.undoList[i][0])
145+
}
146+
}
135147
}
136-
}
137148

149+
var hash = {"objects": dataArray}
138150
const a = document.createElement("a");
139-
a.href = URL.createObjectURL(new Blob([JSON.stringify(allElement, null, 4)], {
151+
a.href = URL.createObjectURL(new Blob([JSON.stringify(hash, null, 4)], {
140152
type: "text/plain"
141153
}));
142154
a.setAttribute("download", "image.txt");

0 commit comments

Comments
 (0)