-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
98 lines (83 loc) · 3.2 KB
/
editor.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
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
const CSS_LINKS = [`https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.2.0/github-markdown.min.css`];
const CHEATSHEET = fetch("https://ambushfall.com/MD_CHEATSHEET.MD").then(res => res.text())
const MD_CFG = {
// async: true,
// pedantic: false,
gfm: true,
mangle: false,
headerIds: false
}
marked.use(MD_CFG);
const MD_PARSER = (rawMD) => DOMPurify.sanitize(marked.parse(rawMD));
// Elements
const editorCode = document.getElementById("editorCode");
const editorPreview =
document.getElementById("editorPreview").contentWindow.document;
const editorCopyButton = document.getElementById("editorCopyClipboard");
const downloadButton = document.getElementById('downloadFile');
if (typeof CSS_LINKS !== 'undefined') {
CSS_LINKS.forEach(linkURL => {
const link = document.createElement('link');
link.href = linkURL;
link.rel = "stylesheet";
editorPreview.head.appendChild(link);
})
}
createEditor(editorCode);
async function createEditor(editorContainer) {
// console.log(CHEATSHEET)
let editor = monaco.editor.create(editorContainer, {
value: typeof CHEATSHEET !== 'undefined' ? await CHEATSHEET : '',
language: "markdown",
theme: "vs-dark",
minimap: { enabled: false },
automaticLayout: true,
contextmenu: false,
fontSize: 16,
scrollbar: {
useShadows: false,
vertical: "visible",
horizontal: "visible",
horizontalScrollbarSize: 12,
verticalScrollbarSize: 12,
},
});
const download = (data, filename, type) => {
var file = new Blob([data], { type: type });
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
editorPreview.body.innerHTML = MD_PARSER(editor.getValue())
editor.onDidChangeModelContent(() => {
editorPreview.body.innerHTML = MD_PARSER(editor.getValue())
});
editorCopyButton.onclick = () => {
// copyToClipboard(editorPreview.body.innerHTML);
navigator.clipboard.writeText(editorPreview.body.innerHTML)
.then(() => {
const editorCopyButtonText = editorCopyButton.innerHTML;
editorCopyButton.innerHTML = "Copied!";
editorCopyButton.disabled = true;
setTimeout(() => {
editorCopyButton.disabled = false;
editorCopyButton.innerHTML = editorCopyButtonText;
}, 500);
})
.catch(() => {
alert("something went wrong");
});
};
downloadButton.onclick = () => download(editorPreview.body.innerHTML, 'page', 'text/html')
}