Skip to content

Commit

Permalink
chore(TS): more conversion of utils (#8148)
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur authored Aug 21, 2022
1 parent 1c4fd41 commit 89f2299
Show file tree
Hide file tree
Showing 9 changed files with 506 additions and 432 deletions.
5 changes: 3 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//@ts-nocheck
import { TMat2D } from "./typedefs";

export const halfPI = Math.PI / 2;
export const PiBy180 = Math.PI / 180;
export const iMatrix = Object.freeze([1, 0, 0, 1, 0, 0]);
export const iMatrix = Object.freeze([1, 0, 0, 1, 0, 0]) as TMat2D;
export const DEFAULT_SVG_FONT_SIZE = 16;

/* "magic number" for bezier approximations of arcs (http://itc.ktu.lt/itc354/Riskus354.pdf) */
Expand Down
11 changes: 10 additions & 1 deletion src/point.class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fabric } from '../HEADER';
import { TRadian } from './typedefs';
import { TMat2D, TRadian } from './typedefs';

export interface IPoint {
x: number
Expand Down Expand Up @@ -350,6 +350,8 @@ export class Point {

/**
* Rotates `point` around `origin` with `radians`
* WARNING: this is probably a source of circular dependency.
* evaluate what to do when importing rotateVector directly from the file
* @static
* @memberOf fabric.util
* @param {Point} origin The origin of the rotation
Expand All @@ -359,6 +361,13 @@ export class Point {
rotate(origin: Point, radians: TRadian): Point {
return fabric.util.rotateVector(this.subtract(origin), radians).add(origin);
}

transform(t: TMat2D, ignoreOffset: boolean): Point {
return new Point(
t[0] * this.x + t[2] * this.y + (ignoreOffset ? 0 : t[4]),
t[1] * this.x + t[3] * this.y + (ignoreOffset ? 0 : t[5])
);
}
}

fabric.Point = Point;
10 changes: 8 additions & 2 deletions src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ interface NominalTag<T> {

type Nominal<Type, Tag> = NominalTag<Tag> & Type;

// eslint-disable-next-line no-unused-vars
const enum Degree {}
// eslint-disable-next-line no-unused-vars
const enum Radian {}

export type TDegree = Nominal<number, Degree>;
export type TRadian = Nominal<number, Radian>;

export const enum StrokeLineJoin {
miter = 'miter',
bevel = 'bevel',
round = 'round',
}

export type TMat2D = [number, number, number, number, number, number];
4 changes: 2 additions & 2 deletions src/util/animate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ts-nocheck
import { Point } from '../point.class';
import { extend } from './lang_object';

(function (global) {
/**
Expand Down Expand Up @@ -32,7 +32,7 @@ import { Point } from '../point.class';
* @type {AnimationContext[]}
*/
var fabric = global.fabric, RUNNING_ANIMATIONS = [];
fabric.util.object.extend(RUNNING_ANIMATIONS, {
extend(RUNNING_ANIMATIONS, {

/**
* cancel all running animations at the next requestAnimFrame
Expand Down
118 changes: 53 additions & 65 deletions src/util/lang_object.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,65 @@
//@ts-nocheck
(function(global) {
var fabric = global.fabric;
/**
* Copies all enumerable properties of one js object to another
* this does not and cannot compete with generic utils.
* Does not clone or extend fabric.Object subclasses.
* This is mostly for internal use and has extra handling for fabricJS objects
* it skips the canvas and group properties in deep cloning.
* @memberOf fabric.util.object
* @param {Object} destination Where to copy to
* @param {Object} source Where to copy from
* @param {Boolean} [deep] Whether to extend nested objects
* @return {Object}
*/

function extend(destination, source, deep) {
// JScript DontEnum bug is not taken care of
// the deep clone is for internal use, is not meant to avoid
// javascript traps or cloning html element or self referenced objects.
if (deep) {
if (!fabric.isLikelyNode && source instanceof Element) {
// avoid cloning deep images, canvases,
destination = source;
/**
* Copies all enumerable properties of one js object to another
* this does not and cannot compete with generic utils.
* Does not clone or extend fabric.Object subclasses.
* This is mostly for internal use and has extra handling for fabricJS objects
* it skips the canvas and group properties in deep cloning.
* @memberOf fabric.util.object
* @param {Object} destination Where to copy to
* @param {Object} source Where to copy from
* @param {Boolean} [deep] Whether to extend nested objects
* @return {Object}
*/

export const extend = (destination, source, deep) => {
// the deep clone is for internal use, is not meant to avoid
// javascript traps or cloning html element or self referenced objects.
if (deep) {
if (!fabric.isLikelyNode && source instanceof Element) {
// avoid cloning deep images, canvases,
destination = source;
}
else if (Array.isArray(source)) {
destination = [];
for (let i = 0, len = source.length; i < len; i++) {
destination[i] = extend({ }, source[i], deep);
}
else if (source instanceof Array) {
destination = [];
for (var i = 0, len = source.length; i < len; i++) {
destination[i] = extend({ }, source[i], deep);
}
else if (source && typeof source === 'object') {
for (const property in source) {
if (property === 'canvas' || property === 'group') {
// we do not want to clone this props at all.
// we want to keep the keys in the copy
destination[property] = null;
}
}
else if (source && typeof source === 'object') {
for (var property in source) {
if (property === 'canvas' || property === 'group') {
// we do not want to clone this props at all.
// we want to keep the keys in the copy
destination[property] = null;
}
else if (source.hasOwnProperty(property)) {
destination[property] = extend({ }, source[property], deep);
}
else if (Object.prototype.hasOwnProperty.call(source, property)) {
destination[property] = extend({ }, source[property], deep);
}
}
else {
// this sounds odd for an extend but is ok for recursive use
destination = source;
}
}
else {
for (var property in source) {
destination[property] = source[property];
}
// this sounds odd for an extend but is ok for recursive use
destination = source;
}
return destination;
}

/**
* Creates an empty object and copies all enumerable properties of another object to it
* This method is mostly for internal use, and not intended for duplicating shapes in canvas.
* @memberOf fabric.util.object
* @param {Object} object Object to clone
* @param {Boolean} [deep] Whether to clone nested objects
* @return {Object}
*/

//TODO: this function return an empty object if you try to clone null
function clone(object, deep) {
return deep ? extend({ }, object, deep) : Object.assign({}, object);
else {
for (const property in source) {
destination[property] = source[property];
}
}
return destination;
}

/**
* Creates an empty object and copies all enumerable properties of another object to it
* This method is mostly for internal use, and not intended for duplicating shapes in canvas.
* @memberOf fabric.util.object
* @param {Object} object Object to clone
* @param {Boolean} [deep] Whether to clone nested objects
* @return {Object}
*/

/** @namespace fabric.util.object */
fabric.util.object = {
extend: extend,
clone: clone
};
fabric.util.object.extend(fabric.util, fabric.Observable);
})(typeof exports !== 'undefined' ? exports : window);
//TODO: this function return an empty object if you try to clone null
export const clone = (object: any, deep: boolean) => deep ? extend({ }, object, deep) : { ...object };
Loading

0 comments on commit 89f2299

Please sign in to comment.