chore: auto scroll

This commit is contained in:
jialin
2024-09-14 22:14:25 +08:00
parent 2c0d813767
commit 86a43d3006
2 changed files with 9 additions and 5 deletions
+8 -4
View File
@@ -4,7 +4,7 @@ import { Terminal } from '@xterm/xterm';
import '@xterm/xterm/css/xterm.css';
import classNames from 'classnames';
import _ from 'lodash';
import { memo, useCallback, useEffect, useRef } from 'react';
import { memo, useCallback, useEffect, useRef, useState } from 'react';
import './index.less';
import useSize from './use-size';
@@ -23,8 +23,13 @@ const LogsViewer: React.FC<LogsViewerProps> = (props) => {
const termwrapRef = useRef<any>({});
const fitAddonRef = useRef<any>({});
const cacheDataRef = useRef<any>(null);
const [logs, setLogs] = useState('');
const size = useSize(scroller);
const throttleScroll = _.debounce(() => {
termRef.current?.scrollToBottom?.();
}, 100);
const updateContent = useCallback(
_.throttle((data: string) => {
cacheDataRef.current = '';
@@ -73,10 +78,9 @@ const LogsViewer: React.FC<LogsViewerProps> = (props) => {
termRef.current.open(termwrapRef.current);
// add event
termRef.current.onWriteParsed((e: any) => {
termRef.current.onLineFeed((e: any) => {
if (cacheDataRef.current) {
termRef.current?.scrollToBottom();
throttleScroll();
}
});
};