Skip to content

Commit 627c19c

Browse files
authored
Apply font loading mechanism for Monaco (codesandbox#975)
1 parent ae2678f commit 627c19c

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

packages/app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
"eslint-config-react-app": "^1.0.5",
135135
"eslint-plugin-vue": "^4.2.2",
136136
"file-saver": "^1.3.3",
137+
"fontfaceobserver": "^2.0.13",
137138
"fuse.js": "^3.2.1",
138139
"geniejs": "^0.5.0",
139140
"glamor": "^2.20.25",

packages/app/src/app/components/CodeEditor/Monaco/MonacoReactComponent.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react';
2+
import FontFaceObserver from 'fontfaceobserver';
23

34
function noop() {}
45

@@ -57,9 +58,35 @@ class MonacoEditor extends React.PureComponent {
5758
openModel: model => this.props.openReference(model),
5859
};
5960

61+
const appliedOptions = { ...options };
62+
63+
const fonts = appliedOptions.fontFamily.split(',').map(x => x.trim());
64+
// We first just set the default fonts for the editor. When the custom font has loaded
65+
// we set that one so that Monaco doesn't get confused.
66+
// https://github.com/Microsoft/monaco-editor/issues/392
67+
let firstFont = fonts[0];
68+
if (firstFont.startsWith('"')) {
69+
// Font is eg. '"aaaa"'
70+
firstFont = JSON.parse(firstFont);
71+
}
72+
const font = new FontFaceObserver(firstFont);
73+
74+
font.load().then(
75+
() => {
76+
if (this.editor && this.props.getEditorOptions) {
77+
this.editor.updateOptions(this.props.getEditorOptions());
78+
}
79+
},
80+
() => {
81+
// Font was not loaded in 3s, do nothing
82+
}
83+
);
84+
85+
appliedOptions.fontFamily = fonts.slice(1).join(', ');
86+
6087
this.editor = context.monaco.editor[
6188
diffEditor ? 'createDiffEditor' : 'create'
62-
](this.containerElement, options);
89+
](this.containerElement, appliedOptions);
6390
if (theme) {
6491
context.monaco.editor.setTheme(theme);
6592
}

packages/app/src/app/components/CodeEditor/Monaco/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,7 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
16021602
editorWillMount={monaco =>
16031603
defineTheme(monaco, this.props.theme.vscodeTheme)
16041604
}
1605+
getEditorOptions={this.getEditorOptions}
16051606
openReference={this.openReference}
16061607
/>
16071608
</CodeContainer>

yarn.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7208,6 +7208,10 @@ follow-redirects@^1.0.0, follow-redirects@^1.2.3:
72087208
dependencies:
72097209
debug "^3.1.0"
72107210

7211+
fontfaceobserver@^2.0.13:
7212+
version "2.0.13"
7213+
resolved "https://registry.yarnpkg.com/fontfaceobserver/-/fontfaceobserver-2.0.13.tgz#47adbb343261eda98cb44db2152196ff124d3221"
7214+
72117215
for-each@^0.3.2:
72127216
version "0.3.3"
72137217
resolved "https://registry.yarnpkg.com/for-each/-/for-each-0.3.3.tgz#69b447e88a0a5d32c3e7084f3f1710034b21376e"

0 commit comments

Comments
 (0)