Skip to content

Commit dc33c00

Browse files
authored
Merge pull request #767 from Lemoncode/feature/#544-drop-button-and-visible-on-thumb
Feature/#544 drop button and visible on thumb
2 parents 0cbf740 + c579fe2 commit dc33c00

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

e2e/helpers/konva-testing.helpers.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { Group } from 'konva/lib/Group';
55
import { E2E_CanvasItemKeyAttrs } from './types/e2e-types';
66
import { getCanvasBoundingBox } from './position.helpers';
77

8+
// MAIN CANVAS HELPERS
9+
810
const getLayer = async (page: Page): Promise<Layer> =>
911
await page.evaluate(() => {
1012
return window.__TESTING_KONVA_LAYER__;
@@ -50,6 +52,50 @@ export const getByShapeType = async (
5052
}
5153
};
5254

55+
// THUMB HELPERS
56+
57+
const getThumbLayer = async (page: Page, pageIndex: number): Promise<Layer> =>
58+
await page.evaluate(index => {
59+
return window.__TESTING_KONVA_THUMB_LAYERS__?.[index];
60+
}, pageIndex);
61+
62+
const getThumbChildren = async (page: Page, pageIndex: number) => {
63+
const layer = await getThumbLayer(page, pageIndex);
64+
return layer?.children || [];
65+
};
66+
67+
// Waits for a thumb to finish rendering (until it has at least one child)
68+
69+
export const waitForThumbToRender = async (
70+
page: Page,
71+
pageIndex: number,
72+
timeout = 5000
73+
) => {
74+
await page.waitForFunction(
75+
async index => {
76+
const layer = window.__TESTING_KONVA_THUMB_LAYERS__?.[index];
77+
if (!layer) return false;
78+
79+
const children = layer.children || [];
80+
return children && children.length > 0;
81+
},
82+
pageIndex,
83+
{ timeout }
84+
);
85+
};
86+
87+
export const getByShapeTypeInThumb = async (
88+
page: Page,
89+
pageIndex: number,
90+
shape: string
91+
): Promise<Group | Shape | undefined> => {
92+
await waitForThumbToRender(page, pageIndex);
93+
94+
// Search for the shape
95+
const children = await getThumbChildren(page, pageIndex);
96+
return children?.find(child => child.attrs.shapeType === shape);
97+
};
98+
5399
export const getTransformer = async (page: Page): Promise<any> => {
54100
const layer = await getLayer(page);
55101
const transformer = layer?.children.find((child: any) => {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { test, expect } from '@playwright/test';
2+
import {
3+
dragAndDrop,
4+
getByShapeTypeInThumb,
5+
getLocatorPosition,
6+
} from '../helpers';
7+
8+
test('drop button in canvas, ensure is visible on thumb', async ({ page }) => {
9+
await page.goto('');
10+
11+
// Drag & drop button in canvas
12+
const button = page.getByAltText('Button', { exact: true });
13+
await expect(button).toBeVisible();
14+
15+
const position = await getLocatorPosition(button);
16+
const targetPosition = { x: position.x + 500, y: position.y };
17+
await dragAndDrop(page, position, targetPosition);
18+
19+
// Open Pages panel
20+
await page.getByText('Pages').click();
21+
22+
// Verify button is visible in thumb
23+
const buttonInThumb = await getByShapeTypeInThumb(page, 0, 'button');
24+
expect(buttonInThumb).toBeDefined();
25+
});

global.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ import Konva from 'konva';
33
declare global {
44
interface Window {
55
__TESTING_KONVA_LAYER__: Konva.Layer;
6+
__TESTING_KONVA_THUMB_LAYERS__: Konva.Layer[];
67
}
78
}

src/pods/thumb-pages/components/thumb-page.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import classes from './thumb-page.module.css';
1212

1313
import React from 'react';
1414
import { useDragDropThumb } from './drag-drop-thumb.hook';
15+
import Konva from 'konva';
16+
import { ENV } from '@/core/constants';
1517

1618
interface Props {
1719
pageIndex: number;
@@ -35,6 +37,7 @@ export const ThumbPage: React.FunctionComponent<Props> = props => {
3537
});
3638

3739
const divRef = useRef<HTMLDivElement>(null);
40+
const layerRef = useRef<Konva.Layer>(null);
3841
const [key, setKey] = React.useState(0);
3942

4043
const { dragging, isDraggedOver } = useDragDropThumb(divRef, pageIndex);
@@ -52,6 +55,15 @@ export const ThumbPage: React.FunctionComponent<Props> = props => {
5255
}, 100);
5356
};
5457

58+
// Exposing thumb layer for testing
59+
60+
if (typeof window !== 'undefined' && ENV.IS_TEST_ENV && layerRef.current) {
61+
if (!window.__TESTING_KONVA_THUMB_LAYERS__) {
62+
window.__TESTING_KONVA_THUMB_LAYERS__ = [];
63+
}
64+
window.__TESTING_KONVA_THUMB_LAYERS__[pageIndex] = layerRef.current;
65+
}
66+
5567
React.useLayoutEffect(() => {
5668
handleResizeAndForceRedraw();
5769
}, []);
@@ -105,7 +117,7 @@ export const ThumbPage: React.FunctionComponent<Props> = props => {
105117
scaleX={finalScale}
106118
scaleY={finalScale}
107119
>
108-
<Layer>
120+
<Layer ref={layerRef}>
109121
{shapes.map(shape => {
110122
if (!fakeShapeRefs.current[shape.id]) {
111123
fakeShapeRefs.current[shape.id] = createRef();

0 commit comments

Comments
 (0)