feat: line number updates on selection
mellbacon committed
Aug 1, 2023 at 14:52 UTC
9ce1d4cb517513754e08877afc7653b46371bd2e
1 file changed
+12
-6
src/lib/Editor.svelte
+12
-6
@@ -58,7 +58,15 @@
58
syntaxHighlighting(defaultHighlightStyle, {fallback: true}),
59
keymap.of([
60
...defaultKeymap
61
- ])
61
+ ]),
62
+ EditorView.updateListener.of(async update => {
63
+ if (update.docChanged) {
64
+ await updateContent();
65
+ }
66
+ if (update.state.selection.ranges.some(r => !r.empty)) {
67
+ updateLineInfo();
68
+ }
69
+ })
70
],
71
doc: content
72
})
@@ -69,7 +77,7 @@
77
});
78
79
let _ = null;
72
- export async function updateContent() {
80
+ async function updateContent() {
81
await tick();
82
content = editorView.state.doc.toString();
83
if (await appSettings.get("editor.autosave") === true) {
@@ -87,6 +95,7 @@
95
else {
96
updateSaveState(false);
97
}
98
+ updateLineInfo();
99
}
100
export async function focus() {
101
// takes two ticks to focus for some reason
@@ -105,9 +114,6 @@
114
async function handleKeyDown(e) {
115
let key = e.code;
116
switch(key) {
108
- case "Backspace": case "Enter":
109
- await updateContent();
110
- updateLineInfo();
117
case "ArrowRight": case "ArrowLeft": case "ArrowDown": case "ArrowUp":
118
updateLineInfo();
119
}
@@ -192,7 +198,7 @@
198
}
199
</script>
200
195
-<div bind:this={ref} class="editor" class:hidden on:input={updateContent} on:mousedown={updateLineInfo} on:keydown={handleKeyDown}></div>
201
+<div bind:this={ref} class="editor" class:hidden on:mousedown={updateLineInfo} on:keydown={handleKeyDown}></div>
202
203
<style lang="scss">
204
.editor {