Skip to content

Commit

Permalink
Merge pull request #562 from Lemoncode/dev
Browse files Browse the repository at this point in the history
text aligment to production
  • Loading branch information
brauliodiez authored Nov 20, 2024
2 parents f5319ef + 794a7db commit 0a427ed
Show file tree
Hide file tree
Showing 24 changed files with 346 additions and 39 deletions.
9 changes: 9 additions & 0 deletions e2e/helpers/konva-testing.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ export const getTransformer = async (page: Page): Promise<any> => {
return transformer;
};

export const getIndexFromCanvasShape = async (
page: Page,
shapeId: number
): Promise<number> => {
const children = await getChildren(page);
const index = children.findIndex(child => child._id === shapeId);
return index;
};

export const getWithinCanvasItemList = async (
page: Page
): Promise<Group['attrs'][]> => {
Expand Down
7 changes: 4 additions & 3 deletions e2e/helpers/position.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Locator, Page } from '@playwright/test';
import { Group } from 'konva/lib/Group';

interface Position {
export interface Position {
x: number;
y: number;
}
Expand Down Expand Up @@ -43,7 +43,8 @@ export const dragAndDrop = async (

export const addComponentsToCanvas = async (
page: Page,
components: string[]
components: string[],
displacementQty: number = 120
) => {
const stageCanvas = await page.locator('#konva-stage canvas').first();
const canvasPosition = await stageCanvas.boundingBox();
Expand All @@ -65,7 +66,7 @@ export const addComponentsToCanvas = async (
};
};

await dragAndDrop(page, position, targetPosition(120, index));
await dragAndDrop(page, position, targetPosition(displacementQty, index));
}
};

Expand Down
92 changes: 92 additions & 0 deletions e2e/z-index/z-index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { test, expect } from '@playwright/test';
import {
addComponentsToCanvas,
getAllByShapeType,
getIndexFromCanvasShape,
getShapePosition,
} from '../helpers';
import { Group } from 'konva/lib/Group';

test('drop three shapes in canvas, select one, try all z-index levels', async ({
page,
}) => {
await page.goto('');

//Drag and drop component to canvas
const componentsAtCanvas = ['Input', 'Input', 'Input'];
await addComponentsToCanvas(page, componentsAtCanvas, 30);

const inputShapes: Group[] = (await getAllByShapeType(
page,
'input'
)) as Group[];
expect(inputShapes.length).toBe(3);

// Get Canvas position
const stageCanvas = await page.locator('#konva-stage canvas').first();
expect(stageCanvas).toBeDefined();
const canvasPosition = await stageCanvas.boundingBox();
if (!canvasPosition) throw new Error('No canvas found');

// Click on empty canvas space to clear selection
await page.mouse.click(500, 500);

// Click on second input shape (will be the test subject)
const inputShapePosition = await getShapePosition(inputShapes[1]);
await page.mouse.click(
canvasPosition.x + inputShapePosition.x + 30,
canvasPosition.y + inputShapePosition.y + 10
);

// Get initial Z-index of all shapes
const InitialzIndexes: number[] = await Promise.all(
inputShapes.map(async shape => {
return await getIndexFromCanvasShape(page, shape._id);
})
);
expect(InitialzIndexes).toEqual([0, 1, 2]);

// FIRST BUTTON: Move to the top (highest z-index)
await page.locator('button[aria-label="Bring to front"]').click();

// Verify Z-index after moving to the top
const zIndexesAfterMoveToTop: number[] = await Promise.all(
inputShapes.map(async shape => {
return await getIndexFromCanvasShape(page, shape._id);
})
);
expect(zIndexesAfterMoveToTop).toEqual([0, 2, 1]); // Second shape is now on top

// SECOND BUTTON: Move to the bottom (lowest z-index)
await page.locator('button[aria-label="Send to back"]').click();

// Verify Z-index after moving to the bottom
const zIndexesAfterMoveToBottom: number[] = await Promise.all(
inputShapes.map(async shape => {
return await getIndexFromCanvasShape(page, shape._id);
})
);
expect(zIndexesAfterMoveToBottom).toEqual([1, 0, 2]); // Second shape is now at the bottom

// THIRD BUTTON: Move up one level
await page.locator('button[aria-label="Bring forward"]').click();

// Verify Z-index after moving up one level
const zIndexesAfterMoveUp: number[] = await Promise.all(
inputShapes.map(async shape => {
return await getIndexFromCanvasShape(page, shape._id);
})
);
expect(zIndexesAfterMoveUp).toEqual([0, 1, 2]); // Second shape moved up one level

// FOURTH BUTTON: Move down one level
await page.locator('button[aria-label="Send backward"]').click();

// Verify Z-index after moving down one level
const zIndexesAfterMoveDown: number[] = await Promise.all(
inputShapes.map(async shape => {
return await getIndexFromCanvasShape(page, shape._id);
})
);
expect(zIndexesAfterMoveDown).toEqual([1, 0, 2]); // Second shape moved down one level again
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const DEFAULT_TEXT_HEIGHT = 38;
const DEFAULT_FONT_VARIANT = 'normal';
const DEFAULT_FONT_STYLE = 'normal';
const DEFAULT_TEXT_DECORATION = 'none';
const DEFAULT_TEXT_ALIGNMENT = 'left';

export interface DefaultStyleShape {
DEFAULT_CORNER_RADIUS: number;
Expand All @@ -32,6 +33,7 @@ export interface DefaultStyleShape {
DEFAULT_FONT_VARIANT: string;
DEFAULT_FONT_STYLE: string;
DEFAULT_TEXT_DECORATION: string;
DEFAULT_TEXT_ALIGNMENT: 'left' | 'center' | 'right';
}

export const BASIC_SHAPE: DefaultStyleShape = {
Expand All @@ -50,6 +52,7 @@ export const BASIC_SHAPE: DefaultStyleShape = {
DEFAULT_FONT_VARIANT,
DEFAULT_FONT_STYLE,
DEFAULT_TEXT_DECORATION,
DEFAULT_TEXT_ALIGNMENT,
};

export const INPUT_SHAPE: DefaultStyleShape = {
Expand All @@ -68,6 +71,7 @@ export const INPUT_SHAPE: DefaultStyleShape = {
DEFAULT_FONT_VARIANT,
DEFAULT_FONT_STYLE,
DEFAULT_TEXT_DECORATION,
DEFAULT_TEXT_ALIGNMENT,
};

//! maybe a function to calc max height base on the text
Expand All @@ -87,6 +91,7 @@ export const POSTIT_SHAPE: DefaultStyleShape = {
DEFAULT_FONT_VARIANT,
DEFAULT_FONT_STYLE,
DEFAULT_TEXT_DECORATION,
DEFAULT_TEXT_ALIGNMENT,
};

interface FontValues {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ export const Heading1Shape = forwardRef<any, ShapeProps>((props, ref) => {
);
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;

const { textColor, textDecoration, fontStyle, fontVariant, fontSize } =
useShapeProps(otherProps, BASIC_SHAPE);
const {
textColor,
textDecoration,
fontStyle,
fontVariant,
fontSize,
textAlignment,
} = useShapeProps(otherProps, BASIC_SHAPE);

const commonGroupProps = useGroupShapeProps(
props,
Expand All @@ -61,7 +67,7 @@ export const Heading1Shape = forwardRef<any, ShapeProps>((props, ref) => {
fontFamily={BASIC_SHAPE.DEFAULT_FONT_FAMILY}
fontSize={fontSize}
fill={textColor}
align="center"
align={textAlignment}
verticalAlign="middle"
ellipsis={true}
wrap="none"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ export const Heading2Shape = forwardRef<any, ShapeProps>((props, ref) => {
);
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;

const { textColor, fontVariant, fontStyle, textDecoration, fontSize } =
useShapeProps(otherProps, BASIC_SHAPE);
const {
textColor,
fontVariant,
fontStyle,
textDecoration,
fontSize,
textAlignment,
} = useShapeProps(otherProps, BASIC_SHAPE);

const commonGroupProps = useGroupShapeProps(
props,
Expand All @@ -61,7 +67,7 @@ export const Heading2Shape = forwardRef<any, ShapeProps>((props, ref) => {
fontFamily={BASIC_SHAPE.DEFAULT_FONT_FAMILY}
fontSize={fontSize}
fill={textColor}
align="center"
align={textAlignment}
verticalAlign="middle"
ellipsis={true}
wrap="none"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ export const Heading3Shape = forwardRef<any, ShapeProps>((props, ref) => {

const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;

const { textColor, fontVariant, fontStyle, textDecoration, fontSize } =
useShapeProps(otherProps, BASIC_SHAPE);
const {
textColor,
fontVariant,
fontStyle,
textDecoration,
fontSize,
textAlignment,
} = useShapeProps(otherProps, BASIC_SHAPE);

const commonGroupProps = useGroupShapeProps(
props,
Expand All @@ -62,7 +68,7 @@ export const Heading3Shape = forwardRef<any, ShapeProps>((props, ref) => {
fontFamily={BASIC_SHAPE.DEFAULT_FONT_FAMILY}
fontSize={fontSize}
fill={textColor}
align="center"
align={textAlignment}
verticalAlign="middle"
ellipsis={true}
wrap="none"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const LinkShape = forwardRef<any, ShapeProps>((props, ref) => {

const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;

const { textColor, textDecoration, fontSize } = useShapeProps(
const { textColor, textDecoration, fontSize, textAlignment } = useShapeProps(
otherProps,
BASIC_SHAPE
);
Expand All @@ -64,7 +64,7 @@ export const LinkShape = forwardRef<any, ShapeProps>((props, ref) => {
fontFamily={BASIC_SHAPE.DEFAULT_FONT_FAMILY}
fontSize={fontSize}
fill={textColor}
align="center"
align={textAlignment}
verticalAlign="middle"
ellipsis={true}
wrap="none"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ export const NormaltextShape = forwardRef<any, ShapeProps>((props, ref) => {
);
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;

const { textColor, fontVariant, fontStyle, textDecoration, fontSize } =
useShapeProps(otherProps, BASIC_SHAPE);
const {
textColor,
fontVariant,
fontStyle,
textDecoration,
fontSize,
textAlignment,
} = useShapeProps(otherProps, BASIC_SHAPE);

const commonGroupProps = useGroupShapeProps(
props,
Expand All @@ -61,7 +67,7 @@ export const NormaltextShape = forwardRef<any, ShapeProps>((props, ref) => {
fontFamily={BASIC_SHAPE.DEFAULT_FONT_FAMILY}
fontSize={fontSize}
fill={textColor}
align="center"
align={textAlignment}
verticalAlign="middle"
ellipsis={true}
wrap="none"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export const ParagraphShape = forwardRef<any, ShapeProps>((props, ref) => {
);
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;

const { textColor, fontSize } = useShapeProps(otherProps, BASIC_SHAPE);
const { textColor, fontSize, textAlignment } = useShapeProps(
otherProps,
BASIC_SHAPE
);

const commonGroupProps = useGroupShapeProps(
props,
Expand All @@ -60,7 +63,7 @@ export const ParagraphShape = forwardRef<any, ShapeProps>((props, ref) => {
fontFamily={BASIC_SHAPE.DEFAULT_FONT_FAMILY}
fontSize={fontSize}
fill={textColor}
align="left"
align={textAlignment}
ellipsis={true}
/>
</Group>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ export const SmalltextShape = forwardRef<any, ShapeProps>((props, ref) => {
);
const { width: restrictedWidth, height: restrictedHeight } = restrictedSize;

const { textColor, fontVariant, fontStyle, textDecoration, fontSize } =
useShapeProps(otherProps, BASIC_SHAPE);
const {
textColor,
fontVariant,
fontStyle,
textDecoration,
fontSize,
textAlignment,
} = useShapeProps(otherProps, BASIC_SHAPE);

const commonGroupProps = useGroupShapeProps(
props,
Expand All @@ -61,7 +67,7 @@ export const SmalltextShape = forwardRef<any, ShapeProps>((props, ref) => {
fontFamily={BASIC_SHAPE.DEFAULT_FONT_FAMILY}
fontSize={fontSize}
fill={textColor}
align="center"
align={textAlignment}
verticalAlign="middle"
ellipsis={true}
wrap="none"
Expand Down
Loading

0 comments on commit 0a427ed

Please sign in to comment.