All Projects → JetBrains → Kotlin Playground

JetBrains / Kotlin Playground

Licence: apache-2.0
Self-contained component to embed in websites for running Kotlin code

Programming Languages

javascript
184084 projects - #8 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to Kotlin Playground

Pulsesensorplayground
A PulseSensor library (for Arduino) that collects our most popular projects in one place.
Stars: ✭ 126 (-41.12%)
Mutual labels:  playground
Widgetsplayground
前端组件管理系统
Stars: ✭ 150 (-29.91%)
Mutual labels:  playground
Xgen
A Swift package for generating Xcode workspaces & playgrounds
Stars: ✭ 178 (-16.82%)
Mutual labels:  playground
Rd3
Playground for React & D3.js
Stars: ✭ 128 (-40.19%)
Mutual labels:  playground
Layer5
Layer5, the service mesh company, representing every service mesh
Stars: ✭ 137 (-35.98%)
Mutual labels:  playground
Play With Docker
You know it, you use it, now it's time to improve it. PWD!.
Stars: ✭ 2,103 (+882.71%)
Mutual labels:  playground
Whats New In Swift 5 1
An Xcode playground that demonstrates the new features introduced in Swift 5.1.
Stars: ✭ 122 (-42.99%)
Mutual labels:  playground
Docker
Docker playground
Stars: ✭ 202 (-5.61%)
Mutual labels:  playground
Coredataplaygrounds
Exploring Core Data through Swift playgrounds
Stars: ✭ 139 (-35.05%)
Mutual labels:  playground
Code Sketch
一个你最初写代码的地方…
Stars: ✭ 176 (-17.76%)
Mutual labels:  playground
Testdrive
Quickly try out any Swift pod or framework in a playground
Stars: ✭ 1,612 (+653.27%)
Mutual labels:  playground
Swift Summary
A summary of Apple's Swift language written on Playgrounds
Stars: ✭ 1,668 (+679.44%)
Mutual labels:  playground
Web Maker
A blazing fast & offline frontend playground
Stars: ✭ 2,029 (+848.13%)
Mutual labels:  playground
What The Filter
A visual playground to JavaScript array & object transformations.
Stars: ✭ 128 (-40.19%)
Mutual labels:  playground
Design Patterns In Swift
📖 Design Patterns implemented in Swift 5.0
Stars: ✭ 13,146 (+6042.99%)
Mutual labels:  playground
Vuep.run
🏃 An online playground for Vue2.0
Stars: ✭ 125 (-41.59%)
Mutual labels:  playground
Whats New In Swift 4
An Xcode playground showcasing the new features in Swift 4.0.
Stars: ✭ 1,860 (+769.16%)
Mutual labels:  playground
Tiup
A component manager for TiDB
Stars: ✭ 207 (-3.27%)
Mutual labels:  playground
Swift Playgrounds
Learning Swift by working through example code in playgrounds
Stars: ✭ 199 (-7.01%)
Mutual labels:  playground
Swift Adventures In Monad Land
A Swift adventure with Optionals, Monads, bananas and squirrels
Stars: ✭ 166 (-22.43%)
Mutual labels:  playground

official JetBrains project NPM version

Kotlin Playground

Component that creates Kotlin-aware editors capable of running code from HTML block elements.

Examples

Installation

Use our CDN

Insert a <script> element into your page and specify what elements should be converted in its data-selector attribute.

<script src="https://unpkg.com/[email protected]" data-selector="code"></script>

Or, if you need to separate process of loading/conversion, omit the data-selector attribute and use a second <script> element like this:

<script src="https://unpkg.com/[email protected]"></script>

<script>
document.addEventListener('DOMContentLoaded', function() {
  KotlinPlayground('.code-blocks-selector');
});
</script>

You can also overwrite the server where the code will be sent to be compiled and analyzed (for example if you host a server instance that includes your own Kotlin libraries). For that you can set the data-server attribute.

And you can also set a default Kotlin version for code snippets to run on. Bear in mind that the version set per editor will take precedence though:

<script src="https://unpkg.com/[email protected]"
        data-selector="code"
        data-server="https://my-kotlin-playground-server"
        data-version="1.3.41">
</script>

Fork & clone the old server repository or the new server.

Host your own instance

Install Kotlin-playground as dependency via NPM.

npm install kotlin-playground -S

And then just use it in your code.

// ES5
var playground = require('kotlin-playground');

document.addEventListener('DOMContentLoaded', function() {
  playground('code'); // attach to all <code> elements
});


// ES6
import playground from 'kotlin-playground';

document.addEventListener('DOMContentLoaded', () => {
  playground('code'); // attach to all <code> elements
});

Use from plugins

  1. Kotlin Playground WordPress pluginWordPress plugin which allows to embed interactive Kotlin playground to any post.
  2. Kotlin Playground Coursera plugin — Allows embedding interactive Kotlin playground for coursera lessons.
  3. Kotlin Playground Orchid plugin — Allows embedding interactive Kotlin playground in Orchid documentation sites.

Options

Kotlin Playground supports several events, and also Kotlin version or server URL overwriting passing an additional options parameter on initialisation.

For example:

function onChange(code) {
  console.log("Editor code was changed:\n" + code);
}

function onTestPassed() {
   console.log("Tests passed!");
}

const options = {
  server: 'https://my-kotlin-playground-server',
  version: '1.3.50',
  onChange: onChange,
  onTestPassed: onTestPassed,
  callback: callback(targetNode, mountNode)
};

playground('.selector', options)

Events description:

  • onChange(code) — Fires every time the content of the editor is changed. Debounce time: 0.5s. code — current playground code.

  • onTestPassed — Is called after all tests passed. Use for target platform junit.

  • onTestFailed — Is called after all tests failed. Use for target platform junit.

  • onCloseConsole — Is called after the console's closed.

  • onOpenConsole — Is called after the console's opened.

  • getJsCode(code) — Is called after compilation Kotlin to JS. Use for target platform js. code — converted JS code from Kotlin.

  • callback(targetNode, mountNode) — Is called after playground's united. targetNode — node with plain text before component initialization. mountNode — new node with runnable editor.

  • getInstance(instance) - Getting playground state API.

    instance.state      // playground attributes, dependencies and etc.
    instance.nodes      // playground NodeElement.
    instance.codemirror // editor specification.
    instance.execute()  // function for executing code snippet.
    instance.getCode()  // function for getting code from snippet.
    

Customizing editors

Use the following attributes on elements that are converted to editors to adjust their behavior.

  • data-version: Target Kotlin compiler version:

     <code data-version="1.0.7">
     /*
     Your code here
     */
     </code>
    
  • args: Command line arguments.

    <code args="1 2 3">
    /*
    Your code here
    */
    </code>
    
  • data-target-platform: target platform: junit, canvas, js or java (default).

     <code data-target-platform="js">
      /*
      Your code here
      */
     </code>
    
  • data-highlight-only: Read-only mode, with only highlighting. data-highlight-only="nocursor" - no focus on editor.

    <code data-highlight-only>
      /*
      Your code here
      */
    </code>
    

    Or, you can make only a part of code read-only by placing it between //sampleStart and //sampleEnd markers. If you don't need this just use attribute none-markers. For adding hidden files: put files between <textarea> tag with class hidden-dependency.

    <code>
    import cat.Cat
    
    fun main(args: Array<String>) {
    //sampleStart
        val cat = Cat("Kitty")
        println(cat.name)  
    //sampleEnd                 
    }
      <textarea class="hidden-dependency">
        package cat
        class Cat(val name: String)
      </textarea>
    </code>
    

    Also if you want to hide code snippet just set the attribute folded-button to false value.

  • data-js-libs: By default component loads jQuery and makes it available to the code running in the editor. If you need any additional JS libraries, specify them as comma-separated list in this attribute.

    <code data-js-libs="https://my-awesome-js-lib/lib.min.js">
      /*
      Your code here
      */
     </code>
    
  • auto-indent="true|false": Whether to use the context-sensitive indentation. Defaults to false.

  • theme="idea|darcula|default": Editor IntelliJ IDEA themes.

  • mode="kotlin|js|java|groovy|xml|c|shell|swift|obj-c": Different languages styles. Runnable snippets only with kotlin. Default to kotlin.

  • data-min-compiler-version="1.0.7": Minimum target Kotlin compiler version

  • data-autocomplete="true|false": Get completion on every key press. If false => Press ctrl-space to activate autocompletion. Defaults to false.

  • highlight-on-fly="true|false": Errors and warnings check for each change in the editor. Defaults to false.

  • indent="4": How many spaces a block should be indented. Defaults to 4.

  • lines="true|false": Whether to show line numbers to the left of the editor. Defaults to false.

  • from="5" to="10: Create a part of code. Example from line 5 to line 10.

  • data-output-height="200": Set the iframe height in px in output. Use for target platform canvas.

  • match-brackets="true|false"": Determines whether brackets are matched whenever the cursor is moved next to a bracket. Defaults to false.

Supported keyboard shortcuts

  • Ctrl+Space — code completion
  • Ctrl+F9/Cmd+R — execute snippet
  • Ctrl+/ — comment code
  • Ctrl+Alt+L/Cmd+Alt+L — format code
  • Shift+Tab — decrease indent
  • Ctrl+Alt+H/Cmd+Alt+H — highlight code
  • Ctrl+Alt+Enter/Cmd+Alt+Enter — show import suggestions

Develop and contribute

  1. Fork & clone our repository.
  2. Install required dependencies yarn install.
  3. yarn start to start local development server at http://localhost:9000.
  4. yarn run build to create production bundles.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].