From 3db468885f076e7a4ef0684d7a1bbdc26bfccb6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=20=D0=90?= =?UTF-8?q?=D0=BD=D1=82=D0=BE=D0=BD?= Date: Tue, 14 May 2019 03:52:32 +0500 Subject: [PATCH] [paper] Include Base class definition + type save getters and setters (#35285) * [paper] Add Base class * [paper] Type save getters and setters * [paper] import/export test * [paper] Fix: "Common mistakes" * [paper] Tabs to spaces --- types/paper/index.d.ts | 349 +-- types/paper/paper-tests.ts | 5602 ++++++++++++++++++------------------ 2 files changed, 2984 insertions(+), 2967 deletions(-) diff --git a/types/paper/index.d.ts b/types/paper/index.d.ts index 2d8f839c20..07ef128da3 100644 --- a/types/paper/index.d.ts +++ b/types/paper/index.d.ts @@ -4,7 +4,9 @@ // Jon Lucas , // Sebastian Lopez // Ryan Friedman +// Mikhaylov Anton // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 type NativeMouseEvent = MouseEvent; @@ -13,6 +15,68 @@ type NativeMouseEvent = MouseEvent; */ declare module paper { + /** + * Base class for all library classes. + */ + class Base { + /** + * Checks if two values or objects are equals to each other, by using their + * equals() methods if available, and also comparing elements of arrays and + * properties of objects. + */ + static equals(obj1: any, obj2: any): boolean; + + /** + * Imports (deserializes) the stored JSON data into the target, if the + * classes match. If they do not match, a newly created object is returned + * instead. + */ + static importJSON(json: object | any[] | string, target?: any): any; + static importJSON(json: T, target?: T): T; + + /** + * Exports (serializes) object to a JSON data object or string. + * + * + * @param options [optional] - the serialization options + * @option [options.asString=true] - whether the JSON is returned as a Object or a String. + * @option [options.precision=5] - the amount of fractional digits in numbers used in JSON data. + * @return the exported JSON data + */ + static exportJSON(obj: any, options?: { asString?: true; precision?: number; }): string; + static exportJSON(obj: any, options: { asString: false; precision?: number; }): object | any[]; + + /** + * Imports (deserializes) the stored JSON data into the object, if the + * classes match. If they do not match, a newly created object is returned + * instead. + */ + importJSON(json: string): void; + importJSON(json: T): T; + + /** + * Exports (serializes) this object to a JSON data object or string. + * + * + * @param options [optional] - the serialization options + * @option [options.asString=true] - whether the JSON is returned as a Object or a String. + * @option [options.precision=5] - the amount of fractional digits in numbers used in JSON data. + * @return the exported JSON data + */ + exportJSON(options?: { asString?: true; precision?: number; }): string; + exportJSON(options: { asString: false; precision?: number; }): object | any[]; + + /** + * Renders base objects to strings in object literal notation. + */ + toString(): string; + + /** + * Same as `exportJSON({ asString: false })`. + */ + toJSON(): string; + } + /** * The version of Paper.js, as a string. */ @@ -72,8 +136,7 @@ declare module paper { * Such a coordinate transformation can be represented by a 3 row by 3 column matrix with an implied last row of [ 0 0 1 ]. This matrix transforms source coordinates (x,y) into destination coordinates (x',y') by considering them to be a column vector and multiplying the coordinate vector by the matrix according to the following process: * This class is optimized for speed and minimizes calculations based on its knowledge of the underlying matrix (as opposed to say simply performing matrix multiplication). */ - export class Matrix { - + export class Matrix extends Base { /** * Creates a 2D affine transform. * @param a - the a property of the transform @@ -190,11 +253,6 @@ declare module paper { */ equals(matrix: Matrix): boolean; - /** - * returns a string representation of this transform - */ - toString(): string; - /** * Resets the matrix by setting its values to the ones of the identity matrix that results in no transformation. */ @@ -360,7 +418,7 @@ declare module paper { * Attempts to decompose the affine transformation described by this matrix into scaling, rotation and shearing, and returns an object with these properties if it succeeded, null otherwise. */ decompose(): any; - + /** * Inverts the matrix, causing it to perform the opposite transformation. If the matrix is not invertible (in which case isSingular() returns true), null is returned. */ @@ -381,7 +439,7 @@ declare module paper { /** * The Point object represents a point in the two dimensional space of the Paper.js project. It is also used to represent two dimensional vector objects. */ - export class Point { + export class Point extends Base { /** * Returns a new point object with the smallest x and y of the supplied points. @@ -514,11 +572,6 @@ declare module paper { */ clone(): Point; - /** - * a string representation of the point - */ - toString(): string; - /** * Returns the smaller angle between two vectors. The angle is unsigned, no information about rotational direction is given. * @param point - @@ -692,7 +745,7 @@ declare module paper { /** * A Rectangle specifies an area that is enclosed by it's top-left point (x, y), its width, and its height. It should not be confused with a rectangular path, it is not an item. */ - export class Rectangle { + export class Rectangle extends Base { /** * Creates a Rectangle object. @@ -882,11 +935,6 @@ declare module paper { */ equals(rect: Rectangle): boolean; - /** - * a string representation of this rectangle - */ - toString(): string; - /** * Returns true if the rectangle is empty, false otherwise */ @@ -962,7 +1010,7 @@ declare module paper { /** * The Size object is used to describe the size or dimensions of something, through its width and height properties. */ - export class Size { + export class Size extends Base { /** * Returns a new size object with the smallest width and height of the supplied sizes. @@ -1034,11 +1082,6 @@ declare module paper { */ clone(): Size; - /** - * a string representation of the size - */ - toString(): string; - /** * Checks if this size has both the width and height set to 0. */ @@ -1109,32 +1152,32 @@ declare module paper { modulo(size: number[]): Size; modulo(size: number): Size; - /** - * Sets the size with the given width and height values. + /** + * Sets the size with the given width and height values. * @param width - the width * @param height - the height - */ - set(width: number, height: number): Size; + */ + set(width: number, height: number): Size; - /** - * Sets the size using the numbers in the given array as dimensions. + /** + * Sets the size using the numbers in the given array as dimensions. * @param array - an array of numbers - */ - set(array: number[]): Size; - - /** - * Sets the size using the properties in the given object. + */ + set(array: number[]): Size; + + /** + * Sets the size using the properties in the given object. * @param object - the object literal containing properies (width:10, height:10 etc) - */ - set(object: any): Size; - - /** - * Sets the size using the coordinates of the given Size object. + */ + set(object: any): Size; + + /** + * Sets the size using the coordinates of the given Size object. * @param size - the size to duplicate from */ - set(size: Size): Size; - - /** + set(size: Size): Size; + + /** * Sets the size using the point.x and point.y values of the given Point object. * @param point - the point from which to create a size */ @@ -1165,7 +1208,7 @@ declare module paper { * Paper classes can only be accessed through PaperScope objects. Thus in PaperScript they are global, while in JavaScript, they are available on the global paper object. For JavaScript you can use paperScope.install(scope) to install the Paper classes and objects on the global scope. Note that when working with more than one scope, this still works for classes, but not for objects like paperScope.project, since they are not updated in the injected scope if scopes are switched. * The global paper object is simply a reference to the currently active PaperScope. */ - export class PaperScope { + export class PaperScope extends Base { /** * The version of Paper.js, as a string. @@ -1308,12 +1351,12 @@ declare module paper { * only hit selected items. */ selected?: boolean; - + } /** * The Item type allows you to access and modify the items in Paper.js projects. Its functionality is inherited by different project item types such as Path, CompoundPath, Group, Layer and Raster. They each add a layer of functionality that is unique to their type, but share the underlying properties and functions that they inherit from Item. */ - export class Item { + export class Item extends Base { /** * The tangential vector to the #curve at the given location. @@ -1350,13 +1393,15 @@ declare module paper { /** * The path style of the item. */ - style: Style; - + style: Partial