Skip to content

Commit

Permalink
possible-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur committed Feb 28, 2023
1 parent 3443f6d commit e1a6734
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 32 deletions.
20 changes: 10 additions & 10 deletions src/ClassRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* different sources you will need to import all fabric because you may need all classes.
*/

import { Constructor } from './typedefs';

export const JSON = 'json';
export const SVG = 'svg';

Expand All @@ -34,26 +32,28 @@ export class ClassRegistry {

setClass(classConstructor: any, classType?: string) {
if (classType) {
this[JSON].set(classType, classConstructor);
this[JSON].set(
classType,
classConstructor
);
} else {
this[JSON].set(classConstructor.name, classConstructor);
// legacy
// @TODO: needs to be removed in fabric 7 or 8
this[JSON].set(classConstructor.name.toLowerCase(), classConstructor);
}

}

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

setSVGClass(classConstructor: any, SVGTagName?: string) {
if (SVGTagName) {
this[SVG].set(SVGTagName, classConstructor);
} else {
this[SVG].set(classConstructor.name, classConstructor);
// legacy
this[SVG].set(classConstructor.name.toLowerCase(), classConstructor);
}
this[SVG].set(
SVGTagName ?? classConstructor.name.toLowerCase(),
classConstructor
);
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/Collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,14 @@ export function createCollectionMixin<TBase extends Constructor>(Base: TBase) {

/**
* Returns an array of children objects of this instance
* @param {...String} [types] When specified, only objects of these types are returned
* @return {Array}
*/
getObjects() {
return [...this._objects];
getObjects(...types: string[]) {
if (types.length === 0) {
return [...this._objects];
}
return this._objects.filter((o) => types.includes(o.type));
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { loadImage } from './util/misc/objectEnlive';
import { pick } from './util/misc/pick';
import { toFixed } from './util/misc/toFixed';
import { classRegistry } from './ClassRegistry';
import {deprecate} from 'util';

export type TPatternRepeat = 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';

Expand Down Expand Up @@ -39,6 +40,23 @@ type TCanvasSource = { source: HTMLCanvasElement };
* @see {@link http://fabricjs.com/dynamic-patterns demo}
*/
export class Pattern {

/**
* Legacy identifier of the class. Prefer using this.constructor.name 'Pattern'
* or utils like isPattern
* Will be removed in fabric 7 or 8.
* @TODO add sustainable warning message
* @type string
* @deprecated
*/
get type() {
return 'pattern';
}

set type(value) {
console.warn('Setting type has no effect', value)
}

/**
* @type TPatternRepeat
* @defaults
Expand Down Expand Up @@ -207,3 +225,5 @@ export class Pattern {
}

classRegistry.setClass(Pattern);
// kept for compatibility reason
classRegistry.setClass(Pattern, 'pattern');
4 changes: 4 additions & 0 deletions src/shapes/IText/IText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ export class IText<
return { ...super.getDefaults(), ...IText.ownDefaults };
}

get type() {
return 'i-text';
}

/**
* Constructor
Expand Down
2 changes: 1 addition & 1 deletion src/shapes/Object/FabricObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class FabricObject<EventSpec extends ObjectEvents = ObjectEvents>

applyMixins(FabricObject, [FabricObjectSVGExportMixin]);

classRegistry.setClass(FabricObject, 'Object');
classRegistry.setClass(FabricObject);
classRegistry.setClass(FabricObject, 'object');

export { cacheProperties } from './defaultValues';
41 changes: 23 additions & 18 deletions src/shapes/Object/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import type {
import { classRegistry } from '../../ClassRegistry';
import { runningAnimations } from '../../util/animation/AnimationRegistry';
import { cloneDeep } from '../../util/internals/cloneDeep';
import { capitalize } from '../../util/lang_string';
import { capValue } from '../../util/misc/capValue';
import { createCanvasElement, toDataURL } from '../../util/misc/dom';
import { invertTransform, qrDecompose } from '../../util/misc/matrix';
Expand Down Expand Up @@ -274,11 +273,28 @@ export class FabricObject<EventSpec extends ObjectEvents = ObjectEvents>
return { ...FabricObject.ownDefaults };
}

/**
* Legacy identifier of the class. Prefer using utils like isType or instanceOf
* Will be removed in fabric 7 or 8.
* The setter exists because is very hard to catch all the ways in which a type value
* could be set in the instance
* @TODO add sustainable warning message
* @type string
* @deprecated
*/
get type() {
return this.constructor.name.toLowerCase();
}

set type(value) {
console.warn('Setting type has no effect', value)
}

/**
* Constructor
* @param {Object} [options] Options object
*/
constructor(options?: Partial<ObjectProps>) {
constructor(options: Partial<ObjectProps>) {
super();
Object.assign(
this,
Expand Down Expand Up @@ -500,10 +516,7 @@ export class FabricObject<EventSpec extends ObjectEvents = ObjectEvents>
: null,
object = {
...pick(this, propertiesToInclude as (keyof this)[]),
type:
this.constructor.name === 'FabricObject'
? 'Object'
: this.constructor.name,
type: this.constructor.name,
version: VERSION,
originX: this.originX,
originY: this.originY,
Expand Down Expand Up @@ -600,11 +613,7 @@ export class FabricObject<EventSpec extends ObjectEvents = ObjectEvents>
* @return {String}
*/
toString() {
return `#<${
this.constructor.name === 'FabricObject'
? 'Object'
: this.constructor.name
}>`;
return `#<${this.constructor.name}>`;
}

/**
Expand Down Expand Up @@ -1447,11 +1456,7 @@ export class FabricObject<EventSpec extends ObjectEvents = ObjectEvents>
* @return {Boolean}
*/
isType(...types: string[]) {
return types.includes(
this.constructor.name === 'FabricObject'
? 'Object'
: this.constructor.name
);
return types.includes(this.constructor.name) || types.includes(this.type);
}

/**
Expand Down Expand Up @@ -1544,7 +1549,7 @@ export class FabricObject<EventSpec extends ObjectEvents = ObjectEvents>
// from the resulting enlived options, extract options.extraParam to arg0
// to avoid accidental overrides later
if (extraParam) {
const { [extraParam]: arg0, ...rest } = allOptions;
const { [extraParam]: arg0, type, ...rest } = allOptions;
// @ts-ignore;
return new this(arg0, rest);
} else {
Expand All @@ -1569,5 +1574,5 @@ export class FabricObject<EventSpec extends ObjectEvents = ObjectEvents>
}
}

classRegistry.setClass(FabricObject, 'Object');
classRegistry.setClass(FabricObject);
classRegistry.setClass(FabricObject, 'object');
2 changes: 1 addition & 1 deletion src/shapes/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { config } from '../config';
import { SHARED_ATTRIBUTES } from '../parser/attributes';
import { parseAttributes } from '../parser/parseAttributes';
import { Point } from '../Point';
import { PathData, TClassProperties } from '../typedefs';
import { PathData } from '../typedefs';
import { makeBoundingBoxFromPoints } from '../util/misc/boundingBoxFromPoints';
import { toFixed } from '../util/misc/toFixed';
import {
Expand Down

0 comments on commit e1a6734

Please sign in to comment.