Skip to content

Commit

Permalink
fix: scroll to marks
Browse files Browse the repository at this point in the history
  • Loading branch information
lllzhmio committed Nov 12, 2024
1 parent 4401c01 commit 9cfb791
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/Page/PageContent/EssayMarks/BrightSpot/BrightSpot.css
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@
}

.BrightSpot__goodSent--selected {
border: 2px solid #EF9F6C;
box-shadow: 0 0 10px rgba(239, 159, 108, 0.3);
background: rgba(211, 214, 216, 0.38);
}

.BrightSpot__goodSent__text {
Expand Down
19 changes: 12 additions & 7 deletions src/Page/PageContent/EssayMarks/BrightSpot/BrightSpot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import TypewriterAnimationForString from 'src/Page/_components/TypewriterAnimati
import shortenSent from '../shortenSent';

import './BrightSpot.css';
import { useScrollToMark } from 'src/hooks/useScrollToMark';

export function judgeRhetoric(label: string) {
switch (label) {
Expand All @@ -28,14 +29,16 @@ export default function BrightSpot() {
const selectedSentence = useEssay(store => store.selectedSentence);
const setSelectedSentence = useEssay(store => store.setSelectedSentence);

const goodWords = good_words.map(({ paragraph_id, sent_id, start, end }) => (
<div className="BrightSpot__goodWord" key={`${paragraph_id} ${sent_id}`}>
<div className="BrightSpot__goodWord__title">好词</div>
<div className="BrightSpot__goodWord__content">
{sents[paragraph_id][sent_id].substring(start, end)}
const goodWords = useMemo(() => {
return good_words.map(({ paragraph_id, sent_id, start, end }) => (
<div className="BrightSpot__goodWord" key={`${paragraph_id} ${sent_id}`}>
<div className="BrightSpot__goodWord__title">好词</div>
<div className="BrightSpot__goodWord__content">
{sents[paragraph_id][sent_id].substring(start, end)}
</div>
</div>
</div>
));
));
}, [good_words, sents]);

const goodSentsList = useMemo(() => {
return good_sents_arranged.map((goodSentsInParagraph, paragraphId) => (
Expand Down Expand Up @@ -77,6 +80,8 @@ export default function BrightSpot() {
});
}, [selectedSentence]);

useScrollToMark(selectedSentence, '.BrightSpot__goodSent', '.BrightSpot');

return (
<div className="BrightSpot">
<TypewriterAnimation duration={1} list={goodWords} key="goodWords" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@
}

.SickExpression__sickSent--selected {
border: 2px solid #EF9F6C;
box-shadow: 0 0 10px rgba(239, 159, 108, 0.3);
background: rgba(211, 214, 216, 0.38);
}

.SickExpression__sickSent__header {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useEssay } from '@store';
import TypewriterAnimation from 'src/Page/_components/TypewriterAnimation/TypewriterAnimation';

import './SickExpression.css';
import { useScrollToMark } from 'src/hooks/useScrollToMark';

export default function SickExpression() {
const { sick_sents_arranged, textCorrections_arranged, sents } = useEssay(store => store.essay);
Expand Down Expand Up @@ -80,6 +81,9 @@ export default function SickExpression() {
});
}, [selectedSentence]);

// 添加滚动功能
useScrollToMark(selectedSentence, '.SickExpression__sickSent', '.SickExpression');

return (
<div className="SickExpression">
<TypewriterAnimation duration={1} list={sickSentsList} key="sickSents" />
Expand Down
34 changes: 34 additions & 0 deletions src/hooks/useScrollToMark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useEffect } from 'react';

export function useScrollToMark(
selectedSentence: { paragraphId: number; sentId: number } | null,
selector: string,
containerSelector: string
) {
useEffect(() => {
if (!selectedSentence) return;

const container = document.querySelector(containerSelector) as HTMLElement;
if (!container) return;

const elements = container.querySelectorAll(selector);
elements.forEach(el => {
const element = el as HTMLElement; // 添加类型断言
const paragraphId = Number(element.getAttribute('data-paragraph-id'));
const sentId = Number(element.getAttribute('data-sent-id'));

if (selectedSentence.paragraphId === paragraphId &&
selectedSentence.sentId === sentId) {
// 计算元素相对于容器的位置
const elementTop = element.offsetTop;
const containerHeight = container.clientHeight;
const scrollTop = elementTop - containerHeight / 2 + element.offsetHeight / 2;

container.scrollTo({
top: scrollTop,
behavior: 'smooth'
});
}
});
}, [selectedSentence]);
}

0 comments on commit 9cfb791

Please sign in to comment.