-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
chore(TS) Add more types to Text, BREAKING: change return type of _getStyleDeclaration #9018
Conversation
Build Stats
|
still some work to do here, not ready for review |
@@ -668,9 +676,9 @@ export class IText< | |||
* Unused by the library, is for the end user | |||
* @return {String | TFiller} Character color (fill) | |||
*/ | |||
getCurrentCharColor(): string | TFiller { | |||
getCurrentCharColor(): string | TFiller | null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fill and stroke being null is the cause of many issue with types and a lot of extra null checks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we removing null why this this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because i m not removing null in this PR.
But this PR brought up the issue again
const { lineIndex, charIndex } = this.get2DCursorLocation(index); | ||
|
||
if (!this._getLineStyle(lineIndex)) { | ||
this._setLineStyle(lineIndex); | ||
} | ||
|
||
if (!this._getStyleDeclaration(lineIndex, charIndex)) { | ||
if (!Object.keys(this._getStyleDeclaration(lineIndex, charIndex)).length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the only code change i made.
there were 5 occurrencies in which returning undefined from _getStyleDeclaration
needed to be fixed with an || {}
while just one where returning undefined ( or null ) was useful, to know if there was no style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is fine but BREAKING if anyone used it before
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i would make this breaking, and i someone asks, i would add the hasStyleDeclaration method that return true/false, or maybe i can add it right away
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should
Then there is a clear migration path (Object.keys is pretty clear also but...)
src/shapes/Text/StyledText.ts
Outdated
const lineStyle = this.styles && this.styles[lineIndex]; | ||
return lineStyle ? lineStyle[charIndex] : null; | ||
return lineStyle ? lineStyle[charIndex] : {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the code change that caused the object.keys above.
@@ -63,20 +69,16 @@ type TPathAlign = 'baseline' | 'center' | 'ascender' | 'descender'; | |||
* needs the the info of previous graphemes already filled | |||
* Override to customize measuring | |||
*/ | |||
export type GraphemeBBox<onPath = false> = { | |||
export type GraphemeBBox = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was too hard to be flexible ( or simply i can't use it )
I m thinking that maybe we just keep al the properties required and we just add the extra 3 everywhere.
src/shapes/Text/Text.ts
Outdated
@@ -1528,22 +1566,23 @@ export class Text< | |||
const lineLeftOffset = this._getLineLeftOffset(i); | |||
let boxStart = 0; | |||
let boxWidth = 0; | |||
let lastDecoration = this.getValueOfPropertyAt(i, 0, type); | |||
let lastFill = this.getValueOfPropertyAt(i, 0, 'fill'); | |||
let lastDecoration = this.getValueOfPropertyAt<typeof type>(i, 0, type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typeof type is fantastic
textContent = element.textContent; | ||
} | ||
|
||
textContent = textContent |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ie9 support is gone since long.
@@ -87,7 +87,7 @@ export const textDefaultValues: Partial<TClassProperties<Text>> = { | |||
textBackgroundColor: '', | |||
stroke: null, | |||
shadow: null, | |||
path: null, | |||
path: undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure why null was chose in first place.
@@ -244,7 +252,7 @@ export class Textbox extends IText { | |||
* @param {Object} style | |||
* @private | |||
*/ | |||
_setLineStyle(lineIndex: number) { | |||
protected _setLineStyle(lineIndex: number) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
those are protected in the other classes
This is ready to be discussed @ShaMan123 @melchiar also @jiayihu since you are around TS. |
@@ -59,7 +59,7 @@ | |||
skewY: 0, | |||
charSpacing: 0, | |||
styles: [], | |||
path: null, | |||
path: undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder if i need to make a backward compatibilitu statement or change somewhere.
The code never check if path is undefined, all it does is checking if path is truthy or not.
So null or undeined is the same also when reloading old data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I want to understand the problem with
GraphemeBBox<true | false>
- generic
getValueOfPropertyAt
Apart from that looks good
@@ -668,9 +676,9 @@ export class IText< | |||
* Unused by the library, is for the end user | |||
* @return {String | TFiller} Character color (fill) | |||
*/ | |||
getCurrentCharColor(): string | TFiller { | |||
getCurrentCharColor(): string | TFiller | null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we removing null why this this needed?
const { lineIndex, charIndex } = this.get2DCursorLocation(index); | ||
|
||
if (!this._getLineStyle(lineIndex)) { | ||
this._setLineStyle(lineIndex); | ||
} | ||
|
||
if (!this._getStyleDeclaration(lineIndex, charIndex)) { | ||
if (!Object.keys(this._getStyleDeclaration(lineIndex, charIndex)).length) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is fine but BREAKING if anyone used it before
const lineStyle = this.styles && this.styles[lineIndex]; | ||
return lineStyle ? lineStyle[charIndex] : null; | ||
return lineStyle ? lineStyle[charIndex] ?? {} : {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we ok with abbreviations now or not yet?
return lineStyle ? lineStyle[charIndex] ?? {} : {}; | |
return lineStyle?.[charIndex] ?? {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is so much longer the abbreviation because safari.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i m also not sure about performance in general of that abbrevieation
if (this._styleMap && !this.isWrapping) { | ||
const map = this._styleMap[lineIndex]; | ||
if (!map) { | ||
return null; | ||
return {}; | ||
} | ||
lineIndex = map.line; | ||
charIndex = map.offset + charIndex; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
though this is not related I would like to say that this piece of logic has a lot more usages than just in style.
It maps between a wrapped position to a non wrapped position.
It is confusing when to use the wrapped/non wrapped position when working on textbox, error prone and not friendly.
I would have a method that returns the offset give a line, char, isWrapped since it is not changed by wrapping (I hope there is no edge case for that) and use that across all methods as a standard.
This will make working on Text and Textbox easier or else we must keep in mind this constant difference that is applied at a certain point in the lifecycle. e.g. measuring uses to non wrapped, rendering uses the wrapped.
All should us the offset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as i said there are so many things that have been bolted on top of normal text, and this stylemap was one.
This works, is confusing, but it works,
Removed some extra low hanging fruits and broke test. |
GraphemeBBox starts as but then later is true, and there is no easy way to inform the code that it has been updated because is done with mutation through different methods |
Needs to decide what to do with the breaking change. |
I would make that not mutate but return a new object regardless of types |
i don't want to accomodate the logic to types, and we still don't have a benchmark strategy either. |
Good point |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
left overs
left: '0', | ||
top: '0', | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really do not like this
You should use CSS type and this will be resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I see that in CCSStyleDeclration that is string anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setAttribute wants strings.
is call setStyles but we are not setting the CSS we are setting the attributes
Co-authored-by: Shachar <34343793+ShaMan123@users.noreply.github.com>
i want to merge this and move forward with more types. |
Go ahead! |
Motivation
I start to not feel confindent with large classes having ts-nocheck still in place.
Especially text.
Those types i did are not perfect, but better than nothing.
Before merging, discussing if we should get rid of stroke and fill being null since it doesn't makes much sense.
We should understand what null does today ( skill fill? ) and use maybe
transparent
in place of it and skip fill whentransparent
is used.It simplify types a lot and makes every fill not skipped safe anyway.