Skip to content

Commit 717f18f

Browse files
committed
fix(files): avoid stale editor views on cached navigation
1 parent 1734687 commit 717f18f

7 files changed

Lines changed: 201 additions & 26 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/find/use-markdown-find.ts‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ export function useMarkdownFind({
8686
*/
8787
const revealActiveMatch = useCallback(() => {
8888
requestAnimationFrame(() => {
89-
editorRef.current?.view.dom
90-
.querySelector(`.${ACTIVE_MATCH_CLASS}`)
91-
?.scrollIntoView({ block: 'center' })
89+
const current = editorRef.current
90+
if (!current || current.isDestroyed) return
91+
current.view.dom.querySelector(`.${ACTIVE_MATCH_CLASS}`)?.scrollIntoView({ block: 'center' })
9292
})
9393
}, [])
9494

@@ -99,7 +99,7 @@ export function useMarkdownFind({
9999
*/
100100
const syncTally = useCallback(() => {
101101
const current = editorRef.current
102-
if (!current) return
102+
if (!current || current.isDestroyed) return
103103
const { matches, activeIndex, truncated } = getFindTally(current.state)
104104
setTally((previous) =>
105105
previous.count === matches.length &&
@@ -116,7 +116,7 @@ export function useMarkdownFind({
116116
* on each of those. Not subscribed while the bar is closed, so typing costs nothing then.
117117
*/
118118
useEffect(() => {
119-
if (!editor || !isOpen) return
119+
if (!editor || editor.isDestroyed || !isOpen) return
120120
syncTally()
121121
editor.on('transaction', syncTally)
122122
return () => {
@@ -132,15 +132,15 @@ export function useMarkdownFind({
132132
const queryRef = useRef(query)
133133
queryRef.current = query
134134
useEffect(() => {
135-
if (!editor || queryRef.current.length === 0) return
135+
if (!editor || editor.isDestroyed || queryRef.current.length === 0) return
136136
setFindQuery(editor, queryRef.current)
137137
}, [editor])
138138

139139
const setQuery = useCallback(
140140
(next: string) => {
141141
setQueryState(next)
142142
const current = editorRef.current
143-
if (!current) return
143+
if (!current || current.isDestroyed) return
144144
setFindQuery(current, next)
145145
revealActiveMatch()
146146
},
@@ -150,7 +150,7 @@ export function useMarkdownFind({
150150
const step = useCallback(
151151
(delta: number) => {
152152
const current = editorRef.current
153-
if (!current) return
153+
if (!current || current.isDestroyed) return
154154
stepFindMatch(current, delta)
155155
revealActiveMatch()
156156
},
@@ -162,13 +162,18 @@ export function useMarkdownFind({
162162

163163
const replaceCurrent = useCallback(() => {
164164
const current = editorRef.current
165-
if (!current || !replaceActiveFindMatch(current, replacement, warnReplacementLimit)) return
165+
if (
166+
!current ||
167+
current.isDestroyed ||
168+
!replaceActiveFindMatch(current, replacement, warnReplacementLimit)
169+
)
170+
return
166171
revealActiveMatch()
167172
}, [replacement, revealActiveMatch])
168173

169174
const replaceAll = useCallback(() => {
170175
const current = editorRef.current
171-
if (!current) return
176+
if (!current || current.isDestroyed) return
172177
replaceAllFindMatches(current, replacement, warnReplacementLimit)
173178
}, [replacement])
174179

@@ -179,7 +184,7 @@ export function useMarkdownFind({
179184
setReplacement('')
180185
setTally(EMPTY_TALLY)
181186
const current = editorRef.current
182-
if (current) setFindQuery(current, '')
187+
if (current && !current.isDestroyed) setFindQuery(current, '')
183188
requestAnimationFrame(() => {
184189
if (current && !current.isDestroyed) current.commands.focus()
185190
})

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/bubble-menu.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ export function EditorBubbleMenu({
221221
* preventing a lost pointer release from suppressing later keyboard selections.
222222
*/
223223
useEffect(() => {
224+
if (editor.isDestroyed) return
224225
const dom = editor.view.dom
225226
const onPointerDown = () => {
226227
isPointerDownRef.current = true
@@ -255,6 +256,7 @@ export function EditorBubbleMenu({
255256
}
256257

257258
useEffect(() => {
259+
if (editor.isDestroyed) return
258260
const dom = editor.view.dom
259261
const openLinkOnShortcut = (event: KeyboardEvent) => {
260262
if (!editor.isEditable) return

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/link-hover-card.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export function LinkHoverCard({ editor }: LinkHoverCardProps) {
9595
}, [cancelHide, isEditing])
9696

9797
useEffect(() => {
98+
// Cached routes reconnect child effects before useEditor replaces the disposed editor.
99+
if (editor.isDestroyed) return
98100
const dom = editor.view.dom
99101
const onOver = (event: Event) => {
100102
if (

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/suggestion-list.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export function SuggestionList<T>({
6464
const activeOptionId = hasOptions ? `${optionIdPrefix}-${activeIndex}` : null
6565

6666
useEffect(() => {
67+
if (editor.isDestroyed) return
6768
const dom = editor.view.dom
6869
dom.setAttribute('aria-haspopup', 'listbox')
6970
dom.setAttribute('aria-expanded', 'true')
@@ -76,6 +77,7 @@ export function SuggestionList<T>({
7677
}, [editor])
7778

7879
useEffect(() => {
80+
if (editor.isDestroyed) return
7981
const dom = editor.view.dom
8082
if (activeOptionId) {
8183
dom.setAttribute('aria-controls', listboxId)

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/use-bubble-menu-floating.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function bubbleMenuAnchorRange(selection: Selection): { from: number; to:
2121
* editor mounts so the caller skips positioning.
2222
*/
2323
function selectionVirtualElement(editor: Editor) {
24+
if (editor.isDestroyed) return null
2425
const { view, state } = editor
2526
if (!view.dom.isConnected) return null
2627
const { from, to } = bubbleMenuAnchorRange(state.selection)
@@ -53,10 +54,11 @@ export function useBubbleMenuFloating(
5354

5455
useEffect(() => {
5556
const menu = menuRef.current
56-
if (!menu) return
57+
if (!menu || editor.isDestroyed) return
5758
let stop: (() => void) | undefined
5859
const trackSelection = () => {
5960
stop?.()
61+
if (editor.isDestroyed) return
6062
const { node } = editor.view.domAtPos(editor.state.selection.from)
6163
const reference = node instanceof Element ? node : (node.parentElement ?? editor.view.dom)
6264
stop = autoUpdate(reference, menu, () => {

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/use-editor-toolbar.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function useEditorToolbar({
5454
})
5555

5656
useEffect(() => {
57+
if (editor.isDestroyed) return
5758
let frame: number | undefined
5859
const enterToolbar = (event: globalThis.KeyboardEvent) => {
5960
if (

0 commit comments

Comments
 (0)