-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
88 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Compartment, EditorState } from "@codemirror/state"; | ||
import { EditorView, lineNumbers } from "@codemirror/view"; | ||
import { basicSetup } from "codemirror"; | ||
import { boysAndGirls, tomorrow } from "thememirror"; | ||
|
||
const themeConfig = new Compartment(); | ||
|
||
function createEditorState(initialContents, myTheme) { | ||
let extensions = [basicSetup, themeConfig.of(themeIdentifier(myTheme))]; | ||
|
||
return EditorState.create({ | ||
doc: initialContents, | ||
extensions, | ||
}); | ||
} | ||
|
||
function themeIdentifier(myTheme) { | ||
switch (myTheme) { | ||
case "light": | ||
return tomorrow; | ||
case "dark": | ||
return boysAndGirls; | ||
default: | ||
return tomorrow; | ||
} | ||
} | ||
|
||
function changeEditorTheme(myEditor, myTheme) { | ||
myEditor.dispatch({ | ||
effects: themeConfig.reconfigure(themeIdentifier(myTheme)), | ||
}); | ||
} | ||
|
||
function createEditorView(parent, initialDoc) { | ||
const initState = createEditorState(initialDoc, "light"); | ||
const editorView = new EditorView({state: initState, parent: parent}); | ||
window | ||
.matchMedia("(prefers-color-scheme: dark)") | ||
.addEventListener("change", (event) => { | ||
if (event.matches) { | ||
changeEditorTheme(editorView, "dark"); // Switch to dark mode | ||
} else { | ||
changeEditorTheme(editorView, "light"); // Switch to light mode | ||
} | ||
}); | ||
|
||
if (window.matchMedia("(prefers-color-scheme: dark)").matches) { | ||
changeEditorTheme(editorView, "dark"); | ||
} else { | ||
changeEditorTheme(editorView, "light"); | ||
} | ||
return editorView; | ||
} | ||
|
||
export { createEditorView }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters