简体   繁体   English

获取 Monaco 编辑器背景颜色

[英]Get Monaco editor background color

I've got a Monaco setup where you can select from over 40 themes, the problem is that I want to adapt my website Color's to Monaco theme ones.我有一个摩纳哥设置,您可以在其中从 40 多个主题中获得 select,问题是我想将我的网站颜色调整为摩纳哥主题。 How do I get that?我怎么得到它? I tried using getComputedStyle(document.querySelector('.monaco-editor')) .我尝试使用getComputedStyle(document.querySelector('.monaco-editor')) That gets the background color of the editor, but for the theme you had selected before.这将获得编辑器的背景颜色,但对于您之前选择的主题。 If you had selected a light theme and now you select a dark theme, the color will output the light theme's background.如果您选择了浅色主题,而现在您选择了深色主题,则颜色将为浅色主题的背景。 What could I do?我能做什么?

SOLVED : The problem was because it was trying to get the colour before the theme was loading.解决:问题是因为它试图在加载主题之前获取颜色。 I got it working using promises.我使用 Promise 让它工作。

fetch(`/themes/${p}.json`)
  .then(data => data.json())
  .then(data => {
    monaco.editor.defineTheme('theme', data);
    monaco.editor.setTheme('theme');
    $('.sidebar').style.backgroundColor = pSBC(0.1, getComputedStyle($('.monaco-editor')).backgroundColor)
    $('.sidesidebar').style.backgroundColor = pSBC(0.1, getComputedStyle($('.monaco-editor')).backgroundColor)
    $('.skypackbar').style.backgroundColor = pSBC(0.1, getComputedStyle($('.monaco-editor')).backgroundColor)
  })

The Monaco editor uses CSS variables to define its colors. Monaco 编辑器使用 CSS 变量来定义其 colors。 Check the Monaco Editor Playground: https://microsoft.github.io/monaco-editor/playground.html检查摩纳哥编辑器游乐场: https://microsoft.github.io/monaco-editor/playground.html

在此处输入图像描述

Check the Visual Studio Code theme color reference for each color name: https://code.visualstudio.com/api/references/theme-color检查每个颜色名称的 Visual Studio Code 主题颜色参考: https://code.visualstudio.com/api/references/theme-color

Why Visual Studio Code, you may ask.为什么是 Visual Studio Code,您可能会问。 Well, that's because vscode uses Monaco as the central control and that was actually extracted from the vscode code base, to be able to publish it as a standalone tool.好吧,那是因为 vscode 使用 Monaco 作为中央控制,并且实际上是从 vscode 代码库中提取的,以便能够将其作为独立工具发布。

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

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