From 2dbebb87d6b45d099db571958e28e2a010df3589 Mon Sep 17 00:00:00 2001 From: Joost De Cock Date: Sun, 22 Jul 2018 20:43:42 +0200 Subject: [PATCH] :construction: Wrestling with TypeScript --- lib/attributes.ts | 2 +- lib/hooks.ts | 18 ------------------ lib/part.ts | 2 +- lib/pattern.ts | 1 + lib/point.ts | 2 +- lib/types.ts | 9 +++++++++ lib/utils.ts | 4 +++- 7 files changed, 16 insertions(+), 22 deletions(-) diff --git a/lib/attributes.ts b/lib/attributes.ts index dbd29c468aa..ba3bdba08a8 100644 --- a/lib/attributes.ts +++ b/lib/attributes.ts @@ -1,7 +1,7 @@ export class Attributes { list: any = {}; - constructor(init) { + constructor(init?) { for (let key in init) { let val = init[key]; this.add(key, val); diff --git a/lib/hooks.ts b/lib/hooks.ts index 0195f65b57f..23b438301f7 100644 --- a/lib/hooks.ts +++ b/lib/hooks.ts @@ -7,17 +7,6 @@ export class Hooks { this.all = ['preRenderSvg', 'postRenderSvg']; } - ///** Add hook */ - //on(hook, method): void { - // // This gets called from the pattern context - // // so 'this' is not actually this class - // let self = this.hooks; - // if(typeof self._hooks[method] === 'undefined') { - // self._hooks[hook] = []; - // } - // self._hooks[hook].push(method); - //} - list(hook): function[] { if(typeof this._hooks[hook] === 'undefined') { return false; @@ -26,13 +15,6 @@ export class Hooks { return this._hooks[hook]; } - attachPre (hook: string, obj: object): void { - this._attach('pre', hook, obj); - } - attachPost (hook: string, obj: object): void { - this._attach('post', hook, obj); - } - attach (hook: string, obj: object): void { if(typeof this._hooks[hook] === 'undefined') return; for(let func of this._hooks[hook]) { diff --git a/lib/part.ts b/lib/part.ts index ee2d0f9ee17..a1999b26488 100644 --- a/lib/part.ts +++ b/lib/part.ts @@ -26,7 +26,7 @@ export class Part { return this; } - macroRunner(args) { + macroRunner(args?) { let self = this; let data = args; let method = function (key, data) { diff --git a/lib/pattern.ts b/lib/pattern.ts index 8f895f52cfc..84dff11b1b6 100644 --- a/lib/pattern.ts +++ b/lib/pattern.ts @@ -64,6 +64,7 @@ export class Pattern { render(): string { this.hooks.attach('preRenderSvg', this.svg); this.hooks.attach('postRenderSvg', this.svg); + //this.hooks.attach('insertText', this.svg); return this.svg.render(this); } diff --git a/lib/point.ts b/lib/point.ts index ccba1eb6038..1b7a058815f 100644 --- a/lib/point.ts +++ b/lib/point.ts @@ -14,7 +14,7 @@ export class Point { } /** Adds an attribute. This is here to make this call chainable in assignment */ - attr(name, value): Point { + attr(name: string, value: string): Point { this.attributes.add(name, value); return this; diff --git a/lib/types.ts b/lib/types.ts index 7626ddbb6b5..7880ce6d223 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,3 +1,11 @@ +export type context = { + settings: {[propName: string]: any}; + options: {[propName: string]: any}; + values: {[propName: string]: any}; +} + +///////////////////////////////////////////////////////////// +/* export type PatternOptionType = "measure" | "percentage" | "angle" | "choice" | "constant"; export interface PatternOption { @@ -63,3 +71,4 @@ declare namespace Pattern { [propName: string]: any; } } +*/ diff --git a/lib/utils.ts b/lib/utils.ts index e5a7e770d3c..9417fcabcc4 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,4 +1,6 @@ +import { Part } from './part' import { Point } from './point' +import {context} from './types' /** Rounds a value to PRECISION */ export function round(value: number): number { @@ -70,7 +72,7 @@ export function beamCrossesY(from: Point, to: Point, y: number) { } /** Returns an object with shorthand access for pattern design */ -export function shorthand(part, context): {} { +export function shorthand(part: Part, context: context): {} { let final = (context.settings.mode === 'draft') ? true : false; let paperless = (context.settings.paperless === true) ? true : false; return {