From 8459d5ed79df53afb99eeb7f04a8135f96a23f60 Mon Sep 17 00:00:00 2001 From: Dobes Vandermeer Date: Fri, 17 May 2019 09:47:54 -0700 Subject: [PATCH] [paper] Fix hitTest types, add constructors to PaperScope (#35361) * Result of hitTest may be null * Add classes to PaperScope In paper you construct objects via the classes inside PaperScope so that those classes will update globals specific to that scope. Constructing these objects via the "global" paper object exported from the module will make global changes to that singleton. * Fix type of hitTest options.class * Refine type of class Use a better type for hitTest class. * Remove unnecessary curly braces --- types/paper/index.d.ts | 39 ++++++++++++++++++++++++++++++++++---- types/paper/paper-tests.ts | 21 +++++++++++++++++--- 2 files changed, 53 insertions(+), 7 deletions(-) diff --git a/types/paper/index.d.ts b/types/paper/index.d.ts index 07ef128da3..4266ca0be7 100644 --- a/types/paper/index.d.ts +++ b/types/paper/index.d.ts @@ -1279,6 +1279,37 @@ declare module paper { */ static get(id: string): PaperScope; + /** + * All the classes for paper can be constructed via the PaperScope instance, so + * that they are associated with that scope + */ + Matrix: typeof Matrix; + Point: typeof Point; + Rectangle: typeof Rectangle; + Size: typeof Size; + PaperScope: typeof PaperScope; + Item: typeof Item; + Group: typeof Group; + Layer: typeof Layer; + Shape: typeof Shape; + Raster: typeof Raster; + PlacedSymbol: typeof PlacedSymbol; + PathItem: typeof PathItem; + Path: typeof Path; + CompoundPath: typeof CompoundPath; + Segment: typeof Segment; + Curve: typeof Curve; + CurveLocation: typeof CurveLocation; + Project: typeof Project; + Symbol: typeof Symbol; + Style: typeof Style; + Color: typeof Color; + Gradient: typeof Gradient; + GradientStop: typeof GradientStop; + View: typeof View; + Tool: typeof Tool; + TextItem: typeof TextItem; + PointText: typeof PointText; } export interface IHitTestOptions{ @@ -1288,9 +1319,9 @@ declare module paper { tolerance?: number; /** - * only hit-test again a certain item class and its sub-classes: Group, Layer, Path, CompoundPath, Shape, Raster, PlacedSymbol, PointText, etc. + * only hit-test against a certain item class and its sub-classes: Group, Layer, Path, CompoundPath, Shape, Raster, PlacedSymbol, PointText, etc. */ - class?: string; + class?: new(...args: any[]) => Item; /** * a match function to be called for each found hit result: Return true to return the result, false to keep searching @@ -1768,7 +1799,7 @@ declare module paper { * @param options.guides - hit-test items that have Item#guide set to true. * @param options.selected - only hit selected items. */ - hitTest(point: Point, options?: IHitTestOptions): HitResult; + hitTest(point: Point, options?: IHitTestOptions): HitResult | null; /** * Performs a hit-test on the item and its children (if it is a Group or Layer) at the location of the specified point, returning all found hits. @@ -4197,7 +4228,7 @@ declare module paper { * @param options.guides - hit-test items that have Item#guide set to true. * @param options.selected - only hit selected items. */ - hitTest(point: Point, options?: IHitTestOptions): HitResult; + hitTest(point: Point, options?: IHitTestOptions): HitResult | null; /** * Performs a hit-test on the item and its children (if it is a Group or Layer) at the location of the specified point, returning all found hits. diff --git a/types/paper/paper-tests.ts b/types/paper/paper-tests.ts index 78e6b2c4b5..6860037e70 100644 --- a/types/paper/paper-tests.ts +++ b/types/paper/paper-tests.ts @@ -39,14 +39,14 @@ dottedLineTool.onMouseUp = function(event: any) { // These objects are to make sure older code which didn't have the IHitTestOptions available still work. let hitOptionsEmpty = {}; let hitOptionsPartial = {tolerance: 0, extra: true}; -let hitOptionsFull = {tolerance: 0, class: 'Path', match: (hit: paper.HitResult)=>{return true;}, fill: true, stroke: false, segments: true, curves: false, handles: true, ends: true, position: false, center: true, bounds: true, guides: false, selected: true}; +let hitOptionsFull: paper.IHitTestOptions = {tolerance: 0, class: paper.Path, match: (hit: paper.HitResult)=>{return true;}, fill: true, stroke: false, segments: true, curves: false, handles: true, ends: true, position: false, center: true, bounds: true, guides: false, selected: true}; // These objects are to make sure new code which uses the IHitTestOptions work. let hitOptionsInterfaceEmpty:paper.IHitTestOptions = {}; let hitOptionsInterfacePartial:paper.IHitTestOptions = {match: (hit: paper.HitResult)=>{return true;}}; -let hitOptionsInterfaceFull:paper.IHitTestOptions = {tolerance: 0, class: 'Path', match: (hit: paper.HitResult)=>{return true;}, fill: true, stroke: false, segments: true, curves: false, handles: true, ends: true, position: false, center: true, bounds: true, guides: false, selected: true}; +let hitOptionsInterfaceFull:paper.IHitTestOptions = {tolerance: 0, class: paper.Path, match: (hit: paper.HitResult)=>{return true;}, fill: true, stroke: false, segments: true, curves: false, handles: true, ends: true, position: false, center: true, bounds: true, guides: false, selected: true}; let compoundPath: paper.CompoundPath = new paper.CompoundPath(dottedLinePath); let hitTestPoint = dottedLinePath.segments[0].point; -let hitTestResult: paper.HitResult; +let hitTestResult: paper.HitResult | null; let hitTestResults: paper.HitResult[]; // These are Item hit tests hitTestResult = compoundPath.hitTest(hitTestPoint); @@ -85,6 +85,21 @@ paper.settings.insertItems = true const paperScope = new paper.PaperScope(); paperScope.settings.insertItems = false; +// When multiple paper scopes may be in play you have to use the classes from inside +// the right scope to create new objects rather than the global classes from the +// module's default export, +const scopedRectangle: paper.Rectangle = new paperScope.Rectangle(2,2,7,7); +const scopedPoint: paper.Point = new paperScope.Point(25, 25); +const scopedPath: paper.Path = new paperScope.Path.Line(new paperScope.Point(0,0), scopedPoint); +const scopedTool: paper.Tool = new paperScope.Tool(); +const scopedMatrix: paper.Matrix = new paperScope.Matrix(1,2,3,4,5,6); +const scopedLayer: paper.Layer = new paperScope.Layer([]); +const scopedShape: paper.Shape = paperScope.Shape.Circle(new paperScope.Point(20,20),5); +const scopedRaster: paper.Raster = new paperScope.Raster('http://github.com/favicon.png'); +const scopedProject: paper.Project = new paperScope.Project('id'); +const scopedColor: paper.Color = new paperScope.Color(255, 255, 255); +const scopedPointText: paper.PointText = new paperScope.PointText(new paperScope.Point(1,1)); + function Examples() { function BooleanOperations(){ let text = new paper.PointText({