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

BREAKING: remove object type in favor of Class.name #8714

Merged
merged 31 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0e661de
refactor(): static `type`
ShaMan123 Feb 20, 2023
c8e380a
class reg
ShaMan123 Feb 20, 2023
5d49cc6
Update Object.ts
ShaMan123 Feb 20, 2023
b822482
Update Gradient.ts
ShaMan123 Feb 20, 2023
9e42a9d
filters
ShaMan123 Feb 20, 2023
9f1c175
Update Pattern.ts
ShaMan123 Feb 20, 2023
2020422
update tests
ShaMan123 Feb 20, 2023
b1a0f21
more tests
ShaMan123 Feb 20, 2023
1ef2937
no type
ShaMan123 Feb 20, 2023
44dd260
safeguard
ShaMan123 Feb 20, 2023
1e872f6
tests
ShaMan123 Feb 20, 2023
ea6a18c
Update CHANGELOG.md
ShaMan123 Feb 20, 2023
0ec8a74
Merge branch 'master' into static-type
ShaMan123 Feb 27, 2023
bc6fb0d
expel type
ShaMan123 Feb 28, 2023
206fd5d
Update ClassRegistry.ts
ShaMan123 Feb 28, 2023
4b18f9c
Update ColorMatrixFilters.ts
ShaMan123 Feb 28, 2023
5b9c3ab
registry + capitle
ShaMan123 Feb 28, 2023
b20aa69
tests
ShaMan123 Feb 28, 2023
21ec988
drop filter getType
ShaMan123 Feb 28, 2023
08e9431
legacy resolution
ShaMan123 Feb 28, 2023
7364291
checkout filters in favor of #8742
ShaMan123 Feb 28, 2023
905d18d
Update CHANGELOG.md
ShaMan123 Feb 28, 2023
b416b55
remove `getObjects(...types)` => `getObjects()`
ShaMan123 Feb 28, 2023
3443f6d
Merge branch 'master' into static-type
asturur Feb 28, 2023
e1a6734
possible-commit
asturur Feb 28, 2023
2d9e35a
Merge branch 'master' into static-type
asturur Mar 4, 2023
6d7f518
fix merge conflicts
asturur Mar 4, 2023
41c9f11
fixed tests
asturur Mar 4, 2023
a5ab357
done
asturur Mar 4, 2023
8536f67
apply code reviw
asturur Mar 4, 2023
d0b7f67
removed console.log
asturur Mar 4, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- BREAKING: static object/filter `type` [#87104](https://github.com/fabricjs/fabric.js/pull/8714)
- chore(TS): remove default values from Objects prototypes, ( filters in a followup ) [#8719](https://github.com/fabricjs/fabric.js/issues/8719)
- fix(Intersection): bug causing selection edge case [#8735](https://github.com/fabricjs/fabric.js/pull/8735)
- chore(TS): class interface for options/brevity [#8674](https://github.com/fabricjs/fabric.js/issues/8674)
Expand Down
10 changes: 2 additions & 8 deletions src/ClassRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,15 @@ export class ClassRegistry {
}

setClass(classConstructor: any, classType?: string) {
this[JSON].set(
classType ?? classConstructor.prototype.type,
classConstructor
);
this[JSON].set(classType ?? classConstructor.type, classConstructor);
}

getSVGClass(SVGTagName: string): any {
return this[SVG].get(SVGTagName);
}

setSVGClass(classConstructor: any, SVGTagName?: string) {
this[SVG].set(
SVGTagName ?? classConstructor.prototype.type,
classConstructor
);
this[SVG].set(SVGTagName ?? classConstructor.type, classConstructor);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export function createCollectionMixin<TBase extends Constructor>(Base: TBase) {
if (types.length === 0) {
return [...this._objects];
}
return this._objects.filter((o) => types.includes(o.type));
return this._objects.filter((o) => o.isType(...types));
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ type TCanvasSource = { source: HTMLCanvasElement };
* @see {@link http://fabricjs.com/dynamic-patterns demo}
*/
export class Pattern {
type = 'pattern';

/**
* @type TPatternRepeat
* @defaults
Expand Down
28 changes: 16 additions & 12 deletions src/filters/BaseFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ export interface AnyFilter
export abstract class AbstractBaseFilter<T> {
/**
* Filter type
* @param {String} type
* @default
*/
declare type: string;
static readonly type = 'filter' as string;

/**
* Array of attributes to send with buffers. do not modify
Expand All @@ -52,9 +50,10 @@ export abstract class AbstractBaseFilter<T> {
* Constructor
* @param {Object} [options] Options object
*/
constructor(
options: Partial<AbstractBaseFilterOptions<T>> & Record<string, any> = {}
) {
constructor({
type: _,
...options
}: Partial<AbstractBaseFilterOptions<T>> & Record<string, any> = {}) {
Object.assign(this, options);
}

Expand Down Expand Up @@ -90,7 +89,7 @@ export abstract class AbstractBaseFilter<T> {
gl.compileShader(vertexShader);
if (!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
throw new Error(
`Vertex shader compile error for ${this.type}: ${gl.getShaderInfoLog(
`Vertex shader compile error for ${this.getType()}: ${gl.getShaderInfoLog(
vertexShader
)}`
);
Expand All @@ -100,7 +99,7 @@ export abstract class AbstractBaseFilter<T> {
gl.compileShader(fragmentShader);
if (!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
throw new Error(
`Fragment shader compile error for ${this.type}: ${gl.getShaderInfoLog(
`Fragment shader compile error for ${this.getType()}: ${gl.getShaderInfoLog(
fragmentShader
)}`
);
Expand All @@ -111,8 +110,9 @@ export abstract class AbstractBaseFilter<T> {
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
throw new Error(
// eslint-disable-next-line prefer-template
'Shader link error for "${this.type}" ' + gl.getProgramInfoLog(program)
`Shader link error for ${this.getType()} ${gl.getProgramInfoLog(
program
)}`
);
}

Expand Down Expand Up @@ -261,8 +261,12 @@ export abstract class AbstractBaseFilter<T> {

abstract applyTo2d(options: T2DPipelineState): void;

protected getType() {
return (this.constructor as typeof BaseFilter).type;
}

getCacheKey() {
return this.type;
return this.getType();
}

/**
Expand Down Expand Up @@ -371,7 +375,7 @@ export abstract class AbstractBaseFilter<T> {
toObject() {
const mainP = this.mainParameter;
return {
type: this.type,
type: this.getType(),
...(mainP ? { [mainP]: this[mainP] } : {}),
};
}
Expand Down
7 changes: 4 additions & 3 deletions src/filters/BlendColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { classRegistry } from '../ClassRegistry';
* canvas.renderAll();
*/
export class BlendColor extends AbstractBaseFilter<Record<string, string>> {
static readonly type = 'BlendColor';

/**
* Color to make the blend operation with. default to a reddish color since black or white
* gives always strong result.
Expand Down Expand Up @@ -74,7 +76,7 @@ export class BlendColor extends AbstractBaseFilter<Record<string, string>> {
}

getCacheKey() {
return `${this.type}_${this.mode}`;
return `${this.getType()}_${this.mode}`;
}

getFragmentSource(): string {
Expand Down Expand Up @@ -202,7 +204,7 @@ export class BlendColor extends AbstractBaseFilter<Record<string, string>> {
*/
toObject() {
return {
type: this.type,
type: this.getType(),
color: this.color,
mode: this.mode,
alpha: this.alpha,
Expand All @@ -215,7 +217,6 @@ export class BlendColor extends AbstractBaseFilter<Record<string, string>> {
}

export const blendColorDefaultValues: Partial<TClassProperties<BlendColor>> = {
type: 'BlendColor',
color: '#F95C63',
mode: 'multiply',
alpha: 1,
Expand Down
6 changes: 4 additions & 2 deletions src/filters/BlendImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { classRegistry } from '../ClassRegistry';
* canvas.renderAll();
*/
export class BlendImage extends AbstractBaseFilter<Record<string, string>> {
static readonly type = 'BlendImage';

/**
* Color to make the blend operation with. default to a reddish color since black or white
* gives always strong result.
Expand All @@ -43,7 +45,7 @@ export class BlendImage extends AbstractBaseFilter<Record<string, string>> {
declare alpha: number;

getCacheKey() {
return `${this.type}_${this.mode}`;
return `${this.getType()}_${this.mode}`;
}

getFragmentSource(): string {
Expand Down Expand Up @@ -178,7 +180,7 @@ export class BlendImage extends AbstractBaseFilter<Record<string, string>> {
*/
toObject() {
return {
type: this.type,
type: this.getType(),
image: this.image && this.image.toObject(),
mode: this.mode,
alpha: this.alpha,
Expand Down
3 changes: 2 additions & 1 deletion src/filters/Blur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import { classRegistry } from '../ClassRegistry';
* canvas.renderAll();
*/
export class Blur extends BaseFilter {
static readonly type = 'Blur';

/**
* blur value, in percentage of image dimensions.
* specific to keep the image blur constant at different resolutions
Expand Down Expand Up @@ -169,7 +171,6 @@ export class Blur extends BaseFilter {
}

export const blurDefaultValues: Partial<TClassProperties<Blur>> = {
type: 'Blur',
fragmentSource: `
precision highp float;
uniform sampler2D uTexture;
Expand Down
3 changes: 2 additions & 1 deletion src/filters/Boilerplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { T2DPipelineState, TWebGLUniformLocationMap } from './typedefs';
* object.applyFilters();
*/
export class MyFilter extends BaseFilter {
static readonly type = 'MyFilter';

/**
* MyFilter value, from -1 to 1.
* translated to -255 to 255 for 2d
Expand Down Expand Up @@ -72,7 +74,6 @@ export class MyFilter extends BaseFilter {
}

export const myFilterDefaultValues: Partial<TClassProperties<MyFilter>> = {
type: 'MyFilter',
fragmentSource: `
precision highp float;
uniform sampler2D uTexture;
Expand Down
3 changes: 2 additions & 1 deletion src/filters/Brightness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { classRegistry } from '../ClassRegistry';
* object.applyFilters();
*/
export class Brightness extends BaseFilter {
static readonly type = 'Brightness';

/**
* Brightness value, from -1 to 1.
* translated to -255 to 255 for 2d
Expand Down Expand Up @@ -73,7 +75,6 @@ export class Brightness extends BaseFilter {
}

export const brightnessDefaultValues: Partial<TClassProperties<Brightness>> = {
type: 'Brightness',
fragmentSource: `
precision highp float;
uniform sampler2D uTexture;
Expand Down
3 changes: 2 additions & 1 deletion src/filters/ColorMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { classRegistry } from '../ClassRegistry';
* object.applyFilters();
*/
export class ColorMatrix extends BaseFilter {
static readonly type = 'ColorMatrix' as string;

/**
* Colormatrix for pixels.
* array of 20 floats. Numbers in positions 4, 9, 14, 19 loose meaning
Expand Down Expand Up @@ -134,7 +136,6 @@ export class ColorMatrix extends BaseFilter {

export const colorMatrixDefaultValues: Partial<TClassProperties<ColorMatrix>> =
{
type: 'ColorMatrix',
fragmentSource: `
precision highp float;
uniform sampler2D uTexture;
Expand Down
6 changes: 5 additions & 1 deletion src/filters/ColorMatrixFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ export function createColorMatrixFilter(key: string, matrix: number[]) {
* @param {String} type
* @default
*/
type = key;
static readonly type = key;

getType() {
return super.getType();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TS complains since protected methods can't be in a mixin so I made it public


/**
* Colormatrix for the effect
Expand Down
15 changes: 7 additions & 8 deletions src/filters/Composed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import { classRegistry } from '../ClassRegistry';
/**
* A container class that knows how to apply a sequence of filters to an input image.
*/
// @ts-expect-error
// @ts-expect-error doesn't implement abstract methods
export class Composed extends BaseFilter {
static readonly type = 'Composed';

/**
* A non sparse array of filters to apply
*/
Expand Down Expand Up @@ -66,20 +68,17 @@ export class Composed extends BaseFilter {
* @returns {Promise<Composed>}
*/
static fromObject(
object: Record<string, any>,
object: Record<string, any> & {
subFilters?: (Record<string, any> & { type: string })[];
},
options: { signal: AbortSignal }
) {
return Promise.all(
((object.subFilters || []) as AnyFilter[]).map((filter) =>
(object.subFilters || []).map((filter) =>
classRegistry.getClass(filter.type).fromObject(filter, options)
)
).then((enlivedFilters) => new Composed({ subFilters: enlivedFilters }));
}
}

export const composedDefaultValues: Partial<TClassProperties<Composed>> = {
type: 'Composed',
};

Object.assign(Composed.prototype, composedDefaultValues);
classRegistry.setClass(Composed);
3 changes: 2 additions & 1 deletion src/filters/Contrast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { classRegistry } from '../ClassRegistry';
* object.applyFilters();
*/
export class Contrast extends BaseFilter {
static readonly type = 'Contrast';

/**
* contrast value, range from -1 to 1.
* @param {Number} contrast
Expand Down Expand Up @@ -74,7 +76,6 @@ export class Contrast extends BaseFilter {
}

export const contrastDefaultValues: Partial<TClassProperties<Contrast>> = {
type: 'Contrast',
fragmentSource: `
precision highp float;
uniform sampler2D uTexture;
Expand Down
5 changes: 3 additions & 2 deletions src/filters/Convolute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import { classRegistry } from '../ClassRegistry';
* canvas.renderAll();
*/
export class Convolute extends AbstractBaseFilter<Record<string, string>> {
static readonly type = 'Convolute';

/*
* Opaque value (true/false)
*/
Expand All @@ -55,7 +57,7 @@ export class Convolute extends AbstractBaseFilter<Record<string, string>> {
declare matrix: number[];

getCacheKey() {
return `${this.type}_${Math.sqrt(this.matrix.length)}_${
return `${this.getType()}_${Math.sqrt(this.matrix.length)}_${
this.opaque ? 1 : 0
}`;
}
Expand Down Expand Up @@ -178,7 +180,6 @@ export class Convolute extends AbstractBaseFilter<Record<string, string>> {
}

export const convoluteDefaultValues: Partial<TClassProperties<Convolute>> = {
type: 'Convolute',
opaque: false,
matrix: [0, 0, 0, 0, 1, 0, 0, 0, 0],
fragmentSource: {
Expand Down
3 changes: 2 additions & 1 deletion src/filters/Gamma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export type GammaInput = [number, number, number];
* object.applyFilters();
*/
export class Gamma extends BaseFilter {
static readonly type = 'Gamma';

/**
* Gamma array value, from 0.01 to 2.2.
* @param {Array} gamma
Expand Down Expand Up @@ -103,7 +105,6 @@ export class Gamma extends BaseFilter {
}

export const gammaDefaultValues: Partial<TClassProperties<Gamma>> = {
type: 'Gamma',
fragmentSource: `
precision highp float;
uniform sampler2D uTexture;
Expand Down
5 changes: 3 additions & 2 deletions src/filters/Grayscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { classRegistry } from '../ClassRegistry';
* object.applyFilters();
*/
export class Grayscale extends AbstractBaseFilter<Record<string, string>> {
static readonly type = 'Grayscale';

declare mode: 'average' | 'lightness' | 'luminosity';

/**
Expand Down Expand Up @@ -43,7 +45,7 @@ export class Grayscale extends AbstractBaseFilter<Record<string, string>> {
}

getCacheKey() {
return `${this.type}_${this.mode}`;
return `${this.getType()}_${this.mode}`;
}

getFragmentSource() {
Expand Down Expand Up @@ -94,7 +96,6 @@ export class Grayscale extends AbstractBaseFilter<Record<string, string>> {
}

export const grayscaleDefaultValues: Partial<TClassProperties<Grayscale>> = {
type: 'Grayscale',
fragmentSource: {
average: `
precision highp float;
Expand Down
Loading