Skip to content

Commit

Permalink
Merge pull request #401 from greggman/dark-light-codemirror
Browse files Browse the repository at this point in the history
make codemirror handle light/dark
  • Loading branch information
toji authored Mar 13, 2024
2 parents 8aeb7c9 + 953812c commit e01d6a3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@codemirror/lang-javascript": "^6.2.2",
"@codemirror/view": "^6.25.0",
"@uiw/codemirror-theme-github": "^4.21.24",
"@uiw/codemirror-theme-monokai": "^4.21.24",
"codemirror": "^6.0.1",
"dat.gui": "^0.7.6",
Expand Down
13 changes: 9 additions & 4 deletions public/css/HomePage.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
--tooltip-background: #eee;
--tooltip-border: rgba(0, 0, 0, 0.1);

--source-tab-color: white;
--source-tab-background: #444;
--source-tab-active-background: #282828;
--source-tab-active-shadow: rgb(167, 167, 167);
--source-tab-color: black;
--source-tab-background: #CCC;
--source-tab-active-background: #FFF;
--source-tab-active-shadow: #EEE;
}

@media (prefers-color-scheme: dark) {
Expand All @@ -34,6 +34,11 @@

--tooltip-background: #111;
--tooltip-border: rgba(255, 255, 255, 0.1);

--source-tab-color: white;
--source-tab-background: #444;
--source-tab-active-background: #282828;
--source-tab-active-shadow: rgb(167, 167, 167);
}
}

Expand Down
39 changes: 34 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { createElem as el } from './utils/elem';
import { SampleInfo, SourceInfo, pageCategories } from './samples';
import { monokai } from '@uiw/codemirror-theme-monokai';
import { githubLight } from '@uiw/codemirror-theme-github';
import { EditorView } from '@codemirror/view';
import { EditorState } from '@codemirror/state';
import { EditorState, Compartment } from '@codemirror/state';
import { javascript } from '@codemirror/lang-javascript';
import { basicSetup } from 'codemirror';
import { Converter } from 'showdown';
Expand Down Expand Up @@ -33,18 +34,30 @@ const titleElem = getElem('#title', sampleElem);
const descriptionElem = getElem('#description', sampleElem);
const menuToggleElem = getElem('#menuToggle') as HTMLInputElement;

const darkMatcher = window.matchMedia('(prefers-color-scheme: dark)');

/**
* Gets the CodeMirrorTheme based on light or dark
*/
function getCodeMirrorTheme() {
const isDarkMode = darkMatcher.matches;
return isDarkMode ? monokai : githubLight;
}

// Get the parts of a string past the last `/`
const basename = (name: string) => name.substring(name.lastIndexOf('/') + 1);

// Make a new codemirror editor
const readOnly = EditorState.readOnly.of(true);
const themeConfig = new Compartment();

async function makeCodeMirrorEditor(parent: HTMLElement, filename: string) {
const source = await (await fetch(filename)).text();

new EditorView({
return new EditorView({
extensions: [
basicSetup,
monokai,
themeConfig.of([getCodeMirrorTheme()]),
EditorView.lineWrapping,
javascript(),
readOnly,
Expand Down Expand Up @@ -96,9 +109,24 @@ function isSameDomain(url: string) {
return new URL(url, window.location.href).origin === window.location.origin;
}

// That current sample so we don't reload an iframe if the user picks the same sample.
// The current sample so we don't reload an iframe if the user picks the same sample.
let currentSampleInfo: SampleInfo | undefined;

// The current set of codeMirrorEditors
const codeMirrorEditors: Promise<EditorView>[] = [];

// Switch the code mirror themes if the color preference changes.
darkMatcher.addEventListener('change', () => {
const theme = getCodeMirrorTheme();
for (const editorViewPromise of codeMirrorEditors) {
editorViewPromise.then((editorView) => {
editorView.dispatch({
effects: themeConfig.reconfigure([theme]),
});
});
}
});

/**
* Change the iframe (and source editors) to the given sample or none
*/
Expand Down Expand Up @@ -154,6 +182,7 @@ function setSampleIFrame(
codeTabsElem.innerHTML = '';
sourcesElem.innerHTML = '';
sourcesElem.style.display = sources.length > 0 ? '' : 'none';
codeMirrorEditors.length = 0;
sources.forEach((source, i) => {
const { path } = source;
const active = (i === 0).toString();
Expand Down Expand Up @@ -182,7 +211,7 @@ function setSampleIFrame(
});
sourcesElem.appendChild(elem);
const url = isSameDomain(path) ? `${filename}/${path}` : source.path;
makeCodeMirrorEditor(elem, url);
codeMirrorEditors.push(makeCodeMirrorEditor(elem, url));
});
}

Expand Down

0 comments on commit e01d6a3

Please sign in to comment.