Compare commits

...

12 Commits
main ... pages

@ -1,3 +1,3 @@
# loader # loader
loader: loads content in a frame and sets up sandbox and CSP loader: builds content to load in a frame and set up sandbox and CSP

@ -1,8 +1,49 @@
class Builder { const defaultHtml = `
<!doctype html>
<html>
<head>
<title>macchiato.dev</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<style type="text/css">
html, body {
margin: 0;
padding: 0;
}
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
</style>
</head>
<body>
${ '<' + 'script type="module" src="/app.js"><' + '/script>' }
</body>
</html>
`.trim()
const defaultIntro = `
window.Macchiato = {
modules: {},
}
`.trim()
export class Builder {
constructor(files) { constructor(files) {
this.files = files this.files = files
} }
buildStyle(file) {
const style = document.createElement('style')
style.textContent = file.data
return style.outerHTML
}
buildModule(file) { buildModule(file) {
const script = document.createElement('script') const script = document.createElement('script')
script.setAttribute('type', 'module') script.setAttribute('type', 'module')
@ -20,7 +61,7 @@ class Builder {
return `// append: ${s}\n${match}` return `// append: ${s}\n${match}`
} }
).replaceAll( ).replaceAll(
/^\s*import\s+(\{[^}]+\})\s+from\s+("[^"]+"|')/gms, /^\s*import\s+(\{[^}]+\})\s+from\s+("[^"]+")/gms,
(match, p1, p2) => { (match, p1, p2) => {
const vars = p1.replaceAll(' as ', ': ') const vars = p1.replaceAll(' as ', ': ')
const importPath = p2.slice(1, -1) const importPath = p2.slice(1, -1)
@ -39,44 +80,81 @@ class Builder {
return script.outerHTML return script.outerHTML
} }
buildReplace(filesMap) {
if ('_replace.js' in filesMap) {
const rSrc = filesMap['_replace.js']
return new Function(
rSrc.match(/\((\w+)\)/)[1],
rSrc.slice(
rSrc.indexOf('{') + 1,
rSrc.lastIndexOf('}')
)
)
} else {
return ({data}) => data
}
}
build() { build() {
const modules = this.files.map(file => { const filesMap = Object.fromEntries(
return this.buildModule(file) this.files.map(
({name, data}) => ([name, data])
)
)
const replace = this.buildReplace(filesMap)
const intro = this.buildModule({
name: '_intro.js',
data: replace({
name: '_intro.js',
data: (
'_intro.js' in filesMap ?
filesMap['_intro.js'] :
defaultIntro
),
files: this.files,
}) })
return ` })
<!doctype html> const modules = this.files.filter(({name}) => (
<html> name.endsWith('.js') &&
<head> !name.startsWith('_')
<title>Editor</title> )).map(file => (
<style> this.buildModule({
html, body, iframe { ...file,
margin: 0; data: replace({
padding: 0; ...file,
width: 100%; files: this.files,
height: 100%; }),
} })
iframe { ))
border: none; const styles = this.files.filter(({name}) => (
display: block; name.endsWith('.css')
} )).map(file => (
html { this.buildStyle(file)
box-sizing: border-box; ))
} let html = (
*, *:before, *:after { 'index.html' in filesMap ?
box-sizing: inherit; filesMap['index.html'] :
} defaultHtml
</style> )
</head> let replaced = false
<body> html = html.replace(/\s*<\/head>/, match => {
${'<'}script type="module"> replaced = true
window.Macchiato = {modules: {}} return styles.join("\n") + "\n" + match
${'</'}script> })
${modules.join("\n")} if (!replaced) {
${'<'}script type="module"> html = styles.join("\n") + "\n" + html
}
${'</'}script> replaced = false
</body> html = html.replace(
</html> '<' + 'script type="module" src="/app.js"><' + '/script>',
`.trim() () => {
replaced = true
return intro + "\n" + modules.join("\n")
}
)
if (!replaced) {
html += "\n" + intro + "\n" + modules.join("\n")
}
return html
} }
} }

@ -0,0 +1,40 @@
import { Builder } from "/loader/builder.js"
export class EditorBuild {
deps = [
'forms/button-group.js',
'dialog/dialog.js',
'menu/dropdown.js',
'editor/header.js',
'editor/file-group.js',
'editor/file-view.js',
'editor/text-edit.js',
'editor/code-edit.js',
'loader/builder.js',
'editor/app.js',
]
constructor() {
this.files = undefined
}
async loadFiles() {
const files = []
for (const name of this.deps) {
const resp = await fetch(name)
files.push({
name,
data: await resp.text(),
})
}
this.files = files
}
async build() {
if (this.files === undefined) {
await this.loadFiles()
}
const builder = new Builder(this.files)
return builder.build()
}
}
Loading…
Cancel
Save