Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color theme selector (dark mode) #1

Open
wants to merge 1 commit into
base: editor-ochafik.com
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion example/www/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>
<html>
<html class="monaco-editor">

<head>
<meta name="viewport" content="width=device-width, user-scalable=no" />
Expand Down Expand Up @@ -357,6 +357,15 @@ <h2>OpenSCAD Playground<sup style="color: red">ALPHA</sup></h2>
<span class="text-fragment settings"><input type="checkbox" id="show-experimental">Show experiments</span>

<span class="spacer settings"></span>

<span class="text-fragment settings">
<label for="set-theme">Theme:</label>
<select name="set-theme" id="set-theme">
<option value="2">OS</option>
<option value="1">Dark</option>
<option value="0">Light</option>
</select>
</span>
</div>
<div id="viewer" tabindex="0"></div>
<span id="features" class="features"></span>
Expand Down
21 changes: 21 additions & 0 deletions example/www/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,27 @@ function pollCameraChanges() {
}, 1000); // TODO only if active tab
}

const setTheme = ( () => {
let html = document.querySelector('html'),
select = document.querySelector('select#set-theme'),
theme = {
editor: ['vs', 'vs-dark'],
html: ['light', 'dark']
};
const getPrefersColor = () => (window.matchMedia('(prefers-color-scheme: dark)').matches) ? 1 : 0,
set = (value) => {
value = (isNaN(+value) || value > 1) ? 2 : +value
let scheme = (value > 1) ? getPrefersColor() : value
html.style.colorScheme = theme.html[scheme]
monaco.editor.setTheme(theme.editor[scheme])
window.localStorage.prefersColor = value
return {value, scheme}
};
select.value = set(window.localStorage.prefersColor ?? 2).value
select.oninput = () => set(select.value)
return set
})();

try {
const workingDir = '/home';
const fs = await createEditorFS(workingDir);
Expand Down