Files
leporello-js/src/examples.js

98 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-07-02 20:22:41 +03:00
export const write_example = (name, contents) => {
localStorage['examples_' + name] = contents
}
const read_example = name => {
return localStorage['examples_' + name]
}
2023-07-14 03:02:10 +03:00
export const examples = [
{
path: 'github_api',
entrypoint: 'github_api/index.js',
},
2023-07-17 05:03:21 +03:00
{
path: 'fibonacci',
entrypoint: 'fibonacci/index.js',
},
2023-07-14 03:02:10 +03:00
{
path: 'todos-preact',
entrypoint: 'todos-preact/index.js',
with_app_window: true,
files: [
'todos-preact/app.js',
]
},
2023-07-14 03:45:48 +03:00
{
path: 'ethers',
entrypoint: 'ethers/block_by_timestamp.js',
},
2023-07-17 05:21:05 +03:00
{
path: 'plot',
entrypoint: 'plot/index.js',
},
2024-03-07 15:30:27 +08:00
{
path: 'fractal_tree',
entrypoint: 'fractal_tree/fractal_tree.js',
with_app_window: true,
},
{
path: 'animated_fractal_tree',
entrypoint: 'animated_fractal_tree/animated_fractal_tree.js',
with_app_window: true,
},
{
path: 'canvas_animation_bubbles',
entrypoint: 'canvas_animation_bubbles/bubbles.js',
with_app_window: true,
},
2023-07-14 03:02:10 +03:00
].map(e => ({...e, entrypoint: e.entrypoint ?? e.path}))
2023-07-02 20:22:41 +03:00
2023-07-14 03:02:10 +03:00
const files_list = examples
.map(e => {
return (e.files ?? []).concat([e.entrypoint])
})
.flat()
.map(l => l.split('/'))
2023-07-02 20:22:41 +03:00
const get_children = path => {
2023-07-14 03:02:10 +03:00
const children = files_list.filter(l => path.every((elem, i) => elem == l[i] ))
2023-07-02 20:22:41 +03:00
const files = children.filter(c => c.length == path.length + 1)
const dirs = [...new Set(children
.filter(c => c.length != path.length + 1)
.map(c => c[path.length])
)]
return Promise.all(files.map(async f => {
const name = f[path.length]
const filepath = f.slice(0, path.length + 1).join('/')
return {
name,
path: filepath,
kind: 'file',
contents:
read_example(filepath) ??
await fetch(globalThis.location.origin + '/docs/examples/'+ filepath)
.then(r => r.text()),
}
})
.concat(dirs.map(async d => {
const p = [...path, d]
return {
name: d,
path: p.join('/'),
kind: 'directory',
children: await get_children(p),
}
})))
}
2023-07-14 03:02:10 +03:00
export const examples_dir_promise = get_children([]).then(children => {
2023-07-02 20:22:41 +03:00
return {
kind: 'directory',
name: 'examples',
path: null,
children,
}
})