From 64988f6c06a46ae08273b46c97dc3474246023f4 Mon Sep 17 00:00:00 2001 From: Eugene Obrezkov Date: Sun, 10 May 2020 14:54:30 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20DeckBuilder=20can=20acce?= =?UTF-8?q?pt=20a=20predefined=20shapes\animations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DeckBuilder.start() method can accept a predefined set of shapes and animations created before. Moreover, it infers the names of the shapes and animations and can suggest autocompletion for slide builders inside of deck builder chaining. BREAKING CHANGE: 🧨 DeckBuilder has no longer methods withShape(), withAnimation(). If you want to add a "global" shape, you can create a predefined set of shapes\animations and pass them into DeckBuilder.start(shapes, animations). --- packages/kittik-deck/spec/DeckBuilder.spec.ts | 50 +++++++++++++------ packages/kittik-deck/src/DeckBuilder.ts | 33 ++++++------ 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/packages/kittik-deck/spec/DeckBuilder.spec.ts b/packages/kittik-deck/spec/DeckBuilder.spec.ts index 5d15d89..2245a89 100644 --- a/packages/kittik-deck/spec/DeckBuilder.spec.ts +++ b/packages/kittik-deck/spec/DeckBuilder.spec.ts @@ -1,4 +1,4 @@ -import { AnimationBuilder, ShapeBuilder, SlideBuilder } from '../src/Deck'; +import { AnimationBuilder, ShapeBuilder } from '../src/Deck'; import { Canvas } from 'terminal-canvas'; import { DeckBuilder } from '../src/DeckBuilder'; @@ -6,29 +6,51 @@ describe('deck builder', () => { it('should properly create deck using DeckBuilder', () => { expect.hasAssertions(); + const PREDEFINED_SHAPES = { 'Test Shape': ShapeBuilder.start('Text').end() }; + const PREDEFINED_ANIMATIONS = { 'Test Animation': AnimationBuilder.start('Focus').end() }; const deck = DeckBuilder - .start() + .start(PREDEFINED_SHAPES, PREDEFINED_ANIMATIONS) .withCanvas(Canvas.create()) - .withAnimation( - 'Test Animation', - AnimationBuilder - .start('Focus') - .end() - ) - .withShape( - 'Test Shape', - ShapeBuilder - .start('Text') + .withSlide( + (builder) => builder + .withName('Slide #1') + .withOrder('Test Shape', ['Test Animation']) .end() ) .withSlide( - SlideBuilder - .start() + (builder) => builder + .withName('Slide #2') + .withAnimation('Local Animation', AnimationBuilder.start('Focus').end()) + .withOrder('Test Shape', ['Test Animation', 'Local Animation']) .end() ) .end(); expect(deck.canvas).toBeInstanceOf(Canvas); + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + const [firstSlide, secondSlide] = deck.slides; + + expect(firstSlide.shapes.size).toBe(1); + expect(firstSlide.shapes.has('Test Shape')).toBe(true); + expect(firstSlide.animations.size).toBe(1); + expect(firstSlide.animations.has('Test Animation')).toBe(true); + expect(firstSlide.order).toStrictEqual([{ + shape: 'Test Shape', + animations: ['Test Animation'] + }]); + + expect(secondSlide.shapes.size).toBe(1); + expect(secondSlide.shapes.has('Test Shape')).toBe(true); + expect(secondSlide.animations.size).toBe(2); + expect(secondSlide.animations.has('Test Animation')).toBe(true); + expect(secondSlide.animations.has('Local Animation')).toBe(true); + expect(secondSlide.order).toStrictEqual([{ + shape: 'Test Shape', + animations: ['Test Animation', 'Local Animation'] + }]); + deck.exit(); }); }); diff --git a/packages/kittik-deck/src/DeckBuilder.ts b/packages/kittik-deck/src/DeckBuilder.ts index be5f9b8..9de2b0b 100644 --- a/packages/kittik-deck/src/DeckBuilder.ts +++ b/packages/kittik-deck/src/DeckBuilder.ts @@ -1,35 +1,36 @@ +import { Slide, SlideBuilder } from 'kittik-slide'; import { Animationable } from 'kittik-animation-basic'; import { Canvas } from 'terminal-canvas'; import { Deck } from './Deck'; import { ShapeRenderable } from 'kittik-shape-basic'; -import { Slide } from 'kittik-slide'; -export class DeckBuilder { +export class DeckBuilder { private readonly deck: Deck = new Deck(); + private readonly shapes: Record; + private readonly animations: Record; - public static start (): DeckBuilder { - return new this(); + public constructor (shapes: Record = {}, animations: Record = {}) { + this.shapes = shapes; + this.animations = animations; } - public withCanvas (canvas: Canvas): this { - this.deck.canvas = canvas; - - return this; + public static start ( + shapes?: Record, + animations?: Record + ): DeckBuilder { + return new this(shapes, animations); } - public withShape (name: string, shape: ShapeRenderable): this { - this.deck.addShape(name, shape); + public withCanvas (canvas: Canvas): this { + this.deck.canvas = canvas; return this; } - public withAnimation (name: string, animation: Animationable): this { - this.deck.addAnimation(name, animation); - - return this; - } + public withSlide (fn: (builder: SlideBuilder) => Slide): this { + const builder = SlideBuilder.start(this.shapes, this.animations); + const slide = fn(builder); - public withSlide (slide: Slide): this { this.deck.addSlide(slide); return this;