Codesnip Template
A codesnippet which you can use to demonstrate APIs, patterns, bugs, or other techniques. (Note that the code is run in an iframe and will have reduced permissions, including only readonly access to the nomad.fs API.)
Source
Section titled “Source”<!doctype html5><html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="/ui.css"> </head> <body> <main> <section> <header>index.js</header> <div class="editor" id="js"></div> </section> <section> <header>index.html</header> <div class="editor" id="html"></div> </section> <section> <header>index.css</header> <div class="editor" id="css"></div> </section> <section> <header>result</header> <div id="result"></div> </section> </main> <script src="/ace/ace.js"></script> <script type="module" src="/ui.js"></script> </body></html>{ "title": "Codesnip", "description": "An editable, shareable code snippet.", "fallback": "/index.html"}var editors = { js: ace.edit('js'), css: ace.edit('css'), html: ace.edit('html')}window.editors = editorseditors.js.session.setMode('ace/mode/javascript')editors.css.session.setMode('ace/mode/css')editors.html.session.setMode('ace/mode/html')for (let k in editors) { editors[k].session.setUseWorker(false) editors[k].session.setTabSize(2) editors[k].session.setUseSoftTabs(true)}
var result = document.getElementById('result')var iframe = document.createElement('iframe')iframe.setAttribute('sandbox', 'allow-scripts')
setup()
async function setup () { var info = await nomad.fs.getInfo()
// Snippet content lives in /snippet.* — /index.html is this editor (the drive's // manifest `fallback` shell), so the snippet can't be stored there. editors.js.setValue(await nomad.fs.readFile('/snippet.js').catch(e => '')) editors.css.setValue(await nomad.fs.readFile('/snippet.css').catch(e => '')) editors.html.setValue(await nomad.fs.readFile('/snippet.html').catch(e => '')) for (let k in editors) { editors[k].selection.moveTo(0,0) } result.append(iframe)
for (let k in editors) { editors[k].session.on('change', debounce(e => onChangeEditor(k), 1e3)) }
runSnippet()}
function runSnippet () { iframe.src = `data:text/html;base64,${btoa(` <style> ${editors.css.getValue()} </style> ${editors.html.getValue()} <script> window.snippetUrl = "${window.location.toString()}"; ${editors.js.getValue()} </script> `)}`}
function debounce (fn, timeout) { var timer return () => { if (timer) clearTimeout(timer) timer = setTimeout(fn, timeout) }}
async function onChangeEditor (id) { var editor = editors[id] var filename = `/snippet.${id}` var current = await nomad.fs.readFile(filename).catch(e => '') if (current !== editor.getValue()) { runSnippet() nomad.fs.writeFile(filename, editor.getValue()).catch(e => undefined) }}body { --border-color: #aab; --header-background: #f3f3f8; --header-height: 28px;
margin: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;}
main { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; width: 100vw; height: 100vh;}
main section { position: relative; min-width: 0; min-height: 0;}
main section:nth-child(1) { border-right: 1px solid var(--border-color); border-bottom: 1px solid var(--border-color);}
main section:nth-child(2) { border-bottom: 1px solid var(--border-color);}
main section:nth-child(3) { border-right: 1px solid var(--border-color);}
main section header { background: var(--header-background); border-bottom: 1px solid #dde; padding: 0 10px; height: var(--header-height); line-height: var(--header-height); box-sizing: border-box; font-weight: bold; letter-spacing: 0.5px; font-size: 13px;}
.editor { position: absolute; top: var(--header-height); left: 0; bottom: 0; right: 0;}
iframe { width: 100%; height: calc(100% - var(--header-height)); border: 0; overflow: auto;}MIT License
Copyright (c) 2020 Blue Link Labs
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.