mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-05-07 18:48:39 +00:00
handle scroll remake
This commit is contained in:
@@ -129,12 +129,6 @@ const CodeEditor = forwardRef(
|
|||||||
|
|
||||||
onCursorChange(line);
|
onCursorChange(line);
|
||||||
}
|
}
|
||||||
if(update.viewportChanged) {
|
|
||||||
const { from } = update.view.viewport;
|
|
||||||
const line = update.state.doc.lineAt(from).number;
|
|
||||||
|
|
||||||
onViewChange(line);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const highlightExtension = renderer === 'V3'
|
const highlightExtension = renderer === 'V3'
|
||||||
@@ -169,7 +163,7 @@ const CodeEditor = forwardRef(
|
|||||||
themeCompartment.of(themeExtension),
|
themeCompartment.of(themeExtension),
|
||||||
highlightActiveLine(),
|
highlightActiveLine(),
|
||||||
highlightActiveLineGutter(),
|
highlightActiveLineGutter(),
|
||||||
|
|
||||||
//keyboard shortcut
|
//keyboard shortcut
|
||||||
keymap.of([...defaultKeymap, foldKeymap, ...searchKeymap]),
|
keymap.of([...defaultKeymap, foldKeymap, ...searchKeymap]),
|
||||||
generalKeymap,
|
generalKeymap,
|
||||||
@@ -195,9 +189,35 @@ const CodeEditor = forwardRef(
|
|||||||
parent : editorRef.current,
|
parent : editorRef.current,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const view = viewRef.current;
|
||||||
|
|
||||||
|
let ticking = false;
|
||||||
|
|
||||||
|
const handleScroll = ()=>{
|
||||||
|
if(ticking) return;
|
||||||
|
|
||||||
|
ticking = true;
|
||||||
|
requestAnimationFrame(()=>{
|
||||||
|
const view = viewRef.current;
|
||||||
|
if(!view?.scrollDOM) return;
|
||||||
|
|
||||||
|
const top = view.scrollDOM.scrollTop;
|
||||||
|
const block = view.lineBlockAtHeight(top);
|
||||||
|
const line = view.state.doc.lineAt(block.from).number;
|
||||||
|
|
||||||
|
onViewChange(line);
|
||||||
|
ticking = false;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
view.scrollDOM.addEventListener('scroll', handleScroll);
|
||||||
|
|
||||||
docsRef.current[tab] = state;
|
docsRef.current[tab] = state;
|
||||||
|
|
||||||
return ()=>viewRef.current?.destroy();
|
return ()=>{
|
||||||
|
view.scrollDOM.removeEventListener('scroll', handleScroll);
|
||||||
|
viewRef.current?.destroy();
|
||||||
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
useEffect(()=>{
|
useEffect(()=>{
|
||||||
|
|||||||
@@ -266,29 +266,6 @@ const Editor = createReactClass({
|
|||||||
this.forceUpdate();
|
this.forceUpdate();
|
||||||
},
|
},
|
||||||
|
|
||||||
//temporary fix until cm6 comes next update
|
|
||||||
attachCodeMirrorListeners : function(cm) {
|
|
||||||
if(!cm) return;
|
|
||||||
// detach previous (important on remount / view switch)
|
|
||||||
if(this._cm) {
|
|
||||||
this._cm.off('cursorActivity', this._onCursor);
|
|
||||||
this._cm.off('scroll', this._onScroll);
|
|
||||||
}
|
|
||||||
|
|
||||||
this._cm = cm;
|
|
||||||
|
|
||||||
this._onCursor = ()=>{
|
|
||||||
this.updateCurrentCursorPage(cm.getCursor());
|
|
||||||
};
|
|
||||||
|
|
||||||
this._onScroll = _.throttle(()=>{
|
|
||||||
const topLine = cm.lineAtHeight(cm.getScrollInfo().top, 'local');
|
|
||||||
this.updateCurrentViewPage(topLine);
|
|
||||||
}, 200);
|
|
||||||
|
|
||||||
cm.on('cursorActivity', this._onCursor);
|
|
||||||
cm.on('scroll', this._onScroll);
|
|
||||||
},
|
|
||||||
renderEditor : function(){
|
renderEditor : function(){
|
||||||
if(this.isText()){
|
if(this.isText()){
|
||||||
return <>
|
return <>
|
||||||
|
|||||||
Reference in New Issue
Block a user