-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (70 loc) · 2.33 KB
/
script.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
document.addEventListener("DOMContentLoaded", async () => {
const node = await window.Ipfs.create({ repo: "ipfs-" + Math.random() });
window.node = node;
if (window.location.hash.substr(1) != "") {
(async () => {
window.loadDoc();
})();
} else {
(async () => {
for await (const data of window.node.cat(
"QmXBA1GRRj7UCzARoJ6pFXyhAHUKUjYKiFeQttB2Zp8hzY"
)) {
window.editor.render(JSON.parse(data.toString()));
}
})();
}
document.getElementById("savebtn").disabled = false;
document.getElementById("savebtn").setAttribute("class", "block");
document.getElementById("savebtn").innerHTML = "Save!";
document.getElementById("clearbtn").disabled = false;
document.getElementById("clearbtn").setAttribute("class", "block");
document.getElementById("downloadbtn").disabled = false;
document.getElementById("downloadbtn").setAttribute("class", "block");
});
window.loadDoc = async function() {
for await (const data of window.node.cat(window.location.hash.substr(1))) {
window.editor.render(JSON.parse(data.toString()));
}
};
window.saveDoc = function() {
window.editor
.save()
.then(outputData => {
console.log("Article data: ", outputData.blocks);
(async () => {
var res = JSON.stringify({ blocks: outputData.blocks });
const { cid } = await window.node.add(res);
await window.node.pin.add(cid.string);
navigator.clipboard
.writeText(`https://${location.hostname}/#${cid.string}`)
.then(
function() {
document.getElementById("savebtn").innerHTML = "Saved!";
setTimeout(function() {
document.getElementById("savebtn").innerHTML = "Save!";
}, 3000);
},
function() {
document.getElementById("savebtn").innerHTML =
"Failed to save :/";
setTimeout(function() {
document.getElementById("savebtn").innerHTML = "Save!";
}, 3000);
}
);
})();
})
.catch(error => {
console.log("Saving failed: ", error);
});
};
function downloadAsHtml() {
window.editor.save().then(outputData => {
window.download(
window.EditorJSParser.render(outputData),
`${new Date().toISOString()}.html`,
"text/html"
);
});
}