From ec74b994d7d239fbbceb806dff7c26666b6c26be Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Tue, 10 Sep 2024 00:43:44 -0400 Subject: [PATCH] Simplify `scroll` event for source editor using lodash Throttle --- client/homebrew/editor/editor.jsx | 4 +++- shared/naturalcrit/codeEditor/codeEditor.jsx | 11 ++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/client/homebrew/editor/editor.jsx b/client/homebrew/editor/editor.jsx index 1fa720f81..3b87c7bcc 100644 --- a/client/homebrew/editor/editor.jsx +++ b/client/homebrew/editor/editor.jsx @@ -154,8 +154,9 @@ const Editor = createClass({ scrollingJump = false; }, - handleSourceScroll : function(e) { + handleSourceScroll : function() { if(!this.props.liveScroll) return; + console.log("handleSourceScroll") scrollingJump = true; this.brewJump(); scrollingJump = false; @@ -356,6 +357,7 @@ const Editor = createClass({ }, brewJump : function(targetPage=this.getCurrentPage()){ + console.log('jumpbrew') if(lockBrewJump) return; if(!window) return; lockSourceJump = true; diff --git a/shared/naturalcrit/codeEditor/codeEditor.jsx b/shared/naturalcrit/codeEditor/codeEditor.jsx index 46ae64edb..1387edfee 100644 --- a/shared/naturalcrit/codeEditor/codeEditor.jsx +++ b/shared/naturalcrit/codeEditor/codeEditor.jsx @@ -7,7 +7,6 @@ const closeTag = require('./close-tag'); const autoCompleteEmoji = require('./autocompleteEmoji'); let CodeMirror; -let isScrolling; if(typeof window !== 'undefined'){ CodeMirror = require('codemirror'); @@ -54,6 +53,7 @@ const CodeEditor = createClass({ value : '', wrap : true, onChange : ()=>{}, + onscroll : ()=>{}, enableFolding : true, editorTheme : 'default' }; @@ -191,14 +191,7 @@ const CodeEditor = createClass({ // Note: codeMirror passes a copy of itself in this callback. cm === this.codeMirror. Either one works. this.codeMirror.on('change', (cm)=>{this.props.onChange(cm.getValue());}); - this.codeMirror.on('scroll', (cm)=>{ - window.clearTimeout(isScrolling); - const props = this.props; - isScrolling = setTimeout(function() { - cm.setCursor({ line: cm.lineAtHeight(cm.getWrapperElement().getBoundingClientRect().top) + 1, ch: 0 }); - props.onScroll(cm.lineAtHeight(cm.getWrapperElement().getBoundingClientRect().top)); - }, 66); - }); + this.codeMirror.on('scroll', _.throttle(this.props.onScroll, 200)); this.updateSize(); },