forked from Orillusion/orillusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
70 lines (67 loc) · 2.68 KB
/
index.ts
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
/******** Load all samples in /src/sample/ ********/
{
// find all demos in /sample
const modules = import.meta.glob(['./*/*.ts', '!./*/_*.ts'])
// create menu
let title = '', list = `<svg onclick="document.body.classList.remove('show')" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg" class="close"><path fill="currentColor" d="M764.288 214.592 512 466.88 259.712 214.592a31.936 31.936 0 0 0-45.12 45.12L466.752 512 214.528 764.224a31.936 31.936 0 1 0 45.12 45.184L512 557.184l252.288 252.288a31.936 31.936 0 0 0 45.12-45.12L557.12 512.064l252.288-252.352a31.936 31.936 0 1 0-45.12-45.184z"></path></svg>`
for (const path in modules) {
if (!path.includes('Sample_')) continue
const arr = path.split('/')
const _title = arr[1]
const _demo = arr[2].replace(/Sample_|\.ts/g, '')
if (_title != title) {
list += `<p>${_title}</p>`
title = _title
}
list += `<a id="${path}">${_demo}</a>`
}
const menu = document.createElement('div')
menu.className = 'menu'
menu.innerHTML = list
document.body.appendChild(menu)
// change sessionStorage.target on click, and reload iframe
menu.addEventListener('click', (e: Event) => {
const button = e.target as HTMLElement
if (!button.id)
return
// remove prev iframe to clear memory
document.querySelector('iframe')?.remove()
const target = button.id
if (target && modules[target]) {
addIframe()
document.querySelector('.active')?.classList.remove('active')
button.classList.add('active')
sessionStorage.top = menu.scrollTop
sessionStorage.target = target
}
})
// load target on refresh
if (sessionStorage.target) {
const target = sessionStorage.target
const a = document.querySelector(`[id="${target}"]`)
if (a) {
addIframe()
a.classList.add('active')
menu.scrollTop = sessionStorage.top
}
} else {
document.querySelector('a')?.click()
}
// create an iframe inside page to load sample
function addIframe() {
const iframe = document.createElement('iframe') as HTMLIFrameElement
iframe.srcdoc = `
<style>html,body{margin:0;padding:0;overflow:hidden}canvas{touch-action:none}</style>
<script>
let target = sessionStorage.target
if(target)
import('./samples/'+target).then(m=>{
for(let i in m){
new m[i]().run()
break
}
})
</script>`
document.body.appendChild(iframe)
}
}