GithubHelp home page GithubHelp logo

bjf2015 / codeflask.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kazzkiq/codeflask

0.0 2.0 0.0 6.14 MB

A micro code-editor for awesome web pages.

Home Page: https://kazzkiq.github.io/CodeFlask.js

License: MIT License

CSS 7.92% JavaScript 55.92% HTML 36.16%

codeflask.js's Introduction

npm version

CodeFlask.js

A micro code-editor for awesome web pages.

CodeFlask.js

About

CodeFlask.js lets you easily and effortless put an code-editor to your web page.

It was made as an attempt to create a leaner editor for simple purposes. Just a few lines of code and you're ready to play with code in the browser.

If you want a robust web code editor you can check projects that aim that big, such as CodeMirror.

Install

Bower:

bower install codeflask

NPM:

npm install codeflask

CDN (provided by cdnjs):

https://cdnjs.cloudflare.com/ajax/libs/CodeFlask.js/0.1.1/codeflask.min.js

Or download them manually if you want to.

Usage

In order to use CodeFlask.js you need also import Prism.js (for code highlight) into your project. Prism basically uses two files, an .js file and an .css one (where you can theme the code syntax).

After importing Prism, you will need two files from CodeFlask.js also. They are the codeflask.js and codeflask.css. Both are really small and only with the necessarily to make everything work properly.

Example:

...
<head>
  <link rel="stylesheet" href="prism.min.css">
  <link rel="stylesheet" href="codeflask.css">
  <script src="prism.min.js" async></script>
  <script src="codeflask.js" async></script>
</head>

Notice that prism.min.js must be declared before codeflask.js, as CodeFlask.js have dependency on the first.
Obs.: It is recomended to declare JavaScript files at the end of your <body> tag, the above example is just for ease of understanding purposes.

After that, all you need to do is to define an element where your editor should be rendered, and call it on JavaScript:

...
<body>
  <div id="my-code-wrapper" data-language="javascript"></div>

  <script>
    var flask = new CodeFlask;
    flask.run('#my-code-wrapper');
  </script>
</body>

Alternatively, you can define a language directly on your function call:

var flask = new CodeFlask;
flask.run('#my-code-wrapper', { language: 'javascript' })

It is important to remember that CodeFlask.js checks primarily for data-language attribute, then for the function call version. If none of those are declared, the editor will render in HTML syntax;

Listening for changes and updating your editor

You can also listen for changes in your editor. This is useful if you want to do some kind of realtime rendering of what you're coding, or detecting the input code for validation purposes, etc.

You can listen for it using .onUpdate():

var flask = new CodeFlask;
flask.run('#my-editor');

flask.onUpdate(function(code) {
    console.log("User's input code: " + code);
});

Alternatively, if you want to update an editor, you can use .update():

var flask = new CodeFlask;
flask.run('#my-editor');

flask.update("<button>Heeeey, whats up?</button>");

Configuring right-to-left

If you would like CodeFlask to start in the right to left configuration, pass 'true' to the 'rtl' parameter as shown below:

...
const flask = new CodeFlask;
flask.run('#editor', {language: 'javascript', rtl: true}) // add rlt functionality

Loading multiple editors

If you have a lot of editors on your page you can load them all by using .runAll() instead of .run():

...
<body>
  <div class="my-code-wrappers" data-language="javascript"></div>
  <div class="my-code-wrappers" data-language="css"></div>
  <div class="my-code-wrappers" data-language="ruby"></div>

  <script>
    var flask = new CodeFlask;
    flask.runAll('.my-code-wrappers');
  </script>
</body>

Note: When using .runAll(), the listener and update APIs are not enabled anymore.

How do I define the size of my editor?

The editor will assume the size of the element it was declared in. All you need to do is to set the dimensions of this element in your style, example:

...
<body>
  <style>
  #my-code-wrapper {
      width:350px;
      height:250px;
      position:relative; /* Position must be: relative, absolute or fixed */
  }
  </style>

  <div id="my-code-wrapper" data-language="python"></div>

  <script>
    var flask = new CodeFlask;
    flask.run('#my-code-wrappers');
  </script>
</body>

Credits & Thanks

CodeFlask.js was made possible by lots of pizzas and Prism.js by Lea Verou.

codeflask.js's People

Contributors

kazzkiq avatar pleasedontbelong avatar alexcp avatar nexts avatar jeroenvisser101 avatar leaverou avatar

Watchers

James Cloos avatar  avatar

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.