From c2d224520a104c34b2681a92478004c69b44b7e3 Mon Sep 17 00:00:00 2001 From: Koldo Picaza <1093654+kpicaza@users.noreply.github.com> Date: Tue, 24 Oct 2023 20:32:28 +0200 Subject: [PATCH] Store drawing palette and brush --- README.md | 2 +- app/Listeners/OpenDocumentWindow.php | 13 +- infection.log | 174 ++++++++++++------ .../js/src/components/canvas/SketchCanvas.ts | 29 ++- .../components/drawing-tools/BrushOptions.ts | 70 +++++-- .../components/sketch-book/SketchNavigator.ts | 2 +- resources/js/src/domain/model/Brush.ts | 3 +- resources/js/src/domain/model/DrawingTool.ts | 15 +- resources/js/src/domain/model/Palette.ts | 7 + resources/js/src/domain/model/SketchBook.ts | 26 ++- resources/js/src/pages/OpenSketch.ts | 74 ++++---- .../drawing-tools/BrushOptions.test.ts | 20 +- .../sketch-book/SketchNavigator.test.ts | 39 ++-- resources/js/test/pages/OpenSketch.test.ts | 7 +- .../Domain/Command/SaveSketchBookCommand.php | 47 ++++- .../Domain/Handler/CreateNewSketchBook.php | 5 +- .../Domain/Handler/SaveSketchBook.php | 3 +- src/SketchBook/Domain/Model/Brush.php | 34 ++++ src/SketchBook/Domain/Model/Palette.php | 55 ++++++ src/SketchBook/Domain/Model/Sketch.php | 8 +- src/SketchBook/Domain/Model/SketchBook.php | 83 ++++++--- .../Infrastructure/Http/PutSketchBook.php | 3 +- .../FileSystemSketchBookRepository.php | 20 +- tests/Feature/ExportSketchTest.php | 12 +- tests/Feature/GetSketchBookTest.php | 24 ++- tests/Feature/OpenExistingSketchBookTest.php | 24 ++- tests/Feature/SaveSketchBookTest.php | 24 ++- .../Domain/Handler/DownloadSketchTest.php | 5 +- .../Handler/ResetSketchBookLocationTest.php | 10 +- .../Domain/Handler/SaveSketchBookTest.php | 11 +- .../Domain/Model/SketchBookTest.php | 15 +- 31 files changed, 608 insertions(+), 256 deletions(-) create mode 100644 resources/js/src/domain/model/Palette.ts create mode 100644 src/SketchBook/Domain/Model/Brush.php create mode 100644 src/SketchBook/Domain/Model/Palette.php diff --git a/README.md b/README.md index 37b2bc6..02f52e9 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ provides the tools you need. * [x] Brush Color * [x] Recent used brush color palette * [x] Brush Size -* [ ] Store Drawing settings +* [x] Store Drawing settings ### Sketch Book diff --git a/app/Listeners/OpenDocumentWindow.php b/app/Listeners/OpenDocumentWindow.php index c284763..e6aebcc 100644 --- a/app/Listeners/OpenDocumentWindow.php +++ b/app/Listeners/OpenDocumentWindow.php @@ -13,11 +13,15 @@ use OpenSketch\SketchBook\Domain\Handler\CreateNewSketchBook; use OpenSketch\SketchBook\Domain\Handler\ResetSketchBookLocation; use OpenSketch\SketchBook\Domain\Handler\SaveSketchBook; +use OpenSketch\SketchBook\Domain\Model\SketchBook; use OpenSketch\Window\Domain\Command\OpenWindowCommand; use OpenSketch\Window\Domain\Handler\OpenFileDialog; use OpenSketch\Window\Domain\Handler\OpenWindow; use Ramsey\Uuid\Uuid; +/** + * @phpstan-import-type SketchBookNormalized from SketchBook + */ final class OpenDocumentWindow { private const JSON_DEPTH = 512; @@ -42,11 +46,7 @@ public function handle(DocumentOpened $event): void } /** - * @var array{ - * id: string, - * sketches: array, - * background: string|null - * } $sketchBookData + * @var SketchBookNormalized $sketchBookData */ $sketchBookData = json_decode( $fileContents, @@ -73,7 +73,8 @@ public function handle(DocumentOpened $event): void SaveSketchBookCommand::from( $sketchBookId, $sketchBookData['sketches'], - $sketchBookData['background'] ?? '#ffffff', + $sketchBookData['brush'], + $sketchBookData['palette'], ) ); } diff --git a/infection.log b/infection.log index af6bbf9..02024fe 100644 --- a/infection.log +++ b/infection.log @@ -9,7 +9,7 @@ Escaped mutants: } catch (MissedSketchBookReference) { $sketchBookId = Uuid::uuid4()->toString(); $this->createNewSketchBook->handle(CreateNewSketchBookCommand::withIdAndPath($sketchBookId, $path)); -- $this->saveSketchBook->handle(SaveSketchBookCommand::from($sketchBookId, $sketchBookData['sketches'], $sketchBookData['background'] ?? '#ffffff')); +- $this->saveSketchBook->handle(SaveSketchBookCommand::from($sketchBookId, $sketchBookData['sketches'], $sketchBookData['brush'], $sketchBookData['palette'])); + } $this->openWindow->handle(new OpenWindowCommand($sketchBookId, 'sketch-book', $path, 'sketch-book')); @@ -17,66 +17,98 @@ Escaped mutants: } -2) /home/kpicaza/server/open.sketch/dev/app/Listeners/OpenDocumentWindow.php:76 [M] Coalesce +2) /home/kpicaza/server/open.sketch/dev/app/Listeners/OpenDocumentWindow.php:82 [M] MethodCallRemoval --- Original +++ New @@ @@ - } catch (MissedSketchBookReference) { - $sketchBookId = Uuid::uuid4()->toString(); $this->createNewSketchBook->handle(CreateNewSketchBookCommand::withIdAndPath($sketchBookId, $path)); -- $this->saveSketchBook->handle(SaveSketchBookCommand::from($sketchBookId, $sketchBookData['sketches'], $sketchBookData['background'] ?? '#ffffff')); -+ $this->saveSketchBook->handle(SaveSketchBookCommand::from($sketchBookId, $sketchBookData['sketches'], '#ffffff' ?? $sketchBookData['background'])); + $this->saveSketchBook->handle(SaveSketchBookCommand::from($sketchBookId, $sketchBookData['sketches'], $sketchBookData['brush'], $sketchBookData['palette'])); } - $this->openWindow->handle(new OpenWindowCommand($sketchBookId, 'sketch-book', $path, 'sketch-book')); +- $this->openWindow->handle(new OpenWindowCommand($sketchBookId, 'sketch-book', $path, 'sketch-book')); ++ } } -3) /home/kpicaza/server/open.sketch/dev/app/Listeners/OpenDocumentWindow.php:81 [M] MethodCallRemoval +3) /home/kpicaza/server/open.sketch/dev/app/Listeners/StoreDocument.php:37 [M] MethodCallRemoval --- Original +++ New @@ @@ - $this->createNewSketchBook->handle(CreateNewSketchBookCommand::withIdAndPath($sketchBookId, $path)); - $this->saveSketchBook->handle(SaveSketchBookCommand::from($sketchBookId, $sketchBookData['sketches'], $sketchBookData['background'] ?? '#ffffff')); + return; } -- $this->openWindow->handle(new OpenWindowCommand($sketchBookId, 'sketch-book', $path, 'sketch-book')); + $command = CreateNewSketchBookCommand::withIdAndPath(Uuid::uuid4()->toString(), $path); +- $this->createNewSketchBook->handle($command); + + $this->windowManager->handle(new OpenWindowCommand($command->sketchBookId, 'sketch-book', $path, 'sketch-book')); } } -4) /home/kpicaza/server/open.sketch/dev/app/Listeners/StoreDocument.php:37 [M] MethodCallRemoval +4) /home/kpicaza/server/open.sketch/dev/app/Listeners/StoreDocument.php:39 [M] MethodCallRemoval --- Original +++ New @@ @@ - return; } $command = CreateNewSketchBookCommand::withIdAndPath(Uuid::uuid4()->toString(), $path); -- $this->createNewSketchBook->handle($command); + $this->createNewSketchBook->handle($command); +- $this->windowManager->handle(new OpenWindowCommand($command->sketchBookId, 'sketch-book', $path, 'sketch-book')); + - $this->windowManager->handle(new OpenWindowCommand($command->sketchBookId, 'sketch-book', $path, 'sketch-book')); } } -5) /home/kpicaza/server/open.sketch/dev/app/Listeners/StoreDocument.php:39 [M] MethodCallRemoval +5) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Domain/Handler/SaveSketchBook.php:22 [M] MethodCallRemoval --- Original +++ New @@ @@ - } - $command = CreateNewSketchBookCommand::withIdAndPath(Uuid::uuid4()->toString(), $path); - $this->createNewSketchBook->handle($command); -- $this->windowManager->handle(new OpenWindowCommand($command->sketchBookId, 'sketch-book', $path, 'sketch-book')); + public function handle(SaveSketchBookCommand $command) : void + { + $sketchBook = $this->repository->get($command->sketchBookId); +- $sketchBook->setBrush($command->brush()); ++ + $sketchBook->setPalette($command->palette()); + $sketchBook->updateSketches(Sketch::fromNormalizedSketches($command->sketches)); + $this->repository->save($sketchBook); + } + } + + +6) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Domain/Handler/SaveSketchBook.php:23 [M] MethodCallRemoval + +--- Original ++++ New +@@ @@ + { + $sketchBook = $this->repository->get($command->sketchBookId); + $sketchBook->setBrush($command->brush()); +- $sketchBook->setPalette($command->palette()); + + $sketchBook->updateSketches(Sketch::fromNormalizedSketches($command->sketches)); + $this->repository->save($sketchBook); } } -6) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Domain/Model/SketchBook.php:90 [M] UnwrapStrReplace +7) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Domain/Model/SketchBook.php:36 [M] LogicalNot + +--- Original ++++ New +@@ @@ + } + public static function new(string $sketchBookId, string $storagePath) : self + { +- if (!str_ends_with($storagePath, '.json')) { ++ if (str_ends_with($storagePath, '.json')) { + $storagePath = sprintf('%s.json', $storagePath); + } + return new self($sketchBookId, $storagePath, [new Sketch(1, 'data:,')], Brush::default(), Palette::default()); + + +8) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Domain/Model/SketchBook.php:85 [M] UnwrapStrReplace --- Original +++ New @@ -91,7 +123,7 @@ Escaped mutants: { -7) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:29 [M] ArrayItemRemoval +9) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:34 [M] ArrayItemRemoval --- Original +++ New @@ -106,51 +138,79 @@ Escaped mutants: } -8) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:45 [M] DecrementInteger +10) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:44 [M] DecrementInteger --- Original +++ New @@ @@ - * background: string - * }|null $sketchBookSerialized - */ -- $sketchBookSerialized = json_decode($content, true, 512, JSON_THROW_ON_ERROR); -+ $sketchBookSerialized = json_decode($content, true, 511, JSON_THROW_ON_ERROR); - return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookSerialized['sketches'] ?? []), $sketchBookSerialized['background'] ?? '#ffffff'); + } + $content = Storage::get($sketchBookReference->storage_path) ?? '[]'; + /** @var SketchBookNormalized|null $sketchBookNormalized */ +- $sketchBookNormalized = json_decode($content, true, 512, JSON_THROW_ON_ERROR); ++ $sketchBookNormalized = json_decode($content, true, 511, JSON_THROW_ON_ERROR); + return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookNormalized['sketches'] ?? []), Brush::fromNormalized($sketchBookNormalized['brush'] ?? (array) Brush::default()), Palette::fromNormalized($sketchBookNormalized['palette'] ?? (array) Palette::default())); } } -9) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:45 [M] IncrementInteger +11) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:44 [M] IncrementInteger --- Original +++ New @@ @@ - * background: string - * }|null $sketchBookSerialized - */ -- $sketchBookSerialized = json_decode($content, true, 512, JSON_THROW_ON_ERROR); -+ $sketchBookSerialized = json_decode($content, true, 513, JSON_THROW_ON_ERROR); - return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookSerialized['sketches'] ?? []), $sketchBookSerialized['background'] ?? '#ffffff'); + } + $content = Storage::get($sketchBookReference->storage_path) ?? '[]'; + /** @var SketchBookNormalized|null $sketchBookNormalized */ +- $sketchBookNormalized = json_decode($content, true, 512, JSON_THROW_ON_ERROR); ++ $sketchBookNormalized = json_decode($content, true, 513, JSON_THROW_ON_ERROR); + return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookNormalized['sketches'] ?? []), Brush::fromNormalized($sketchBookNormalized['brush'] ?? (array) Brush::default()), Palette::fromNormalized($sketchBookNormalized['palette'] ?? (array) Palette::default())); } } -10) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:52 [M] Coalesce +12) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:51 [M] CastArray --- Original +++ New @@ @@ - * }|null $sketchBookSerialized - */ - $sketchBookSerialized = json_decode($content, true, 512, JSON_THROW_ON_ERROR); -- return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookSerialized['sketches'] ?? []), $sketchBookSerialized['background'] ?? '#ffffff'); -+ return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookSerialized['sketches'] ?? []), '#ffffff' ?? $sketchBookSerialized['background']); + $content = Storage::get($sketchBookReference->storage_path) ?? '[]'; + /** @var SketchBookNormalized|null $sketchBookNormalized */ + $sketchBookNormalized = json_decode($content, true, 512, JSON_THROW_ON_ERROR); +- return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookNormalized['sketches'] ?? []), Brush::fromNormalized($sketchBookNormalized['brush'] ?? (array) Brush::default()), Palette::fromNormalized($sketchBookNormalized['palette'] ?? (array) Palette::default())); ++ return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookNormalized['sketches'] ?? []), Brush::fromNormalized($sketchBookNormalized['brush'] ?? Brush::default()), Palette::fromNormalized($sketchBookNormalized['palette'] ?? (array) Palette::default())); } } -11) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelDialogProvider.php:24 [M] Coalesce +13) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:52 [M] CastArray + +--- Original ++++ New +@@ @@ + $content = Storage::get($sketchBookReference->storage_path) ?? '[]'; + /** @var SketchBookNormalized|null $sketchBookNormalized */ + $sketchBookNormalized = json_decode($content, true, 512, JSON_THROW_ON_ERROR); +- return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookNormalized['sketches'] ?? []), Brush::fromNormalized($sketchBookNormalized['brush'] ?? (array) Brush::default()), Palette::fromNormalized($sketchBookNormalized['palette'] ?? (array) Palette::default())); ++ return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookNormalized['sketches'] ?? []), Brush::fromNormalized($sketchBookNormalized['brush'] ?? (array) Brush::default()), Palette::fromNormalized($sketchBookNormalized['palette'] ?? Palette::default())); + } + } + + +14) /home/kpicaza/server/open.sketch/dev/src/SketchBook/Infrastructure/Persistence/FileSystemSketchBookRepository.php:52 [M] Coalesce + +--- Original ++++ New +@@ @@ + $content = Storage::get($sketchBookReference->storage_path) ?? '[]'; + /** @var SketchBookNormalized|null $sketchBookNormalized */ + $sketchBookNormalized = json_decode($content, true, 512, JSON_THROW_ON_ERROR); +- return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookNormalized['sketches'] ?? []), Brush::fromNormalized($sketchBookNormalized['brush'] ?? (array) Brush::default()), Palette::fromNormalized($sketchBookNormalized['palette'] ?? (array) Palette::default())); ++ return new SketchBook($sketchBookReference->id, $sketchBookReference->storage_path, Sketch::fromNormalizedSketches($sketchBookNormalized['sketches'] ?? []), Brush::fromNormalized($sketchBookNormalized['brush'] ?? (array) Brush::default()), Palette::fromNormalized((array) Palette::default() ?? $sketchBookNormalized['palette'])); + } + } + + +15) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelDialogProvider.php:24 [M] Coalesce --- Original +++ New @@ -165,7 +225,7 @@ Escaped mutants: public function save(string $title, string $path) : ?string -12) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelDialogProvider.php:28 [M] UnwrapStrReplace +16) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelDialogProvider.php:28 [M] UnwrapStrReplace --- Original +++ New @@ -180,7 +240,7 @@ Escaped mutants: { -13) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelDialogProvider.php:31 [M] Coalesce +17) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelDialogProvider.php:31 [M] Coalesce --- Original +++ New @@ -195,7 +255,7 @@ Escaped mutants: { -14) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelDialogProvider.php:42 [M] Coalesce +18) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelDialogProvider.php:42 [M] Coalesce --- Original +++ New @@ -209,7 +269,7 @@ Escaped mutants: } -15) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:17 [M] MethodCallRemoval +19) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:17 [M] MethodCallRemoval --- Original +++ New @@ -224,7 +284,7 @@ Escaped mutants: Window::open($command->openingWindowName)->title(sprintf('%s: %s', __('sketch_book'), $command->fileName()))->route($command->routeName, ['id' => $command->sketchBookId]); -16) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:19 [M] Ternary +20) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:19 [M] Identical --- Original +++ New @@ -233,7 +293,7 @@ Escaped mutants: Window::open('welcome'); /** @phpstan-ignore-next-line */ - Window::close(Window::current()->id === 'welcome' ? 'sketch-book' : Window::current()->id); -+ Window::close(Window::current()->id === 'welcome' ? Window::current()->id : 'sketch-book'); ++ Window::close(Window::current()->id !== 'welcome' ? 'sketch-book' : Window::current()->id); Window::open($command->openingWindowName)->title(sprintf('%s: %s', __('sketch_book'), $command->fileName()))->route($command->routeName, ['id' => $command->sketchBookId]); $window->maximize($command->openingWindowName); Window::close('welcome'); @@ -241,7 +301,7 @@ Escaped mutants: } -17) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:19 [M] MethodCallRemoval +21) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:19 [M] MethodCallRemoval --- Original +++ New @@ -259,7 +319,7 @@ Escaped mutants: } -18) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:19 [M] Identical +22) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:19 [M] Ternary --- Original +++ New @@ -268,7 +328,7 @@ Escaped mutants: Window::open('welcome'); /** @phpstan-ignore-next-line */ - Window::close(Window::current()->id === 'welcome' ? 'sketch-book' : Window::current()->id); -+ Window::close(Window::current()->id !== 'welcome' ? 'sketch-book' : Window::current()->id); ++ Window::close(Window::current()->id === 'welcome' ? Window::current()->id : 'sketch-book'); Window::open($command->openingWindowName)->title(sprintf('%s: %s', __('sketch_book'), $command->fileName()))->route($command->routeName, ['id' => $command->sketchBookId]); $window->maximize($command->openingWindowName); Window::close('welcome'); @@ -276,7 +336,7 @@ Escaped mutants: } -19) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:22 [M] MethodCallRemoval +23) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:22 [M] MethodCallRemoval --- Original +++ New @@ -292,7 +352,7 @@ Escaped mutants: } -20) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:24 [M] ArrayItemRemoval +24) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:24 [M] ArrayItemRemoval --- Original +++ New @@ -308,7 +368,7 @@ Escaped mutants: } -21) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:25 [M] ArrayItem +25) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:25 [M] ArrayItem --- Original +++ New @@ -324,7 +384,7 @@ Escaped mutants: } -22) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:28 [M] MethodCallRemoval +26) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:28 [M] MethodCallRemoval --- Original +++ New @@ -339,7 +399,7 @@ Escaped mutants: } -23) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:30 [M] MethodCallRemoval +27) /home/kpicaza/server/open.sketch/dev/src/Window/Infrastructure/Electron/LaravelWindowProvider.php:30 [M] MethodCallRemoval --- Original +++ New diff --git a/resources/js/src/components/canvas/SketchCanvas.ts b/resources/js/src/components/canvas/SketchCanvas.ts index 9efb4da..0620b78 100644 --- a/resources/js/src/components/canvas/SketchCanvas.ts +++ b/resources/js/src/components/canvas/SketchCanvas.ts @@ -1,8 +1,7 @@ import {LitElement, css, html} from "lit"; import {customElement, property, query} from "lit/decorators.js"; import {consume} from "@lit/context"; -import {brushContext, featuresContext, sketchBookContext} from "../../store/AppContext.js"; -import {Brush} from "../../domain/model/Brush.js"; +import {featuresContext, sketchBookContext} from "../../store/AppContext.js"; import {DrawingTool} from "../../domain/model/DrawingTool.js"; import {ToggleRouter} from "../../services/ToggleRouter.js"; import {SketchBook} from "../../domain/model/SketchBook.js"; @@ -52,10 +51,6 @@ export class SketchCanvas extends LitElement { @query(".cursor") cursor!: HTMLDivElement; - @consume({context: brushContext, subscribe: true}) - @property({attribute: false}) - brush: Brush - @consume({context: sketchBookContext, subscribe: true}) @property({attribute: false}) sketchBook: SketchBook @@ -83,7 +78,7 @@ export class SketchCanvas extends LitElement { this.canvasHeight = this.parentElement!.offsetHeight - 50; this.drawingTool = new DrawingTool(this.canvas.getContext("2d")) this.drawingTool.clearCanvas(this.canvasWidth, this.canvasHeight, this.image); - this.drawingTool.setBrush(this.brush); + this.drawingTool.setBrushAndPalette(this.sketchBook.brush, this.sketchBook.palette); this.canvasBackgroundColor = this.features!.isEnabled('canvas-background-color') if (this.canvasBackgroundColor) { this.canvas.classList.remove('white-background') @@ -94,12 +89,12 @@ export class SketchCanvas extends LitElement { protected async updated(_changedProperties) { super.updated(_changedProperties); - this.drawingTool.setBrush(this.brush); + this.drawingTool.setBrushAndPalette(this.sketchBook.brush, this.sketchBook.palette); const image = _changedProperties.get('image'); if (typeof image === 'string') { this.image = new URL(this.image); } - if (this.resetCanvas === true) { + if (this.resetCanvas) { this.canvasWidth = this.offsetWidth; this.canvasHeight = this.parentElement!.offsetHeight - 50; this.drawingTool.clearCanvas(this.canvasWidth, this.canvasHeight, this.image); @@ -112,12 +107,12 @@ export class SketchCanvas extends LitElement { } protected moveCursor(event: MouseEvent) { - const cursorWidth = this.brush.lineWidth > 3 ? this.brush.lineWidth : 3; + const cursorWidth = this.sketchBook.brush.width > 3 ? this.sketchBook.brush.width : 3; this.cursor.style.width = `${cursorWidth}px`; this.cursor.style.height = `${cursorWidth}px`; - this.cursor.style.background = this.brush.color; - const mouseY = event.offsetY - (this.brush.lineWidth / 2); - const mouseX = event.offsetX - (this.brush.lineWidth / 2); + this.cursor.style.background = this.sketchBook.palette.primaryColor; + const mouseY = event.offsetY - (this.sketchBook.brush.width / 2); + const mouseX = event.offsetX - (this.sketchBook.brush.width / 2); this.cursor.style.transform = `translate3d(${mouseX}px, ${mouseY}px, 0)`; } @@ -127,8 +122,8 @@ export class SketchCanvas extends LitElement { } protected startDrawing(event: MouseEvent) { - if (!this.drawingTool.hasCurrentBrush(this.brush)) { - this.drawingTool.setBrush(this.brush); + if (!this.drawingTool.hasCurrentBrushAndPalette(this.sketchBook.brush, this.sketchBook.palette)) { + this.drawingTool.setBrushAndPalette(this.sketchBook.brush, this.sketchBook.palette); } this.drawingTool.startDrawing(event.offsetX, event.offsetY); @@ -147,7 +142,7 @@ export class SketchCanvas extends LitElement { detail: { id: this.sketchId, image: new URL(this.canvas.toDataURL("image/png")), - background: this.sketchBook.background, + background: this.sketchBook.palette.backgroundColor, } } )) @@ -157,7 +152,7 @@ export class SketchCanvas extends LitElement { if (this.canvasBackgroundColor) { return html` + - ${this.renderSecondaryColorPickers()} ` } @@ -283,19 +317,19 @@ export class BrushOptions extends LitElement { .value=${this.lineWidthInput} > , - background: string, + brush: Brush, + palette: Palette, } + +export const defaultSketchBook = (): SketchBook => ({ + id: "", + sketches: [ + { + id: 1, + image: new URL("data:,"), + } + ], + brush: { + width: 3, + type: 'pen' + }, + palette: { + primaryColor: '#484545', + backgroundColor: '#ffffff', + secondaryColor1: '#527474', + secondaryColor2: '#844ab2', + secondaryColor3: '#e89cb9', + }, + } as SketchBook) diff --git a/resources/js/src/pages/OpenSketch.ts b/resources/js/src/pages/OpenSketch.ts index b601f8c..8f7ed47 100644 --- a/resources/js/src/pages/OpenSketch.ts +++ b/resources/js/src/pages/OpenSketch.ts @@ -6,11 +6,10 @@ import {MdIconButton} from "@material/web/iconbutton/icon-button.js"; import "@material/web/iconbutton/filled-icon-button.js"; import {loadSketchBook, saveSketchBook, downloadSketch} from "../store/SketchBookState.js"; import {featuresAvailable} from "../store/FeatureFlags.js"; -import {brushContext, featuresContext, sketchBookContext} from "../store/AppContext.js"; +import {featuresContext, sketchBookContext} from "../store/AppContext.js"; import {ToggleRouter} from "../services/ToggleRouter.js"; -import {SketchBook} from "../domain/model/SketchBook.js"; +import {defaultSketchBook, SketchBook} from "../domain/model/SketchBook.js"; import {Sketch} from "../domain/model/Sketch.js"; -import {Brush} from "../domain/model/Brush.js"; import "../components/canvas/SketchCanvas.js"; import "../components/settings/SettingsMenu.js"; import "../components/sketch-book/AddSketch.js"; @@ -95,24 +94,11 @@ export class OpenSketch extends LitElement { } `; - @provide({context: brushContext}) brush: Brush = { - lineWidth: 3, - color: '#484545', - type: 'pen' - } - - @provide({context: sketchBookContext}) sketchBook: SketchBook = { - id: "", - sketches: [ - { - id: 1, - image: new URL("data:,"), - } - ], - background: '#e8f6f1' - } + @provide({context: sketchBookContext}) + sketchBook: SketchBook = defaultSketchBook(); - @provide({context: featuresContext}) features!: ToggleRouter = new ToggleRouter([]); + @provide({context: featuresContext}) + features!: ToggleRouter = new ToggleRouter([]); @query("painting-board") sketchWrapper: HTMLDivElement; @@ -174,7 +160,8 @@ export class OpenSketch extends LitElement { this.sketchBook = { id: this.sketchBook.id, sketches, - background: this.sketchBook.background + brush: this.sketchBook.brush, + palette: this.sketchBook.palette, }; const body = this.parentElement.parentElement; @@ -195,7 +182,8 @@ export class OpenSketch extends LitElement { this.sketchBook = { id: this.sketchBook.id, sketches, - background: event.detail.background + brush: this.sketchBook.brush, + palette: this.sketchBook.palette, }; await saveSketchBook(this.sketchBook); @@ -213,7 +201,8 @@ export class OpenSketch extends LitElement { this.sketchBook = { id: this.sketchBook.id, sketches: newSketches, - background: this.sketchBook.background + brush: this.sketchBook.brush, + palette: this.sketchBook.palette, }; this.resetCanvas = true; @@ -224,26 +213,24 @@ export class OpenSketch extends LitElement { downloadSketch(this.sketchBookId, event.detail) } - protected changeBrushLineWidth(event: CustomEvent) { - this.brush.lineWidth = event.detail - } - - protected changeBrushColor(event: CustomEvent) { - this.brush.color = event.detail; - } - - protected async changeBackgroundColor(event: CustomEvent) { + private async changeBrush(event: CustomEvent) { this.sketchBook = { id: this.sketchBook.id, sketches: this.sketchBook.sketches, - background: event.detail + brush: event.detail, + palette: this.sketchBook.palette, }; - await saveSketchBook(this.sketchBook); } - private changeBrush(event: CustomEvent) { - this.brush.type = event.detail; + protected async changePalette(event: CustomEvent) { + this.sketchBook = { + id: this.sketchBook.id, + sketches: this.sketchBook.sketches, + brush: this.sketchBook.brush, + palette: event.detail, + }; + await saveSketchBook(this.sketchBook); } private canvasReset() { @@ -283,12 +270,17 @@ export class OpenSketch extends LitElement { return html`