From 449ffd748eb7452f68263968c759e974ff5d8696 Mon Sep 17 00:00:00 2001 From: Blake Wilson Date: Wed, 7 Apr 2021 12:37:20 -0500 Subject: [PATCH] Temporarily use manually defined types * rename setTextBlock method to setText * Prepare 1.0.3 release --- README.md | 4 ++-- __tests__/nodeCanvasTest.ts | 2 +- package.json | 7 +++--- src/CanvasTextBlock.ts | 18 +++------------ tsconfig.json | 4 ++-- types/CanvasTextBlock.d.ts | 44 +++++++++++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+), 23 deletions(-) create mode 100644 types/CanvasTextBlock.d.ts diff --git a/README.md b/README.md index e03f265..a005fe4 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ canvas.width = 1000; canvas.height = 1000; // Init the package. -const canvasTextBlock = new CanvasTextBlock( +const textBlock = new CanvasTextBlock( canvas, // An HTML Canvas Element 100, // The x position of the canvas where the text block should start 100, // The y position of the canvas where the text block should start @@ -64,7 +64,7 @@ const canvasTextBlock = new CanvasTextBlock( ); // Set the text -canvasTextBlock.setTextBlock( +textBlock.setText( "This text block will be rendered in the region specified in the constructor above" ); ``` diff --git a/__tests__/nodeCanvasTest.ts b/__tests__/nodeCanvasTest.ts index a21ee0c..658b150 100644 --- a/__tests__/nodeCanvasTest.ts +++ b/__tests__/nodeCanvasTest.ts @@ -5,5 +5,5 @@ test("it should work with Node Canvas", () => { const canvas = createCanvas(1000, 1000); const instance = new CreateTextBlock(canvas, 100, 100, 800, 800); - instance.setTextBlock("This is the text block message"); + instance.setText("This is the text block message"); }); diff --git a/package.json b/package.json index 7735dfb..beb8719 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,18 @@ { "name": "canvas-text-block", - "version": "1.0.2", + "version": "1.0.3", "description": "Easily render a wrapping block of text in an HTML Canvas Element.", "main": "dist/CanvasTextBlock.common.js", "module": "dist/CanvasTextBlock.esm.js", "browser": "dist/CanvasTextBlock.js", - "types": "dist/CanvasTextBlock.d.ts", + "types": "types/CanvasTextBlock.d.ts", "homepage": "https://github.com/blakewilson/canvas-text-block", "files": [ "src", "dist", "LICENSE", - "README.md" + "README.md", + "types" ], "scripts": { "test": "jest", diff --git a/src/CanvasTextBlock.ts b/src/CanvasTextBlock.ts index e8ed8ee..a472c24 100644 --- a/src/CanvasTextBlock.ts +++ b/src/CanvasTextBlock.ts @@ -1,4 +1,4 @@ -import { CanvasTextBlockOptions } from "./types"; +import type { CanvasTextBlockOptions } from "./types"; import type { Canvas as NodeCanvas, CanvasRenderingContext2D as NodeCanvasRenderingContext2D, @@ -23,7 +23,7 @@ class CanvasTextBlock { private options: Required; constructor( - canvas: any, + canvas: HTMLCanvasElement | NodeCanvas, x: number, y: number, width: number, @@ -75,23 +75,14 @@ class CanvasTextBlock { } } - /** - * Get the max width of the text block. - */ private getTextBlockMaxWidth = () => { return this.width; }; - /** - * Get the options used for the canvas text block - */ private getOptions = () => { return this.options; }; - /** - * Get the max amount of lines possible in the text block. - */ private getMaxLineCount = (): number => { return Math.floor(this.height / this.options.lineHeight); }; @@ -103,10 +94,7 @@ class CanvasTextBlock { } }; - /** - * Set the text in the text block region. - */ - setTextBlock = (text: string) => { + setText = (text: string) => { const lines = this.getLinesFromText(text); // Loop through each line and render then to the canvas. diff --git a/tsconfig.json b/tsconfig.json index 19eb1a8..65fb83c 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,10 +6,10 @@ "moduleResolution": "node", "strictNullChecks": true, "esModuleInterop": true, + "allowSyntheticDefaultImports": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, - "declaration": true + "declaration": false }, "include": ["src"], - "exclude": ["__tests__"] } diff --git a/types/CanvasTextBlock.d.ts b/types/CanvasTextBlock.d.ts new file mode 100644 index 0000000..14aa443 --- /dev/null +++ b/types/CanvasTextBlock.d.ts @@ -0,0 +1,44 @@ +import type { Canvas as NodeCanvas } from "canvas"; + +export = CanvasTextBlock; + +declare class CanvasTextBlock { + /** + * Create a text block instance + * + * @param canvas The canvas element + * @param x The x-axis position in pixels where the text block should start + * @param y The y-axis position in pixels where the text block should start + * @param width The width of the text block in pixels + * @param height The height of the text block in pixels + * @param options + */ + constructor( + canvas: HTMLCanvasElement | NodeCanvas, + x: number, + y: number, + width: number, + height: number, + options: CanvasTextBlock.CanvasTextBlockOptions + ); + + /** + * Set the text of the text block instance. + * + * @param text + */ + setText(text: string): void; +} + +declare namespace CanvasTextBlock { + export interface CanvasTextBlockOptions { + color?: string; + backgroundColor?: string; + fontFamily?: string; + fontSize?: number; + lineHeight?: number; + weight?: string; + ellipsis?: boolean; + overflow?: boolean; + } +}