Skip to content

Commit 8a2f348

Browse files
authored
fix #320 (#710)
1 parent a0e3813 commit 8a2f348

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/editor/src/lib/Editor.svelte

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,25 @@
5252
}}
5353
/>
5454

55+
<!-- svelte-ignore a11y_no_static_element_interactions -->
5556
<div
5657
class="container"
5758
bind:this={container}
58-
onfocusin={() => {
59+
onpointerdown={() => {
60+
workspace.enable_tab_indent();
61+
}}
62+
onkeydown={(e) => {
63+
if (e.key !== 'Tab') {
64+
workspace.enable_tab_indent();
65+
}
66+
}}
67+
onfocusin={(e) => {
5968
clearTimeout(remove_focus_timeout);
6069
preserve_editor_focus = true;
6170
}}
6271
onfocusout={() => {
72+
workspace.disable_tab_indent();
73+
6374
// Heuristic: user did refocus themmselves if iframe_took_focus
6475
// doesn't happen in the next few miliseconds. Needed
6576
// because else navigations inside the iframe refocus the editor.

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CompileError, CompileOptions, CompileResult } from 'svelte/compiler';
2-
import { EditorState } from '@codemirror/state';
2+
import { Compartment, EditorState } from '@codemirror/state';
33
import { compile_file } from './compile-worker';
44
import { BROWSER } from 'esm-env';
55
import { basicSetup, EditorView } from 'codemirror';
@@ -51,10 +51,12 @@ function file_type(file: Item) {
5151
return file.name.split('.').pop();
5252
}
5353

54+
const tab_behaviour = new Compartment();
55+
5456
const default_extensions = [
5557
basicSetup,
5658
EditorState.tabSize.of(2),
57-
keymap.of([{ key: 'Tab', run: acceptCompletion }, indentWithTab]),
59+
tab_behaviour.of(keymap.of([{ key: 'Tab', run: acceptCompletion }])),
5860
indentUnit.of('\t'),
5961
theme
6062
];
@@ -209,6 +211,20 @@ export class Workspace {
209211
return item;
210212
}
211213

214+
disable_tab_indent() {
215+
this.#view?.dispatch({
216+
effects: tab_behaviour.reconfigure(keymap.of([{ key: 'Tab', run: acceptCompletion }]))
217+
});
218+
}
219+
220+
enable_tab_indent() {
221+
this.#view?.dispatch({
222+
effects: tab_behaviour.reconfigure(
223+
keymap.of([{ key: 'Tab', run: acceptCompletion }, indentWithTab])
224+
)
225+
});
226+
}
227+
212228
focus() {
213229
setTimeout(() => {
214230
this.#view?.focus();

0 commit comments

Comments
 (0)