-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.js
More file actions
113 lines (102 loc) · 3.36 KB
/
main.js
File metadata and controls
113 lines (102 loc) · 3.36 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
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
import { basicSetup, EditorView } from "codemirror";
import { Compartment, EditorState } from "@codemirror/state";
import { javascript } from "@codemirror/lang-javascript";
import { css } from "@codemirror/lang-css";
import { html } from "@codemirror/lang-html";
import { json } from "@codemirror/lang-json";
import { markdown } from "@codemirror/lang-markdown";
import { python } from "@codemirror/lang-python";
import { rust } from "@codemirror/lang-rust";
import { cpp } from "@codemirror/lang-cpp";
import { java } from "@codemirror/lang-java";
import { php } from "@codemirror/lang-php";
import { sql } from "@codemirror/lang-sql";
import { go } from "@codemirror/lang-go";
import { yaml } from "@codemirror/lang-yaml";
import { csharp } from "@replit/codemirror-lang-csharp";
import { nix } from "@replit/codemirror-lang-nix";
import { flavors } from "@catppuccin/palette";
import {
catppuccinFrappe,
catppuccinLatte,
catppuccinMacchiato,
catppuccinMocha,
} from "@catppuccin/codemirror";
const samplesUrl =
"https://raw.githubusercontent.com/catppuccin/catppuccin/refs/heads/main/samples";
const themeConfig = new Compartment();
const langConfig = new Compartment();
const languageProviders = {
"cpp.cpp": cpp(),
"cs.cs": csharp(),
"css.css": css(),
"go.go": go(),
"html.html": html(),
"java.java": java(),
"javascript.js": javascript(),
"json.json": json(),
"jsx.jsx": javascript({ jsx: true }),
"markdown.md": markdown(),
"nix.nix": nix(),
"php.php": php(),
"python.py": python(),
"rust.rs": rust(),
"sql.sql": sql(),
"tsx.tsx": javascript({ typescript: true, jsx: true }),
"typescript.ts": javascript({ typescript: true }),
"yaml.yaml": yaml(),
};
const editor = new EditorView({
doc: "",
parent: document.getElementById("code"),
extensions: [
basicSetup,
langConfig.of(javascript({ typescript: true })),
EditorState.readOnly.of(true),
EditorView.editable.of(false),
EditorView.contentAttributes.of({ tabindex: "0" }),
themeConfig.of([catppuccinMocha]),
],
});
const themes = {
mocha: catppuccinMocha,
macchiato: catppuccinMacchiato,
frappe: catppuccinFrappe,
latte: catppuccinLatte,
};
const input = document.getElementById("flavor-select");
const selectTheme = () => {
const theme = input.options[input.selectedIndex].value;
location.hash = "#" + theme;
// Set styles for the demo.
flavors[theme].colorEntries.forEach(([name, { hex }]) => {
document.documentElement.style.setProperty(`--${name}`, hex);
});
// Update editor with selected theme.
editor.dispatch({
effects: themeConfig.reconfigure([themes[theme]]),
});
};
input.addEventListener("change", selectTheme);
const defaultTheme = location.hash.slice(1);
if (themes[defaultTheme]) input.value = defaultTheme;
const languageSelector = document.getElementById("language-select");
const selectLanguage = () => {
const languageFile = languageSelector.value;
const languageProvider = languageProviders[languageFile] ?? javascript();
fetch(`${samplesUrl}/${languageFile}`)
.then((response) => response.text())
.then((text) => {
editor.dispatch({
changes: {
from: 0,
to: editor.state.doc.length,
insert: text,
},
effects: langConfig.reconfigure(languageProvider),
});
});
};
languageSelector.addEventListener("change", selectLanguage);
selectTheme();
selectLanguage();