Skip to content

Commit 832aa7b

Browse files
committed
fix: font issue with terminal
1 parent 513ce1e commit 832aa7b

File tree

2 files changed

+17
-40
lines changed

2 files changed

+17
-40
lines changed

src/lib/fonts.js

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import robotoMono from "../res/fonts/RobotoMono.ttf";
99
const fonts = new Map();
1010
const customFontNames = new Set();
1111
const CUSTOM_FONTS_KEY = "custom_fonts";
12+
const FONT_FACE_STYLE_ID = "font-face-style";
13+
const EDITOR_STYLE_ID = "editor-font-style";
1214

1315
add(
1416
"Fira Code",
@@ -188,29 +190,14 @@ function has(name) {
188190
async function setFont(name) {
189191
loader.showTitleLoader();
190192
try {
191-
const $style = tag.get("style#font-style") ?? (
192-
<style id="font-style"></style>
193-
);
194-
let css = get(name);
195-
196-
// Get all URL font references
197-
const urls = [...css.matchAll(/url\((.*?)\)/g)].map((match) => match[1]);
198-
199-
urls?.map(async (url) => {
200-
if (!/^https?/.test(url)) return;
201-
if (/^https?:\/\/localhost/.test(url)) return;
202-
const fontFile = await downloadFont(name, url);
203-
const internalUrl = await helpers.toInternalUri(fontFile);
204-
css = css.replace(url, internalUrl);
205-
}),
206-
($style.textContent = `${css}
207-
.editor-container.ace_editor{
193+
await loadFont(name);
194+
const $style = ensureStyleElement(EDITOR_STYLE_ID);
195+
$style.textContent = `.editor-container.ace_editor{
208196
font-family: "${name}", NotoMono, Monaco, MONOSPACE !important;
209197
}
210198
.ace_text{
211199
font-family: inherit !important;
212-
}`);
213-
document.head.append($style);
200+
}`;
214201
} catch (error) {
215202
toast(`${name} font not found`, "error");
216203
setFont("Roboto Mono");
@@ -238,7 +225,7 @@ async function downloadFont(name, link) {
238225
}
239226

240227
async function loadFont(name) {
241-
const $style = tag.get("style#font-style") ?? <style id="font-style"></style>;
228+
const $style = ensureStyleElement(FONT_FACE_STYLE_ID);
242229
let css = get(name);
243230

244231
if (!css) {
@@ -260,12 +247,20 @@ async function loadFont(name) {
260247
// Add font face to document if not already present
261248
if (!$style.textContent.includes(`font-family: '${name}'`)) {
262249
$style.textContent = `${$style.textContent}\n${css}`;
263-
document.head.append($style);
264250
}
265251

266252
return css;
267253
}
268254

255+
function ensureStyleElement(id) {
256+
const selector = `style#${id}`;
257+
const $style = tag.get(selector) ?? <style id={id}></style>;
258+
if (!$style.isConnected) {
259+
document.head.append($style);
260+
}
261+
return $style;
262+
}
263+
269264
export default {
270265
add,
271266
addCustom,

src/main.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import { setKeyBindings } from "ace/commands";
1818
import { initModes } from "ace/modelist";
1919
import Contextmenu from "components/contextmenu";
2020
import Sidebar from "components/sidebar";
21-
import {
22-
DEFAULT_TERMINAL_SETTINGS,
23-
TerminalManager,
24-
} from "components/terminal";
21+
import { TerminalManager } from "components/terminal";
2522
import tile from "components/tile";
2623
import toast from "components/toast";
2724
import tutorial from "components/tutorial";
@@ -37,7 +34,6 @@ import checkPluginsUpdate from "lib/checkPluginsUpdate";
3734
import EditorFile from "lib/editorFile";
3835
import EditorManager from "lib/editorManager";
3936
import { initFileList } from "lib/fileList";
40-
import fonts from "lib/fonts";
4137
import lang from "lib/lang";
4238
import loadPlugins from "lib/loadPlugins";
4339
import Logger from "lib/logger";
@@ -492,20 +488,6 @@ async function loadApp() {
492488

493489
initFileList();
494490

495-
const terminalSettings = settings.value.terminalSettings || {};
496-
const terminalFontFamily =
497-
terminalSettings.fontFamily || DEFAULT_TERMINAL_SETTINGS.fontFamily;
498-
if (terminalFontFamily) {
499-
try {
500-
await fonts.loadFont(terminalFontFamily);
501-
} catch (error) {
502-
console.error(
503-
`Failed to preload terminal font ${terminalFontFamily}:`,
504-
error,
505-
);
506-
}
507-
}
508-
509491
TerminalManager.restorePersistedSessions().catch((error) => {
510492
console.error("Terminal restoration failed:", error);
511493
});

0 commit comments

Comments
 (0)