-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathindex.html
155 lines (131 loc) · 6.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous"></script>
<!--dependencies-->
<script crossorigin src="https://unpkg.com/jszip/dist/jszip.min.js"></script>
<!--library-->
<script src="dist/docx-preview.js"></script>
<!--thumbnail example-->
<script src="./demo/thumbnail.example.js"></script>
<link href="./demo/thumbnail.example.css" rel="stylesheet">
<script crossorigin src="https://unpkg.com/tiff.js@1.0.0/tiff.min.js"></script>
<script src="./demo/tiff-preprocessor.js"></script>
<style>
@media print {
.no-print {
display: none;
}
.overflow-auto {
overflow: initial !important;
}
}
</style>
</head>
<body class="vh-100 d-flex flex-column">
<div class="hstack bg-black no-print">
<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 no-print">
<input id="files" type="file" class="form-control" style="width: 50ch;" accept=".docx" />
<div class="col-2">
<select id="testDocuments" class="form-select">
<option disabled selected>-- Test Documents --</option>
</select>
</div>
<button id="loadButton" class="btn btn-primary px-4">Reload</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>
<button id="saveTestButton" class="btn btn-primary px-4" style="display: none">Save Test</button>
</div>
<div class="flex-grow-1 d-flex flex-row" style="height: 0;">
<details class="docx-thumbnails h-100 no-print">
<summary></summary>
<div id="thumbnails-container" class="docx-thumbnails-container"></div>
</details>
<div id="document-container" class="overflow-auto flex-grow-1 h-100"></div>
</div>
<script>
let currentDocument = null;
const docxOptions = Object.assign(docx.defaultOptions, {
debug: true,
experimental: true,
hideWrapperOnPrint: true
});
const container = document.querySelector("#document-container");
const fileInput = document.querySelector("#files");
const loadButton = document.querySelector("#loadButton");
const testDocuments = document.querySelector("#testDocuments");
async function renderDocx(file) {
currentDocument = file;
if (!currentDocument)
return;
// optional, find and convert all tiff images
let docxBlob = preprocessTiff(currentDocument);
// render document
let res = await docx.renderAsync(docxBlob, container, null, docxOptions)
// optional - render thumbnails
renderThumbnails(container, document.querySelector("#thumbnails-container"));
console.log(res);
}
fileInput.addEventListener("change", ev => {
renderDocx(fileInput.files[0]);
testDocuments.selectedIndex = 0;
});
loadButton.addEventListener("click", ev => renderDocx(fileInput.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]);
});
for (let testName of ['text','underlines','text-break','line-spacing','numbering','page-layout','table','table-spans','footnote','header-footer','revision','equation'])
{
var op = document.createElement("option");
op.label = testName;
testDocuments.add(op);
}
testDocuments.addEventListener("change", async e => {
const selected = testDocuments.selectedOptions[0].label;
if (selected) {
let resp = await fetch(`tests/render-test/${selected}/document.docx`);
renderDocx(await resp.blob());
fileInput.value = "";
}
});
document.querySelector("#saveTestButton").addEventListener("click", async () => {
const file = await showSaveFilePicker({ types: [ { description: "HTML File", accept: { "text/html": [".html"] } }] });
const stream = await file.createWritable();
await stream.write(container.innerHTML);
await stream.close();
});
</script>
</body>
</html>