Skip to content

Commit

Permalink
chore(TS): Move cache properties as static (#8662)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Feb 7, 2023
1 parent a39e01c commit ab6b4b4
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [next]

- TS(): Moved cache properties to static properties on classes [#xxxx](https://github.com/fabricjs/fabric.js/pull/xxxx)
- refactor(): Moved cache properties to static properties on classes [#8662](https://github.com/fabricjs/fabric.js/pull/8662)
- docs(): v6 announcements [#8664](https://github.com/fabricjs/fabric.js/issues/8664)
- ci(): remove TS transformer [#8660](https://github.com/fabricjs/fabric.js/pull/8660)
- refactor(): BREAKING remove stateful mixin and functionality [#8663](https://github.com/fabricjs/fabric.js/pull/8663)
Expand Down
8 changes: 7 additions & 1 deletion src/shapes/Circle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export class Circle extends FabricObject {
*/
declare endAngle: number;

static cacheProperties = [
...cacheProperties,
'radius',
'startAngle',
'endAngle',
];

/**
* @private
* @param {String} key
Expand Down Expand Up @@ -198,7 +205,6 @@ export const circleDefaultValues: Partial<TClassProperties<Circle>> = {

Object.assign(Circle.prototype, {
...circleDefaultValues,
cacheProperties: [...cacheProperties, 'radius', 'startAngle', 'endAngle'],
});

classRegistry.setClass(Circle);
Expand Down
3 changes: 2 additions & 1 deletion src/shapes/Ellipse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class Ellipse extends FabricObject {
*/
declare ry: number;

static cacheProperties = [...cacheProperties, 'rx', 'ry'];

/**
* Constructor
* @param {Object} [options] Options object
Expand Down Expand Up @@ -147,7 +149,6 @@ export const ellipseDefaultValues: Partial<TClassProperties<Ellipse>> = {

Object.assign(Ellipse.prototype, {
...ellipseDefaultValues,
cacheProperties: [...cacheProperties, 'rx', 'ry'],
});

classRegistry.setClass(Ellipse);
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class Image extends FabricObject {
protected declare _originalElement: ImageSource;
protected declare _filteredEl: ImageSource;

static cacheProperties = [...cacheProperties, 'cropX', 'cropY'];
/**
* Constructor
* Image can be initialized with any canvas drawable or a string.
Expand Down Expand Up @@ -780,7 +781,6 @@ export const imageDefaultValues: Partial<TClassProperties<Image>> = {

Object.assign(Image.prototype, {
...imageDefaultValues,
cacheProperties: [...cacheProperties, 'cropX', 'cropY'],
});

classRegistry.setClass(Image);
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/Line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Line extends FabricObject {
*/
declare y2: number;

static cacheProperties = [...cacheProperties, 'x1', 'x2', 'y1', 'y2'];
/**
* Constructor
* @param {Array} [points] Array of points
Expand Down Expand Up @@ -322,7 +323,6 @@ export const lineDefaultValues: Partial<TClassProperties<Line>> = {

Object.assign(Line.prototype, {
...lineDefaultValues,
cacheProperties: [...cacheProperties, 'x1', 'x2', 'y1', 'y2'],
});

classRegistry.setClass(Line);
Expand Down
6 changes: 3 additions & 3 deletions src/shapes/Object/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ export class FabricObject<
* and refreshed at the next render
* @type Array
*/
declare cacheProperties: string[];
static cacheProperties: string[] = cacheProperties;

/**
* a fabricObject that, without stroke define a clipping area with their shape. filled in black
Expand Down Expand Up @@ -1023,7 +1023,8 @@ export class FabricObject<

if (isChanged) {
const groupNeedsUpdate = this.group && this.group.isOnACache();
if (this.cacheProperties.includes(key)) {
// @ts-ignore TS and constructor issue
if (this.constructor.cacheProperties.includes(key)) {
this.dirty = true;
groupNeedsUpdate && this.group!.set('dirty', true);
} else if (groupNeedsUpdate && this.stateProperties.includes(key)) {
Expand Down Expand Up @@ -1889,7 +1890,6 @@ export class FabricObject<
* For inheritance reasons ( used in the superclass but static in the subclass )
*/
Object.assign(FabricObject.prototype, {
cacheProperties,
stateProperties,
...fabricObjectDefaultValues,
});
Expand Down
3 changes: 2 additions & 1 deletion src/shapes/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export class Path extends FabricObject {

declare segmentsInfo?: TPathSegmentsInfo[];

static cacheProperties = [...cacheProperties, 'path', 'fillRule'];

/**
* Constructor
* @param {Array|String} path Path data (sequence of coordinates and corresponding "command" tokens)
Expand Down Expand Up @@ -386,7 +388,6 @@ export const pathDefaultValues: Partial<TClassProperties<Path>> = {

Object.assign(Path.prototype, {
...pathDefaultValues,
cacheProperties: [...cacheProperties, 'path', 'fillRule'],
});

classRegistry.setClass(Path);
Expand Down
3 changes: 2 additions & 1 deletion src/shapes/Polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class Polyline extends FabricObject {

declare strokeOffset: Point;

static cacheProperties = [...cacheProperties, 'points'];

/**
* Constructor
* @param {Array} points Array of points (where each point is an object with x and y)
Expand Down Expand Up @@ -316,7 +318,6 @@ export const polylineDefaultValues: Partial<TClassProperties<Polyline>> = {

Object.assign(Polyline.prototype, {
...polylineDefaultValues,
cacheProperties: [...cacheProperties, 'points'],
strokeBBoxAffectingProperties: [
'skewX',
'skewY',
Expand Down
3 changes: 2 additions & 1 deletion src/shapes/Rect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export class Rect extends FabricObject {
*/
declare ry: number;

static cacheProperties = [...cacheProperties, 'rx', 'ry'];

/**
* Constructor
* @param {Object} [options] Options object
Expand Down Expand Up @@ -192,7 +194,6 @@ export const rectDefaultValues: Partial<TClassProperties<Rect>> = {

Object.assign(Rect.prototype, {
...rectDefaultValues,
cacheProperties: [...cacheProperties, 'rx', 'ry'],
});

classRegistry.setClass(Rect);
Expand Down
3 changes: 2 additions & 1 deletion src/shapes/Text/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@ export class Text<

declare initialized?: true;

static cacheProperties = [...cacheProperties, ...additionalProps];

constructor(text: string, options: any) {
super({ ...options, text, styles: options?.styles || {} });
this.initialized = true;
Expand Down Expand Up @@ -1917,7 +1919,6 @@ export const textDefaultValues: Partial<TClassProperties<Text>> = {
baseline: 0.11, // baseline-shift factor (downwards)
},
textBackgroundColor: '',
cacheProperties: [...cacheProperties, ...additionalProps],
stroke: null,
shadow: null,
path: null,
Expand Down
8 changes: 6 additions & 2 deletions test/unit/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -962,13 +962,15 @@

QUnit.test('dirty flag on set property', function(assert) {
var object = new fabric.Object({ scaleX: 3, scaleY: 2});
object.cacheProperties = ['propA', 'propB'];
const originalCacheProps = fabric.Object.cacheProperties;
fabric.Object.cacheProperties = ['propA', 'propB'];
object.dirty = false;
assert.equal(object.dirty, false, 'object starts with dirty flag disabled');
object.set('propC', '3');
assert.equal(object.dirty, false, 'after setting a property out of cache, dirty flag is still false');
object.set('propA', '2');
assert.equal(object.dirty, true, 'after setting a property from cache, dirty flag is true');
fabric.Object.cacheProperties = originalCacheProps;
});

QUnit.test('_createCacheCanvas sets object as dirty', function(assert) {
Expand All @@ -983,11 +985,13 @@
QUnit.test('isCacheDirty', function(assert) {
var object = new fabric.Object({ scaleX: 3, scaleY: 2, width: 1, height: 2});
assert.equal(object.dirty, true, 'object is dirty after creation');
object.cacheProperties = ['propA', 'propB'];
const originalCacheProps = fabric.Object.cacheProperties;
fabric.Object.cacheProperties = ['propA', 'propB'];
object.dirty = false;
assert.equal(object.isCacheDirty(), false, 'object is not dirty if dirty flag is false');
object.dirty = true;
assert.equal(object.isCacheDirty(), true, 'object is dirty if dirty flag is true');
fabric.Object.cacheProperties = originalCacheProps;
});

QUnit.test('_getCacheCanvasDimensions returns dimensions and zoom for cache canvas', function(assert) {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
QUnit.test('cache properties', function(assert) {
var rect = new fabric.Rect();

assert.ok(rect.cacheProperties.indexOf('rx') > -1, 'rx is in cacheProperties array');
assert.ok(rect.cacheProperties.indexOf('ry') > -1, 'ry is in cacheProperties array');
assert.ok(fabric.Rect.cacheProperties.indexOf('rx') > -1, 'rx is in cacheProperties array');
assert.ok(fabric.Rect.cacheProperties.indexOf('ry') > -1, 'ry is in cacheProperties array');
});

QUnit.test('toObject', function(assert) {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@

QUnit.test('cacheProperties for text', function(assert) {
var text = new fabric.Text('a');
assert.equal(text.cacheProperties.join('-'), 'fill-stroke-strokeWidth-strokeDashArray-width-height-paintFirst-strokeUniform-strokeLineCap-strokeDashOffset-strokeLineJoin-strokeMiterLimit-backgroundColor-clipPath-fontFamily-fontWeight-fontSize-text-underline-overline-linethrough-textAlign-fontStyle-lineHeight-textBackgroundColor-charSpacing-styles-direction-path-pathStartOffset-pathSide-pathAlign');
assert.equal(fabric.Text.cacheProperties.join('-'), 'fill-stroke-strokeWidth-strokeDashArray-width-height-paintFirst-strokeUniform-strokeLineCap-strokeDashOffset-strokeLineJoin-strokeMiterLimit-backgroundColor-clipPath-fontFamily-fontWeight-fontSize-text-underline-overline-linethrough-textAlign-fontStyle-lineHeight-textBackgroundColor-charSpacing-styles-direction-path-pathStartOffset-pathSide-pathAlign');
});

QUnit.test('_getLineLeftOffset', function(assert) {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/textbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
assert.equal(textbox.text, 'test');
assert.equal(textbox.type, 'textbox');
assert.deepEqual(textbox.styles, { });
assert.ok(textbox.cacheProperties.indexOf('width') > -1, 'width is in cacheProperties');
assert.ok(fabric.Textbox.cacheProperties.indexOf('width') > -1, 'width is in cacheProperties');
});

QUnit.test('toObject', function(assert) {
Expand Down Expand Up @@ -530,7 +530,7 @@
var styles = {};
for (var index = 0; index < text.length; index++) {
styles[index] = { fontSize: 4 };

}
var textbox = new fabric.Textbox(text, {
styles: { 0: styles },
Expand Down

0 comments on commit ab6b4b4

Please sign in to comment.