0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 20:42:43 +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;
},
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;

View File

@@ -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();
},