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

feat: Factory types #1812

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

psychedelicious
Copy link

Add types to the Factory helpers & validators.

Notes:

  • I intended for this to only include changes to types, but it uncovered some very minor code issues. See PR comments.

@@ -2660,7 +2660,7 @@ export abstract class Node<Config extends NodeConfig = NodeConfig> {
rotation: GetSet<number, this>;
zIndex: GetSet<number, this>;

scale: GetSet<Vector2d | undefined, this>;
scale: GetSet<Vector2d, this>;
Copy link
Author

Choose a reason for hiding this comment

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

Either this GetSet type is incorrect for the NodeConfig.scale type is incorrect. I assume it's this one.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like it is not correct, I don't think scale can return undefined.

@@ -3102,7 +3102,7 @@ addGetterSetter(Node, 'offsetY', 0, getNumberValidator());
* node.offsetY(3);
*/

addGetterSetter(Node, 'dragDistance', null, getNumberValidator());
addGetterSetter(Node, 'dragDistance', undefined, getNumberValidator());
Copy link
Author

@psychedelicious psychedelicious Aug 25, 2024

Choose a reason for hiding this comment

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

This is a subtle API change.

Perhaps we should widen the type of dragDistance in NodeConfig to dragDistance?: number | null;?

There are a number of other places where the initial value is set to null apparently erroneously.

Copy link
Member

Choose a reason for hiding this comment

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

The drag distance must be always number. So undefined should fallback to default value. I am not really sure what it was null here.

Comment on lines +846 to +850
strokeLinearGradientStartPointX: GetSet<number, this>;
strokeLinearGradientStartPointY: GetSet<number, this>;
strokeLinearGradientEndPointX: GetSet<number, this>;
strokeLinearGradientEndPointY: GetSet<number, this>;
fillRule: GetSet<CanvasFillRule, this>;
Copy link
Author

Choose a reason for hiding this comment

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

These annotations were missing. I don't think this changes anything because the Factory adds the GetSets anyways.

Copy link
Author

Choose a reason for hiding this comment

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

I think I have understood the intention with these validators - they should create warnings but otherwise let the value through, even if it is invalid.

Comment on lines +320 to +323
pointerDirection: GetSet<
'left' | 'top' | 'right' | 'bottom' | typeof NONE,
this
>;
Copy link
Author

Choose a reason for hiding this comment

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

The factory sets the initial value to NONE.

Comment on lines -1748 to -1749
Factory.addGetterSetter(Transformer, 'node');

Copy link
Author

Choose a reason for hiding this comment

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

I think this was added accidentally - Transformer never references this.node as far as I can see...

Copy link
Member

Choose a reason for hiding this comment

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

This is an old (deprecated) API. Should be safe to remove.

addGetterSetter(constructor, attr, def?, validator?, after?) {
addGetterSetter<T extends Constructor, U extends Attr<T>>(
constructor: T,
attr: U,
Copy link
Author

Choose a reason for hiding this comment

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

We can make this a bit safer with a type to constrain attr to be a GetSet.

type EnforceGetSet<
  T extends Constructor,
  U extends Attr<T>
> = InstanceType<T>[U] extends GetSet<any, any> ? U : never;

Then, type attr as attr: EnforceGetSet<T, U>.

However, this causes a problem in Canvas.ts at https://github.com/psychedelicious/konva/blob/9c41a6eed1bba46e80ce282903ac86bb11493e84/src/Canvas.ts#L171

@psychedelicious
Copy link
Author

For this PR, I was just kinda having fun exploring typescript to see if I could get autocomplete working for the Factory methods. Because it is an internal API, it's not a big deal if it has perfect type safety or not - I'm not dead-set on this PR being accepted.

@@ -79,7 +149,7 @@ export const Factory = {
key;

if (validator) {
val = validator.call(this, val);
val = validator.call(this, val, attr);
Copy link
Member

Choose a reason for hiding this comment

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

What does this change do?

@@ -3102,7 +3102,7 @@ addGetterSetter(Node, 'offsetY', 0, getNumberValidator());
* node.offsetY(3);
*/

addGetterSetter(Node, 'dragDistance', null, getNumberValidator());
addGetterSetter(Node, 'dragDistance', undefined, getNumberValidator());
Copy link
Member

Choose a reason for hiding this comment

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

The drag distance must be always number. So undefined should fallback to default value. I am not really sure what it was null here.

Comment on lines -1748 to -1749
Factory.addGetterSetter(Transformer, 'node');

Copy link
Member

Choose a reason for hiding this comment

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

This is an old (deprecated) API. Should be safe to remove.

@@ -551,7 +551,7 @@ Factory.addGetterSetter(TextPath, 'text', EMPTY_STRING);
* // underline text
* shape.textDecoration('underline');
*/
Factory.addGetterSetter(TextPath, 'textDecoration', null);
Factory.addGetterSetter(TextPath, 'textDecoration', undefined);
Copy link
Member

Choose a reason for hiding this comment

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

This probably should be an empty string as in Text class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants