Skip to content

Commit 48b9899

Browse files
fix: ratel editor freezing when typing (#391)
* fix infinite loop * keep fixminimal * trailing space * space
1 parent 07de8e0 commit 48b9899

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

client/src/containers/Editor.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export default function Editor({
3030

3131
const [editorInstance, setEditorInstance] = useState(undefined)
3232
const [keywords, setKeywords] = useState([])
33-
const getValue = () => editorInstance?.getValue() || ''
33+
34+
const lastSetValueRef = useRef('')
35+
const isSettingContent = useRef(false)
3436

3537
const allState = useSelector((state) => state)
3638

@@ -145,8 +147,15 @@ export default function Editor({
145147

146148
// Every time editor is created or callback for onUpdateQuery is updated
147149
useEditorEffect(() => {
150+
if (!editorInstance) {
151+
return
152+
}
153+
148154
const onChangeHandler = (cm) => {
155+
if (isSettingContent.current) return
149156
const value = editorInstance.getValue()
157+
lastSetValueRef.current = value
158+
150159
const isJsonValue = isJSON()
151160

152161
if (editorInstance.getMode().name === 'graphql') {
@@ -180,8 +189,17 @@ export default function Editor({
180189

181190
// Every time query changes
182191
useEditorEffect(() => {
183-
if (query !== getValue()) {
192+
if (query !== lastSetValueRef.current) {
193+
isSettingContent.current = true
194+
const cursor = editorInstance.getCursor()
195+
184196
editorInstance.setValue(query)
197+
198+
lastSetValueRef.current = query
199+
editorInstance.setCursor(cursor)
200+
setTimeout(() => {
201+
isSettingContent.current = false
202+
}, 0)
185203
}
186204
}, [query])
187205

0 commit comments

Comments
 (0)