55'use strict' ;
66
77import { Theme , IThemeRule } from 'vs/editor/common/modes/supports/tokenization' ;
8- import { IStandaloneColorService } from 'vs/editor/common/services/standaloneColorService' ;
8+ import { IStandaloneColorService , BuiltinTheme , ITheme } from 'vs/editor/common/services/standaloneColorService' ;
99import { vs , vs_dark , hc_black } from 'vs/editor/common/standalone/themes' ;
1010import * as dom from 'vs/base/browser/dom' ;
1111import { TokenizationRegistry } from 'vs/editor/common/modes' ;
1212
13+ class KnownTheme {
14+ cssClassName : string ;
15+ rules : IThemeRule [ ] ;
16+
17+ constructor ( cssClassName : string , rules : IThemeRule [ ] ) {
18+ this . cssClassName = cssClassName ;
19+ this . rules = rules ;
20+ }
21+ }
22+
23+ const VS_THEME_NAME = 'vs' ;
24+ const VS_DARK_THEME_NAME = 'vs-dark' ;
25+ const HC_BLACK_THEME_NAME = 'hc-black' ;
26+
27+ function isBuiltinTheme ( themeName : string ) : themeName is BuiltinTheme {
28+ return (
29+ themeName === VS_THEME_NAME
30+ || themeName === VS_DARK_THEME_NAME
31+ || themeName === HC_BLACK_THEME_NAME
32+ ) ;
33+ }
34+
35+ function getBuiltinRules ( builtinTheme : BuiltinTheme ) : IThemeRule [ ] {
36+ switch ( builtinTheme ) {
37+ case VS_THEME_NAME :
38+ return vs ;
39+ case VS_DARK_THEME_NAME :
40+ return vs_dark ;
41+ case HC_BLACK_THEME_NAME :
42+ return hc_black ;
43+ }
44+ }
45+
1346export class StandaloneColorServiceImpl implements IStandaloneColorService {
1447
1548 _serviceBrand : any ;
1649
17- private _theme : Theme ;
50+ private _knownThemes : Map < string , KnownTheme > ;
1851 private _styleElement : HTMLStyleElement ;
52+ private _theme : Theme ;
1953
2054 constructor ( ) {
55+ this . _knownThemes = new Map < string , KnownTheme > ( ) ;
56+ this . _knownThemes . set ( VS_THEME_NAME , new KnownTheme ( VS_THEME_NAME , getBuiltinRules ( VS_THEME_NAME ) ) ) ;
57+ this . _knownThemes . set ( VS_DARK_THEME_NAME , new KnownTheme ( VS_DARK_THEME_NAME , getBuiltinRules ( VS_DARK_THEME_NAME ) ) ) ;
58+ this . _knownThemes . set ( HC_BLACK_THEME_NAME , new KnownTheme ( HC_BLACK_THEME_NAME , getBuiltinRules ( HC_BLACK_THEME_NAME ) ) ) ;
2159 this . _styleElement = dom . createStyleSheet ( ) ;
2260 this . _styleElement . className = 'monaco-tokens-styles' ;
23- this . setTheme ( 'vs' ) ;
61+ this . setTheme ( VS_THEME_NAME ) ;
2462 }
2563
2664 private static _generateCSS ( colorMap : string [ ] ) : string {
@@ -35,30 +73,45 @@ export class StandaloneColorServiceImpl implements IStandaloneColorService {
3573 return rules . join ( '\n' ) ;
3674 }
3775
76+ public defineTheme ( themeName : string , themeData : ITheme ) : void {
77+ if ( ! / ^ [ a - z 0 - 9 \- ] + $ / i. test ( themeName ) || isBuiltinTheme ( themeName ) ) {
78+ throw new Error ( 'Illegal theme name!' ) ;
79+ }
80+ if ( ! isBuiltinTheme ( themeData . base ) ) {
81+ throw new Error ( 'Illegal theme base!' ) ;
82+ }
83+
84+ let cssClassName = themeData . base + ' ' + themeName ;
85+
86+ let rules : IThemeRule [ ] = [ ] ;
87+ if ( themeData . inherit ) {
88+ rules = rules . concat ( getBuiltinRules ( themeData . base ) ) ;
89+ }
90+ rules = rules . concat ( themeData . rules ) ;
91+
92+ this . _knownThemes . set ( themeName , new KnownTheme ( cssClassName , rules ) ) ;
93+ }
94+
3895 public getTheme ( ) : Theme {
3996 return this . _theme ;
4097 }
4198
42- public setTheme ( themeName : string ) : void {
43- let themeRules : IThemeRule [ ] = null ;
44- switch ( themeName ) {
45- case 'vs' :
46- themeRules = vs ;
47- break ;
48- case 'vs-dark' :
49- themeRules = vs_dark ;
50- break ;
51- case 'hc-black' :
52- themeRules = hc_black ;
53- break ;
54- default :
55- themeRules = [ ] ;
99+ public setTheme ( themeName : string ) : string {
100+ let themeData : KnownTheme ;
101+ if ( this . _knownThemes . has ( themeName ) ) {
102+ themeData = this . _knownThemes . get ( themeName ) ;
103+ } else {
104+ themeData = this . _knownThemes . get ( VS_THEME_NAME ) ;
56105 }
57- this . _theme = Theme . createFromRawTheme ( themeRules ) ;
106+
107+
108+ this . _theme = Theme . createFromRawTheme ( themeData . rules ) ;
58109 let colorMap = this . _theme . getColorMap ( ) ;
59110 let cssRules = StandaloneColorServiceImpl . _generateCSS ( colorMap ) ;
60111 this . _styleElement . innerHTML = cssRules ;
61112
62113 TokenizationRegistry . setColorMap ( colorMap ) ;
114+
115+ return themeData . cssClassName ;
63116 }
64117}
0 commit comments