Skip to content

Commit 1d430cd

Browse files
committed
Fetch styles used in blockly theme; fixes #92
The var() function can be used in css styles, but not in css shorthand properties which are used to calculate the sizes of blocks in blockly. Notice that all blockly examples set style values to fixed values. A solution is to retrieve the values of css variables when setting the theme to the workspace. Since changes in jupyterlab settings, e.g., selecting a different theme, may result in changes of css styles, the blockly theme needs to be re-applied after each such change.
1 parent 7590973 commit 1d430cd

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

packages/blockly/src/layout.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Signal } from '@lumino/signaling';
1010
import * as Blockly from 'blockly';
1111

1212
import { BlocklyManager } from './manager';
13-
import { THEME } from './utils';
13+
import { getTheme } from './utils';
1414

1515
/**
1616
* A blockly layout to host the Blockly editor.
@@ -207,6 +207,8 @@ export class BlocklyLayout extends SplitLayout {
207207
*/
208208
protected onFitRequest(msg: Message): void {
209209
super.onFitRequest(msg);
210+
// Can be a result of a theme change
211+
this._workspace.setTheme(getTheme());
210212
this._resizeWorkspace();
211213
}
212214

@@ -218,7 +220,7 @@ export class BlocklyLayout extends SplitLayout {
218220
//inject Blockly with appropiate JupyterLab theme.
219221
this._workspace = Blockly.inject(this._host.node, {
220222
toolbox: this._manager.toolbox,
221-
theme: THEME
223+
theme: getTheme()
222224
});
223225

224226
this._workspace.addChangeListener(() => {

packages/blockly/src/utils.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -352,25 +352,30 @@ export const TOOLBOX = {
352352
]
353353
};
354354

355-
// Defining a Blockly Theme in accordance with the current JupyterLab Theme.
356-
const jupyterlab_theme = Blockly.Theme.defineTheme('jupyterlab', {
357-
name: 'JupyterLab Blockly',
358-
base: Blockly.Themes.Classic,
359-
componentStyles: {
360-
workspaceBackgroundColour: 'var(--jp-layout-color0)',
361-
toolboxBackgroundColour: 'var(--jp-layout-color2)',
362-
toolboxForegroundColour: 'var(--jp-ui-font-color0)',
363-
flyoutBackgroundColour: 'var(--jp-border-color2)',
364-
flyoutForegroundColour: 'var(--jp-layout-color3)',
365-
flyoutOpacity: 1,
366-
scrollbarColour: 'var(--jp-border-color0)',
367-
insertionMarkerOpacity: 0.3,
368-
scrollbarOpacity: 0.4,
369-
cursorColour: 'var(--jp-scrollbar-background-color)'
370-
},
371-
fontStyle: {
372-
family: 'var(--jp-ui-font-family)'
373-
}
374-
});
355+
const getJupyterLabTheme = (): Blockly.Theme => {
356+
const rootStyles = getComputedStyle(document.documentElement);
375357

376-
export const THEME: Blockly.Theme = jupyterlab_theme;
358+
const getStyle = (style: string) => rootStyles.getPropertyValue(style);
359+
360+
return Blockly.Theme.defineTheme('jupyterLab', {
361+
name: 'JupyterLab',
362+
base: Blockly.Themes.Classic,
363+
componentStyles: {
364+
workspaceBackgroundColour: getStyle('--jp-layout-color0'),
365+
toolboxBackgroundColour: getStyle('--jp-layout-color2'),
366+
toolboxForegroundColour: getStyle('--jp-ui-font-color0'),
367+
flyoutBackgroundColour: getStyle('--jp-border-color2'),
368+
flyoutForegroundColour: getStyle('--jp-layout-color3'),
369+
flyoutOpacity: 1,
370+
scrollbarColour: getStyle('--jp-border-color0'),
371+
insertionMarkerOpacity: 0.3,
372+
scrollbarOpacity: 0.4,
373+
cursorColour: getStyle('--jp-scrollbar-background-color')
374+
},
375+
fontStyle: {
376+
family: getStyle('--jp-ui-font-family')
377+
}
378+
});
379+
};
380+
381+
export const getTheme = getJupyterLabTheme;

0 commit comments

Comments
 (0)