0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-03 17:02:40 +00:00

Simplify scroll event for source editor using lodash Throttle

This commit is contained in:
Trevor Buckner
2024-09-10 00:43:44 -04:00
parent b5155ed256
commit ec74b994d7
2 changed files with 5 additions and 10 deletions

View File

@@ -154,8 +154,9 @@ const Editor = createClass({
scrollingJump = false; scrollingJump = false;
}, },
handleSourceScroll : function(e) { handleSourceScroll : function() {
if(!this.props.liveScroll) return; if(!this.props.liveScroll) return;
console.log("handleSourceScroll")
scrollingJump = true; scrollingJump = true;
this.brewJump(); this.brewJump();
scrollingJump = false; scrollingJump = false;
@@ -356,6 +357,7 @@ const Editor = createClass({
}, },
brewJump : function(targetPage=this.getCurrentPage()){ brewJump : function(targetPage=this.getCurrentPage()){
console.log('jumpbrew')
if(lockBrewJump) return; if(lockBrewJump) return;
if(!window) return; if(!window) return;
lockSourceJump = true; lockSourceJump = true;

View File

@@ -7,7 +7,6 @@ const closeTag = require('./close-tag');
const autoCompleteEmoji = require('./autocompleteEmoji'); const autoCompleteEmoji = require('./autocompleteEmoji');
let CodeMirror; let CodeMirror;
let isScrolling;
if(typeof window !== 'undefined'){ if(typeof window !== 'undefined'){
CodeMirror = require('codemirror'); CodeMirror = require('codemirror');
@@ -54,6 +53,7 @@ const CodeEditor = createClass({
value : '', value : '',
wrap : true, wrap : true,
onChange : ()=>{}, onChange : ()=>{},
onscroll : ()=>{},
enableFolding : true, enableFolding : true,
editorTheme : 'default' 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. // 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('change', (cm)=>{this.props.onChange(cm.getValue());});
this.codeMirror.on('scroll', (cm)=>{ this.codeMirror.on('scroll', _.throttle(this.props.onScroll, 200));
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.updateSize(); this.updateSize();
}, },