forked from VolodymyrBaydalka/docxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
109 lines (94 loc) · 3.08 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
108
109
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<link href="lib/bootstrap.min.css" rel="stylesheet" />
<script src="lib/bootstrap.bundle.min.js"></script>
<!--polyfills-->
<script src="lib/minified.js"></script>
<!--dependencies-->
<script src="lib/jszip.min.js"></script>
<!--library-->
<script src="dist/docx-preview.js"></script>
</head>
<body class="vh-100 d-flex flex-column">
<div class="hstack bg-black">
<a class="mx-auto p-2 text-white" href="https://war.ukraine.ua"
>Support Ukraine 🇺🇦</a
>
</div>
<div
class="hstack p-2 gap-2 bg-light position-sticky top-0"
style="z-index: 1"
>
<input
id="files"
type="file"
class="form-control"
style="width: 50ch"
accept=".docx"
/>
<button id="loadButton" class="btn btn-primary px-4">Load</button>
<div class="dropdown">
<button
class="btn btn-secondary dropdown-toggle"
type="button"
id="optionsMenuButton"
data-bs-toggle="dropdown"
aria-expanded="false"
>
Options
</button>
<ul
id="optionsMenu"
class="dropdown-menu"
aria-labelledby="optionsMenuButton"
></ul>
</div>
</div>
<div id="document-container" class="flex-grow-1 overflow-auto"></div>
<script>
let currentDocument = null;
const docxOptions = Object.assign(docx.defaultOptions, {
debug: true,
experimental: true,
});
const container = document.querySelector("#document-container");
function renderDocx(file) {
currentDocument = file;
if (!currentDocument) return;
docx
.renderAsync(currentDocument, container, null, docxOptions)
.then((x) => console.log(x));
}
document.querySelector("#loadButton").addEventListener("click", (ev) => {
renderDocx(document.getElementById("files").files[0]);
});
const menu = document.querySelector("#optionsMenu");
Object.keys(docxOptions)
.filter((key) => !/className/i.test(key))
.forEach(function (key) {
const listItem = document.createElement("li");
listItem.innerHTML = `
<div class="dropdown-item">
<div class="form-check">
<label class="form-check-name"><input type="checkbox" class="form-check-input" ${
docxOptions[key] ? "checked" : ""
}> ${key}</label>
</div>
</div>`;
const checkInput = listItem.querySelector("input");
checkInput.addEventListener("click", (e) => {
docxOptions[key] = checkInput.checked;
renderDocx(currentDocument);
});
menu.appendChild(listItem);
});
container.addEventListener("dragover", (ev) => ev.preventDefault());
container.addEventListener("drop", (ev) => {
ev.preventDefault();
renderDocx(ev.dataTransfer.files[0]);
});
</script>
</body>
</html>