Skip to content

Commit

Permalink
refactor fromElementGenerator => polyFromElement
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 committed Nov 1, 2022
1 parent 65cfcd6 commit 5681fce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 33 deletions.
10 changes: 8 additions & 2 deletions src/shapes/polygon.class.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fabric } from '../../HEADER';
import { TClassProperties } from '../typedefs';
import { FabricObject } from './object.class';
import { Polyline } from './polyline.class';
import { polyFromElement, Polyline } from './polyline.class';

export class Polygon extends Polyline {
protected isOpen() {
Expand All @@ -18,7 +18,13 @@ export class Polygon extends Polyline {
* @param {Function} callback callback function invoked after parsing
* @param {Object} [options] Options object
*/
static fromElement = Polyline.fromElementGenerator(Polygon);
static fromElement(
element: SVGElement,
callback: (poly: Polygon | null) => any,
options?: any
) {
return polyFromElement(Polygon, element, callback, options);
}

/* _FROM_SVG_END_ */

Expand Down
67 changes: 36 additions & 31 deletions src/shapes/polyline.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@ import { toFixed } from '../util/misc/toFixed';
import { FabricObject } from './fabricObject.class';
import { fabricObjectDefaultValues } from './object.class';

export function polyFromElement<
T extends {
new (points: IPoint[], options: any): any;
ATTRIBUTE_NAMES: string[];
}
>(
klass: T,
element: SVGElement,
callback: (poly: InstanceType<T> | null) => any,
options = {}
) {
if (!element) {
return callback(null);
}
const points = parsePointsAttribute(element.getAttribute('points')),
// we omit left and top to instruct the constructor to position the object using the bbox
// eslint-disable-next-line @typescript-eslint/no-unused-vars
{ left, top, ...parsedAttributes } = parseAttributes(
element,
klass.ATTRIBUTE_NAMES
);
callback(
new klass(points || [], {
...parsedAttributes,
...options,
fromSVG: true,
})
);
}

export class Polyline extends FabricObject {
/**
* Points array
Expand Down Expand Up @@ -277,39 +307,14 @@ export class Polyline extends FabricObject {
* @param {Function} callback callback function invoked after parsing
* @param {Object} [options] Options object
*/
static fromElementGenerator<
T extends {
new (points: IPoint[], options: any): any;
ATTRIBUTE_NAMES: string[];
}
>(klass: T) {
return function (
element: SVGElement,
callback: (poly: InstanceType<T> | null) => any,
options = {}
) {
if (!element) {
return callback(null);
}
const points = parsePointsAttribute(element.getAttribute('points')),
// we omit left and top to instruct the constructor to position the object using the bbox
// eslint-disable-next-line @typescript-eslint/no-unused-vars
{ left, top, ...parsedAttributes } = parseAttributes(
element,
klass.ATTRIBUTE_NAMES
);
callback(
new klass(points || [], {
...parsedAttributes,
...options,
fromSVG: true,
})
);
};
static fromElement(
element: SVGElement,
callback: (poly: Polyline | null) => any,
options?: any
) {
return polyFromElement(Polyline, element, callback, options);
}

static fromElement = Polyline.fromElementGenerator(Polyline);

/* _FROM_SVG_END_ */

/**
Expand Down

0 comments on commit 5681fce

Please sign in to comment.