|
2 | 2 | import { onMount } from "svelte"; |
3 | 3 | import { EditorView } from "@codemirror/view"; |
4 | 4 | import { basicSetup } from "codemirror"; |
5 | | - import { EditorState, StateEffect } from "@codemirror/state"; |
6 | | - import { syntaxHighlighting } from "@codemirror/language"; |
7 | | - import { customTheme, customHighlight } from "$lib/editor-theme"; |
| 5 | + import { EditorState } from "@codemirror/state"; |
| 6 | + import { customTheme } from "$lib/editor-theme"; |
8 | 7 | import { getLanguageExtension, type LanguageType } from "$lib/editor-lang"; |
| 8 | + import { shikiToCodeMirror, updateEffect } from "@cmshiki/shiki"; |
| 9 | + import { getSingletonHighlighter } from "shiki"; |
9 | 10 |
|
10 | 11 | let { |
11 | 12 | value = $bindable(""), |
12 | 13 | language = "yaml" as LanguageType, |
13 | 14 | editable = false, |
| 15 | + theme = "ayu-dark", |
14 | 16 | } = $props(); |
15 | 17 |
|
16 | 18 | let editorRef: HTMLDivElement; |
17 | 19 | let view: EditorView | null = null; |
18 | 20 |
|
19 | | - let extensionsConfig = $derived([ |
20 | | - basicSetup, |
21 | | - ...getLanguageExtension(language), |
22 | | - customTheme, |
23 | | - syntaxHighlighting(customHighlight), |
24 | | - EditorState.readOnly.of(!editable), |
25 | | - EditorView.editable.of(editable), |
26 | | - ]); |
| 21 | + async function getThemeBg(themeName: string): Promise<string> { |
| 22 | + const highlighter = await getSingletonHighlighter({ |
| 23 | + themes: [themeName], |
| 24 | + langs: [], |
| 25 | + }); |
| 26 | + return highlighter.getTheme(themeName).bg ?? "#1e1e1e"; |
| 27 | + } |
| 28 | +
|
| 29 | + function toShikiLang(lang: LanguageType): string | null { |
| 30 | + return lang === "plain_text" ? null : lang; |
| 31 | + } |
| 32 | +
|
| 33 | + async function buildEditor(doc: string) { |
| 34 | + const shikiLang = toShikiLang(language); |
| 35 | + const [bg, shikiResult] = await Promise.all([ |
| 36 | + getThemeBg(theme), |
| 37 | + shikiLang |
| 38 | + ? shikiToCodeMirror({ |
| 39 | + lang: shikiLang, |
| 40 | + theme, |
| 41 | + engine: "javascript", |
| 42 | + }) |
| 43 | + : Promise.resolve(null), |
| 44 | + ]); |
| 45 | +
|
| 46 | + const themeExtension = EditorView.theme({ |
| 47 | + "&": { backgroundColor: bg }, |
| 48 | + ".cm-gutters": { backgroundColor: bg, borderRight: "none" }, |
| 49 | + ".cm-activeLineGutter": { backgroundColor: `${bg}cc` }, |
| 50 | + }); |
27 | 51 |
|
28 | | - onMount(() => { |
29 | | - view = new EditorView({ |
| 52 | + return new EditorView({ |
30 | 53 | state: EditorState.create({ |
31 | | - doc: value, |
32 | | - extensions: extensionsConfig, |
| 54 | + doc, |
| 55 | + extensions: [ |
| 56 | + basicSetup, |
| 57 | + ...getLanguageExtension(language), |
| 58 | + customTheme(bg), |
| 59 | + themeExtension, |
| 60 | + ...(shikiResult ? [shikiResult.shiki] : []), |
| 61 | + EditorState.readOnly.of(!editable), |
| 62 | + EditorView.editable.of(editable), |
| 63 | + ], |
33 | 64 | }), |
34 | 65 | parent: editorRef, |
35 | 66 | dispatchTransactions(trs, view) { |
|
40 | 71 | } |
41 | 72 | }, |
42 | 73 | }); |
| 74 | + } |
| 75 | +
|
| 76 | + onMount(async () => { |
| 77 | + view = await buildEditor(value); |
43 | 78 | }); |
44 | 79 |
|
45 | 80 | $effect(() => { |
46 | | - if (view) { |
47 | | - view.dispatch({ |
48 | | - effects: StateEffect.reconfigure.of(extensionsConfig), |
49 | | - }); |
50 | | - } |
| 81 | + // reactive deps |
| 82 | + const _lang = language; |
| 83 | + const _theme = theme; |
| 84 | + const _editable = editable; |
| 85 | +
|
| 86 | + if (!view) return; |
| 87 | +
|
| 88 | + (async () => { |
| 89 | + const currentDoc = view!.state.doc.toString(); |
| 90 | + view!.destroy(); |
| 91 | + view = await buildEditor(currentDoc); |
| 92 | + })(); |
51 | 93 | }); |
52 | 94 | </script> |
53 | 95 |
|
54 | | -<div bind:this={editorRef} class="w-full h-full"></div> |
| 96 | +<div |
| 97 | + bind:this={editorRef} |
| 98 | + class="w-full h-full overflow-scroll" |
| 99 | + spellcheck="false" |
| 100 | +></div> |
55 | 101 |
|
56 | 102 | <style> |
57 | 103 | :global(.cm-editor) { |
58 | 104 | height: 100%; |
59 | 105 | outline: none !important; |
60 | 106 | } |
61 | | -
|
62 | 107 | :global(.cm-scroller) { |
63 | 108 | font-family: "Cascadia Code", "Fira Code", monospace !important; |
64 | 109 | } |
|
0 commit comments