feat(codemirror): add model for themes

This commit is contained in:
Elian Doran 2025-05-11 20:19:40 +03:00
parent cf7c5d3cb4
commit 36246104dd
No known key found for this signature in database
4 changed files with 85 additions and 17 deletions

View File

@ -31,6 +31,8 @@
"@codemirror/legacy-modes": "6.5.1",
"@codemirror/search": "6.5.10",
"@codemirror/view": "6.36.7",
"@fsegurai/codemirror-theme-abcdef": "6.1.4",
"@fsegurai/codemirror-theme-abyss": "6.1.4",
"@replit/codemirror-lang-nix": "6.0.1",
"@replit/codemirror-vim": "6.3.0",
"@ssddanbrown/codemirror-lang-smarty": "1.0.0",

View File

@ -0,0 +1,19 @@
import type { Extension } from '@codemirror/state';
export interface ThemeDefinition {
name: string;
load(): Promise<Extension>;
}
const themes: ThemeDefinition[] = [
{
name: "abyss",
load: async () => (await import("@fsegurai/codemirror-theme-abyss")).abyss
},
{
name: "abcdef",
load: async () => (await import("@fsegurai/codemirror-theme-abcdef")).abcdef
}
]
export default themes;

View File

@ -6,6 +6,9 @@ import { highlightSelectionMatches } from "@codemirror/search";
import { vim } from "@replit/codemirror-vim";
import byMimeType from "./syntax_highlighting.js";
import smartIndentWithTab from "./extensions/custom_tab.js";
import type { ThemeDefinition } from "./color_themes.js";
export * from "./color_themes.js";
type ContentChangedListener = () => void;
@ -23,10 +26,12 @@ export default class CodeMirror extends EditorView {
private config: EditorConfig;
private languageCompartment: Compartment;
private historyCompartment: Compartment;
private themeCompartment: Compartment;
constructor(config: EditorConfig) {
const languageCompartment = new Compartment();
const historyCompartment = new Compartment();
const themeCompartment = new Compartment();
let extensions: Extension[] = [];
@ -38,6 +43,7 @@ export default class CodeMirror extends EditorView {
...extensions,
languageCompartment.of([]),
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
themeCompartment.of([]),
highlightActiveLine(),
highlightSelectionMatches(),
bracketMatching(),
@ -78,6 +84,7 @@ export default class CodeMirror extends EditorView {
this.config = config;
this.languageCompartment = languageCompartment;
this.historyCompartment = historyCompartment;
this.themeCompartment = themeCompartment;
}
#onDocumentUpdated(v: ViewUpdate) {
@ -100,6 +107,10 @@ export default class CodeMirror extends EditorView {
})
}
async setTheme(theme: ThemeDefinition) {
this.themeCompartment.reconfigure(await theme.load());
}
/**
* Clears the history of undo/redo. Generally useful when changing to a new document.
*/

70
pnpm-lock.yaml generated
View File

@ -1187,6 +1187,12 @@ importers:
'@codemirror/view':
specifier: 6.36.7
version: 6.36.7
'@fsegurai/codemirror-theme-abcdef':
specifier: 6.1.4
version: 6.1.4(@codemirror/language@6.11.0)(@codemirror/state@6.5.2)(@codemirror/view@6.36.7)(@lezer/highlight@1.2.1)
'@fsegurai/codemirror-theme-abyss':
specifier: 6.1.4
version: 6.1.4(@codemirror/language@6.11.0)(@codemirror/state@6.5.2)(@codemirror/view@6.36.7)(@lezer/highlight@1.2.1)
'@replit/codemirror-lang-nix':
specifier: 6.0.1
version: 6.0.1(@codemirror/autocomplete@6.18.6)(@codemirror/language@6.11.0)(@codemirror/state@6.5.2)(@codemirror/view@6.36.7)(@lezer/common@1.2.3)(@lezer/highlight@1.2.1)(@lezer/lr@1.4.2)
@ -3095,6 +3101,22 @@ packages:
'@floating-ui/utils@0.2.9':
resolution: {integrity: sha512-MDWhGtE+eHw5JW7lq4qhc5yRLS11ERl1c7Z6Xd0a58DozHES6EnNNwUWbMiG4J9Cgj053Bhk8zvlhFYKVhULwg==}
'@fsegurai/codemirror-theme-abcdef@6.1.4':
resolution: {integrity: sha512-GN0+41KESPiMR87jwlNq7DTqNh7NlVe/DRZwXY1sfGoLGWFmFCmRshU44wfaW9BDMtLVdddFi9zhJyeEk3rfcw==}
peerDependencies:
'@codemirror/language': ^6.0.0
'@codemirror/state': ^6.0.0
'@codemirror/view': ^6.0.0
'@lezer/highlight': ^1.0.0
'@fsegurai/codemirror-theme-abyss@6.1.4':
resolution: {integrity: sha512-Rr5LM7z3LSoeZobunHe3cxlpQ+FU+eo5hagxoAlW+UCl50VGK55h7M8tI/XF8Ry3H7G2PWwacGHjweYiRhhzlA==}
peerDependencies:
'@codemirror/language': ^6.0.0
'@codemirror/state': ^6.0.0
'@codemirror/view': ^6.0.0
'@lezer/highlight': ^1.0.0
'@fullcalendar/core@6.1.17':
resolution: {integrity: sha512-0W7lnIrv18ruJ5zeWBeNZXO8qCWlzxDdp9COFEsZnyNjiEhUVnrW/dPbjRKYpL0edGG0/Lhs0ghp1z/5ekt8ZA==}
@ -15327,27 +15349,23 @@ snapshots:
- supports-color
- utf-8-validate
'@ckeditor/ckeditor5-collaboration-core@45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)':
'@ckeditor/ckeditor5-collaboration-core@45.0.0':
dependencies:
'@ckeditor/ckeditor5-comments': 45.0.0
'@ckeditor/ckeditor5-core': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-icons': 45.0.0
'@ckeditor/ckeditor5-theme-lark': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-track-changes': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-track-changes': 45.0.0
'@ckeditor/ckeditor5-ui': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-utils': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@types/luxon': 3.4.2
ckeditor5: 45.0.0(patch_hash=8331a09d41443b39ea1c784daaccfeb0da4f9065ed556e7de92e9c77edd9eb41)
luxon: 3.5.0
transitivePeerDependencies:
- bufferutil
- supports-color
- utf-8-validate
'@ckeditor/ckeditor5-comments@45.0.0':
dependencies:
'@ckeditor/ckeditor5-clipboard': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-collaboration-core': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-collaboration-core': 45.0.0
'@ckeditor/ckeditor5-core': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-engine': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-enter': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
@ -15368,7 +15386,7 @@ snapshots:
'@ckeditor/ckeditor5-comments@45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)':
dependencies:
'@ckeditor/ckeditor5-clipboard': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-collaboration-core': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-collaboration-core': 45.0.0
'@ckeditor/ckeditor5-core': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-engine': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-enter': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
@ -15807,6 +15825,14 @@ snapshots:
- supports-color
- utf-8-validate
'@ckeditor/ckeditor5-highlight@45.0.0':
dependencies:
'@ckeditor/ckeditor5-core': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-icons': 45.0.0
'@ckeditor/ckeditor5-ui': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-utils': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
ckeditor5: 45.0.0(patch_hash=8331a09d41443b39ea1c784daaccfeb0da4f9065ed556e7de92e9c77edd9eb41)
'@ckeditor/ckeditor5-highlight@45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)':
dependencies:
'@ckeditor/ckeditor5-core': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
@ -16189,7 +16215,7 @@ snapshots:
'@ckeditor/ckeditor5-operations-compressor': 45.0.0
'@ckeditor/ckeditor5-revision-history': 45.0.0
'@ckeditor/ckeditor5-theme-lark': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-track-changes': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-track-changes': 45.0.0
'@ckeditor/ckeditor5-ui': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-utils': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
ckeditor5: 45.0.0(patch_hash=8331a09d41443b39ea1c784daaccfeb0da4f9065ed556e7de92e9c77edd9eb41)
@ -16390,7 +16416,7 @@ snapshots:
- supports-color
- utf-8-validate
'@ckeditor/ckeditor5-track-changes@45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)':
'@ckeditor/ckeditor5-track-changes@45.0.0':
dependencies:
'@ckeditor/ckeditor5-clipboard': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-code-block': 45.0.0(patch_hash=2361d8caad7d6b5bddacc3a3b4aa37dbfba260b1c1b22a450413a79c1bb1ce95)
@ -16402,7 +16428,7 @@ snapshots:
'@ckeditor/ckeditor5-find-and-replace': 45.0.0
'@ckeditor/ckeditor5-font': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-heading': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-highlight': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-highlight': 45.0.0
'@ckeditor/ckeditor5-icons': 45.0.0
'@ckeditor/ckeditor5-image': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-link': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
@ -16419,10 +16445,6 @@ snapshots:
ckeditor5: 45.0.0(patch_hash=8331a09d41443b39ea1c784daaccfeb0da4f9065ed556e7de92e9c77edd9eb41)
ckeditor5-collaboration: 45.0.0
es-toolkit: 1.32.0
transitivePeerDependencies:
- bufferutil
- supports-color
- utf-8-validate
'@ckeditor/ckeditor5-typing@45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)':
dependencies:
@ -17551,6 +17573,20 @@ snapshots:
'@floating-ui/utils@0.2.9': {}
'@fsegurai/codemirror-theme-abcdef@6.1.4(@codemirror/language@6.11.0)(@codemirror/state@6.5.2)(@codemirror/view@6.36.7)(@lezer/highlight@1.2.1)':
dependencies:
'@codemirror/language': 6.11.0
'@codemirror/state': 6.5.2
'@codemirror/view': 6.36.7
'@lezer/highlight': 1.2.1
'@fsegurai/codemirror-theme-abyss@6.1.4(@codemirror/language@6.11.0)(@codemirror/state@6.5.2)(@codemirror/view@6.36.7)(@lezer/highlight@1.2.1)':
dependencies:
'@codemirror/language': 6.11.0
'@codemirror/state': 6.5.2
'@codemirror/view': 6.36.7
'@lezer/highlight': 1.2.1
'@fullcalendar/core@6.1.17':
dependencies:
preact: 10.12.1
@ -21873,7 +21909,7 @@ snapshots:
ckeditor5-collaboration@45.0.0:
dependencies:
'@ckeditor/ckeditor5-collaboration-core': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-collaboration-core': 45.0.0
ckeditor5@45.0.0(patch_hash=8331a09d41443b39ea1c784daaccfeb0da4f9065ed556e7de92e9c77edd9eb41):
dependencies:
@ -21904,7 +21940,7 @@ snapshots:
'@ckeditor/ckeditor5-font': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-fullscreen': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-heading': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-highlight': 45.0.0(bufferutil@4.0.9)(utf-8-validate@6.0.5)
'@ckeditor/ckeditor5-highlight': 45.0.0
'@ckeditor/ckeditor5-horizontal-line': 45.0.0
'@ckeditor/ckeditor5-html-embed': 45.0.0
'@ckeditor/ckeditor5-html-support': 45.0.0