Skip to content

chore: output-only embedded mode #1217

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

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import { Repl } from '@sveltejs/repl';
import { mapbox_setup } from '../../../../../config.js';
import { page } from '$app/state';
import { decode_and_decompress_text } from '../gzip.js';
import type { File } from 'editor';

let { data } = $props();

Expand All @@ -25,10 +27,40 @@
});
}

// TODO make this munging unnecessary
function munge(data: any): File {
const basename = `${data.name}.${data.type}`;

return {
type: 'file',
name: basename,
basename,
contents: data.source,
text: true
};
}

async function set_files() {
const hash = location.hash.slice(1);

if (!hash) {
repl?.set({
files: data.gist.components.map(munge)
});

return;
}

try {
const recovered = JSON.parse(await decode_and_decompress_text(hash));
repl.set({ files: recovered.files });
} catch {
alert(`Couldn't load the code from the URL. Make sure you copied the link correctly.`);
}
}

afterNavigate(() => {
repl?.set({
files: data.gist.components
});
set_files();
});

const relaxed = $derived(data.gist.relaxed || (data.user && data.user.id === data.gist.owner));
Expand All @@ -51,7 +83,7 @@
can_escape
injectedJS={mapbox_setup}
previewTheme={theme.current}
embedded
embedded={page.url.searchParams.has('output-only') ? 'output-only' : true}
/>
{/if}
</div>
Expand Down
25 changes: 14 additions & 11 deletions packages/repl/src/lib/Output/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
interface Props {
status: string | null;
runtimeError?: Error | null;
embedded?: boolean;
embedded?: boolean | 'output-only';
relaxed?: boolean;
can_escape?: boolean;
injectedJS: string;
Expand Down Expand Up @@ -182,16 +182,18 @@
});
</script>

<div class="view-toggle">
{#if workspace.current.name.endsWith('.md')}
<button class="active">Markdown</button>
{:else}
<button aria-current={view === 'result'} onclick={() => (view = 'result')}>Result</button>
<button aria-current={view === 'js'} onclick={() => (view = 'js')}>JS output</button>
<button aria-current={view === 'css'} onclick={() => (view = 'css')}>CSS output</button>
<button aria-current={view === 'ast'} onclick={() => (view = 'ast')}>AST output</button>
{/if}
</div>
{#if embedded !== 'output-only'}
<div class="view-toggle">
{#if workspace.current.name.endsWith('.md')}
<button class="active">Markdown</button>
{:else}
<button aria-current={view === 'result'} onclick={() => (view = 'result')}>Result</button>
<button aria-current={view === 'js'} onclick={() => (view = 'js')}>JS output</button>
<button aria-current={view === 'css'} onclick={() => (view = 'css')}>CSS output</button>
<button aria-current={view === 'ast'} onclick={() => (view = 'ast')}>AST output</button>
{/if}
</div>
{/if}

<!-- component viewer -->
<div class="tab-content" class:visible={!is_markdown && view === 'result'}>
Expand All @@ -202,6 +204,7 @@
{can_escape}
{injectedJS}
{injectedCSS}
onLog={embedded === 'output-only' ? () => {} : undefined}
theme={previewTheme}
/>
</div>
Expand Down
23 changes: 17 additions & 6 deletions packages/repl/src/lib/Repl.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
interface Props {
packagesUrl?: string;
svelteVersion?: string;
embedded?: boolean;
embedded?: boolean | 'output-only';
orientation?: 'columns' | 'rows';
relaxed?: boolean;
can_escape?: boolean;
Expand Down Expand Up @@ -152,7 +152,7 @@
let mobile = $derived(width < 540);

$effect(() => {
$toggleable = mobile && orientation === 'columns';
$toggleable = mobile && orientation === 'columns' && embedded !== 'output-only';
});

let runes = $derived(
Expand All @@ -166,13 +166,24 @@

<svelte:window onbeforeunload={before_unload} />

<div class="container" class:embedded class:toggleable={$toggleable} bind:clientWidth={width}>
<div
class="container {embedded === 'output-only' ? '' : 'container-normal'}"
class:embedded
class:toggleable={$toggleable}
bind:clientWidth={width}
>
<div class="viewport" class:output={show_output}>
<SplitPane
id="main"
type={orientation === 'rows' ? 'vertical' : 'horizontal'}
pos="{mobile || fixed ? fixedPos : orientation === 'rows' ? 60 : 50}%"
min="100px"
pos="{embedded === 'output-only'
? 0
: mobile || fixed
? fixedPos
: orientation === 'rows'
? 60
: 50}%"
min={embedded === 'output-only' ? '0px' : '100px'}
max="-4.1rem"
>
{#snippet a()}
Expand Down Expand Up @@ -264,7 +275,7 @@

/* on mobile, override the <SplitPane> controls */
@media (max-width: 799px) {
:global {
.container-normal :global {
[data-pane='main'] {
--pos: 50% !important;
}
Expand Down