Skip to content

Commit ecf0050

Browse files
authored
Merge pull request #12917 from CesiumGS/monaco-highlight-fix
Attempt to fix highlighting issue in monaco
2 parents 1ff67a4 + 1d87ed0 commit ecf0050

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

packages/sandcastle/src/SandcastleEditor.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,28 @@ function SandcastleEditor({
8383
const {
8484
settings: { fontFamily, fontSize, fontLigatures },
8585
} = useContext(SettingsContext);
86+
const documentRef = useRef(document);
8687
useEffect(() => {
87-
internalEditorRef.current?.updateOptions({
88-
fontFamily: availableFonts[fontFamily]?.cssValue ?? "Droid Sans Mono",
89-
});
88+
const cssName = availableFonts[fontFamily]?.cssValue ?? "Droid Sans Mono";
89+
const fontFace = [...documentRef.current.fonts.values()].find(
90+
(font) => font.family === cssName && font.weight === "400",
91+
);
92+
if (fontFace?.status !== "loaded") {
93+
// Monaco needs to check the size of the font for things like cursor position
94+
// and variable highlighting. If it does this check before the font has loaded
95+
// it will get the wrong size and may be horribly offset especially with ligatures
96+
// https://github.com/microsoft/monaco-editor/issues/392
97+
documentRef.current.fonts.load(`1rem ${cssName}`).then(() => {
98+
internalEditorRef.current?.updateOptions({
99+
fontFamily: cssName,
100+
});
101+
monaco.editor.remeasureFonts();
102+
});
103+
} else {
104+
internalEditorRef.current?.updateOptions({
105+
fontFamily: cssName,
106+
});
107+
}
90108
}, [fontFamily]);
91109
useEffect(() => {
92110
internalEditorRef.current?.updateOptions({

0 commit comments

Comments
 (0)