Skip to content

Commit 534c1c0

Browse files
committed
render outdated decors pale
1 parent bde95ee commit 534c1c0

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

packages/jsrepl/src/app/repl/components/code-editor.module.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
.codeEditor {
2+
&.jsreplDecorsOutdated .jsreplDecor {
3+
&::before,
4+
&::after {
5+
--opacity: 0.2;
6+
}
7+
}
8+
29
/* Fix text clipping due to Tailwind CSS preflight styles: `* { box-sizing: border-box; }` */
310
& :global(.monaco-editor .action-widget) {
411
box-sizing: content-box;

packages/jsrepl/src/hooks/useReplDecorations.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import { ReplStateContext } from '@/context/repl-state-context'
88
import { getEditorContentsWithReplDecors } from '@/lib/code-editor-utils'
99
import { createDecorations } from '@/lib/repl-payload/decorations'
1010
import { renderToHoverContents } from '@/lib/repl-payload/render-hover'
11+
import useReplDecorationsOutdated from './useReplDecorationsOutdated'
1112

1213
export default function useReplDecorations() {
1314
const { replState } = useContext(ReplStateContext)!
1415
const { rewindMode } = useContext(ReplRewindModeContext)!
1516
const { payloads } = useContext(ReplPayloadsContext)!
1617
const { editorRef } = useContext(MonacoEditorContext)!
18+
const { setDecorationsOutdated } = useReplDecorationsOutdated()
1719

1820
const decorationsDisposable = useRef<() => void>()
1921
const provideHoverRef = useRef<typeof provideHover>()
@@ -44,6 +46,7 @@ export default function useReplDecorations() {
4446
}
4547

4648
decorationsDisposable.current?.()
49+
setDecorationsOutdated(false)
4750

4851
const payloads = getDisplayedPayloads((payload) => payload.ctx.filePath === activeModel)
4952
const highlightedPayloadIds =
@@ -59,6 +62,7 @@ export default function useReplDecorations() {
5962
rewindMode.active,
6063
rewindMode.currentPayloadId,
6164
replState.activeModel,
65+
setDecorationsOutdated,
6266
])
6367

6468
useEffect(() => {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { useCallback, useContext, useEffect, useRef } from 'react'
2+
import codeEditorStyles from '@/app/repl/components/code-editor.module.css'
3+
import { MonacoEditorContext } from '@/context/monaco-editor-context'
4+
import { ReplModelsContext } from '@/context/repl-models-context'
5+
6+
export default function useReplDecorationsOutdated() {
7+
const { editorRef } = useContext(MonacoEditorContext)!
8+
const { models } = useContext(ReplModelsContext)!
9+
10+
const decorationsOutdatedRef = useRef(false)
11+
const timeoutIdRef = useRef<NodeJS.Timeout>()
12+
13+
const setDecorationsOutdated = useCallback(
14+
(outdated: boolean) => {
15+
if (decorationsOutdatedRef.current === outdated) {
16+
return
17+
}
18+
19+
decorationsOutdatedRef.current = outdated
20+
21+
clearTimeout(timeoutIdRef.current)
22+
23+
const fn = () => {
24+
const domNode = editorRef.current!.getContainerDomNode()
25+
domNode.classList.toggle(codeEditorStyles.jsreplDecorsOutdated!, outdated)
26+
}
27+
28+
if (outdated) {
29+
timeoutIdRef.current = setTimeout(fn, 700)
30+
} else {
31+
fn()
32+
}
33+
},
34+
[editorRef]
35+
)
36+
37+
useEffect(() => {
38+
const editor = editorRef.current!
39+
const disposable = editor.onDidChangeModelContent(() => {
40+
if (decorationsOutdatedRef.current) {
41+
return
42+
}
43+
44+
const model = editor.getModel()
45+
if (model && models.has(model.uri.path)) {
46+
setDecorationsOutdated(true)
47+
}
48+
})
49+
50+
return () => {
51+
disposable.dispose()
52+
}
53+
}, [models, editorRef, setDecorationsOutdated])
54+
55+
return { setDecorationsOutdated }
56+
}

0 commit comments

Comments
 (0)