Skip to content
This repository was archived by the owner on Dec 2, 2019. It is now read-only.

Commit a8351e2

Browse files
author
Wendell
committed
✨ support theme switch
1 parent 007890f commit a8351e2

File tree

9 files changed

+107
-13
lines changed

9 files changed

+107
-13
lines changed

angular.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"output": "/assets/vs/"
3232
}
3333
],
34-
"styles": ["src/styles.scss"],
34+
"styles": ["src/styles.scss", "src/override.less"],
3535
"scripts": [],
3636
"es5BrowserSupport": true
3737
},

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"@angular/router": "~7.2.0",
2222
"@silicic/share-styles": "0.0.1",
2323
"core-js": "^2.5.4",
24+
"ng-zorro-antd": "^7.4.1",
2425
"rxjs": "~6.3.3",
2526
"tslib": "^1.9.0",
2627
"zone.js": "~0.8.26"

projects/monaco-ng/src/lib/monaco.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
} from './typings';
1212
import { tryTriggerFunc } from './utils';
1313

14+
declare const monaco: any;
15+
1416
/**
1517
*
1618
*/
@@ -71,6 +73,10 @@ export class MonacoService {
7173
}
7274

7375
this.option = { ...this.option, ...option };
76+
77+
if (option.theme) {
78+
monaco.editor.setTheme(option.theme);
79+
}
7480
}
7581

7682
private loadMonacoScript(): void {

src/app/app.component.html

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
<h1 class="header">
44
Monaco for Angular
55
</h1>
6+
<div class="theme-switcher">
7+
<nz-switch [(ngModel)]="dark"
8+
[nzUnCheckedChildren]="lightTpl"
9+
[nzCheckedChildren]="darkTpl"
10+
(ngModelChange)="onToggleTheme()"></nz-switch>
11+
<ng-template #darkTpl>
12+
<i nz-icon
13+
nzType="bulb"></i>
14+
</ng-template>
15+
<ng-template #lightTpl>
16+
<i nz-icon
17+
nzType="poweroff"></i>
18+
</ng-template>
19+
</div>
620
</header>
721
<div class="content-container">
822
<section id="demo">
@@ -13,7 +27,9 @@ <h3>Normal Editor</h3>
1327
</p>
1428
<div class="full-width-container">
1529
<div class="editor-container">
16-
<si-monaco-editor id="editor" [ngModel]="code" [editorOption]="editorOption"></si-monaco-editor>
30+
<si-monaco-editor id="editor"
31+
[ngModel]="code"
32+
[editorOption]="editorOption"></si-monaco-editor>
1733
</div>
1834
</div>
1935
<p class="center"><i>Try to type on it.</i></p>
@@ -23,8 +39,12 @@ <h3>Diff Editor</h3>
2339
</p>
2440
<div class="full-width-container">
2541
<div class="editor-container">
26-
<si-monaco-editor id="editor" [ngModel]="code" [mode]="'diff'" [originalText]="originalText"
27-
[editorOption]="{ language: 'typescript' }" (ngModelChange)="onModelChange($event)">
42+
<si-monaco-editor id="editor"
43+
[ngModel]="code"
44+
[mode]="'diff'"
45+
[originalText]="originalText"
46+
[editorOption]="{ language: 'typescript' }"
47+
(ngModelChange)="onModelChange($event)">
2848
</si-monaco-editor>
2949
</div>
3050
</div>

src/app/app.component.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
.theme-switcher {
2+
height: 42px;
3+
line-height: 42px;
4+
margin-bottom: 14px;
5+
}
6+
17
.editor-container {
28
margin: auto;
39
height: 300px;

src/app/app.component.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import { Component } from '@angular/core';
1+
import { Component, Inject, OnInit, Renderer2 } from '@angular/core';
2+
import { DOCUMENT } from '@angular/platform-browser';
23
import { editor } from 'monaco-editor';
4+
import { MonacoService } from 'projects/monaco-ng/src/public-api';
35

46
@Component({
57
selector: 'app-root',
68
templateUrl: './app.component.html',
79
styleUrls: ['./app.component.scss']
810
})
9-
export class AppComponent {
11+
export class AppComponent implements OnInit {
1012
title = 'monaco-ng';
13+
dark = false;
1114

1215
editorOption: editor.IEditorConstructionOptions = {
1316
scrollBeyondLastLine: false,
@@ -34,7 +37,28 @@ export class AppComponent {
3437
title = 'monaco-ng';
3538
}`;
3639

40+
constructor(
41+
private monacoService: MonacoService,
42+
private renderer: Renderer2,
43+
@Inject(DOCUMENT) private document: any
44+
) {}
45+
3746
onModelChange(value: string): void {
3847
console.log(value);
3948
}
49+
50+
onToggleTheme(): void {
51+
const next = this.dark ? 'vs-dark' : 'vs';
52+
this.monacoService.updateDefaultOption({ theme: next });
53+
this.toggleBodyTheme();
54+
}
55+
56+
toggleBodyTheme(): void {
57+
this.renderer.addClass(this.document.body, this.dark ? 'dark' : 'light');
58+
this.renderer.removeClass(this.document.body, this.dark ? 'light' : 'dark');
59+
}
60+
61+
ngOnInit(): void {
62+
this.toggleBodyTheme();
63+
}
4064
}

src/app/app.module.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { BrowserModule } from '@angular/platform-browser';
21
import { NgModule } from '@angular/core';
3-
4-
import { AppComponent } from './app.component';
2+
import { FormsModule } from '@angular/forms';
3+
import { BrowserModule } from '@angular/platform-browser';
4+
import { BulbOutline, PoweroffOutline } from '@ant-design/icons-angular/icons';
5+
import { NZ_ICONS, NzIconModule, NzSwitchModule } from 'ng-zorro-antd';
56
import {
6-
SiMonacoModule,
7-
MONACO_CONFIG
7+
MONACO_CONFIG,
8+
SiMonacoModule
89
} from 'projects/monaco-ng/src/public-api';
9-
import { FormsModule } from '@angular/forms';
10+
import { AppComponent } from './app.component';
1011

1112
@NgModule({
1213
declarations: [AppComponent],
13-
imports: [BrowserModule, FormsModule, SiMonacoModule],
14+
imports: [
15+
BrowserModule,
16+
FormsModule,
17+
SiMonacoModule,
18+
NzSwitchModule,
19+
NzIconModule
20+
],
1421
providers: [
1522
{
1623
provide: MONACO_CONFIG,
@@ -23,6 +30,10 @@ import { FormsModule } from '@angular/forms';
2330
console.log('load');
2431
}
2532
}
33+
},
34+
{
35+
provide: NZ_ICONS,
36+
useValue: [BulbOutline, PoweroffOutline]
2637
}
2738
],
2839
bootstrap: [AppComponent]

src/override.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@import '../node_modules/ng-zorro-antd/style/entry.less';
2+
@import '../node_modules/ng-zorro-antd/switch/style/entry.less';
3+
4+
@primary-color: rgb(255, 177, 75);

src/styles.scss

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
11
/* You can add global styles to this file, and also import other style files */
22
@import '../node_modules/@silicic/share-styles/dist/index.css';
3+
4+
.dark {
5+
--text: white;
6+
--background: rgb(27, 27, 27);
7+
}
8+
9+
.light {
10+
--text: #333;
11+
--background: #fff;
12+
}
13+
14+
body {
15+
h1,
16+
h2,
17+
h3,
18+
p,
19+
li {
20+
color: var(--text);
21+
}
22+
23+
background-color: var(--background) !important; // Fuck antd!
24+
}

0 commit comments

Comments
 (0)