Skip to content

Commit

Permalink
feat(blocks): support block colors
Browse files Browse the repository at this point in the history
Closes #122
  • Loading branch information
mariosimao authored Apr 26, 2023
1 parent 37d98f3 commit e5735ea
Show file tree
Hide file tree
Showing 23 changed files with 394 additions and 74 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
"scripts": {
"ci:phpcs": "phpcs -q --report=checkstyle | cs2pr",
"ci:psalm": "psalm --output-format=github --shepherd --stats",
"ci:coverage": "paratest --coverage-clover dist/phpunit/clover.xml",
"ci:coverage": [
"Composer\\Config::disableProcessTimeout",
"paratest --coverage-clover dist/phpunit/clover.xml"
],
"ci:unit": "paratest --testsuite Unit",
"ci:integration": "paratest --testsuite Integration",
"ci:mutation": [
Expand All @@ -53,7 +56,7 @@
],
"test:phpcs": "phpcs",
"test:psalm": "psalm --no-cache",
"test:unit": "paratest --testsuite Unit",
"test:unit": "phpunit --testsuite Unit",
"test:integration": "paratest --testsuite Integration",
"test:coverage": "paratest --coverage-html dist/phpunit/html && echo \"Open the result on your browser: $PWD/dist/phpunit/html/index.html\""
},
Expand Down
29 changes: 24 additions & 5 deletions src/Blocks/BulletedListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Notion\Blocks;

use Notion\Common\Color;
use Notion\Common\RichText;

/**
Expand All @@ -13,6 +14,7 @@
* @psalm-type BulletedListItemJson = array{
* bulleted_list_item: array{
* rich_text: list<RichTextJson>,
* color?: string,
* children?: list<BlockMetadataJson>,
* },
* }
Expand All @@ -28,6 +30,7 @@ class BulletedListItem implements BlockInterface
private function __construct(
private readonly BlockMetadata $metadata,
public readonly array $text,
public readonly Color $color,
public readonly array $children,
) {
$this->metadata->checkType(BlockType::BulletedListItem);
Expand All @@ -40,7 +43,7 @@ public static function create(): self
{
$metadata = BlockMetadata::create(BlockType::BulletedListItem);

return new self($metadata, [], []);
return new self($metadata, [], Color::Default, []);
}

/**
Expand All @@ -51,7 +54,7 @@ public static function fromString(string $content): self
$metadata = BlockMetadata::create(BlockType::BulletedListItem);
$text = [ RichText::fromString($content) ];

return new self($metadata, $text, []);
return new self($metadata, $text, Color::Default, []);
}

public static function fromArray(array $array): self
Expand All @@ -64,9 +67,11 @@ public static function fromArray(array $array): self

$text = array_map(fn($t) => RichText::fromArray($t), $item["rich_text"]);

$color = Color::tryFrom($item["color"] ?? "") ?? Color::Default;

$children = array_map(fn($b) => BlockFactory::fromArray($b), $item["children"] ?? []);

return new self($metadata, $text, $children);
return new self($metadata, $text, $color, $children);
}

public function toArray(): array
Expand All @@ -75,6 +80,7 @@ public function toArray(): array

$array["bulleted_list_item"] = [
"rich_text" => array_map(fn(RichText $t) => $t->toArray(), $this->text),
"color" => $this->color->value,
"children" => array_map(fn(BlockInterface $b) => $b->toArray(), $this->children),
];

Expand All @@ -94,7 +100,7 @@ public function metadata(): BlockMetadata

public function changeText(RichText ...$text): self
{
return new self($this->metadata->update(), $text, $this->children);
return new self($this->metadata->update(), $text, $this->color, $this->children);
}

/**
Expand All @@ -105,7 +111,7 @@ public function addText(RichText $text): self
$texts = $this->text;
$texts[] = $text;

return new self($this->metadata, $texts, $this->children);
return new self($this->metadata, $texts, $this->color, $this->children);
}

public function changeChildren(BlockInterface ...$children): self
Expand All @@ -115,6 +121,7 @@ public function changeChildren(BlockInterface ...$children): self
return new self(
$this->metadata->updateHasChildren($hasChildren),
$this->text,
$this->color,
$children,
);
}
Expand All @@ -127,15 +134,27 @@ public function addChild(BlockInterface $child): self
return new self(
$this->metadata->updateHasChildren(true),
$this->text,
$this->color,
$children,
);
}

public function changeColor(Color $color): self
{
return new self(
$this->metadata->update(),
$this->text,
$color,
$this->children,
);
}

public function archive(): BlockInterface
{
return new self(
$this->metadata->archive(),
$this->text,
$this->color,
$this->children,
);
}
Expand Down
32 changes: 26 additions & 6 deletions src/Blocks/Callout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Notion\Blocks;

use Notion\Common\Color;
use Notion\Common\Emoji;
use Notion\Common\File;
use Notion\Common\Icon;
Expand All @@ -16,6 +17,7 @@
* @psalm-type CalloutJson = array{
* callout: array{
* rich_text: list<RichTextJson>,
* color?: string,
* children?: list<BlockMetadataJson>,
* icon: EmojiJson|FileJson,
* },
Expand All @@ -33,6 +35,7 @@ private function __construct(
private readonly BlockMetadata $metadata,
public readonly array $text,
public readonly Icon $icon,
public readonly Color $color,
public readonly array $children,
) {
$metadata->checkType(BlockType::Callout);
Expand All @@ -43,7 +46,7 @@ public static function create(): self
$metadata = BlockMetadata::create(BlockType::Callout);
$icon = Icon::fromEmoji(Emoji::fromString(""));

return new self($metadata, [], $icon, []);
return new self($metadata, [], $icon, Color::Default, []);
}

public static function fromString(string $emoji, string $content): self
Expand All @@ -52,7 +55,7 @@ public static function fromString(string $emoji, string $content): self
$text = [ RichText::fromString($content) ];
$icon = Icon::fromEmoji(Emoji::fromString($emoji));

return new self($metadata, $text, $icon, []);
return new self($metadata, $text, $icon, Color::Default, []);
}

public static function fromArray(array $array): self
Expand All @@ -76,9 +79,11 @@ public static function fromArray(array $array): self
$icon = Icon::fromFile($file);
}

$color = Color::tryFrom($callout["color"] ?? "") ?? Color::Default;

$children = array_map(fn($b) => BlockFactory::fromArray($b), $callout["children"] ?? []);

return new self($metadata, $text, $icon, $children);
return new self($metadata, $text, $icon, $color, $children);
}

public function toArray(): array
Expand All @@ -88,6 +93,7 @@ public function toArray(): array
$array["callout"] = [
"rich_text" => array_map(fn(RichText $t) => $t->toArray(), $this->text),
"icon" => $this->icon->toArray(),
"color" => $this->color->value,
"children" => array_map(fn(BlockInterface $b) => $b->toArray(), $this->children),
];

Expand All @@ -106,15 +112,15 @@ public function metadata(): BlockMetadata

public function changeText(RichText ...$text): self
{
return new self($this->metadata, $text, $this->icon, $this->children);
return new self($this->metadata, $text, $this->icon, $this->color, $this->children);
}

public function addText(RichText $text): self
{
$texts = $this->text;
$texts[] = $text;

return new self($this->metadata, $texts, $this->icon, $this->children);
return new self($this->metadata, $texts, $this->icon, $this->color, $this->children);
}

public function changeIcon(Emoji|File|Icon $icon): self
Expand All @@ -127,7 +133,7 @@ public function changeIcon(Emoji|File|Icon $icon): self
$icon = Icon::fromFile($icon);
}

return new self($this->metadata, $this->text, $icon, $this->children);
return new self($this->metadata, $this->text, $icon, $this->color, $this->children);
}

public function changeChildren(BlockInterface ...$children): self
Expand All @@ -138,6 +144,7 @@ public function changeChildren(BlockInterface ...$children): self
$this->metadata->updateHasChildren($hasChildren),
$this->text,
$this->icon,
$this->color,
$children,
);
}
Expand All @@ -151,16 +158,29 @@ public function addChild(BlockInterface $child): self
$this->metadata->updateHasChildren(true),
$this->text,
$this->icon,
$this->color,
$children,
);
}

public function changeColor(Color $color): self
{
return new self(
$this->metadata->update(),
$this->text,
$this->icon,
$color,
$this->children,
);
}

public function archive(): BlockInterface
{
return new self(
$this->metadata->archive(),
$this->text,
$this->icon,
$this->color,
$this->children,
);
}
Expand Down
Loading

0 comments on commit e5735ea

Please sign in to comment.