Skip to content

Commit ad454f0

Browse files
committed
chore: remove unnecessary setTimeout
1 parent 6b9cd2f commit ad454f0

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

packages/frontend/src/components/RichTextEditor/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ const Editor = ({
218218
editor?.commands.focus()
219219

220220
if (isMulticol && editor) {
221-
singleLineEditorScroll(editor)
221+
requestAnimationFrame(() => {
222+
singleLineEditorScroll(editor)
223+
})
222224
}
223225
},
224226
[editor, isMulticol],

packages/frontend/src/components/RichTextEditor/utils.ts

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -175,35 +175,29 @@ export const singleLineEditorScroll = (editor: Editor) => {
175175
return
176176
}
177177

178-
setTimeout(() => {
179-
const singleLineEditor = editor.view.dom.closest(
180-
'.single-line-editor',
181-
) as HTMLElement
182-
if (singleLineEditor) {
183-
// Get the current cursor position
184-
const pos = editor.state.selection.$head.pos
185-
let targetVariable = findClosestVariableNode(
186-
editor,
187-
pos,
188-
singleLineEditor,
189-
)
190-
191-
// If we still haven't found it, fall back to the last variable
192-
if (!targetVariable) {
193-
const variables = Array.from(
194-
singleLineEditor.getElementsByClassName('node-variable'),
195-
) as HTMLElement[]
196-
if (variables.length > 0) {
197-
targetVariable = variables[variables.length - 1]
198-
}
178+
const singleLineEditor = editor.view.dom.closest(
179+
'.single-line-editor',
180+
) as HTMLElement
181+
if (singleLineEditor) {
182+
// Get the current cursor position
183+
const pos = editor.state.selection.$head.pos
184+
let targetVariable = findClosestVariableNode(editor, pos, singleLineEditor)
185+
186+
// If we still haven't found it, fall back to the last variable
187+
if (!targetVariable) {
188+
const variables = Array.from(
189+
singleLineEditor.getElementsByClassName('node-variable'),
190+
) as HTMLElement[]
191+
if (variables.length > 0) {
192+
targetVariable = variables[variables.length - 1]
199193
}
194+
}
200195

201-
// Scroll to the target variable if found
202-
if (targetVariable) {
203-
scrollVariableIntoView(targetVariable, singleLineEditor)
204-
}
196+
// Scroll to the target variable if found
197+
if (targetVariable) {
198+
scrollVariableIntoView(targetVariable, singleLineEditor)
205199
}
206-
}, 10)
200+
}
207201
}
208202

209203
export function findClosestVariableNode(

0 commit comments

Comments
 (0)