-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.html
359 lines (316 loc) · 9.67 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html>
<head>
<title>Algoliasearch Netlify Test Website</title>
<meta name="description" content="Algolia x Netlify testing website." />
<link rel="icon" href="/favicon.ico" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/necolas/normalize.css@8.0.1/normalize.css" />
<style>
* {
box-sizing: border-box;
}
body {
max-width: 1024px;
margin: auto;
font-family: 'BlinkMacSystemFont', 'Segoe UI', 'Roboto',
'Helvetica Neue', 'Arial', 'Noto Sans', 'sans-serif',
'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji';
}
h1 {
text-align: center;
}
button {
padding: 4px 8px;
border-radius: 2px;
border: 1px solid #ccc;
}
#search {
display: flex;
gap: 16px;
}
#searchBig {
width: 60%;
height: 32px;
}
#searchSmall {
height: 32px;
width: 20%;
}
#debug-script {
background: #2d3155;
color: white;
border-radius: 4px;
padding: 16px;
}
#debug-script-grid {
display: grid;
grid-template-columns: 1fr 20fr;
align-items: baseline;
column-gap: 16px;
row-gap: 8px;
}
#debug-script-grid>*:nth-child(2n - 1),
#error-title {
font-weight: bold;
white-space: nowrap;
}
#debug-script-grid>*:nth-child(2n) {
font-family: monospace;
}
#toggle-dev-prod-container {
text-align: right;
}
#toggle-dev-prod {
font-size: 0.8em;
}
#source-editor {
height: 300px;
margin-top: 4px;
border-radius: 4px;
}
#error-title {
display: none;
margin-top: 16px;
}
#error {
color: #f86e7e;
margin-top: 8px;
font-family: monospace;
font-size: 1.2em;
}
#source-buttons {
text-align: right;
margin-top: 8px;
}
</style>
</head>
<body>
<h1>Algoliasearch Netlify Test Website</h1>
<div id="search">
<div id="searchBig"></div>
<div id="searchSmall"></div>
</div>
<h2>Script</h2>
<div id="debug-script">
<div id="toggle-dev-prod-container">
<button id="toggle-dev-prod">Pending...</button>
</div>
<div id="debug-script-grid">
<div>Base URL</div>
<div id="base-url"></div>
<div>CSS</div>
<div id="lib-css"></div>
<div>JS</div>
<div id="lib-js"></div>
<div>Code</div>
<div></div>
</div>
<div id="source-editor">Test before</div>
<div id="error-title">Error</div>
<pre id="error"></pre>
<div id="source-buttons">
<button id="source-reset">Reset</button>
<button id="source-test">Test</button>
</div>
</div>
<h2>Test content</h2>
<p>Some content to index.</p>
<p>Links to other pages:
<ul>
<li>
<a href="1.html">1.html</a>
</li>
<li>
<a href="2.html">2.html</a>
</li>
</ul>
</p>
<script src="https://cdn.jsdelivr.net/npm/ace-builds@1.4.12/src-min-noconflict/ace.js"
type="text/javascript"></script>
<script type="text/javascript">
const DEFAULT_SOURCE = `
algoliasearchNetlify({
appId: '4C7VLPQA76',
apiKey: 'a9cfed5acb56143690b612167d89a1b5',
selector: '#searchBig',
siteId: '9209706f-d5b7-46e2-bb88-5d6bedd2823f',
branch: 'master',
hitsPerPage: 3,
});
algoliasearchNetlify({
appId: '4C7VLPQA76',
apiKey: 'a9cfed5acb56143690b612167d89a1b5',
selector: '#searchSmall',
siteId: '9209706f-d5b7-46e2-bb88-5d6bedd2823f',
branch: 'master',
placeholder: 'Detached Input',
poweredBy: false,
theme: {
mark: '#fff',
background: '#23263b',
selected: '#111432',
text: '#d6d6e7',
colorSourceIcon: '#d6d6e7'
},
detached: { mediaQuery: '(max-width: 900px)' },
debug: true,
});`
.split('\n')
.map((l) => l.replace(/^\s{8}/, ''))
.join('\n');
const usp = new URLSearchParams(window.location.search);
const editor = ace.edit('source-editor');
const $sourceResetButton = document.getElementById('source-reset');
const $sourceTestButton = document.getElementById('source-test');
const $debugToggle = document.getElementById('toggle-dev-prod');
const $baseUrl = document.getElementById('base-url');
const $libCSS = document.getElementById('lib-css');
const $libJS = document.getElementById('lib-js');
const $errorTitle = document.getElementById('error-title');
const $error = document.getElementById('error');
async function checkIfWebpackRunning() {
try {
const response = await fetch(
'http://localhost:9100/algoliasearchNetlify.css',
{
method: 'HEAD',
}
);
return response.status === 200;
} catch (err) {
console.log(err);
return false;
}
}
function reloadWithNewUsp() {
const { origin, pathname } = window.location;
const search = `?${usp.toString()}`;
const newUrl = `${origin}${pathname}${search === '?' ? '' : search}`;
window.location.href = newUrl;
}
function reloadWithNewSource(source) {
if (source === DEFAULT_SOURCE) {
usp.delete('force_source');
} else {
usp.set('force_source', btoa(source));
}
reloadWithNewUsp();
}
(async () => {
// Editor
editor.setTheme('ace/theme/monokai');
editor.session.setMode('ace/mode/javascript');
editor.session.setTabSize(2);
editor.session.setUseSoftTabs(true);
// Source
const source = usp.get('force_source')
? atob(usp.get('force_source'))
: DEFAULT_SOURCE;
editor.setValue(source, -1);
// Source actions
$sourceResetButton.addEventListener('click', () => {
reloadWithNewSource(DEFAULT_SOURCE);
});
$sourceTestButton.addEventListener('click', () => {
reloadWithNewSource(editor.getValue().trim());
});
// Test on Ctrl + S
const handleKeyDown = (e) => {
if (
e.keyCode === 83 &&
(e.metaKey || e.ctrlKey) &&
!(e.altKey || e.shiftKey)
) {
e.preventDefault();
reloadWithNewSource(editor.getValue().trim());
}
};
document.addEventListener('keydown', handleKeyDown);
// Base URL
const webpackReady = await checkIfWebpackRunning();
let usingDev = webpackReady;
if (usp.get('force_env') === 'dev') usingDev = true;
if (usp.get('force_env') === 'prod') usingDev = false;
// Toggle dev <-> prod
$debugToggle.innerText = usingDev
? 'Switch to cdn.jsdelivr.net'
: 'Switch to localhost:9100';
$debugToggle.addEventListener('click', () => {
usp.delete('force_env');
if (usingDev && webpackReady) usp.set('force_env', 'prod');
if (!usingDev && !webpackReady) usp.set('force_env', 'dev');
reloadWithNewUsp();
});
// Base Url
const baseUrl = usingDev
? 'http://localhost:9100'
: 'https://cdn.jsdelivr.net/npm/@algolia/algoliasearch-netlify-frontend@1/dist';
// Debug info
$baseUrl.innerText = baseUrl;
$libCSS.innerText = `${baseUrl}/algoliasearchNetlify.css`;
$libJS.innerText = `${baseUrl}/algoliasearchNetlify.js`;
// Error Handler
const handleError = (err) => {
$errorTitle.style.display = 'block';
$error.innerHTML = err instanceof Error ? err.message : err;
};
// Loading lib & script source
try {
// Library's CSS
let libStyleResolve;
let libStyleReject;
const libStylePromise = new Promise((resolve, reject) => {
libStyleResolve = resolve;
libStyleReject = reject;
});
const $libStyle = document.createElement('link');
$libStyle.rel = 'stylesheet';
$libStyle.href = `${baseUrl}/algoliasearchNetlify.css`;
$libStyle.onload = libStyleResolve;
$libStyle.onerror = () =>
libStyleReject(new Error("Could not load lib's CSS"));
// Library's JS
const $libScript = document.createElement('script');
$libScript.type = 'text/javascript';
$libScript.src = `${baseUrl}/algoliasearchNetlify.js`;
let libScriptResolve;
let libScriptReject;
const libScriptPromise = new Promise((resolve, reject) => {
libScriptResolve = resolve;
libScriptReject = reject;
});
$libScript.onload = libScriptResolve;
$libScript.onerror = libScriptReject;
$libScript.onerror = () =>
libScriptReject(new Error("Could not load lib's JS"));
// JS lib invocation
const $script = document.createElement('script');
$script.type = 'text/javascript';
window.scriptHandleError = handleError;
$script.innerHTML = `try { ${source}; } catch (err) { window.scriptHandleError(err); }`;
let scriptResolve;
let scriptReject;
const scriptPromise = new Promise((resolve, reject) => {
scriptResolve = resolve;
scriptReject = reject;
});
$script.onload = scriptResolve;
$script.onerror = () =>
scriptReject(
new Error('Failed to run code script, check the console')
);
// Actually load elements
document.head.appendChild($libStyle);
await libStylePromise;
document.head.appendChild($libScript);
await libScriptPromise;
document.head.appendChild($script);
await scriptPromise;
} catch (err) {
handleError(err);
}
})();
</script>
</body>
</html>