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

#91 inline-edit HTML is componentized #144

Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 37 additions & 0 deletions src/common/components/inline-edit/html-edit.widget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Html } from 'react-konva-utils';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place this in a subfolder

src/common/components/inline-edit/components/html-edit.widget.tsx

and add a barrel

import { forwardRef } from 'react';
import { EditType, StyleDivProps } from './inline-edit.model';

interface Props {
divProps: StyleDivProps;
value: string;
editType: EditType;
onSetEditText: (e: string) => void;
}

export const HtmlEditWidget = forwardRef<any, Props>(
({ divProps, onSetEditText, value, editType }, ref) => {
const Element = editType === 'textarea' ? 'textarea' : 'input';

return (
<Html
divProps={{
style: {
position: divProps.position,
top: divProps.top,
left: divProps.left,
width: divProps.width,
height: divProps.height,
},
}}
>
<Element
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather use conditional rendering here, reasons why:

  • It's clearer that you are using a textarea or input
  • In the future we could have more editable inputs with different signatures.

ref={ref}
style={{ width: '100%', height: '100%' }}
value={value}
onChange={e => onSetEditText(e.target.value)}
/>
</Html>
);
}
);
11 changes: 11 additions & 0 deletions src/common/components/inline-edit/inline-edit.model.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export type EditType = 'input' | 'textarea';

type PositionType = 'absolute' | 'relative' | 'fixed' | 'static';

export interface StyleDivProps {
position: PositionType;
top: string | number;
left: string | number;
width: string | number;
height: string | number;
}
41 changes: 22 additions & 19 deletions src/common/components/inline-edit/inline-edit.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Coord, Size } from '@/core/model';
import React, { useEffect, useRef, useState } from 'react';
import { Group } from 'react-konva';
import { Html } from 'react-konva-utils';
import { HtmlEditWidget } from './html-edit.widget';

type EditType = 'input' | 'textarea';

Expand All @@ -17,8 +17,16 @@ interface Props {
}

export const EditableComponent: React.FC<Props> = props => {
const { coords, size, isEditable, text, onTextSubmit, scale, children } =
props;
const {
coords,
size,
isEditable,
text,
onTextSubmit,
scale,
children,
editType,
} = props;
const [isEditing, setIsEditing] = useState(false);
const [editText, setEditText] = useState(text);

Expand Down Expand Up @@ -94,24 +102,19 @@ export const EditableComponent: React.FC<Props> = props => {
<>
<Group onDblClick={handleDoubleClick}>{children}</Group>
{isEditing ? (
<Html
<HtmlEditWidget
divProps={{
style: {
position: 'absolute',
top: calculateTextAreaYPosition(),
left: calculateTextAreaXPosition(),
width: calculateWidth(),
height: calculateHeight(),
},
position: 'absolute',
top: calculateTextAreaYPosition(),
left: calculateTextAreaXPosition(),
width: calculateWidth(),
height: calculateHeight(),
}}
>
<input
ref={inputRef}
style={{ width: '100%', height: '100%' }}
value={editText}
onChange={e => setEditText(e.target.value)}
/>
</Html>
ref={inputRef}
value={editText}
onSetEditText={setEditText}
editType={editType}
/>
) : null}
</>
);
Expand Down
156 changes: 156 additions & 0 deletions src/common/utils/shapes/shape-restrictions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { fitSizeToShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions';
import { ShapeSizeRestrictions } from '@/common/utils/shapes/shape-restrictions';

//Mock data
const restrictions: ShapeSizeRestrictions = {
minWidth: 50,
minHeight: 30,
maxWidth: 500,
maxHeight: 300,
defaultWidth: 100,
defaultHeight: 100,
};

describe('./pods/canvas/canvas.util', () => {
it('should set width to minimum width if width is 0', () => {
// Arrange
const width = 0;
const height = 50;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 50, height: 50 });
});
it('should set width to minimum width if width is 1', () => {
// Arrange
const width = 1;
const height = 50;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 50, height: 50 });
});
it('should return width within bounds if width is within the minimum and maximum limits', () => {
// Arrange
const width = 300;
const height = 150;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 300, height: 150 });
});
it('should return max width if width is above max bound', () => {
// Arrange
const width = 950;
const height = 150;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 500, height: 150 });
});
it('should return minWidth if width is under min bound', () => {
// Arrange
const width = 30;
const height = 150;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 50, height: 150 });
});
it('should return maxHeight if height is above max bound', () => {
// Arrange
const width = 80;
const height = 500;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 80, height: 300 });
});
it('should return minHeight if height is under min bound', () => {
// Arrange
const width = 80;
const height = 15;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 80, height: 30 });
});
it('should return maxWidth and maxHeight if both are above max bound', () => {
// Arrange
const width = 800;
const height = 500;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 500, height: 300 });
});
it('should return minWidth and minHeight if both are under min bound', () => {
// Arrange
const width = 30;
const height = 10;

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions, width, height);

//Assert
expect(result).toEqual({ width: 50, height: 30 });
});
it('should return width and height as is when all bounds are -1', () => {
// Arrange
const width = 400;
const height = 250;
const restrictions2: ShapeSizeRestrictions = {
minWidth: -1,
minHeight: -1,
maxWidth: -1,
maxHeight: -1,
defaultWidth: 100,
defaultHeight: 100,
};

// Act
const result = fitSizeToShapeSizeRestrictions(restrictions2, width, height);

//Assert
expect(result).toEqual({ width: 400, height: 250 });
});
it('should return width and height as is when maxWidth and maxHeight are -1', () => {
// Arrange
const width = 400;
const height = 250;
const defaultShapeSizeRestrictions: ShapeSizeRestrictions = {
minWidth: 0,
minHeight: 0,
maxWidth: -1,
maxHeight: -1,
defaultWidth: 100,
defaultHeight: 100,
};

// Act
const result = fitSizeToShapeSizeRestrictions(
defaultShapeSizeRestrictions,
width,
height
);

//Assert
expect(result).toEqual({ width: 400, height: 250 });
});
});
2 changes: 2 additions & 0 deletions src/common/utils/shapes/shape-restrictions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ export const fitSizeToShapeSizeRestrictions = (
height: Math.max(newHeight, shapeSizeRestrictions.minHeight),
};
};

export type { ShapeSizeRestrictions };
Loading