-
Notifications
You must be signed in to change notification settings - Fork 655
Fix flow editor slowness due to many instances of monaco #5621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 26 commits
cbdcc74
6cdcd57
d70eeff
ae256e6
3126069
9ea976c
4bba218
cd1260c
07779c4
82dd084
80dd6e4
4b3e967
712ed88
37ae655
4fa2bd3
e0f9edc
9ad3c46
30c6a5f
84a7530
c3a37c9
5bf1540
e9acbe1
9080602
ba28323
c20fca1
2183c15
3f896cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,112 @@ | ||||||
<!-- Used to avoid height jitter when loading monaco asynchronously --> | ||||||
|
||||||
<script lang="ts"> | ||||||
import { getOS } from '$lib/utils' | ||||||
|
||||||
type Props = { | ||||||
code?: string | ||||||
autoheight?: boolean | ||||||
lineNumbersWidth?: number | ||||||
lineNumbersOffset?: number | ||||||
class?: string | ||||||
fontSize?: number | ||||||
} | ||||||
|
||||||
let { | ||||||
code, | ||||||
autoheight = false, | ||||||
lineNumbersWidth = 51, | ||||||
lineNumbersOffset = 0, | ||||||
class: className = '', | ||||||
fontSize = 14 | ||||||
}: Props = $props() | ||||||
|
||||||
// https://github.com/microsoft/vscode/blob/baa2dad3cdacd97ac02eff0604984faf1167ff1e/src/vs/editor/common/config/editorOptions.ts#L5421 | ||||||
const DEFAULT_WINDOWS_FONT_FAMILY = "Consolas, 'Courier New', monospace" | ||||||
const DEFAULT_MAC_FONT_FAMILY = "Menlo, Monaco, 'Courier New', monospace" | ||||||
const DEFAULT_LINUX_FONT_FAMILY = "'Droid Sans Mono', 'monospace', monospace" | ||||||
const fontFamily = | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid redundant |
||||||
getOS() === 'Windows' | ||||||
? DEFAULT_WINDOWS_FONT_FAMILY | ||||||
: getOS() === 'macOS' | ||||||
? DEFAULT_MAC_FONT_FAMILY | ||||||
: DEFAULT_LINUX_FONT_FAMILY | ||||||
|
||||||
// https://github.com/microsoft/vscode/blob/baa2dad3cdacd97ac02eff0604984faf1167ff1e/src/vs/editor/common/config/fontInfo.ts#L14 | ||||||
const GOLDEN_LINE_HEIGHT_RATIO = getOS() == 'macOS' ? 1.5 : 1.35 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use strict equality (
Suggested change
|
||||||
|
||||||
let lines = $derived(code?.split('\n') ?? []) | ||||||
|
||||||
const charWidth = 9 // try to match as closely as possible to monaco editor | ||||||
|
||||||
const lineHeight = fontSize * GOLDEN_LINE_HEIGHT_RATIO | ||||||
|
||||||
let [clientWidth, clientHeight] = $state([0, 0]) | ||||||
let showHorizontalScrollbar = $derived( | ||||||
lines.some((line) => line.length * charWidth > clientWidth - 40) | ||||||
) | ||||||
|
||||||
let [editorWidth, editorHeight] = $derived([ | ||||||
clientWidth, | ||||||
autoheight ? lines.length * lineHeight + (showHorizontalScrollbar ? 12 : 0) : clientHeight | ||||||
]) | ||||||
</script> | ||||||
|
||||||
<!-- Copy pasted from actual monaco editor in the web inspector --> | ||||||
|
||||||
<div | ||||||
bind:clientWidth | ||||||
bind:clientHeight | ||||||
class="h-full w-full relative editor dark:bg-[#272D38] {className}" | ||||||
style="--vscode-editorCodeLens-lineHeight: 18px; --vscode-editorCodeLens-fontSize: 12px; --vscode-editorCodeLens-fontFeatureSettings: 'liga' off, 'calt' off; --code-editorInlayHintsFontFamily: {fontFamily};" | ||||||
> | ||||||
<div | ||||||
class="monaco-editor no-user-select mac standalone showUnused showDeprecated vs-dark" | ||||||
role="code" | ||||||
> | ||||||
<div class="overflow-guard" style="width: {editorWidth}px; height: {editorHeight}px;"> | ||||||
<div | ||||||
class="margin" | ||||||
role="presentation" | ||||||
aria-hidden="true" | ||||||
style="position: absolute; transform: translate3d(0px, 0px, 0px); contain: strict; top: 0px; width: {editorWidth}px; height: {editorHeight}px;" | ||||||
> | ||||||
<div | ||||||
class="margin-view-overlays" | ||||||
role="presentation" | ||||||
aria-hidden="true" | ||||||
style="position: absolute; font-family: {fontFamily}; font-weight: normal; font-size: {fontSize}px; font-feature-settings: 'liga' 0, 'calt' 0; font-variation-settings: normal; line-height: {lineHeight}px; letter-spacing: 0px; width: {lineNumbersWidth}px; height: 4893px;" | ||||||
> | ||||||
{#each lines as _, i} | ||||||
<div style="top:{lineHeight * i}px;height:{lineHeight}px;"> | ||||||
<div class="line-numbers" style="left:{lineNumbersOffset}px;width:25px;">{i + 1}</div> | ||||||
</div> | ||||||
{/each} | ||||||
</div> | ||||||
</div> | ||||||
<div | ||||||
class="monaco-scrollable-element editor-scrollable vs-dark mac" | ||||||
style="position: absolute; overflow: hidden; left: {lineNumbersWidth}px; width: {editorWidth}px; height: {editorHeight}px" | ||||||
> | ||||||
<div | ||||||
class="lines-content monaco-editor-background" | ||||||
style="position: absolute; overflow: hidden; width: 1.67772e+07px; height: 1.67772e+07px; transform: translate3d(0px, 0px, 0px); contain: strict; top: 0px; left: 0px;" | ||||||
> | ||||||
<div | ||||||
class="view-lines monaco-mouse-cursor-text text-tertiary/60" | ||||||
style="line-height: {lineHeight}px; position: absolute; font-family: {fontFamily}; font-weight: normal; font-size: {fontSize}px; font-feature-settings: 'liga' 0, 'calt' 0; font-variation-settings: normal; line-height: {lineHeight}px; letter-spacing: 0px; width: 1143px; height: 789px;" | ||||||
> | ||||||
{#each lines as line, i} | ||||||
<div | ||||||
style="height: {lineHeight}px; top: {i * lineHeight}px;" | ||||||
class="text-nowrap whitespace-pre" | ||||||
> | ||||||
{line} | ||||||
</div> | ||||||
{/each} | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
</div> | ||||||
</div> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commenting out the
setTimeout
means that whenloadAsync
is true,loadMonaco
is never called. This may break the async loading behavior. Consider either removing theloadAsync
branch entirely or callingloadMonaco
directly (withoutsetTimeout
) so that the loading strategy is consistent. Also, avoid leaving dead/commented code in the repository.