Skip to content

Commit e7a95af

Browse files
authored
EditorInput - remove auto scroll on content change (#1132)
Fixes - https://raspberrypifoundation.slack.com/archives/C022FDFRM7B/p1730800544670889
1 parent 817e28c commit e7a95af

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## Unreleased
88

9+
### Fixed
10+
11+
- stopped autoscrolling to top on project content change (#1132)
12+
913
## [0.28.8] - 2024-11-04
1014

1115
### Changed

src/components/Editor/EditorInput/EditorInput.jsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { createRef, useEffect, useRef, useState } from "react";
1+
import React, { createRef, useEffect, useRef, useState, useMemo } from "react";
22
import { DragDropContext } from "@hello-pangea/dnd";
33
import { useDispatch, useSelector } from "react-redux";
44
import { TabPanel, Tabs } from "react-tabs";
@@ -32,6 +32,11 @@ const EditorInput = () => {
3232
const isMobile = useMediaQuery({ query: MOBILE_MEDIA_QUERY });
3333
const readOnly = useSelector((state) => state.editor.readOnly);
3434

35+
const [numberOfComponents, setNumberOfComponents] = useState(
36+
project?.components?.length,
37+
);
38+
const [fileNames, setFileNames] = useState();
39+
3540
const { t } = useTranslation();
3641

3742
const onDragStart = (input) => {
@@ -78,9 +83,6 @@ const EditorInput = () => {
7883
dispatch(closeFile(fileName));
7984
};
8085

81-
const [numberOfComponents, setNumberOfComponents] = useState(
82-
project?.components?.length,
83-
);
8486
let tabRefs = useRef(project?.components?.map(createRef));
8587

8688
useEffect(() => {
@@ -95,19 +97,26 @@ const EditorInput = () => {
9597
}, [project?.components]);
9698

9799
useEffect(() => {
98-
if (!project?.components) return;
100+
const newFileNames = project.components.map(
101+
(file) => `${file.name}.${file.extension}`,
102+
);
103+
if (newFileNames.join() !== fileNames?.join()) {
104+
setFileNames(newFileNames);
105+
}
106+
}, [fileNames, project.components]);
107+
108+
useEffect(() => {
109+
if (!fileNames) return;
99110

100111
focussedFileIndices.forEach((index, i) => {
101112
const fileName = openFiles[i][index];
102-
const componentIndex = project.components.findIndex(
103-
(file) => `${file.name}.${file.extension}` === fileName,
104-
);
113+
const componentIndex = fileNames.findIndex((name) => name === fileName);
105114
const fileRef = tabRefs.current[componentIndex];
106115
if (fileRef && fileRef.current) {
107116
fileRef.current.parentElement.scrollIntoView();
108117
}
109118
});
110-
}, [focussedFileIndices, openFiles, numberOfComponents, project]);
119+
}, [focussedFileIndices, openFiles, numberOfComponents, fileNames]);
111120

112121
if (!project || !project.components) {
113122
return null;

0 commit comments

Comments
 (0)