GithubHelp home page GithubHelp logo

porsager / flems Goto Github PK

View Code? Open in Web Editor NEW
485.0 25.0 81.0 2.76 MB

A single file web playground and sandbox

License: Do What The F*ck You Want To Public License

JavaScript 97.48% HTML 0.57% CSS 1.95%

flems's Introduction

Flems

As seen on Flems.io

Flems is a static web app - no strings attached - browser code playground. It's great for documentation, examples, presentations, issues and what not.

Flems example

Getting started

Just load a single file flems.html in a script tag to get started. - why should i load an .html file as a script tag?

<script src="https://flems.io/flems.html" type="text/javascript" charset="utf-8"></script>
<script>
const flems = Flems(document.body, {
    files: [{
        name: 'app.js',
        content: 'm.render(document.body, m("h1", "Hello world"))'
    }],
    links: [{
        name: 'mithril',
        type: 'script',
        url: 'https://unpkg.com/mithril'
    }]
})
</script>

Flems.io of the above

Contents

Content is added to the files and links arrays in options.

.files

The files array should contains objects with the following structure

{
    name        : String, 
    compiler    : String | Function,
    content     : String
}

The following extensions are handled for files (others are ignored)

  • .html - Only the first html file is used (others are ignored)
  • .js
  • .css
  • .ts - Will be compiled by typescript
  • .ls - Will be compiled by livescript

The following compilers are currently only avaible for js files:

  • ts (typescript)
  • ls (livescript)
  • babel

It is also possible to supply a function that receives the file and returns a promise which resolves to an object with code and map eg.

function compile(file) {
    return Promise.resolve({
        code: file.content.replace(/var /g, 'const '), // Don't do this
        map: null // The JSON for a sourcemap
    })
}

.links

The links array should contain objects with the following structure

{
    name        : String,
    type        : String, // js | css
    url         : String
}

If the url supports CORS, Flems will open files shorther than 200.000 chars in the editor, if not they'll simply be linked to.

Options

Flems is customizable to fit your need. If you don't want the toolbar or don't care for console output you can easily hide that away. The following options with their defaults are available:

{
    middle          : 50,
    selected        : '.js',
    color           : 'rgb(38,50,56)',
    theme           : 'material', // and 'none' or 'default'
    resizeable      : true,
    editable        : true,
    toolbar         : true,
    fileTabs        : true,
    linkTabs        : true,
    shareButton     : true,
    reloadButton    : true,
    console         : true,
    autoReload      : true,
    autoReloadDelay : 400,
    autoHeight      : false
}

Methods

There are a few methods exposed to control the Flems runtime:

.reload()

Reloads the runtime page

.focus()

Set focus in the editor for the currently selected file

.redraw()

Call this if you have changed the size of the container or changed eg. display: none to display: block

Bundling Flems

Bundling Flems doesn't really make sense. Flems uses an iframe as a runtime that needs to be pointed at a URL containing specific code. This is currently done by using the included script both as the required module Flems and as the html file src for the iframe. If for some reason you'd still want to bundle Flems, be aware you'll either need to make the same setup or point to a Flems specific runtime.html file resulting in the same amount of requires. Feel free to create an issue if you need some pointers for doing that.

Tools used to build Flems

Mithril is one of few Javascript frameworks that embraces Javascript - the good parts, it's a small package of [8kb] with everything required to make your web app work.

BSS is a css-in-js framework taking components to the extreme. No more defining intermediate class names for no reason, just focus on building your components using the javascript and the css properties you know. - The perfect companion for mithril.

Wright is a developmet environment taking away the hassle of setting up a dev server and running a live reload environment. It even hot reloads JS functions and CSS with no specific app modifications needed.

CodeMirror powers the editor in Flems, and ensures it works great on any device.

Rollup is a module bundler for JavaScript which compiles small pieces of code into something larger and more complex, such as a library or application.

Html script tag

To allow you to use Flems with only a single file to be required the javascript and the html for the iframe runtime has been merged into a single file disguised as flems.html. It works by having the javascript code contained in html comments and the html code contained in javascript comments. In that way if loaded like javascript the html is ignored and when loaded as html the javascript part is ignored.

Thanks

The mithril community has been an amazing help and source for feedback - Thanks to all of you!

flems's People

Contributors

dead-claudia avatar fuzetsu avatar horsley avatar ondreian avatar porsager avatar pygy avatar tormodvm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flems's Issues

Add new files to the top of their stack

Usually when adding a new file it will be to break out a piece of functionality the previous file depends on. When I create A, B, C, chances are B references C and A references B. With this in mind can we have it such that new files are inserted at the front of the list?

Also, moving files up and down is made difficult by the fact that the move down button collapses when disabled, causing the up button to change position depending on state. By rights the down button should leave an empty space to keep the up buttons in line.

All Flems stop working after a while offline

It's happenned to me twice to try things out in pre-loaded Flems while offline, and after a while (maybe an hour?), the iframe turns into the loading spinner and stays there forever.

There are network errors in the console but I didn't save them :-/

Feature Request: CSS pre-processor support?

Yes, I know that as the inventor of BSS that this makes you want to stab me with a dull fork. But it wouldn't hurt in trying to broaden your audience, and as a completely random side effect it would make some of my own work juuuuuuust a little easier :)

Babel isn't good sometimes

writing enough es6 to trigger the babel compiler, then switching back to es5 code without turning off Babel will throw ugly console.

Keyboard shortcuts regressions?

I was used to use cmd + l to highlight the location bar, that no longer works (probably Sublime keybindings related). I have the cmd + l, cmd + c sequence hardwired in my fingers to export URLs (not just for Flems) and it is annoying to have it broken...

Also IIRC (but I'm not really sure) CMD + / used to toggle comments, not it shows/hides the console.

Very unusual indentation with unclosed parentheses

For example, type this in JS mode:

foo(() =>
)

When you get to this point, then inside the parentheses, hit enter.

 foo(() =>
  //<- Where the indentation starts
 )

This is what's expected.

Now, try this:

foo(bar(() =>
))

And inside the parentheses, hit enter.

foo(bar(() =>
        //<- Where the indentation starts
))

This is a very odd inconsistency.

build failed

I'm trying to build Flems, but throw below errors:

mac:flems pro$ yarn
yarn run v1.5.1
$ npm run build:main && npm run build:runtime && npm run build:standalone

> [email protected] build:main /Users/pro/github/flems
> node scripts/main

/bin/sh: svgcleaner: command not found
{ Error: Command failed: svgcleaner /Users/pro/github/flems/src/icons/logo.svg -c
/bin/sh: svgcleaner: command not found

    at error (/Users/pro/github/flems/node_modules/rollup/dist/rollup.js:168:15)
    at Object.error (/Users/pro/github/flems/node_modules/rollup/dist/rollup.js:17918:21)
    at /Users/pro/github/flems/node_modules/rollup/dist/rollup.js:17927:29
    at <anonymous>
  error: null,
  cmd: 'svgcleaner /Users/pro/github/flems/src/icons/logo.svg -c',
  file: '/bin/sh',

Anything I'm missed?

Import default + others not working

As discussed on the Mithril channel, something like import modal, {status1, status2} from './modal' doesn't work. You suggested it was a regex issue, and after studying it, it looks like the isModuleRegex.test() is returning undefined. I could make it work by adding |\w just before (?!type). Not sure if that's correct, though.

UI for external dependencies that have been edited

We've discussed it in the past and I thought there was already an issue about it, but apparently there isn't...

Flems supports editing foreign dependencies and stores the diff in its state.

It would be nice if

  1. the name of files that have been edited were marked in some way (maybe italicize the name and add a *?)
  2. there was a way to visualize the diff

(cc. @foucist who also suggested this in gitter)

Feature discussion: Multiple HTML files and working links

I didn't know how you'd feel about such a feature, so I thought it would be best to bring it up here. Multiple HTML files is handy for teaching students how websites work.

Multiple HTML files is relatively easy to implement. The trickery comes when trying to make links work to switch between them. I have a working local branch, but it needs polishing before I can make a pull request.

The way I've done it there, is having a new value in state called htmlInView, that is set to whichever html file that was clicked last. I've also added a script in the iframe that catches all links pointing to local files and changes them to parameter/hash values. Then I monitor the location.href of the runtime and change htmlInView if it points to a html file in state.files. It's working, but I don't know if this breaks the purpose of a sandbox or if there's an easier way to do this?

Any thoughts?

some way to clear the console without reloading

It would be nice to have some way to clear the console without having to reload.

Ctlr+L would be nice (while focused on the console's input line).

Also, typing console.clear() clears the console in devtools, but not on the flem.

PS: Many have said it, but I'll say it again, flems is awesome 👍

Flems is Awesome

Yeah, I know this is not really an issue. Just wanted to say thanks for writing it.

Like Mithril, it hits that sweet spot of usability and completeness.

Feature Request: Allow getUserMedia video and audio in Flems

As of recent changes to Chrome (64), it is no longer possible to get access to the end users' cameras and microphones as used by WebRTC (getUserMedia) in an iframe without some additional attributes.

Essentially, this example should ideally work in Flems OOTB on at least Chrome and Firefox. I'm not completely sure of what the actual support is on Edge and Safari, so to some extent this is a Chrome-only issue for the time being (as of r64)...

From what I can tell, adding the following allow="audio; video" to the iframe tag could be enough to resolve the issue, based on this thread - however I'm not sure if there is some mishmash between what browsers might expect int the future

Make adding dependencies more robust

In this example, a dependency is intentionally added twice, and the UI suggests that the there might be duplicate downloads of the resource. This is probably not as intended, though actual functionality seems to load the same file into the editor when clicking either tab.

Convert code to flems link

I'm thinking for blog posts that it might be better to show a code block normally using <pre><code>... and having a link to flems.io. Obviously, I can go to flems.io, copy/paste code and then copy the link, but that's a bit tedious.

An additional API. similar to Flems(element, options) that builds a link would be helpful.

Feature request: public vs private pastes & nameable flems

Just a thought, but what if flems were nameable and public by default, so they showed up on a list of flems from most recent to oldest?

Could be a good way to browse examples and ideas and experiments. The list could have filters for the libraries used too.

I guess it could be helpful to show sizes of the non-library files included.

Feature idea: informative document title

It's too hard to look for old unnamed flems in my history. Zero indication of what they were about.

There's perhaps a few ways to make the document title informative for free:

  1. Could include count of libraries, or include letters of each library in order of dependency?
    Example: [m/ms] for mithril & mithril-stream

  2. Could include line count?
    Example: [h0,j50,c0] (html lines = 0, js lines = 50, css lines = 0)

side idea: Could also include line count in the flems tabs themselves

Ghost file / tab

There is a strange intermittent bug whereby a ghost file appears and gains focus and the next interaction makes it disappear - @barneycarroll

Fetch all code from editor

Hi,
I'm sorry if this is a stupid question, but is there any way to implement a function that fetches all the code in the current project? I thought you could do it with a custom compiler, but didn't succeed, and now I think I probably misunderstood.

I just started to teach programming and html to young students in Norway (even though I'm not an expert) and would love to implement a function that saves the students project to our database as they type.

Thanks,
Tormod

Feature request: support SVG files through service workers

I would have liked to write a repro for MithrilJS/mithril.js#2014 but the SVG <use> tag has same origin policy restrictions that make it currently impossible.

It would be nice to be able to add new SVG files (like new *Script/CSS ones) and have their content served through a service worker to the sandbox.

I've worked around the problem by using a gist and https://bl.ocks.org, but the workflow is obviously more choppy. OTOH I don't know when I'll need that functionality again, so there's no hurry as far as I'm concerned.

Converting <code> blocks to Flems

I want to convert <code> blocks to Flems. Tried the following which works but the Flems are squashed vertically so only the first line of code shows. Should the autoHeight affect this?

<html>
<body>

<h2>Test</h2>

<code>
const helloWorld = {
  view: () => m('h1', 'Hello World')
}

m.mount(document.body, helloWorld);
</code>

<code>
let youSaid = '123'

const helloWorld = {
  view: () => m('div', [
    m('h1', 'Hello World'),
    m('input', {
      value: youSaid,
      oninput: e => youSaid = e.target.value
    }),
    ' You said: ' + youSaid
  ])
}
m.mount(document.body, helloWorld);
</code>

<script src="https://flems.io/flems.html" type="text/javascript" charset="utf-8"></script>

<script>
function Example(el, code, css) {
  Flems(el, {
    files: [
      {
        name: '.js',
        content: code
      },
      {
        name: '.css',
        content: css || ''
      }
    ],
    links: [
      {
        name: 'mithril',
        type: 'script',
        url: 'https://unpkg.com/mithril'
      },
      {
        name: 'mithril-stream',
        type: 'script',
        url: 'https://unpkg.com/mithril-stream'
      },
      {
        name: 'purecss',
        type: 'css',
        url: 'https://unpkg.com/purecss'
      }
    ],
    middle        : 55,
    selected      : '.js',
    color         : 'rgb(38,50,56)',
    theme         : 'material',
    resizeable    : true,
    editable      : true,
    toolbar       : true,
    fileTabs      : true,
    linkTabs      : true,
    shareButton   : true,
    reloadButton  : true,
    console       : true,
    autoReload    : true,
    autoHeight    : true,
  })
}

examples = document.getElementsByTagName('code');

for (var i = 0; i < examples.length; ++i) {
  var ex = examples[i];
  var html = ex.textContent;
  Example(ex, html);
}
</script>

</body>
</html>

image

Also, what does resizeable do?

Unexpected auto indent

Kindly reported by @osban

Here. When placing the cursor at the end of m('h1', "Test"),+ enter = OK. Placing it at the end of m('button#test', {+enter = it ends up underneath m( instead of there+1 indent. Placing at the end of class: "test",+enter = it ends up underneath m( instead of class.

Add field for location hash

Being able to view and set the location hash would make testing apps with routing a lot nicer.

For example...

flems-hash-mockup

some way of recovering from infinite loops

jsbin has this: https://github.com/jsbin/loop-protect (might be a bit heavy handed for flems)

I think at least it would be nice to have a way to stop the code from auto running after getting into the infinite loop situation (url param maybe if UI method is not possible?).

As it is now it is not possible to recover even if you manually edit the json and add { autoReload: false } because the initial load still triggers the infinite loop.

Feature request: editable option on per file basis

I know there's already an option for making content editable or not, but it would be really nice if one could make certain files not editable while others are.

I'm using flems for education, so having static html while you can edit the css could save us teachers some headache. ;)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.