简体   繁体   English

如何获取 monaco-editor 的当前主题

[英]How to get current theme of monaco-editor

Is there any method to get the current theme of monaco-editor?有什么方法可以获取 monaco-editor 的当前主题吗? In monaco-editor api doc,I only find methods to set theme, for example, setTheme and defineTheme ,but no getTheme .在 monaco-editor api 文档中,我只找到了设置主题的方法,例如setThemedefineTheme ,但没有getTheme

If you have a reference to the editor, you can use editor._themeService._theme如果你有对编辑器的引用,你可以使用editor._themeService._theme

If you paste the following code into the Monaco Playground , open the developer tools console and run it you will see the theme name being logged out:如果您将以下代码粘贴到Monaco Playground中,打开开发者工具控制台并运行它,您将看到主题名称被注销:

// Theme matching (i.e. applying a style to a token) happens in JavaScript.
// We must therefore register the theme rules in JavaScript.

// A custom theme must name its base theme (i.e. 'vs', 'vs-dark' or 'hc-black')
// It can then choose to inherit the rules from the base theme or not
// A rule token matching is prefix based: e.g.
//  - string will match a token with type: string, string.double.js or string.html
//  - string.double will match a token with type string.double but will not match string or string.html

// !!! Tokens can be inspected using F1 > Developer: Inspect Tokens !!!

monaco.editor.defineTheme('myCustomTheme', {
    base: 'vs', // can also be vs-dark or hc-black
    inherit: true, // can also be false to completely replace the builtin rules
    rules: [
        { token: 'comment', foreground: 'ffa500', fontStyle: 'italic underline' },
        { token: 'comment.js', foreground: '008800', fontStyle: 'bold' },
        { token: 'comment.css', foreground: '0000ff' } // will inherit fontStyle from `comment` above
    ],
    colors: {
        'editor.foreground': '#000000'
    }
});

var editor = monaco.editor.create(document.getElementById('container'), {
    value: getCode(),
    language: 'text/html',
    theme: 'myCustomTheme'
});

function getCode() {
    return (
        '<html><!-- // !!! Tokens can be inspected using F1 > Developer: Inspect Tokens !!! -->\n<head>\n   <!-- HTML comment -->\n <style type="text/css">\n       /* CSS comment */\n </style>\n  <script type="javascript">\n        // JavaScript comment\n </' +
        'script>\n</head>\n<body></body>\n</html>'
    );
}

console.log(editor._themeService._theme)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM