Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wanted to use and customize REPL - still a bit confused #165

Open
0gust1 opened this issue Jan 15, 2020 · 1 comment
Open

Wanted to use and customize REPL - still a bit confused #165

0gust1 opened this issue Jan 15, 2020 · 1 comment
Labels

Comments

@0gust1
Copy link

0gust1 commented Jan 15, 2020

I wanted to use and extend the REPL with the purpose of integrating it in a backoffice application, allowing content contributors (familiar with HTML and a little JS) to edit and create content.

I did some reading in svelte.dev website code : embed.svelte
and ReplWidget.svelte

to see how it's implemented, but I still struggle to get the "big picture" between the widget, the embed component, the REPL component itself and the webworkers.

If I achieve something, I'll be glad to contribute back some documentation.

@mjgartendev
Copy link

I think the easiest way to follow what's happening is to start with Example.svelte and work your way back. ReplWidget.svelte is mostly processing data from various sources into the shape <Repl> expects and can process either a gist or an example, which can make the embed component a bit confusing to start with.

If 'hello-world' is passed as the example prop, the ReplWidget would just fetch https://svelte.dev/examples/hello-world.json:

{
    "title":"Hello world",
    "files":[
        {
            "name":"App.svelte", 
            "source":"&lt;script&gt;\n\tlet name = 'world';\n&lt;/script&gt;\n\n&lt;h1&gt;Hello {name}!&lt;/h1&gt;"
        }
    ]
}

then transform the files array into components with {name, type, source} keys and update <Repl>'s component store programmatically:

fetch(`examples/${example}.json`).then(async response => {
    if (response.ok) {
        const data = await response.json();
	repl.set({
	    components: process_example(data.files)
	});
    }
});

repl.set() is one of the functions exported by <Repl>. Which you would access in your app by binding to the <Repl> component instance, like the site does in ReplWidget.svelte:

<Repl bind:this={repl}/>

You shouldn't need to do anything with the workers except copy them from the imported svelte-repl module to a static/public directory like the svelte/site does:

"copy-workers": "rm -rf static/workers && cp -r node_modules/@sveltejs/svelte-repl/workers static"

Then the workersUrl prop passed to <Repl> would just be 'workers', and the Bundler/Compiler/iframe handlers will be able to manage communication with the workers automatically.

If you reuse ReplWidget.svelte then you mostly just need to worry about navigating between different examples/gists and updating the <Repl>'s component store.

If you want to add preprocessors and such, there's a good example here: mdsvex-playground

@Rich-Harris Rich-Harris transferred this issue from sveltejs/svelte-repl Dec 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants