Skip to content
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

Create getActivePage and use activePageIndex on addNewPage and duplic… #528

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/providers/canvas/canvas.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export interface CanvasContextModel {
fullDocument: DocumentModel;
addNewPage: () => void;
duplicatePage: (pageIndex: number) => void;
getActivePage: () => Page;
setActivePage: (pageId: string) => void;
deletePage: (pageIndex: number) => void;
editPageTitle: (pageIndex: number, newName: string) => void;
Expand Down
14 changes: 11 additions & 3 deletions src/core/providers/canvas/canvas.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ export const CanvasProvider: React.FC<Props> = props => {
const addNewPage = () => {
setDocument(lastDocument =>
produce(lastDocument, draft => {
const newActiveIndex = draft.pages.length;
draft.pages.push({
id: uuidv4(),
name: `Page ${draft.pages.length + 1}`,
name: `Page ${newActiveIndex + 1}`,
shapes: [],
});
draft.activePageIndex = newActiveIndex;
})
);
};
Expand All @@ -71,8 +73,9 @@ export const CanvasProvider: React.FC<Props> = props => {
name: `${document.pages[pageIndex].name} - copy`,
shapes: newShapes,
};
draft.pages.push(newPage);
setActivePage(newPage.id);
const newIndex = draft.activePageIndex + 1;
draft.pages.splice(newIndex, 0, newPage);
draft.activePageIndex = newIndex;
})
);
};
Expand All @@ -94,6 +97,10 @@ export const CanvasProvider: React.FC<Props> = props => {
setActivePage(newActivePageId);
};

const getActivePage = () => {
return document.pages[document.activePageIndex];
};

const setActivePage = (pageId: string) => {
selectionInfo.clearSelection();
selectionInfo.shapeRefs.current = {};
Expand Down Expand Up @@ -296,6 +303,7 @@ export const CanvasProvider: React.FC<Props> = props => {
fullDocument: document,
addNewPage,
duplicatePage,
getActivePage,
setActivePage,
deletePage,
editPageTitle,
Expand Down
11 changes: 6 additions & 5 deletions src/pods/thumb-pages/thumb-pages.pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import { useCanvasContext } from '@/core/providers';
import { PageTitleInlineEdit, ThumbPage } from './components';

export const ThumbPagesPod: React.FC = () => {
const { fullDocument, addNewPage, setActivePage } = useCanvasContext();
const { fullDocument, addNewPage, setActivePage, getActivePage } =
useCanvasContext();
const [pageTitleBeingEdited, setPageTitleBeingEdited] = React.useState<
number | null
>(null);
const [activePageId, setActivePageId] = React.useState<string | null>(null);

const handleAddNewPage = () => {
addNewPage();
};

const handleSetActivePage = (pageId: string) => {
setActivePage(pageId);
setActivePageId(pageId);
};

return (
Expand All @@ -25,7 +24,7 @@ export const ThumbPagesPod: React.FC = () => {
<React.Fragment key={page.id}>
<div
className={`${classes.container} ${
page.id === activePageId ? classes.activeThumb : ''
page.id === getActivePage().id ? classes.activeThumb : ''
}`}
>
<ThumbPage
Expand All @@ -41,7 +40,9 @@ export const ThumbPagesPod: React.FC = () => {
) : (
<div
onDoubleClick={() => setPageTitleBeingEdited(index)}
className={page.id === activePageId ? classes.activeText : ''}
className={
page.id === getActivePage().id ? classes.activeText : ''
}
>
{page.name}
</div>
Expand Down