forked from polotno-project/polotno-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
55 lines (52 loc) · 1.24 KB
/
file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
export const loadJSONFile = (file, store) => {
var reader = new FileReader();
reader.onloadend = function () {
var text = reader.result;
let json;
try {
json = JSON.parse(text);
} catch (e) {
alert('Can not load the project.');
}
if (json) {
store.loadJSON(json);
}
};
reader.onerror = function () {
alert('Can not load Polotno project file.');
};
reader.readAsText(file);
};
export const loadImageFile = (file, store) => {
var reader = new FileReader();
reader.onloadend = function () {
var url = reader.result;
const img = new Image();
img.src = url;
img.onload = () => {
const scale = Math.min(
1,
store.width / img.width,
store.height / img.height
);
const type = file.type.indexOf('svg') > -1 ? 'svg' : 'image';
store.activePage.addElement({
type,
width: img.width * scale,
height: img.height * scale,
src: url,
});
};
};
reader.onerror = function () {
alert('Can not load image.');
};
reader.readAsDataURL(file);
};
export const loadFile = (file, store) => {
if (file.type.indexOf('image') >= 0) {
loadImageFile(file, store);
} else {
loadJSONFile(file, store);
}
};