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

Convert Text to TypeScript #6390

Merged
merged 11 commits into from
Feb 12, 2020
Merged

Convert Text to TypeScript #6390

merged 11 commits into from
Feb 12, 2020

Conversation

qtiki
Copy link
Contributor

@qtiki qtiki commented Feb 6, 2020

Description of change

Convert Text to TypeScript

Pre-Merge Checklist
  • Tests and/or benchmarks are included
  • Documentation is changed or added
  • Lint process passed (npm run lint)
  • Tests passed (npm run test)

@codecov-io
Copy link

codecov-io commented Feb 9, 2020

Codecov Report

Merging #6390 into dev will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##              dev    #6390   +/-   ##
=======================================
  Coverage   74.83%   74.83%           
=======================================
  Files          66       66           
  Lines        3346     3346           
=======================================
  Hits         2504     2504           
  Misses        842      842

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f2818b3...034ef61. Read the comment docs.

@qtiki qtiki marked this pull request as ready for review February 9, 2020 20:25
/**
* @param {string} text - The string that you would like the text to display
* @param {object|PIXI.TextStyle} [style] - The style parameters
* @param {HTMLCanvasElement} [canvas] - The canvas element for drawing text
*/
constructor(text, style, canvas)
constructor(text: string, style: TextStyle, canvas: HTMLCanvasElement)
Copy link
Contributor

@eXponenta eXponenta Feb 9, 2020

Choose a reason for hiding this comment

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

Style type should be ITextStyle instead TextStyle, because we can pass RAW object to constructor together with TextStyle instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it should say Partial<ITextStyle>|TextStyle like elsewhere. I missed that.

@@ -4,7 +4,39 @@
import { TEXT_GRADIENT } from './const';
import { hex2string } from '@pixi/utils';

const defaultStyle = {
export interface ITextStyle {
Copy link
Contributor

Choose a reason for hiding this comment

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

ALL fields is optional and has default values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I made the fields mandatory to force all of the values to be provided in the default values. I've been using Partial<ITextStyle> in places where they're optional. If using Partial is not something you would like to have there I can definitely change that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also having the fields optional wouldn't enforce TextStyle implements ITextStyle properly now that I added it.

packages/text/src/TextStyle.ts Outdated Show resolved Hide resolved
@@ -773,7 +847,7 @@ function getColor(color)
* @param {Array} array2 Second array to compare
* @return {boolean} Do the arrays contain the same values in the same order
*/
function areArraysEqual(array1, array2)
function areArraysEqual(array1: any[], array2: any[]): boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should be generic function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing this to function areArraysEqual<T1, T2>(array1: T1[], array2: T2[]): boolean results in a problem with the !== comparison: This condition will always return 'true' since the types 'T1' and 'T2' have no overlap.

I can change it to function areArraysEqual<T>(array1: T[], array2: T[]): boolean but then it cannot be called with different types of arrays. Then again maybe you don't need to because if they're different types then they definitely are not equal. I'll go with this one.

Copy link
Contributor

@eXponenta eXponenta Feb 10, 2020

Choose a reason for hiding this comment

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

Why need to compare arrays with different type?
It used only in fillGradientStops setter, gradient stops is a number type, isn't it?
https://github.com/quicksave-interactive/pixi.js/blob/dev/packages/text/src/TextStyle.ts#L415

It should be:
function areArraysEqual(array1: number[], array2: number[]): boolean

packages/text/src/TextStyle.ts Outdated Show resolved Hide resolved
@@ -539,7 +572,7 @@ export class TextMetrics
* @param {boolean} breakWords The style attr break words
* @return {boolean} whether to break word or not
*/
static canBreakWords(token, breakWords)
static canBreakWords(token: string, breakWords: TextStyle['breakWords']): boolean
Copy link
Contributor

Choose a reason for hiding this comment

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

I think types should be explicit instead of lookups.

@@ -57,6 +89,38 @@ const genericFontFamilies = [
*/
export class TextStyle
{
public styleID: number;

protected _align: ITextStyle['align'];
Copy link
Contributor

@eXponenta eXponenta Feb 9, 2020

Choose a reason for hiding this comment

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

I think is a bad practice using lookup types in open source project.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can get rid of those. I agree that it's not really nice for classes to reference each other, but given that TextStyle implements ITextStyle I figured it would make sense to specify the types in only one place. But I'll change those to not use lookup, I'll just need to declare a bunch of extra types for the interface so I don't have to type for example 'normal'|'bold'|'bolder'|'lighter'|'100'|'200'|'300'|'400'|'500'|'600'|'700'|'800'|'900' more than once.

Copy link
Member

Choose a reason for hiding this comment

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

Create separate types for things like this maybe? TextStyleAlign, TextStyleWeight, etc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep that's what I did. I didn't have the TextStyle prefix but I just added that to all of them - I think that makes it a bit clearer. I'll push in a bit.

@@ -383,7 +401,7 @@ export class Text extends Sprite
* @param {PIXI.Rectangle} rect - The output rectangle.
* @return {PIXI.Rectangle} The bounds.
*/
getLocalBounds(rect)
getLocalBounds(rect: Rectangle): Rectangle
Copy link
Contributor

Choose a reason for hiding this comment

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

'public' modifier is required!

empty modifier only for 'internal' (not public API, but used outside of type, such as inside Renderer )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah okay. I missed that there is a semantic difference for this, I'll check the visibility for all the methods.

@eXponenta
Copy link
Contributor

eXponenta commented Feb 9, 2020

Cool start!
But:

  • empty modifier used as 'public for internal usage', other fields/methods/props require modifiers.
  • I think we shouldn't use lookup types (aka ITextStyle ['font'])
  • TextStyle should implement ITextStyle, or convince me why this isn't necessary.

packages/text/src/Text.ts Outdated Show resolved Hide resolved
@bigtimebuddy bigtimebuddy added this to the v5.3.0 milestone Feb 10, 2020
@bigtimebuddy
Copy link
Member

@Zyie you good on this?

@Zyie
Copy link
Member

Zyie commented Feb 11, 2020

@Zyie you good on this?

seems good to me

@bigtimebuddy bigtimebuddy merged commit 55de8b7 into pixijs:dev Feb 12, 2020
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.

None yet

5 participants