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

chore(TS) Converting Object to a class #8322

Merged
merged 31 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3e16059
just saving
asturur Sep 26, 2022
44b96d5
just saving
asturur Sep 26, 2022
a755104
ok this builds and tests
asturur Sep 27, 2022
cb4eb2c
removed
asturur Sep 27, 2022
2ea8540
test pass
asturur Sep 29, 2022
09d32e3
Observable changes to TS class from @shaman123 (#8330)
asturur Sep 29, 2022
fa35c31
extends observable
asturur Sep 29, 2022
443ced7
no-need-to-expose-observable
asturur Sep 29, 2022
24e02d4
added observable as a class
asturur Sep 29, 2022
0d9f0f1
prettify
asturur Sep 29, 2022
b77dd21
ok deleted this
asturur Sep 30, 2022
530775f
lots of typings
asturur Oct 12, 2022
4fdf9e3
pretty
asturur Oct 12, 2022
c5a42ea
Merge branch 'master' into converting-object-to-class
asturur Oct 12, 2022
0471973
added a test
asturur Oct 12, 2022
ee394dc
added a test
asturur Oct 12, 2022
f05ee9c
ok this is not working but is a start
asturur Oct 13, 2022
66c6ea2
import type top
asturur Oct 13, 2022
2e55cde
this wasnt here
asturur Oct 13, 2022
b6deb8b
Merge branch 'master' into converting-object-to-class
asturur Oct 13, 2022
fa80e4e
passing
asturur Oct 13, 2022
3e93f57
fix(): TDegree/TRadian & number
ShaMan123 Oct 13, 2022
65e4c33
test passing
asturur Oct 17, 2022
7030b16
remove unneeded changes
asturur Oct 17, 2022
a2fa127
reverted accidental change
asturur Oct 17, 2022
fc8bcbb
Merge branch 'master' into converting-object-to-class
asturur Oct 17, 2022
1a426af
prettier
asturur Oct 17, 2022
247f8e1
Merge branch 'converting-object-to-class' of github.com:fabricjs/fabr…
asturur Oct 17, 2022
cabc310
Merge branch 'converting-object-to-class' of https://github.com/fabri…
ShaMan123 Oct 17, 2022
ab2da0b
Update object.class.ts
ShaMan123 Oct 17, 2022
c661f9e
Merge branch 'master' into converting-object-to-class
asturur Oct 20, 2022
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
3 changes: 2 additions & 1 deletion src/canvas.class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ts-nocheck
import { Point } from './point.class';
import { FabricObject } from './shapes/object.class';

(function (global) {
var fabric = global.fabric,
Expand Down Expand Up @@ -871,7 +872,7 @@ import { Point } from './point.class';
maxX -= strokeOffset;
maxY -= strokeOffset;
// selection border
fabric.Object.prototype._setLineDash.call(
FabricObject.prototype._setLineDash.call(
this,
ctx,
this.selectionDashArray
Expand Down
4 changes: 3 additions & 1 deletion src/controls/default_controls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ts-nocheck
import { FabricObject } from '../shapes/object.class';

(function (global) {
var fabric = global.fabric,
controlsUtils = fabric.controlsUtils,
Expand All @@ -8,7 +10,7 @@
scalingYOrSkewingX = controlsUtils.scalingYOrSkewingX,
scalingXOrSkewingY = controlsUtils.scalingXOrSkewingY,
scaleOrSkewActionName = controlsUtils.scaleOrSkewActionName,
objectControls = fabric.Object.prototype.controls;
objectControls = FabricObject.prototype.controls;

objectControls.ml = new fabric.Control({
x: -0.5,
Expand Down
9 changes: 3 additions & 6 deletions src/gradient/gradient.class.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ts-nocheck
import { fabric } from '../../HEADER';
import { Color } from '../color';
import { iMatrix } from '../constants';
Expand All @@ -20,11 +21,7 @@ import {
GradientUnits,
SVGOptions,
} from './typedefs';

/**
* @todo remove this transient junk
*/
type FabricObject = any;
import { FabricObject } from '../shapes/object.class';

/**
* Gradient class
Expand Down Expand Up @@ -93,7 +90,7 @@ export class Gradient<
gradientTransform,
id,
}: GradientOptions<T>) {
const uid = fabric.Object.__uid++;
const uid = FabricObject.__uid++;
Copy link
Member Author

Choose a reason for hiding this comment

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

this is not the place for the __uid. i remember @ShaMan123 already worked to put it somewhere else

this.id = id ? `${id}_${uid}` : uid;
this.type = type;
this.gradientUnits = gradientUnits;
Expand Down
6 changes: 4 additions & 2 deletions src/mixins/animation.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ts-nocheck
import { FabricObject } from '../shapes/object.class';

(function (global) {
var fabric = global.fabric;
fabric.util.object.extend(
Expand Down Expand Up @@ -113,8 +115,8 @@
);

fabric.util.object.extend(
fabric.Object.prototype,
/** @lends fabric.Object.prototype */ {
FabricObject.prototype,
/** @lends FabricObject.prototype */ {
/**
* Animates object's properties
* @param {String|Object} property Property to animate (if string) or properties to animate (if object)
Expand Down
3 changes: 2 additions & 1 deletion src/mixins/itext.svg_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Color } from '../color';
import { config } from '../config';
import { FabricObject } from '../shapes/object.class';

/* _TO_SVG_START_ */
(function (global) {
Expand Down Expand Up @@ -323,7 +324,7 @@ import { config } from '../config';
* @return {String}
*/
getSvgStyles: function (skipShadow) {
var svgStyle = fabric.Object.prototype.getSvgStyles.call(
var svgStyle = FabricObject.prototype.getSvgStyles.call(
this,
skipShadow
);
Expand Down
7 changes: 4 additions & 3 deletions src/mixins/object.svg_export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Color } from '../color';
import { config } from '../config';
import { FabricObject } from '../shapes/object.class';

/* _TO_SVG_START_ */
(function (global) {
Expand All @@ -27,8 +28,8 @@ import { config } from '../config';
toFixed = fabric.util.toFixed;

fabric.util.object.extend(
fabric.Object.prototype,
/** @lends fabric.Object.prototype */ {
FabricObject.prototype,
/** @lends FabricObject.prototype */ {
/**
* Returns styles-string for svg-export
* @param {Boolean} skipShadow a boolean to skip shadow filter output
Expand Down Expand Up @@ -275,7 +276,7 @@ import { config } from '../config';
index = objectMarkup.indexOf('COMMON_PARTS'),
additionalTransform = options.additionalTransform;
if (clipPath) {
clipPath.clipPathId = 'CLIPPATH_' + fabric.Object.__uid++;
clipPath.clipPathId = 'CLIPPATH_' + FabricObject.__uid++;
clipPathMarkup =
'<clipPath id="' +
clipPath.clipPathId +
Expand Down
6 changes: 4 additions & 2 deletions src/mixins/object_ancestry.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//@ts-nocheck
import { FabricObject } from '../shapes/object.class';

(function (global) {
var fabric = global.fabric;
fabric.util.object.extend(
fabric.Object.prototype,
/** @lends fabric.Object.prototype */ {
FabricObject.prototype,
/** @lends FabricObject.prototype */ {
/**
* Checks if object is decendant of target
* Should be used instead of @link {fabric.Collection.contains} for performance reasons
Expand Down
5 changes: 3 additions & 2 deletions src/mixins/object_geometry.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { Intersection } from '../intersection.class';
import { Point } from '../point.class';
import { FabricObject } from '../shapes/object.class';

(function (global) {
function arrayFromCoords(coords) {
Expand All @@ -20,8 +21,8 @@ import { Point } from '../point.class';
transformPoint = util.transformPoint;

util.object.extend(
fabric.Object.prototype,
/** @lends fabric.Object.prototype */ {
FabricObject.prototype,
/** @lends FabricObject.prototype */ {
/**
* Describe object's corner position in canvas element coordinates.
* properties are depending on control keys and padding the main controls.
Expand Down
5 changes: 3 additions & 2 deletions src/mixins/object_interactivity.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//@ts-nocheck
import { Point } from '../point.class';
import { FabricObject } from '../shapes/object.class';

(function (global) {
var fabric = global.fabric,
degreesToRadians = fabric.util.degreesToRadians;

fabric.util.object.extend(
fabric.Object.prototype,
/** @lends fabric.Object.prototype */ {
FabricObject.prototype,
/** @lends FabricObject.prototype */ {
/**
* Determines which corner has been clicked
* @private
Expand Down
5 changes: 3 additions & 2 deletions src/mixins/object_origin.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ts-nocheck
import { Point } from '../point.class';
import { FabricObject } from '../shapes/object.class';

(function (global) {
var fabric = global.fabric,
Expand All @@ -21,8 +22,8 @@ import { Point } from '../point.class';
*/

fabric.util.object.extend(
fabric.Object.prototype,
/** @lends fabric.Object.prototype */ {
FabricObject.prototype,
/** @lends FabricObject.prototype */ {
/**
* Resolves origin value relative to center
* @private
Expand Down
6 changes: 4 additions & 2 deletions src/mixins/object_stacking.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//@ts-nocheck
import { FabricObject } from '../shapes/object.class';

(function (global) {
var fabric = global.fabric;
fabric.util.object.extend(
fabric.Object.prototype,
/** @lends fabric.Object.prototype */ {
FabricObject.prototype,
/** @lends FabricObject.prototype */ {
/**
* Moves an object to the bottom of the stack of drawn objects
* @return {fabric.Object} thisArg
Expand Down
8 changes: 5 additions & 3 deletions src/mixins/object_straightening.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
//@ts-nocheck
import { FabricObject } from '../shapes/object.class';

(function (global) {
var fabric = global.fabric;
fabric.util.object.extend(
fabric.Object.prototype,
/** @lends fabric.Object.prototype */ {
FabricObject.prototype,
/** @lends FabricObject.prototype */ {
/**
* @private
* @return {Number} angle value
Expand All @@ -26,7 +28,7 @@
},

/**
* Same as {@link fabric.Object.prototype.straighten} but with animation
* Same as {@link FabricObject.prototype.straighten} but with animation
* @param {Object} callbacks Object with callback functions
* @param {Function} [callbacks.onComplete] Invoked on completion
* @param {Function} [callbacks.onChange] Invoked on every step of animation
Expand Down
8 changes: 8 additions & 0 deletions src/mixins/observable.mixin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ts-nocheck
import { fabric as fabricHeader } from '../../HEADER';

(function (global) {
var fabric = global.fabric;

Expand Down Expand Up @@ -142,4 +144,10 @@
once: once,
off: off,
};
fabricHeader.Observable = {
fire: fire,
on: on,
once: once,
off: off,
}
Copy link
Member Author

Choose a reason for hiding this comment

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

this is some bullshit i did when i was half asleep.
I didn't even remember doing it.
Going to delete

Copy link
Contributor

Choose a reason for hiding this comment

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

I saw that in #8103 and removed that there as well

Copy link
Member Author

Choose a reason for hiding this comment

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

seems like i added it here?

Copy link
Contributor

Choose a reason for hiding this comment

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

The diff is too harsh.

})(typeof exports !== 'undefined' ? exports : window);
4 changes: 2 additions & 2 deletions src/parser/parseSVGDocument.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//@ts-nocheck

import { fabric } from '../../HEADER';
import { applyViewboxTransform } from './applyViewboxTransform';
import {
clipPaths,
Expand All @@ -14,6 +13,7 @@ import { getGradientDefs } from './getGradientDefs';
import { hasAncestorWithNodeName } from './hasAncestorWithNodeName';
import { parseElements } from './parseElements';
import { parseUseDirectives } from './parseUseDirectives';
import { FabricObject } from '../shapes/object.class';

/**
* Parses an SVG document, converts it to an array of corresponding fabric.* instances and passes them to a callback
Expand Down Expand Up @@ -41,7 +41,7 @@ export function parseSVGDocument(doc, callback, reviver, parsingOptions) {
}
parseUseDirectives(doc);

let svgUid = fabric.Object.__uid++,
let svgUid = FabricObject.__uid++,
i,
len,
options = applyViewboxTransform(doc),
Expand Down
6 changes: 3 additions & 3 deletions src/parser/setStrokeFillOpacity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//@ts-nocheck
import { fabric } from '../../HEADER';
import { Color } from '../color';
import { toFixed } from '../util/misc/toFixed';
import { colorAttributes } from './constants';
import { FabricObject } from '../shapes/object.class';

/**
* @private
Expand All @@ -19,10 +19,10 @@ export function setStrokeFillOpacity(attributes) {
}

if (typeof attributes[attr] === 'undefined') {
if (!fabric.Object.prototype[attr]) {
if (!FabricObject.prototype[attr]) {
continue;
}
attributes[attr] = fabric.Object.prototype[attr];
attributes[attr] = FabricObject.prototype[attr];
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this part can be removed in the next iteration.
Why would we need a default value if parsing will eventually init a new class that already has defaults and handles that internally.

Copy link
Member Author

Choose a reason for hiding this comment

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

The code speaks about opacity, i assume that if the svg specify opacity 0.5, we have to transfer that on a color, and that color if not differently specified is our default one.

Copy link
Contributor

Choose a reason for hiding this comment

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

good point

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it should be exposed as a static method on FabricObject...?
Doesn't sound it will fix anything.
This is a design thing...

}

if (attributes[attr].indexOf('url(') === 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/pattern.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ifNaN } from './util/internals';
import { loadImage } from './util/misc/objectEnlive';
import { pick } from './util/misc/pick';
import { toFixed } from './util/misc/toFixed';

import { FabricObject } from './shapes/object.class';
export type TPatternRepeat = 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';

type TExportedKeys =
Expand Down Expand Up @@ -86,7 +86,7 @@ export class Pattern {
* @return {fabric.Pattern} thisArg
*/
constructor(options: TPatternOptions = {}) {
this.id = fabric.Object.__uid++;
this.id = FabricObject.__uid++;
this.setOptions(options);
}

Expand Down
3 changes: 2 additions & 1 deletion src/shadow.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Color } from './color';
import { config } from './config';
import { Point } from './point.class';
import { FabricObject } from './shapes/object.class';

(function (global) {
var fabric = global.fabric || (global.fabric = {}),
Expand Down Expand Up @@ -80,7 +81,7 @@ import { Point } from './point.class';
this[prop] = options[prop];
}

this.id = fabric.Object.__uid++;
this.id = FabricObject.__uid++;
},

/**
Expand Down
9 changes: 5 additions & 4 deletions src/shapes/circle.class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//@ts-nocheck
import { fabric } from '../../HEADER';
import { FabricObject } from './object.class';

const degreesToRadians = fabric.util.degreesToRadians;

Expand All @@ -10,14 +11,14 @@ const degreesToRadians = fabric.util.degreesToRadians;
* @see {@link Circle#initialize} for constructor definition
*/
const Circle = fabric.util.createClass(
fabric.Object,
FabricObject,
/** @lends Circle.prototype */ {
/**
* List of properties to consider when checking if state of an object is changed ({@link fabric.Object#hasStateChanged})
* as well as for history (undo/redo) purposes
* @type Array
*/
stateProperties: fabric.Object.prototype.stateProperties.concat(
stateProperties: FabricObject.prototype.stateProperties.concat(
'radius',
'startAngle',
'endAngle'
Expand Down Expand Up @@ -53,7 +54,7 @@ const Circle = fabric.util.createClass(
*/
endAngle: 360,

cacheProperties: fabric.Object.prototype.cacheProperties.concat(
cacheProperties: FabricObject.prototype.cacheProperties.concat(
'radius',
'startAngle',
'endAngle'
Expand Down Expand Up @@ -226,7 +227,7 @@ Circle.fromElement = function (element, callback) {
* @returns {Promise<Circle>}
*/
Circle.fromObject = (object) =>
fabric.Object._fromObject(fabric.Circle, object);
FabricObject._fromObject(fabric.Circle, object);

fabric.Circle = Circle;
export { Circle };
6 changes: 4 additions & 2 deletions src/shapes/ellipse.class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//@ts-nocheck
import { FabricObject } from './object.class';

(function (global) {
var fabric = global.fabric || (global.fabric = {}),
piBy2 = Math.PI * 2;
Expand Down Expand Up @@ -33,7 +35,7 @@
*/
ry: 0,

cacheProperties: fabric.Object.prototype.cacheProperties.concat(
cacheProperties: FabricObject.prototype.cacheProperties.concat(
'rx',
'ry'
),
Expand Down Expand Up @@ -173,6 +175,6 @@
* @returns {Promise<fabric.Ellipse>}
*/
fabric.Ellipse.fromObject = function (object) {
return fabric.Object._fromObject(fabric.Ellipse, object);
return FabricObject._fromObject(fabric.Ellipse, object);
};
})(typeof exports !== 'undefined' ? exports : window);
Loading