Skip to content

Commit

Permalink
feat: Fonts are loaded automatically.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Feb 9, 2022
1 parent 308a576 commit 299ffb4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/font.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '*.ttf';
17 changes: 16 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable react-hooks/exhaustive-deps */
import React, { useImperativeHandle, useEffect, useRef, useState } from 'react';
import * as monaco from 'monaco-editor';
import {editor, languages} from 'monaco-editor';
import { editor, languages } from 'monaco-editor';
// @ts-ignore
import codicon from 'monaco-editor/min/vs/base/browser/ui/codicons/codicon/codicon.ttf';

function noop() {}

Expand Down Expand Up @@ -55,6 +57,14 @@ export interface MonacoEditorProps extends Omit<React.HTMLAttributes<HTMLDivElem
onChange?: (value: string, event: monaco.editor.IModelContentChangedEvent) => void;
}

export async function loadFont(fontFamily: string, url: string): Promise<void> {
const font = new FontFace(fontFamily, `local(${fontFamily}), url(${url})`);
// wait for font to be loaded
await font.load();
// add font to document
document.fonts.add(font);
}

export interface RefEditorInstance {
container: HTMLDivElement | null;
editor?: monaco.editor.IStandaloneCodeEditor;
Expand Down Expand Up @@ -87,6 +97,11 @@ function MonacoEditor(props: MonacoEditorProps, ref: ((instance: RefEditorInstan
onChange!(valueCurrent, event);
});
}
loadFont('codicon', codicon).catch((e) => {
if (e) {
throw new Error('Failed to load font codicon!!');
}
});
}, []);

useEffect(() => {
Expand Down
1 change: 0 additions & 1 deletion src/react-app-env.d.ts

This file was deleted.

0 comments on commit 299ffb4

Please sign in to comment.