Skip to content

Commit ffff25b

Browse files
committed
better implementation
1 parent 24d3344 commit ffff25b

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,17 @@ export class Workspace {
257257
});
258258
}
259259

260-
highlight_range(node: { start: number; end: number } | null) {
260+
highlight_range(node: { start: number; end: number } | null, scroll = false) {
261261
if (!this.#view) return;
262262

263+
const effects: StateEffect<any>[] = [set_highlight.of(node)];
264+
265+
if (scroll && node) {
266+
effects.push(EditorView.scrollIntoView(node.start, { y: 'center' }));
267+
}
268+
263269
this.#view.dispatch({
264-
effects: set_highlight.of(node)
270+
effects
265271
});
266272
}
267273

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,13 @@
9191
const v = view; // so that TS doesn't think it could become something different
9292
const output = v === 'js' ? js_workspace : css_workspace;
9393
94-
const highlight = (line: number, a: SourceMapSegment, b: SourceMapSegment) => {
94+
const highlight = (
95+
line: number,
96+
a: SourceMapSegment,
97+
b: SourceMapSegment,
98+
scroll_input: boolean,
99+
scroll_output: boolean
100+
) => {
95101
const split = {
96102
original: workspace.current!.contents.split('\n'),
97103
generated: current!.result![v]!.code.split('\n')
@@ -107,8 +113,8 @@
107113
end: split.generated.slice(0, line).join('\n').length + 1 + b[0]
108114
};
109115
110-
workspace.highlight_range(original);
111-
output.highlight_range(generated);
116+
workspace.highlight_range(original, scroll_input);
117+
output.highlight_range(generated, scroll_output);
112118
};
113119
114120
const clear = () => {
@@ -137,16 +143,7 @@
137143
if (b[2]! === line && b[3]! < column) continue;
138144
139145
// if we're still here, we have a match
140-
highlight(i, a, b);
141-
142-
if (should_scroll) {
143-
tick().then(() => {
144-
document.querySelector('#output .highlight').scrollIntoView({
145-
block: 'center'
146-
});
147-
});
148-
}
149-
146+
highlight(i, a, b, false, should_scroll);
150147
return;
151148
}
152149
@@ -168,16 +165,7 @@
168165
const b = segments[i + 1];
169166
170167
if (a[0] <= column && b[0] >= column) {
171-
highlight(line, a, b);
172-
173-
if (should_scroll) {
174-
tick().then(() => {
175-
document.querySelector('#input .highlight').scrollIntoView({
176-
block: 'center'
177-
});
178-
});
179-
}
180-
168+
highlight(line, a, b, should_scroll, false);
181169
return;
182170
}
183171

packages/repl/src/lib/Repl.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,15 @@
176176
max="-4.1rem"
177177
>
178178
{#snippet a()}
179-
<section id="input">
179+
<section>
180180
<ComponentSelector {runes} {onchange} {workspace} {can_migrate} />
181181

182182
<Editor {workspace} />
183183
</section>
184184
{/snippet}
185185

186186
{#snippet b()}
187-
<section id="output">
187+
<section>
188188
<Output
189189
status={status_visible ? status : null}
190190
{embedded}

0 commit comments

Comments
 (0)