-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new CanvasProvider state to manage the opening of the ThumbsPagesPod accordion #622
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ export const CanvasProvider: React.FC<Props> = props => { | |
const [fileName, setFileName] = React.useState<string>(''); | ||
const [isThumbnailContextMenuVisible, setIsThumbnailContextMenuVisible] = | ||
React.useState(false); | ||
const [isFileLoaded, setIsFileLoaded] = React.useState(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A flag is a good idea, but true/false won't work the second time you open a document, one trick we can do is to count the number of times we have opened documents, something like: const [howManyLoadedDocuments, setHowManyLoadedDocuments] = React.useState(0); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The setter we can call it directly here in the provider (no need to call it from outside, something like: const loadDocument = (document: DocumentModel) => {
setDocument(document);
+ setHowManyLoadedDocuments(numberOfDocuments => numberOfDocuments + 1);
}; |
||
|
||
const { | ||
addSnapshot, | ||
|
@@ -331,6 +332,8 @@ export const CanvasProvider: React.FC<Props> = props => { | |
activePageIndex: document.activePageIndex, | ||
isThumbnailContextMenuVisible, | ||
setIsThumbnailContextMenuVisible, | ||
isFileLoaded, | ||
setIsFileLoaded, | ||
}} | ||
> | ||
{children} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,11 +14,15 @@ import { PropertiesPod } from '@/pods/properties'; | |
import { FooterPod } from '@/pods/footer/footer.pod'; | ||
import { ThumbPagesPod } from '@/pods/thumb-pages'; | ||
import { useAccordionSectionVisibility } from './accordion-section-visibility.hook'; | ||
import { useCanvasContext } from '@/core/providers'; | ||
|
||
export const MainScene = () => { | ||
const { isThumbPagesPodOpen, thumbPagesPodRef } = | ||
useAccordionSectionVisibility(); | ||
|
||
const { isFileLoaded } = useCanvasContext(); | ||
const forceOpenThumbsPages = isFileLoaded && { open: true }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mmm.. we could create a custom hook to encapsulate the open doc funciontallity, the use it directly in the scene, something like: import { useCanvasContext } from '@/core/providers';
import { useEffect, useRef, useState } from 'react';
export const useAccordionSectionVisibility = () => {
const [isThumbPagesPodOpen, setIsThumbPagesPodOpen] = useState(false);
const thumbPagesPodRef = useRef<HTMLDetailsElement>(null);
const { fullDocument, howManyLoadedDocuments } = useCanvasContext();
useEffect(() => {
if (
howManyLoadedDocuments > 0 &&
thumbPagesPodRef.current &&
fullDocument.pages.length > 1
) {
setIsThumbPagesPodOpen(true);
thumbPagesPodRef.current.open = true;
}
}, [howManyLoadedDocuments]);
useEffect(() => {
const handleToggle = () => {
setIsThumbPagesPodOpen(thumbPagesPodRef.current?.open ?? false);
};
const detailsElement = thumbPagesPodRef.current;
if (detailsElement) {
detailsElement.addEventListener('toggle', handleToggle);
}
// Cleanup event listener on component unmount
return () => {
if (detailsElement) {
detailsElement.removeEventListener('toggle', handleToggle);
}
};
}, []);
return {
thumbPagesPodRef,
isThumbPagesPodOpen,
};
}; |
||
|
||
return ( | ||
<MainLayout> | ||
<ToolbarPod /> | ||
|
@@ -27,6 +31,7 @@ export const MainScene = () => { | |
className={classes.container} | ||
name="toolsLeft" | ||
ref={thumbPagesPodRef} | ||
{...forceOpenThumbsPages} | ||
> | ||
<summary className={classes.title}>Pages</summary> | ||
<ThumbPagesPod isVisible={isThumbPagesPodOpen} /> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather in the provider (see comment)