Skip to content

Commit

Permalink
feat: Real-time reflection of VSCode theme appearance
Browse files Browse the repository at this point in the history
  • Loading branch information
dineug committed Sep 29, 2024
1 parent c146ba3 commit 50e4b5f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/erd-editor-vscode-webview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"webpack": "^5.91.0",
"webpack-bundle-analyzer": "^4.10.2",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
"webpack-dev-server": "^5.0.4",
"vite": "^5.2.8"
}
}
1 change: 1 addition & 0 deletions packages/erd-editor-vscode-webview/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// <reference types="@types/vscode-webview" />
/// <reference types="vite/client" />
33 changes: 31 additions & 2 deletions packages/erd-editor-vscode-webview/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { ReplicationStoreWorker } from '@dineug/erd-editor-replication-store-worker';
import {
AnyAction,
Appearance,
Emitter,
ThemeOptions,
vscodeExportFileAction,
Expand All @@ -29,6 +30,8 @@ const replicationStoreWorker = new ReplicationStoreWorker({
});
const loading = document.querySelector('#loading');

let appearance: Appearance | 'auto' = 'dark';

const dispatch = (action: AnyAction) => {
vscode.postMessage(action);
};
Expand All @@ -53,12 +56,22 @@ setExportFileCallback(async (blob, options) => {
);
});

const getSystemTheme = () =>
document.body.classList.contains('vscode-light') ? 'light' : 'dark';
function getSystemTheme(): Appearance {
const themeKind = document.body.dataset.vscodeThemeKind;

return themeKind
? themeKind === 'vscode-light'
? Appearance.light
: Appearance.dark
: document.body.classList.contains('vscode-light')
? Appearance.light
: Appearance.dark;
}

const handleChangePresetTheme = (event: Event) => {
const e = event as CustomEvent<ThemeOptions>;
dispatch(vscodeSaveThemeAction(e.detail));
appearance = e.detail.appearance;
};

bridge.on({
Expand Down Expand Up @@ -96,6 +109,10 @@ bridge.on({
dispatchWorker(action);
},
webviewUpdateTheme: ({ payload }) => {
if (payload.appearance) {
appearance = payload.appearance;
}

editor.setPresetTheme({
...payload,
appearance:
Expand All @@ -107,6 +124,18 @@ bridge.on({
},
});

const observer = new MutationObserver(() => {
if (appearance === 'auto') {
editor.setPresetTheme({
appearance: getSystemTheme(),
});
}
});
observer.observe(document.body, {
attributes: true,
attributeFilter: ['class', 'data-vscode-theme-kind'],
});

workerBridge.on({
vscodeSaveValue: action => {
dispatch(action);
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 50e4b5f

Please sign in to comment.