-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuseCodemirrorModeExtension.hooks.ts
More file actions
57 lines (51 loc) · 1.94 KB
/
Copy pathuseCodemirrorModeExtension.hooks.ts
File metadata and controls
57 lines (51 loc) · 1.94 KB
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
//adapted v6 modes imports
import { javascript } from "@codemirror/lang-javascript";
import { json } from "@codemirror/lang-json";
import { markdown } from "@codemirror/lang-markdown";
import { sql } from "@codemirror/lang-sql";
import { xml } from "@codemirror/lang-xml";
import { yaml } from "@codemirror/lang-yaml";
import {html} from "@codemirror/lang-html"
import { defaultHighlightStyle, LanguageSupport, StreamLanguage, StreamParser } from "@codemirror/language";
//legacy mode imports
import { jinja2 } from "@codemirror/legacy-modes/mode/jinja2";
import { mathematica } from "@codemirror/legacy-modes/mode/mathematica";
import { ntriples } from "@codemirror/legacy-modes/mode/ntriples";
import { python } from "@codemirror/legacy-modes/mode/python";
import { sparql } from "@codemirror/legacy-modes/mode/sparql";
import { turtle } from "@codemirror/legacy-modes/mode/turtle";
//adaptations
import { adaptedSyntaxHighlighting } from "../tests/codemirrorTestHelper";
const supportedModes = {
markdown,
python,
sparql,
turtle,
xml,
yaml,
jinja2,
json,
ntriples,
mathematica,
sql,
javascript,
html
} as const;
export const supportedCodeEditorModes = Object.keys(supportedModes) as Array<keyof typeof supportedModes>;
export type SupportedCodeEditorModes = (typeof supportedCodeEditorModes)[number];
const v6AdaptedModes: ReadonlyMap<SupportedCodeEditorModes, boolean> = new Map([
["json", true],
["markdown", true],
["xml", true],
["sql", true],
["yaml", true],
["javascript", true],
["html", true]
]);
export const useCodeMirrorModeExtension = (mode?: SupportedCodeEditorModes) => {
return !mode
? adaptedSyntaxHighlighting(defaultHighlightStyle)
: v6AdaptedModes.has(mode)
? ((typeof supportedModes[mode] === "function" ? supportedModes[mode] : () => null) as () => LanguageSupport)()
: StreamLanguage?.define(supportedModes[mode] as StreamParser<unknown>);
};