1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
<script lang="ts">
/*** IMPORT ------------------------------------------- ***/
import { browser } from "$app/environment";
import { onMount } from "svelte";
import type { Extension } from "@codemirror/state";
import type { EditorView } from "@codemirror/view";
import type { GraphQLSchema } from "graphql";
/*** UTILITY ------------------------------------------ ***/
type Props = {
language?: "graphql" | "json";
onChange: (value: string) => void;
readOnly?: boolean;
schema?: string;
theme?: Extension;
value: string;
};
let {
language = "graphql",
onChange,
readOnly = false,
schema,
theme,
value
}: Props = $props();
let buildSchemaFn = $state<((sdl: string) => GraphQLSchema) | null>(null);
let container: HTMLDivElement;
let updateSchemaFn = $state<((v: EditorView, s: GraphQLSchema) => void) | null>(null);
let view = $state<EditorView | null>(null);
onMount(() => {
if (!browser)
return;
let disposed = false;
(async () => {
const [
{ EditorView: EV, keymap, lineNumbers, highlightActiveLine },
{ EditorState },
{ defaultKeymap, history, historyKeymap },
{ syntaxHighlighting, defaultHighlightStyle, bracketMatching, indentOnInput },
{ closeBrackets, closeBracketsKeymap },
{ graphql, updateSchema },
{ json },
{ buildSchema }
] = await Promise.all([
import("@codemirror/view"),
import("@codemirror/state"),
import("@codemirror/commands"),
import("@codemirror/language"),
import("@codemirror/autocomplete"),
import("cm6-graphql"),
import("@codemirror/lang-json"),
import("graphql")
]);
if (disposed)
return;
const themeExt: Extension = theme ?? (await import("@codemirror/theme-one-dark")).oneDark;
const languageExt = language === "graphql" ?
graphql(schema ? buildSchema(schema) : undefined) :
json();
const instance = new EV({
parent: container,
state: EditorState.create({
doc: value,
extensions: [
lineNumbers(),
highlightActiveLine(),
history(),
bracketMatching(),
closeBrackets(),
indentOnInput(),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
keymap.of([...closeBracketsKeymap, ...defaultKeymap, ...historyKeymap]),
languageExt,
themeExt,
EV.editable.of(!readOnly),
EV.updateListener.of((u) => {
if (u.docChanged)
onChange(u.state.doc.toString());
})
]
})
});
view = instance;
updateSchemaFn = updateSchema;
buildSchemaFn = buildSchema;
})();
return () => {
disposed = true;
view?.destroy();
};
});
$effect(() => {
if (!view || !updateSchemaFn || !buildSchemaFn)
return;
if (language !== "graphql" || !schema)
return;
try {
updateSchemaFn(view, buildSchemaFn(schema));
} catch {
// Invalid SDL — silently skip; editor keeps working without schema awareness
}
});
</script>
<style lang="scss">
.editor {
height: 100%;
width: 100%;
:global(.cm-editor) {
height: 100%;
}
:global(.cm-scroller) {
font-family: inherit;
}
}
</style>
<div bind:this={container} class="editor"></div>
|