Skip to content

Commit ff2eaf5

Browse files
committed
WIP sourcemap stuff
1 parent 4ce001a commit ff2eaf5

File tree

4 files changed

+44
-29
lines changed

4 files changed

+44
-29
lines changed

packages/editor/src/lib/Workspace.svelte.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,19 +302,23 @@ export class Workspace {
302302
}
303303

304304
onhover(fn: (pos: number) => void) {
305-
this.#handlers.hover.add(fn);
305+
$effect(() => {
306+
this.#handlers.hover.add(fn);
306307

307-
return () => {
308-
this.#handlers.hover.delete(fn);
309-
};
308+
return () => {
309+
this.#handlers.hover.delete(fn);
310+
};
311+
});
310312
}
311313

312314
onselect(fn: (from: number, to: number) => void) {
313-
this.#handlers.select.add(fn);
315+
$effect(() => {
316+
this.#handlers.select.add(fn);
314317

315-
return () => {
316-
this.#handlers.select.delete(fn);
317-
};
318+
return () => {
319+
this.#handlers.select.delete(fn);
320+
};
321+
});
318322
}
319323

320324
remove(item: Item) {

packages/repl/src/lib/Output/AstNode.svelte

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
key?: string;
1111
value: Ast;
1212
path_nodes?: Ast[];
13-
autoscroll?: boolean;
13+
active?: boolean;
1414
depth?: number;
1515
onhover: (node: { type: string; start: number; end: number } | null) => void;
1616
}
1717
18-
let { key = '', value, path_nodes = [], autoscroll = true, onhover, depth = 0 }: Props = $props();
18+
let { key = '', value, path_nodes = [], active = true, onhover, depth = 0 }: Props = $props();
1919
2020
const { toggleable, workspace } = get_repl_context();
2121
@@ -32,8 +32,8 @@
3232
let key_text = $derived(key ? `${key}:` : '');
3333
3434
$effect(() => {
35-
if (typeof value === 'object' && value !== null) {
36-
const offselect = workspace.onselect((from, to) => {
35+
if (active && typeof value === 'object' && value !== null) {
36+
workspace.onselect((from, to) => {
3737
const nodes = value.type === 'Fragment' ? value.nodes : is_array ? value : [value];
3838
3939
const start = nodes[0]?.start;
@@ -51,10 +51,6 @@
5151
});
5252
}
5353
});
54-
55-
return () => {
56-
offselect();
57-
};
5854
}
5955
});
6056
</script>
@@ -107,7 +103,7 @@
107103
key={is_array ? undefined : k}
108104
value={v}
109105
{path_nodes}
110-
{autoscroll}
106+
{active}
111107
{onhover}
112108
depth={depth + 1}
113109
/>

packages/repl/src/lib/Output/AstView.svelte

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
interface Props {
1111
workspace: Workspace;
1212
ast: Ast;
13-
autoscroll?: boolean;
13+
active?: boolean;
1414
}
1515
16-
let { workspace, ast, autoscroll = true }: Props = $props();
16+
let { workspace, ast, active = true }: Props = $props();
1717
1818
let cursor = $state(0);
1919
@@ -43,14 +43,8 @@
4343
}
4444
}
4545
46-
$effect(() => {
47-
const offhover = workspace.onhover((pos) => {
48-
cursor = pos;
49-
});
50-
51-
return () => {
52-
offhover();
53-
};
46+
workspace.onhover((pos) => {
47+
cursor = pos;
5448
});
5549
</script>
5650

@@ -62,7 +56,7 @@
6256
<AstNode
6357
value={ast}
6458
{path_nodes}
65-
{autoscroll}
59+
{active}
6660
onhover={(node) => {
6761
if (
6862
node === null ||

packages/repl/src/lib/Output/Output.svelte

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import Viewer from './Viewer.svelte';
77
import { Editor, Workspace, type File } from 'editor';
88
import { untrack } from 'svelte';
9+
import { decode } from '@jridgewell/sourcemap-codec';
910
1011
interface Props {
1112
status: string | null;
@@ -81,6 +82,26 @@
8182
css_workspace.update_file(css);
8283
});
8384
});
85+
86+
$effect(() => {
87+
if (!markdown && view === 'js') {
88+
workspace.onhover((pos) => {
89+
if (!current.result.js.map) return;
90+
91+
const lines = decode(current.result.js.map.mappings);
92+
93+
// TODO find the `line`/`column` corresponding to `pos`
94+
95+
for (const segments of lines) {
96+
// TODO find the segment that corresponds to `line`/`column`
97+
}
98+
});
99+
100+
js_workspace.onhover((pos) => {
101+
// TODO same in reverse
102+
});
103+
}
104+
});
84105
</script>
85106

86107
<div class="view-toggle">
@@ -132,7 +153,7 @@
132153
<!-- ast output -->
133154
{#if current?.result}
134155
<div class="tab-content" class:visible={!is_markdown && view === 'ast'}>
135-
<AstView {workspace} ast={current.result.ast} autoscroll={!is_markdown && view === 'ast'} />
156+
<AstView {workspace} ast={current.result.ast} active={!is_markdown && view === 'ast'} />
136157
</div>
137158
{/if}
138159

0 commit comments

Comments
 (0)