Skip to content

Commit d2e9405

Browse files
authored
fix(monaco): ensure correct color map update when switching themes (#1155)
1 parent fcdd337 commit d2e9405

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

packages/monaco/playground/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
"scripts": {
77
"play": "vite"
88
},
9+
"dependencies": {
10+
"@shikijs/monaco": "workspace:*",
11+
"monaco-editor-core": "catalog:integrations",
12+
"shiki": "workspace:*"
13+
},
914
"devDependencies": {
1015
"typescript": "catalog:cli",
1116
"vite": "catalog:bundling"

packages/monaco/playground/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ monaco.languages.register({ id: 'vue' })
2020
monaco.languages.register({ id: 'typescript' })
2121
monaco.languages.register({ id: 'javascript' })
2222

23-
// Register the themes from Shiki, and provide syntax highlighting for Monaco. // [!code highlight:2]
23+
// Register the themes from Shiki, and provide syntax highlighting for Monaco.
2424
shikiToMonaco(highlighter, monaco)
2525

2626
// Create the editor

packages/monaco/src/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ export function shikiToMonaco(
103103
_setTheme(themeName)
104104
}
105105

106+
const _create = monaco.editor.create
107+
monaco.editor.create = (element: HTMLElement, options?: monacoNs.editor.IStandaloneEditorConstructionOptions, override?: monacoNs.editor.IEditorOverrideServices) => {
108+
if (options?.theme) {
109+
monaco.editor.setTheme(options.theme)
110+
}
111+
return _create(element, options, override)
112+
}
113+
106114
// Set the first theme as the default theme
107115
monaco.editor.setTheme(themeIds[0])
108116

@@ -147,7 +155,8 @@ export function shikiToMonaco(
147155
for (let j = 0; j < tokensLength; j++) {
148156
const startIndex = result.tokens[2 * j]
149157
const metadata = result.tokens[2 * j + 1]
150-
const color = normalizeColor(colorMap[EncodedTokenMetadata.getForeground(metadata)] || '')
158+
const colorIdx = EncodedTokenMetadata.getForeground(metadata)
159+
const color = normalizeColor(colorMap[colorIdx] || '')
151160
const fontStyle = EncodedTokenMetadata.getFontStyle(metadata)
152161

153162
// Because Monaco only support one scope per token,

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)