Skip to content

Commit 6cbc07d

Browse files
优化笔记浮动显示位置
1 parent ba745a1 commit 6cbc07d

File tree

1 file changed

+60
-43
lines changed

1 file changed

+60
-43
lines changed

frontend/src/components/PDFReader/index.tsx

Lines changed: 60 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -616,52 +616,61 @@ const PDFReader: React.FC<PDFReaderProps> = ({
616616
};
617617

618618
// 高亮插件配置
619-
const renderHighlightTarget = (props: RenderHighlightTargetProps) => (
620-
<div
621-
className={styles.highlightTarget}
622-
style={{
623-
left: `${props.selectionRegion.left}%`,
624-
top: `${props.selectionRegion.top + props.selectionRegion.height}%`,
625-
}}
626-
>
627-
<div className={styles.highlightTargetInner}>
628-
<div className={styles.highlightTargetOptions}>
629-
<div
630-
className={styles.highlightOption}
631-
onClick={async () => {
632-
try {
633-
highlightOnlyRef.current = true;
634-
props.toggle();
635-
} catch (error) {
636-
console.error('保存高亮失败:', error);
637-
}
638-
}}
639-
title="高亮"
640-
>
641-
<span className={styles.highlightOptionIcon}><IoMdCreate /></span>
642-
<span className={styles.highlightOptionText}>高亮</span>
643-
</div>
644-
<div
645-
className={styles.highlightOption}
646-
onClick={() => {
647-
highlightOnlyRef.current = false;
648-
props.toggle();
649-
setTimeout(() => {
650-
const noteInput = document.getElementById('note-input');
651-
if (noteInput) {
652-
noteInput.focus();
619+
const renderHighlightTarget = (props: RenderHighlightTargetProps) => {
620+
// 判断高亮区域是否在页面下方
621+
// 如果高亮区域的顶部位置+高度超过页面高度的70%,将按钮显示在上方
622+
const isBottomHalf = (props.selectionRegion.top + props.selectionRegion.height) > 70;
623+
624+
return (
625+
<div
626+
className={styles.highlightTarget}
627+
style={{
628+
left: `${props.selectionRegion.left}%`,
629+
// 根据高亮位置决定按钮显示在上方还是下方
630+
top: isBottomHalf
631+
? `calc(${props.selectionRegion.top}% - 40px)` // 高亮在下方,按钮显示在上方
632+
: `${props.selectionRegion.top + props.selectionRegion.height}%`, // 高亮在上方,按钮显示在下方
633+
}}
634+
>
635+
<div className={styles.highlightTargetInner}>
636+
<div className={styles.highlightTargetOptions}>
637+
<div
638+
className={styles.highlightOption}
639+
onClick={async () => {
640+
try {
641+
highlightOnlyRef.current = true;
642+
props.toggle();
643+
} catch (error) {
644+
console.error('保存高亮失败:', error);
653645
}
654-
}, 100);
655-
}}
656-
title="添加批注"
657-
>
658-
<span className={styles.highlightOptionIcon}><MessageIcon /></span>
659-
<span className={styles.highlightOptionText}>添加批注</span>
646+
}}
647+
title="高亮"
648+
>
649+
<span className={styles.highlightOptionIcon}><IoMdCreate /></span>
650+
<span className={styles.highlightOptionText}>高亮</span>
651+
</div>
652+
<div
653+
className={styles.highlightOption}
654+
onClick={() => {
655+
highlightOnlyRef.current = false;
656+
props.toggle();
657+
setTimeout(() => {
658+
const noteInput = document.getElementById('note-input');
659+
if (noteInput) {
660+
noteInput.focus();
661+
}
662+
}, 100);
663+
}}
664+
title="添加批注"
665+
>
666+
<span className={styles.highlightOptionIcon}><MessageIcon /></span>
667+
<span className={styles.highlightOptionText}>添加批注</span>
668+
</div>
660669
</div>
661670
</div>
662671
</div>
663-
</div>
664-
);
672+
);
673+
};
665674

666675
const renderHighlightContent = (props: RenderHighlightContentProps) => {
667676
if (highlightOnlyRef.current) {
@@ -688,13 +697,21 @@ const PDFReader: React.FC<PDFReaderProps> = ({
688697
props.cancel();
689698
return <></>;
690699
}
700+
701+
// 判断高亮区域是否在页面下方
702+
// 如果高亮区域的顶部位置+高度超过页面高度的70%,将输入框显示在上方
703+
const isBottomHalf = (props.selectionRegion.top + props.selectionRegion.height) > 70;
704+
691705
return (
692706
<div
693707
className={styles.highlightContent}
694708
style={{
695709
position: 'absolute',
696710
left: `${props.selectionRegion.left}%`,
697-
top: `${props.selectionRegion.top + props.selectionRegion.height}%`,
711+
// 根据高亮位置决定输入框显示在上方还是下方
712+
top: isBottomHalf
713+
? `${props.selectionRegion.top - 20}%` // 高亮在下方,输入框显示在上方
714+
: `${props.selectionRegion.top + props.selectionRegion.height}%`, // 高亮在上方,输入框显示在下方
698715
zIndex: 5,
699716
}}
700717
>

0 commit comments

Comments
 (0)