reberhardt7 / cplayground

GNU General Public License v3.0
118 stars 14 forks source link

Support for direct ad-hoc links that embed code #46

Open sam-ka opened 4 years ago

sam-ka commented 4 years ago

The Rust playground provides a feature where you can link to their playground with code directly embedded in the URL. Inspecting and visiting [this link](https://play.rust-lang.org/?code=fn%20main(){println!(%22Hello%2C%20world!%22)%3B}) should provide a basic demonstration of how these links work.

These ad-hoc links are immensely useful when learning and teaching Rust and I think I and other cplayground users would benefit from similar functionality. Would there be any issue in adding support for direct code linking to this project?

reberhardt7 commented 4 years ago

Thanks for checking out cplayground. I'm not sure if I'm understanding this correctly, but cplayground already has ad hoc links to code; every time you run a program, a link will be generated that can be shared. I do think the UI could be improved to make it more obvious that this is happening and that those URLs can be shared.

sam-ka commented 4 years ago

Thanks for responding so quickly.

I don't think ad-hoc links - as currently implemented - are sufficient for my use case unfortunately. My issue with the current implementation is that I need to submit my code to cplayground before getting the link. This makes automatically generating links for something like examples in online documentation infeasible; I wouldn't know how to generate something like the examples shown at https://doc.rust-lang.org/std/string/struct.String.html without first manually submitting each example to cplayground by hand (and resubmitting them again if they change).

What I'm hoping for is a way to automatically generate the link without making a request to cplayground first. I should note that I'm not very familiar with cplayground yet, so please forgive me if cplayground already supports this usecase in another way.

reberhardt7 commented 4 years ago

I see. It looks like the Rust playground just takes some code via a GET parameter and populates the editor with that code; nothing needs to be persisted in a database in order to make this work. Am I understanding this right? If so, it should be pretty easy to add support for this to cplayground. On load, the React app already inspects GET params in order to load a saved program and populate the editor. We can just add a different param containing code instead of containing a persisted program ID.

What's the UI like for generating one of these Rust playground URLs? I have only used it a little bit and I know there's a button where you can save a program and get a permalink, but I didn't know you could do this with ?code=. I'm trying to think about how to document this behavior for Cplayground.

sam-ka commented 4 years ago

Yes, that's how it works.

With Rust's playground, normal users generate links by clicking the "share" button at the top right of the page, and then copy the third link "Embedded code in link" that will appear in the bottom pan. On the developers side, you simply pass your code via the code query parameter. This parameter is documented here: https://play.rust-lang.org/help#features-linking. The links are completely self-contained as far as I can tell; They shouldn't require any server-side state.

reberhardt7 commented 4 years ago

That should be pretty easy to implement. I can try doing it next week, although I'm really backlogged at the moment. If you'd like to give it a shot, here's the relevant function: https://github.com/reberhardt7/cplayground/blob/527c30f0dfc8eac1168dd63e4907dd398013e8df/src/client/components/App.tsx#L57 We just need to check if a code parameter is set, and if so, create a SavedProgram containing that code and call setState to set program to that SavedProgram. We should probably also handle the breakpoint parameter (since that does not require any server-side state either), but it shouldn't be possible to set code and p (i.e. program ID) at the same time. If both are set, I'd be fine with doing something dumb/simple for now like logging an error and giving one precedence over the other.

SavedProgram has a few different fields, and the defaults are populated here in the server-side code. It might make sense to move those defaults to src/common so that the frontend can use them for this.

sam-ka commented 4 years ago

When I opened this issue, I tried looking at the source code in hopes that I could provide a quick patch. However, I only have passing experience with javascript an none with typescript so I gave up.

Since I'm the one asking for this feature, It's only fair for me to spend some time looking into typescript and the structures that you linked. I'll update this comment if I think I can be of any help, but I strongly doubt it.

I'll update this comment soon.

Edit: I'm really sorry, but I'm not confident in having enough free time at the moment to properly familiarize myself with this project to the point where I'd feel comfortable with implementing this feature. I'm sorry I couldn't help out.