Skip to content

Commit 58d6bac

Browse files
committed
refactor: switch to svelte-codemirror-editor
Also drop the dependency on codemirror, which just provides presets. This restores the ability to programmatically set the value from Svelte. You could only set the initial value before, but that's not very Svelte.
1 parent 8104f05 commit 58d6bac

File tree

4 files changed

+63
-51
lines changed

4 files changed

+63
-51
lines changed

apps/petal-notes/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"@lezer/highlight": "catalog:",
3434
"@lezer/markdown": "catalog:",
3535
"codemirror": "catalog:",
36-
"hypermd": "catalog:"
36+
"hypermd": "catalog:",
37+
"svelte-codemirror-editor": "catalog:"
3738
},
3839
"devDependencies": {
3940
"@chromatic-com/storybook": "catalog:",

apps/petal-notes/src/lib/hypermd.svelte

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,74 +21,67 @@
2121
dropCursor,
2222
EditorView,
2323
highlightActiveLine,
24-
highlightActiveLineGutter,
2524
keymap,
2625
rectangularSelection,
2726
} from "@codemirror/view";
2827
import { printTree } from "@lezer-unofficial/printer";
2928
import { GFM } from "@lezer/markdown";
3029
import { minimalSetup } from "codemirror";
3130
import { hypermdExtensions, hypermdMarkdownExtensions } from "hypermd";
32-
import { onMount } from "svelte";
31+
import CodeMirror from "svelte-codemirror-editor";
3332
3433
export interface Props {
3534
value: string;
3635
}
3736
3837
let { value = $bindable() }: Props = $props();
3938
40-
let container: HTMLDivElement;
39+
const extensions = [
40+
minimalSetup,
41+
dropCursor(),
42+
EditorState.allowMultipleSelections.of(true),
43+
indentOnInput(),
44+
bracketMatching(),
45+
closeBrackets(),
46+
autocompletion(),
47+
rectangularSelection(),
48+
crosshairCursor(),
49+
highlightActiveLine(),
50+
highlightSelectionMatches(),
4151
42-
onMount(() => {
43-
new EditorView({
44-
doc: value,
45-
extensions: [
46-
minimalSetup,
52+
EditorView.lineWrapping,
53+
hypermdExtensions,
54+
keymap.of([
55+
...closeBracketsKeymap,
56+
...searchKeymap,
57+
...foldKeymap,
58+
...completionKeymap,
4759
48-
highlightActiveLineGutter(),
49-
dropCursor(),
50-
EditorState.allowMultipleSelections.of(true),
51-
indentOnInput(),
52-
bracketMatching(),
53-
closeBrackets(),
54-
autocompletion(),
55-
rectangularSelection(),
56-
crosshairCursor(),
57-
highlightActiveLine(),
58-
highlightSelectionMatches(),
60+
indentWithTab,
5961
60-
markdown({
61-
codeLanguages: languages,
62-
extensions: [GFM, hypermdMarkdownExtensions],
63-
}),
64-
EditorView.lineWrapping,
65-
hypermdExtensions,
66-
keymap.of([
67-
...closeBracketsKeymap,
68-
...searchKeymap,
69-
...foldKeymap,
70-
...completionKeymap,
62+
// Debugging
63+
{
64+
key: "Alt-p",
65+
run: (view) => {
66+
if (import.meta.env.PROD) return true;
7167
72-
indentWithTab,
68+
console.debug(
69+
printTree(syntaxTree(view.state), view.state.doc.toString()),
70+
);
7371
74-
// Debugging
75-
{
76-
key: "Alt-p",
77-
run: (view) => {
78-
if (import.meta.env.PROD) return true;
79-
80-
console.debug(
81-
printTree(syntaxTree(view.state), view.state.doc.toString()),
82-
);
83-
84-
return true;
85-
},
86-
},
87-
]),
88-
],
89-
parent: container,
90-
});
91-
});
72+
return true;
73+
},
74+
},
75+
]),
76+
];
9277
</script>
9378

94-
<div bind:this={container}></div>
79+
<CodeMirror
80+
basic={false}
81+
{extensions}
82+
lang={markdown({
83+
codeLanguages: languages,
84+
extensions: [GFM, hypermdMarkdownExtensions],
85+
})}
86+
bind:value
87+
/>

pnpm-lock.yaml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ catalog:
5858
storybook: ^8.5.6
5959
svelte: ^5.20.1
6060
svelte-check: ^4.1.4
61+
svelte-codemirror-editor: ^1.4.1
6162
svelte2tsx: ^0.7.34
6263
tailwindcss: ^4.0.6
6364
typed-query-selector: ^2.12.0

0 commit comments

Comments
 (0)