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): Path util typings and refactoring #8787

Merged
merged 48 commits into from
Apr 1, 2023
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
583d8d4
progress, create directory for path to store types
Lazauya Dec 24, 2022
b93339a
initial pass at typing path
Lazauya Dec 24, 2022
32561a5
Merge remote-tracking branch 'origin/master' into ts-path
Lazauya Dec 24, 2022
407699c
revert dist
Lazauya Dec 24, 2022
ad2f36a
simpler & more idiomatic parsePath
Lazauya Feb 9, 2023
eb5dacc
remove log & fix typo
Lazauya Feb 12, 2023
836c24c
fix z bug
Lazauya Feb 12, 2023
19ee7bb
fix multiple m bug
Lazauya Feb 12, 2023
6309788
Merge remote-tracking branch 'origin/master' into ts-path
Lazauya Feb 12, 2023
ee877d3
Merge remote-tracking branch 'origin/master' into ts-path
Lazauya Feb 20, 2023
ad52672
misc/temp bug fixes
Lazauya Feb 20, 2023
05a823a
Merge remote-tracking branch 'origin/master' into ts-path
Lazauya Mar 4, 2023
cefe9b9
make makePathSimpler more type friendly
Lazauya Mar 4, 2023
6d1d0a7
remove unused code
Lazauya Mar 4, 2023
d373cdc
Merge remote-tracking branch 'origin/master' into ts-path
Lazauya Mar 12, 2023
02f8d29
remove small z, fix types to get rid of annoying "as" casting
Lazauya Mar 12, 2023
fa1235d
fix tests to no longer consider small z, also fixed imports
Lazauya Mar 13, 2023
a6d6406
Merge remote-tracking branch 'origin/master' into ts-path-util
Lazauya Mar 18, 2023
a5f1193
prettier
Lazauya Mar 18, 2023
b8fd5b6
Path.ts jsdoc quick-wins
Lazauya Mar 18, 2023
acd69ed
path_types name fix
Lazauya Mar 18, 2023
13eb28f
quick doc/type fixes
Lazauya Mar 18, 2023
917710b
quick Object.assign replacement
Lazauya Mar 18, 2023
b5d48f6
minor syntax fixes and such
Lazauya Mar 18, 2023
06743e7
minor syntax fixes and such
Lazauya Mar 18, 2023
4a0297f
minor syntax fixes and such
Lazauya Mar 18, 2023
95277df
minor syntax fixes and such
Lazauya Mar 18, 2023
a02250d
add import type instead of import
asturur Mar 19, 2023
2d85698
fixed merge conflicts
asturur Mar 19, 2023
2b7f9cd
forget the typecheck only case
asturur Mar 19, 2023
48f3fbd
dgi is not necessary for the main regexp
asturur Mar 19, 2023
7f263c4
remove d flag for node 14 support
Lazauya Mar 19, 2023
a2b40ed
Merge remote-tracking branch 'laz-fork/ts-path-util' into ts-path-util
Lazauya Mar 19, 2023
65387dd
prettier
Lazauya Mar 19, 2023
49df036
testing
asturur Mar 19, 2023
05399c1
Merge branch 'ts-path-util' of github.com:Lazauya/fabric.js into ts-p…
asturur Mar 19, 2023
af41e69
restricted regexes
asturur Mar 19, 2023
de154fc
change TBounds to the better TRectBounds
Lazauya Mar 19, 2023
be4b519
fix type desc
Lazauya Mar 19, 2023
d5e3f74
use vars instead of indices
Lazauya Mar 19, 2023
5267d5f
use case instead of spread in getPathSegmentsInfo
Lazauya Mar 19, 2023
17b95db
Fix findPercentageForDistance to not do unnecessary work
Lazauya Mar 20, 2023
109152e
changelog
Lazauya Mar 20, 2023
97dd2b9
remove unused code
Lazauya Mar 20, 2023
3af939c
changelog prettier
Lazauya Mar 20, 2023
43228c7
change bad type names to slightly better ones
Lazauya Mar 24, 2023
827a841
Merge branch 'master' into ts-path-util
asturur Apr 1, 2023
a3ac259
minor changes
asturur Apr 1, 2023
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
4 changes: 2 additions & 2 deletions src/brushes/PatternBrush.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Pattern } from '../Pattern';
import { PathData } from '../typedefs';
import { createCanvasElement } from '../util/misc/dom';
import type { Canvas } from '../canvas/Canvas';
import { PencilBrush } from './PencilBrush';
import { TSimplePathData } from '../util/path/typedefs';

export class PatternBrush extends PencilBrush {
declare source?: CanvasImageSource;
Expand Down Expand Up @@ -56,7 +56,7 @@ export class PatternBrush extends PencilBrush {
/**
* Creates path
*/
createPath(pathData: PathData) {
createPath(pathData: TSimplePathData) {
const path = super.createPath(pathData),
topLeft = path._getLeftTopCoords().scalarAdd(path.strokeWidth / 2);

Expand Down
16 changes: 8 additions & 8 deletions src/brushes/PencilBrush.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { ModifierKey, TEvent } from '../EventTypeDefs';
import { Point } from '../Point';
import { Shadow } from '../Shadow';
import { Path } from '../shapes/Path';
import { PathData } from '../typedefs';
import { getSmoothPathFromPoints, joinPath } from '../util/path';
import type { Canvas } from '../canvas/Canvas';
import { BaseBrush } from './BaseBrush';
import { TSimplePathData } from '../util/path/typedefs';

/**
* @private
* @param {PathData} pathData SVG path commands
* @param {TSimplePathData} pathData SVG path commands
* @returns {boolean}
*/
function isEmptySVGPath(pathData: PathData): boolean {
function isEmptySVGPath(pathData: TSimplePathData): boolean {
return joinPath(pathData) === 'M 0 0 Q 0 0 0 0 L 0 0';
}

Expand Down Expand Up @@ -206,20 +206,20 @@ export class PencilBrush extends BaseBrush {

/**
* Converts points to SVG path
* @param {Array} points Array of points
* @return {PathData} SVG path commands
* @param {Point[]} points Array of points
* @return {TSimplePathData} SVG path commands
*/
convertPointsToSVGPath(points: Point[]): PathData {
convertPointsToSVGPath(points: Point[]): TSimplePathData {
const correction = this.width / 1000;
return getSmoothPathFromPoints(points, correction);
}

/**
* Creates a Path object to add on canvas
* @param {PathData} pathData Path data
* @param {TSimplePathData} pathData Path data
* @return {Path} Path to add on canvas
*/
createPath(pathData: PathData) {
createPath(pathData: TSimplePathData): Path {
const path = new Path(pathData, {
fill: null,
stroke: this.color,
Expand Down
3 changes: 2 additions & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { config } from './config';
import { TBounds } from './typedefs';

export class Cache {
/**
Expand Down Expand Up @@ -83,7 +84,7 @@ export class Cache {
* can eventually clear it.
* It was an internal variable, is accessible since version 2.3.4
*/
boundsOfCurveCache = {};
boundsOfCurveCache: Record<string, TBounds> = {};
}

export const cache = new Cache();
2 changes: 0 additions & 2 deletions src/canvas/Canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,6 @@ export class Canvas extends SelectableCanvas {
this.selection &&
(!target ||
(!target.selectable &&
// @ts-ignore
!target.isEditing &&
target !== this._activeObject))
) {
Expand Down Expand Up @@ -1369,7 +1368,6 @@ export class Canvas extends SelectableCanvas {
: pointer;
// seems used only here.
// @TODO: investigate;
// @ts-ignore
transform.reset = false;
transform.shiftKey = e.shiftKey;
transform.altKey = !!this.centeredKey && e[this.centeredKey];
Expand Down
5 changes: 1 addition & 4 deletions src/parser/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ export const reNum = '(?:[-+]?(?:\\d+|\\d*\\.\\d+)(?:[eE][-+]?\\d+)?)';

export const svgNS = 'http://www.w3.org/2000/svg';

export const commaWsp = '(?:\\s+,?\\s*|,\\s*)';

export const rePathCommand =
/([-+]?((\d+\.\d+)|((\d+)|(\.\d+)))(?:[eE][-+]?\d+)?)/gi;
export const commaWsp = String.raw`(?:\s+,?\s*|,\s*|$)`;
Lazauya marked this conversation as resolved.
Show resolved Hide resolved

export const reFontDeclaration = new RegExp(
'(normal|italic)?\\s*(normal|small-caps)?\\s*' +
Expand Down
20 changes: 10 additions & 10 deletions src/shapes/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { config } from '../config';
import { SHARED_ATTRIBUTES } from '../parser/attributes';
import { parseAttributes } from '../parser/parseAttributes';
import { Point } from '../Point';
import { PathData } from '../typedefs';
import { makeBoundingBoxFromPoints } from '../util/misc/boundingBoxFromPoints';
import { toFixed } from '../util/misc/toFixed';
import {
Expand All @@ -15,14 +14,15 @@ import {
} from '../util/path';
import { classRegistry } from '../ClassRegistry';
import { FabricObject, cacheProperties } from './Object/FabricObject';
import { TSimplePathData } from '../util/path/typedefs';

export class Path extends FabricObject {
/**
* Array of path points
* @type Array
* @default
*/
declare path: PathData;
declare path: TSimplePathData;

declare pathOffset: Point;

Expand All @@ -36,12 +36,12 @@ export class Path extends FabricObject {

/**
* Constructor
* @param {Array|String} path Path data (sequence of coordinates and corresponding "command" tokens)
* @param {TSimplePathData} path Path data (sequence of coordinates and corresponding "command" tokens)
* @param {Object} [options] Options object
* @return {Path} thisArg
*/
constructor(
path: PathData | string,
path: TSimplePathData | string,
{ path: _, left, top, ...options }: any = {}
) {
super(options);
Expand All @@ -58,11 +58,11 @@ export class Path extends FabricObject {

/**
* @private
* @param {PathData | string} path Path data (sequence of coordinates and corresponding "command" tokens)
* @param {TSimplePathData | string} path Path data (sequence of coordinates and corresponding "command" tokens)
* @param {boolean} [adjustPosition] pass true to reposition the object according to the bounding box
* @returns {Point} top left position of the bounding box, useful for complementary positioning
*/
_setPath(path: PathData | string, adjustPosition?: boolean) {
_setPath(path: TSimplePathData | string, adjustPosition?: boolean) {
this.path = makePathSimpler(Array.isArray(path) ? path : parsePath(path));
return this.setDimensions();
}
Expand Down Expand Up @@ -153,7 +153,7 @@ export class Path extends FabricObject {

/**
* Returns string representation of an instance
* @return {String} string representation of an instance
* @return {string} string representation of an instance
*/
toString() {
return `#<Path (${this.complexity()}): { "top": ${this.top}, "left": ${
Expand Down Expand Up @@ -213,7 +213,7 @@ export class Path extends FabricObject {
/**
* Returns svg clipPath representation of an instance
* @param {Function} [reviver] Method for further parsing of svg representation.
* @return {String} svg representation of an instance
* @return {string} svg representation of an instance
*/
toClipPathSVG(reviver) {
const additionalTransform = this._getOffsetTransform();
Expand All @@ -229,7 +229,7 @@ export class Path extends FabricObject {
/**
* Returns svg representation of an instance
* @param {Function} [reviver] Method for further parsing of svg representation.
* @return {String} svg representation of an instance
* @return {string} svg representation of an instance
*/
toSVG(reviver) {
const additionalTransform = this._getOffsetTransform();
Expand All @@ -241,7 +241,7 @@ export class Path extends FabricObject {

/**
* Returns number representation of an instance complexity
* @return {Number} complexity of this instance
* @return {number} complexity of this instance
*/
complexity() {
return this.path.length;
Expand Down
11 changes: 4 additions & 7 deletions src/typedefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
import { BaseFabricObject } from './EventTypeDefs';
import type { Gradient } from './gradient/Gradient';
import type { Pattern } from './Pattern';
import type { Point } from './Point';
import type { IPoint, Point } from './Point';

interface NominalTag<T> {
nominalTag?: T;
}

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

// eslint-disable-next-line @typescript-eslint/ban-types
type TNonFunctionPropertyNames<T> = {
// eslint-disable-next-line @typescript-eslint/ban-types
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T];
export type TClassProperties<T> = Pick<T, TNonFunctionPropertyNames<T>>;
Expand Down Expand Up @@ -66,11 +66,6 @@ export const enum SupportedSVGUnit {

export type TMat2D = [number, number, number, number, number, number];

/**
* SVG path commands
*/
export type PathData = (string | number)[][];

/**
* An invalid keyword and an empty string will be handled as the `anonymous` keyword.
* @see https://developer.mozilla.org/en-US/docs/HTML/CORS_settings_attributes
Expand Down Expand Up @@ -100,6 +95,8 @@ export type TCacheCanvasDimensions = {
y: number;
};

export type TBounds = IPoint[];
Lazauya marked this conversation as resolved.
Show resolved Hide resolved

export type TToCanvasElementOptions = {
left?: number;
top?: number;
Expand Down
Loading