-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
107 lines (91 loc) · 3.49 KB
/
index.html
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Untitled Document</title>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" />
<!-- Netlify Identity Widget -->
<script type="text/javascript" src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>
<script type="text/javascript" src="js/ImageTools.js"></script>
</head>
<body>
<div class="container mt-5">
<h2>Json data example</h2>
<input type="text" id="text" class="form-control" />
<button class="btn btn-outline-dark mt-3" id="save" style="display: none;">
Save
</button>
</div>
<div class="container mt-5">
<h2>Image Example</h2>
<input type="file" id="select" />
<br />
<img id="image" />
</div>
<div class="container mt-5">
<h2>Result:</h2>
<div id="result"></div>
</div>
<script>
const saveBut = document.querySelector("#save");
netlifyIdentity.on("login", function(user) {
console.log(user);
saveBut.style.display = "block";
saveBut.addEventListener("click", function() {
let mytext = document.querySelector("#text").value;
let data = {
text: mytext,
};
let type = "json";
save(data, type);
});
});
async function save(data, type) {
const token = await netlifyIdentity.currentUser().jwt();
let user = await netlifyIdentity.currentUser();
const response = await fetch("/.netlify/functions/save", {
headers: {
Authorization: `Bearer ${token}`,
},
method: "post",
body: JSON.stringify({
data: data,
type: type,
}),
})
.then((response) => response.text())
.then(function(res) {
console.log(res);
document.querySelector(
"#result"
).innerHTML = `Result: <b>${res}</b>`;
});
}
document.getElementById("select").onchange = function(evt) {
ImageTools.resize(
this.files[0], {
width: 500, // maximum width
height: 500, // maximum height
},
function(blob, didItResize) {
// didItResize will be true if it managed to resize it, otherwise false (and will return the original file as 'blob')
// you can also now upload this blob using an XHR.
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
let name = Math.random().toString(36).substr(2, 9) + ".jpg";
let path = "img/" + name;
// first, temporarily show the base64 data
document.querySelector("#image").src = base64data;
// save the image
let data = base64data;
let type = "image";
save(data, type);
};
}
);
};
</script>
</body>
</html>