Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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]

- fix(): `typeAssertions` not respecting subclasses [#8931](https://github.com/fabricjs/fabric.js/pull/8931)
- fix(TS): extending canvas and object event types (`type` => `interface`) [#8926](https://github.com/fabricjs/fabric.js/pull/8926)
- chore(build) simple deps update [#8929](https://github.com/fabricjs/fabric.js/pull/8929)
- fix(Canvas): sync cleanup of dom elements in dispose [#8903](https://github.com/fabricjs/fabric.js/pull/8903)
Expand Down
2 changes: 1 addition & 1 deletion src/gradient/Gradient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class Gradient<
}
if (
object instanceof BaseFabricObject &&
object.isType('Path') &&
object.is('path') &&
this.gradientUnits !== 'percentage'
) {
offsetX -= object.pathOffset.x;
Expand Down
4 changes: 4 additions & 0 deletions src/shapes/ActiveSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { Group } from './Group';
import type { FabricObject } from './Object/FabricObject';

export class ActiveSelection extends Group {
static TAGS = {
selection: true,
};

declare _objects: FabricObject[];

/**
Expand Down
5 changes: 5 additions & 0 deletions src/shapes/IText/IText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export class IText<
extends ITextClickBehavior<Props, SProps, EventSpec>
implements UniqueITextProps
{
static TAGS = {
text: true,
interactive: true,
};

/**
* Index where text selection starts (or where cursor is when there is no selection)
* @type Number
Expand Down
13 changes: 13 additions & 0 deletions src/shapes/Object/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ export class FabricObject<
extends AnimatableObject<EventSpec>
implements ObjectProps
{
static TAGS: Record<string, boolean> = {};

/**
* A workaroud instead of using `type` that is deprecated or `instanceof` that cause import cycles
* @param tags
* @returns
*/
is(...tags: string[]) {
const { TAGS } = this.constructor as typeof FabricObject;
return tags.every((tag) => TAGS[tag]);
}

declare minScaleLimit: number;

declare opacity: number;
Expand Down Expand Up @@ -1443,6 +1455,7 @@ export class FabricObject<

/**
* Returns true if any of the specified types is identical to the type of an instance
* @deprecated use `instanceof` or `is`
* @param {String} type Type to check against
* @return {Boolean}
*/
Expand Down
4 changes: 4 additions & 0 deletions src/shapes/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export class Path<
SProps extends SerializedPathProps = SerializedPathProps,
EventSpec extends ObjectEvents = ObjectEvents
> extends FabricObject<Props, SProps, EventSpec> {
static TAGS = {
path: true,
};

/**
* Array of path points
* @type Array
Expand Down
4 changes: 4 additions & 0 deletions src/shapes/Text/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ export class Text<
extends StyledText<Props, SProps, EventSpec>
implements UniqueTextProps
{
static TAGS = {
text: true,
};

/**
* Properties that requires a text layout recalculation when changed
* @type string[]
Expand Down
6 changes: 3 additions & 3 deletions src/util/typeAssertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,23 @@ export const isCollection = (
export const isActiveSelection = (
fabricObject?: FabricObject
): fabricObject is ActiveSelection => {
return !!fabricObject && fabricObject.isType('ActiveSelection');
return !!fabricObject && fabricObject.is('selection');
};

export const isTextObject = (
fabricObject?: FabricObject
): fabricObject is Text => {
// we could use instanceof but that would mean pulling in Text code for a simple check
// @todo discuss what to do and how to do
return !!fabricObject && fabricObject.isType('Text', 'IText', 'Textbox');
return !!fabricObject && fabricObject.is('text');
};

export const isInteractiveTextObject = (
fabricObject?: FabricObject
): fabricObject is IText | Textbox => {
// we could use instanceof but that would mean pulling in Text code for a simple check
// @todo discuss what to do and how to do
return !!fabricObject && fabricObject.isType('IText', 'Textbox');
return !!fabricObject && fabricObject.is('text', 'interactive');
};

export const isFabricObjectCached = (
Expand Down
1 change: 1 addition & 0 deletions test/unit/activeselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

assert.ok(group);
assert.ok(group instanceof fabric.ActiveSelection, 'should be instance of fabric.ActiveSelection');
assert.ok(group.is('selection'), 'TAG');
});

QUnit.test('toString', function(assert) {
Expand Down
1 change: 1 addition & 0 deletions test/unit/itext.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

assert.ok(iText instanceof fabric.IText);
assert.ok(iText instanceof fabric.Text);
assert.ok(iText.is('text', 'interactive'), 'TAG');
});

QUnit.test('initial properties', function(assert) {
Expand Down
3 changes: 2 additions & 1 deletion test/unit/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
makePathObject(function(path) {
assert.ok(path instanceof fabric.Path);
assert.ok(path instanceof fabric.Object);


assert.ok(path.is('path'), 'TAG');
assert.equal(path.constructor.name, 'Path');

var error;
Expand Down
1 change: 1 addition & 0 deletions test/unit/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
assert.ok(text instanceof fabric.Text);
assert.ok(text instanceof fabric.Object);

assert.ok(text.is('text'), 'TAG');
assert.equal(text.constructor.name, 'Text');
assert.equal(text.get('text'), 'x');
});
Expand Down
1 change: 1 addition & 0 deletions test/unit/textbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
assert.ok(textbox instanceof fabric.Textbox);
assert.ok(textbox instanceof fabric.IText);
assert.ok(textbox instanceof fabric.Text);
assert.ok(textbox.is('text', 'interactive'), 'TAG');
});

QUnit.test('constructor with width', function(assert) {
Expand Down