8000 Fix `<py-repl theme="dark">` (#320) · Siddharth-cmd/pyscript@73a0d9b · GitHub
[go: up one dir, main page]

Skip to content

Commit 73a0d9b

Browse files
Fix <py-repl theme="dark"> (pyscript#320)
* fix: theme for <py-repl> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove comment * fix: populate initial theme on auto generated repls. Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2526d24 commit 73a0d9b

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

pyscriptjs/src/components/pyrepl.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import { addClasses } from '../utils';
1010
import { BaseEvalElement } from './base';
1111

1212
// Premise used to connect to the first available pyodide interpreter
13+
1314
let pyodideReadyPromise;
1415
let environments;
1516
let currentMode;
1617

1718
pyodideLoaded.subscribe(value => {
1819
pyodideReadyPromise = value;
1920
});
21+
2022
loadedEnvironments.subscribe(value => {
2123
environments = value;
2224
});
@@ -30,8 +32,6 @@ mode.subscribe(value => {
3032
currentMode = value;
3133
});
3234

33-
const languageConf = new Compartment();
34-
3535
function createCmdHandler(el) {
3636
// Creates a codemirror cmd handler that calls the el.evaluate when an event
3737
// triggers that specific cmd
@@ -41,6 +41,15 @@ function createCmdHandler(el) {
4141
return toggleCheckbox;
4242
}
4343

44+
let initialTheme;
45+
function getEditorTheme(el: BaseEvalElement): string {
46+
if (initialTheme) {
47+
return initialTheme;
48+
}
49+
50+
return initialTheme = el.getAttribute('theme');
51+
}
52+
4453
export class PyRepl extends BaseEvalElement {
4554
editor: EditorView;
4655
editorNode: HTMLElement;
@@ -58,6 +67,7 @@ export class PyRepl extends BaseEvalElement {
5867
this.checkId();
5968
this.code = this.innerHTML;
6069
this.innerHTML = '';
70+
const languageConf = new Compartment();
6171

6272
const extensions = [
6373
basicSetup,
@@ -68,29 +78,16 @@ export class PyRepl extends BaseEvalElement {
6878
{ key: 'Shift-Enter', run: createCmdHandler(this) },
6979
]),
7080
];
71-
const customTheme = EditorView.theme({
72-
'&.cm-focused .cm-editor': { outline: '0px' },
73-
'.cm-scroller': { lineHeight: 2.5 },
74-
'.cm-activeLine': { backgroundColor: '#fff' },
75-
'.cm-content': { padding: 0, backgroundColor: '#f5f5f5' },
76-
'&.cm-focused .cm-content': { border: '1px solid #1876d2' },
77-
});
7881

79-
if (!this.hasAttribute('theme')) {
80-
this.theme = this.getAttribute('theme');
81-
if (this.theme == 'dark') {
82-
extensions.push(oneDarkTheme);
83-
}
84-
extensions.push(customTheme);
82+
if (getEditorTheme(this) === 'dark') {
83+
extensions.push(oneDarkTheme);
8584
}
8685

87-
const startState = EditorState.create({
88-
doc: this.code.trim(),
89-
extensions: extensions,
90-
});
91-
9286
this.editor = new EditorView({
93-
state: startState,
87+
state: EditorState.create({
88+
doc: this.code.trim(),
89+
extensions,
90+
}),
9491
parent: this.editorNode,
9592
});
9693

@@ -176,15 +173,19 @@ export class PyRepl extends BaseEvalElement {
176173
if (this.hasAttribute('auto-generate')) {
177174
const nextExecId = parseInt(this.getAttribute('exec-id')) + 1;
178175
const newPyRepl = document.createElement('py-repl');
176+
179177
newPyRepl.setAttribute('root', this.getAttribute('root'));
180178
newPyRepl.id = this.getAttribute('root') + '-' + nextExecId.toString();
181179
newPyRepl.setAttribute('auto-generate', null);
180+
182181
if (this.hasAttribute('output')) {
183182
newPyRepl.setAttribute('output', this.getAttribute('output'));
184183
}
184+
185185
if (this.hasAttribute('std-out')) {
186186
newPyRepl.setAttribute('std-out', this.getAttribute('std-out'));
187187
}
188+
188189
if (this.hasAttribute('std-err')) {
189190
newPyRepl.setAttribute('std-err', this.getAttribute('std-err'));
190191
}

0 commit comments

Comments
 (0)
0