-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreviewer-template.html
160 lines (141 loc) · 5.71 KB
/
previewer-template.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
156
157
158
159
160
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>RO-Crate Preview Builder</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f4;
}
header {
background-color: #007acc;
color: white;
padding: 1em;
text-align: center;
}
main {
padding: 2em;
max-width: 800px;
margin: 0 auto;
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
button {
background-color: #007acc;
color: white;
border: none;
padding: 0.5em 1em;
font-size: 1em;
cursor: pointer;
border-radius: 5px;
margin-top: 1em;
}
button:hover {
background-color: #005fa3;
}
textarea {
width: 100%;
padding: 1em;
font-size: 1em;
border: 1px solid #ccc;
border-radius: 5px;
margin-top: 1em;
}
.message {
margin-top: 1em;
color: #ff0000;
}
.output {
margin-top: 2em;
}
</style>
<script type="module">
{{ previewScripts | safe }}
document.addEventListener('DOMContentLoaded', () => {
const output = document.getElementById('output');
const message = document.getElementById('message');
const loadButton = document.getElementById('loadButton');
const generateButton = document.getElementById('generateButton');
async function processROCrate(text) {
const metadata = JSON.parse(text);
const crate = new ROCrate(metadata, { array: true, link: true });
const layout = {{ layout | dump | safe }};
const base64Template = `{{ resultTemplate | safe }}`;
const templateBytes = Uint8Array.from(atob(base64Template), c => c.charCodeAt(0));
const template = new TextDecoder('utf-8').decode(templateBytes);
const crateLite = await roCrateToJSON(metadata, layout);
const renderedHtml = renderTemplate(crateLite, template, layout);
return renderedHtml;
}
if (window.showOpenFilePicker && window.showSaveFilePicker) {
loadButton.style.display = 'inline-block';
loadButton.addEventListener('click', async () => {
try {
// Show file picker
const [fileHandle] = await window.showOpenFilePicker({
types: [{
description: 'RO-Crate Metadata',
accept: { 'application/json': ['.json'] }
}]
});
// Get file contents
const file = await fileHandle.getFile();
const text = await file.text();
const renderedHtml = await processROCrate(text);
// Save the rendered HTML to a file
const newFileHandle = await window.showSaveFilePicker({
suggestedName: 'ro-crate-preview.html',
types: [{
description: 'HTML File',
accept: { 'text/html': ['.html'] }
}]
});
const writableStream = await newFileHandle.createWritable();
await writableStream.write(renderedHtml);
await writableStream.close();
} catch (error) {
console.error('Error:', error);
}
});
}
generateButton.addEventListener('click', async () => {
try {
const text = document.getElementById('jsonInput').value;
const renderedHtml = await processROCrate(text);
// Open the rendered HTML in a new window
const blob = new Blob([renderedHtml], { type: 'text/html' });
const url = URL.createObjectURL(blob);
window.open(url, '_blank');
// Create a download link and trigger a click event to save the file
const a = document.createElement('a');
a.href = url;
a.download = 'ro-crate-preview.html';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
} catch (error) {
console.error('Error:', error);
}
});
});
</script>
</head>
<body>
<header>
<h1>RO-Crate Preview Builder</h1>
</header>
<main>
<p id="message" class="message"></p>
<button id="loadButton" style="display: none;">Load RO-Crate from Disk</button>
<textarea id="jsonInput" rows="10" cols="50" placeholder="Paste your JSON-LD (ro-crate-metadata.json) source here..."></textarea>
<button id="generateButton" >Generate HTML from source & Download</button>
<div id="output" class="output"></div>
<template id="template">{{ base64Template }}</template>
</main>
</body>
</html>