TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Feature request: Please make the Playground editor embeddable in other web applications

Open T18970237136 opened this issue 11 years ago • 21 comments

Hi, I think TypeScript is a great language and I really like the web-based playground editor editor due to its auto-completion/Intellisense features (which also shows method documentation, etc).

Unfortunately the editor doesn't seem to have a license so that it could be used for other web applications, as discussed here: http://typescript.codeplex.com/discussions/401549

Our use case: We are developing a web application where users can type their own Javascripts/Typescripts which extend the server's functionality. When the server runs these scripts, it provides an API object with defined properties and methods. So, TypeScript would be ideal here because we can write a .d.ts definition file containing the API definition and then present a typescript editor which uses this definition file.

Then, when the users edit their scripts they would get auto-completion with IntelliSense etc.

Currently we are trying to use one of the alternatives mentioned in the above Codeplex discussion that is based on the ACE editor (e.g. the one from basarat), but there seem to be some bugs which don't allow a smooth editor experiene, and its auto-complete feature doesn't show property types, documentation etc.

Would it be possible to release the Playground editor under a license (which wouldn't need to be opensource) that allows to embed it in other application? Thanks!

T18970237136 avatar Oct 19 '15 08:10 T18970237136

The playground is a component that we, the TypeScript team, do not own. The VSCode team would be the authority here. We use it in the playground in the website, but can not licence it for repackaging. We have gotten this request multiple times, and the playground editor with all the intellisense support is an awesome product, and personally I would love to see it in more places.

//CC: @egamma, he might have more comments here.

mhegazy avatar Oct 20 '15 17:10 mhegazy

Hi @mhegazy, thanks for your reply!

As an alternative if the repacking won't be possible I could image a variant that would also work for us, using an iframe to embed the editor (but I don't know if this also requires a license or something like that).

For example: The typescript editor currently allows to submit userdefined content in the URL fragment, e.g. http://www.typescriptlang.org/Playground#src=var%20s%3A%20string%20%3D%20%22Hello%20World!%22%3B%0A%0A%0A

Now, if there were a page similar like the one above that would accept parameters in the URL fragment (or in a POST request body) containing the script content and .d.ts definitions (either the content directly or a CORS-enabled URL reference) and a destination URL, the page could allow to edit the submitted content, and if the user clicks on a Save button, the page would do a redirect to the destination URL, containing both the edited typescript and the output javascript in the URL fragment (or as POST parameters).

Edit: In case of an iframe, I think Window.postMessage() would probably also work to allow the host window send commands to the editor in an iframe e.g. to load a specific content or send back the current editor contents, without needing to do redirects or specifying the contents in URL fragments.

That variant would also work for us, because we then could use an iframe in our configuration dialog showing the typescript page. Would something like this be possible?

Thanks!

T18970237136 avatar Oct 21 '15 13:10 T18970237136

@T18970237136 it sounds like what you really want isn't the full TS playground, but the editor (the Monaco editor) used by the playground with the TS language service so that you can embed into your site. In addition, you would like to add a .d.ts for your API. I don't see how this could be covered using the iFrame approach. I don't recommend to go down this path.

My team is currently heads down working on VS Code milestone. We can look into this request in a month or so. The editor actually has API to register an additional .d.ts which isn't exposed in the playground (check out the winjs playground: http://try.buildwinjs.com/playground/)

egamma avatar Oct 22 '15 11:10 egamma

Hi @egamma thanks a lot for your reply!

it sounds like what you really want isn't the full TS playground, but the editor (the Monaco editor) used by the playground with the TS language service so that you can embed into your site. In addition, you would like to add a .d.ts for your API.

Yes, that is what we'd want. Sorry for not being clear.

I don't see how this could be covered using the iFrame approach. I don't recommend to go down this path.

Can you elaborate on why you don't see this being covered by an iframe approach? I thought of using an inline-frame as an alternative if repackaging of the editor code on another (foreign) site won't possible.

In that case, e.g. the typscript page (www.typescriptlang.org) could host a static HTML page similar to the playground, containing the monaco editor. Then, a HTML page from a different host could use that page in an iframe and use window.postMessage() to send commands, e.g. to initialize the editor something like

{
  "command": "ShowContent",
  "editorContent": "if (true) { var s: string = 'Hello World!'; }",
  "tsDefUrls": [
    "https://github.com/Microsoft/TypeScript/blob/master/lib/lib.core.d.ts",
    "https://example.org/myDefinition.d.ts"
  ]
}

The editor HTML page then could listen to MessageEvents to receive the commands, and in case of the above command, load the (CORS-enabled) .d.ts files and show the specified content in the editor. Then, if the surrounding page wants to get the current editor contents (because it wants to save the state) it could send some "GetState" command, and the iframe page could in turn send a message containing the current typescript and javascript contents of the editor.

This would mean the editor library files wouldn't need to be packaged by our (or some other) site but we still would be able to use the editor in an iframe to let users edit their type-/javascripts on our website. However, I don't know how licensing would apply here and I would understand if you don't want to host such a page. If it would be possible to repackage the monaco editor into other sites, I would prefer that approach.

Thanks!

T18970237136 avatar Oct 22 '15 15:10 T18970237136

I can see that you can host the Monaco editor inside an iFrame and use postMessage to work (in fact this is his how VSO embeds the Monaco editor). However I don't think this is applicable when embedding the TS Playground, which is a particular application that uses the Monaco editor. In other words the TS Playground does too much for your scenario.

We can look into the repacking or a CDN approach in roughly +1 month

egamma avatar Oct 22 '15 15:10 egamma

OK, thank you!

T18970237136 avatar Oct 23 '15 06:10 T18970237136

@egamma It would be a huge step in evolving of TS-related public web tools if you'd allow to embed a MonacoEditor (with intellisense and ability to load custom .d.ts) into third-party products.

Eagerly awaiting to hear any progress in this direction!

Artazor avatar Oct 25 '15 18:10 Artazor

+1

hiyelbaz avatar Nov 09 '15 10:11 hiyelbaz

+1

kgusarov avatar Nov 09 '15 13:11 kgusarov

+1

vadkart avatar Nov 09 '15 13:11 vadkart

++

nrshvch avatar Nov 09 '15 13:11 nrshvch

+1

wonderwhy-er avatar Nov 09 '15 14:11 wonderwhy-er

+1

FabianLauer avatar Nov 10 '15 20:11 FabianLauer

it would be great

domotenko avatar Nov 11 '15 10:11 domotenko

+1

pkotov avatar Nov 16 '15 11:11 pkotov

@egamma, Any news?

Artazor avatar Nov 17 '15 11:11 Artazor

https://github.com/Microsoft/vscode/tree/master/src/vs/editor/browser

RyanCavanaugh avatar Nov 18 '15 16:11 RyanCavanaugh

By the way, the open sourcing of the whole of VSC is totally awesome! Hopefully it will help out everyone.

kitsonk avatar Nov 18 '15 20:11 kitsonk

If it is what I'm thinking about - it's awesome!

Artazor avatar Nov 19 '15 01:11 Artazor

That is relly great! Thanks a lot!

Now, I'm trying to use the editor in a HTML page, but I don't know how to do it correctly.

I checked out the vscode repository and invoked tsc in the src folder. Then I created a HTML page in that folder using the TS playground as a template:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<h1>Test Monaco</h1>

<div id="typescriptEditor"></div>

<script type="text/javascript">
        var require = {
            baseUrl: "/"
        };
</script>
<script type="text/javascript" src="vs/loader.js"></script>
<script type="text/javascript" src="vs/editor/editor.main.js"></script>
<script type="text/javascript">

if (!window.console.info) {
    window.console. info = function(){}
}

(function () {
    'use strict';

    var domnode = document.getElementById('typescriptEditor')
    var typescriptMode = null;

    require(['vs/editor/editor.main'], function () {
        editorLoaded = true;

        Monaco.Editor.getOrCreateMode('text/typescript').then(function (mode) {
            typescriptMode = mode;

            var editor = Monaco.Editor.create(domnode, {
                value: '',
                mode: 'text/plain',
                fontIsMonospace: true,
                suggestOnTriggerCharacters: true,
                showTypeScriptWarnings: false
            });
        });


    });

})();
</script>
</body>
</html>

However, when hosting the directory in IIS Express and opening the HTML file in a browser, I'm getting errors such as

GET 
http://localhost:8080/vs/editor/contrib/comment/common/color.css [HTTP/1.1 404 Not Found 16ms]
GET 
http://localhost:8080/vs/editor/contrib/inPlaceReplace/common/gotoError.css [HTTP/1.1 404 Not Found 13ms]
GET 
http://localhost:8080/vs/editor/contrib/referenceSearch/browser/rename.css [HTTP/1.1 404 Not Found 125ms]
TypeError: Browser.isInWebWorker is not a function browserService.js:25:17
TypeError: nls.localize is not a function pluginsRegistry.js:330:30
TypeError: EditorCommon.LineTokensBinaryEncoding is undefined tokenIterator.js:7:9

Can anybody give me some pointers how to embed the editor correctly using the TS service?

Thank you!

T18970237136 avatar Nov 20 '15 11:11 T18970237136