Skip to content

Commit

Permalink
Add support for lines to and from objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Riley19280 committed Jan 11, 2024
1 parent c700e04 commit e613dde
Show file tree
Hide file tree
Showing 16 changed files with 403 additions and 144 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"scripts": {
"phpstan": "./vendor/bin/phpstan analyse --memory-limit=2G",
"test": "./vendor/bin/pest",
"test:coverage": "./vendor/bin/pest --coverage --min=100",
"style": "./vendor/bin/pint"
},
"config": {
Expand Down
36 changes: 36 additions & 0 deletions src/Enums/WidgetSide.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace MiroClipboard\Enums;

enum WidgetSide
{
case Top;
case Bottom;
case Left;
case Right;

public function pointForSide(float $offset = 0.5): array
{
assert($offset >= 0, 'offset must be greater than or equal to 0');
assert($offset <= 1, 'offset must be less than or equal to 1');

return match ($this) {
WidgetSide::Top => [
'x' => $offset,
'y' => 0,
],
WidgetSide::Bottom => [
'x' => $offset,
'y' => 1,
],
WidgetSide::Left => [
'x' => 0,
'y' => $offset,
],
WidgetSide::Right => [
'x' => 1,
'y' => $offset,
],
};
}
}
21 changes: 20 additions & 1 deletion src/MiroClipboardData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MiroClipboard;

use MiroClipboard\Objects\MiroGroup;
use MiroClipboard\Objects\MiroLine;
use MiroClipboard\Objects\MiroObject;
use MiroClipboard\Utility\Makeable;

Expand Down Expand Up @@ -41,6 +42,12 @@ public function addObject(MiroObject $object): static
{
if ($object instanceof MiroGroup) {
return $this->addGroup($object);
} elseif ($object instanceof MiroLine) {
foreach ($object->getAdditionalObjects() as $obj) {
if (!$this->findObject($obj)) {
$this->objects[] = $obj;
}
}
}

$this->objects[] = $object;
Expand Down Expand Up @@ -87,7 +94,19 @@ public function getObjects(): array
return $this->objects;
}

public static function parse(string $data): MiroClipboardData
public function findObject(int|MiroObject $object): false|MiroObject
{
$searchId = is_int($object) ? $object : $object->getId();
foreach ($this->objects as $existing) {
if ($existing->getId() === $searchId) {
return $existing;
}
}

return false;
}

public static function parse(string|array $data): MiroClipboardData
{
return MiroParser::parse($data);
}
Expand Down
37 changes: 9 additions & 28 deletions src/MiroParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use MiroClipboard\Enums\ObjectType;
use MiroClipboard\Enums\WidgetType;
use MiroClipboard\Exceptions\ParserNotResolvedException;
use MiroClipboard\Objects\MiroGroup;
use MiroClipboard\Objects\MiroObject;
use MiroClipboard\Parsers\GroupParser;
use MiroClipboard\Parsers\LineParser;
use MiroClipboard\Parsers\MiroParserInterface;
Expand All @@ -17,15 +15,19 @@
class MiroParser
{
/**
* @param string $data
* @param string|array $data
*
* @throws ParserNotResolvedException
*
* @return MiroClipboardData
*/
public static function parse(string $data): MiroClipboardData
public static function parse(string|array $data): MiroClipboardData
{
$arrayData = decodeMiroDataString($data);
if (is_string($data)) {
$arrayData = decodeMiroDataString($data);
} else {
$arrayData = $data;
}

$newClipboardData = MiroClipboardData::make();

Expand All @@ -35,11 +37,9 @@ public static function parse(string $data): MiroClipboardData
}

foreach ($newClipboardData->getObjects() as $object) {
if (!$object instanceof MiroGroup) {
continue;
if (method_exists($object, 'resolveClipboardDataReferences')) {
$object->resolveClipboardDataReferences($newClipboardData);
}

self::resolveGroupReferences($newClipboardData, $object);
}

return $newClipboardData;
Expand All @@ -64,23 +64,4 @@ private static function resolveWidgetParser(array $data): MiroParserInterface
default => throw new ParserNotResolvedException($data['widgetData']['type'] ?? 'null'),
};
}

private static function resolveGroupReferences(MiroClipboardData $clipboardData, MiroGroup $group): void
{
$groupObjectIds = $group->toArray()['items'];

$groupObjects = array_reduce(
$clipboardData->getObjects(),
function(array $ax, MiroObject $object) use ($groupObjectIds) {
if (in_array($object->toArray()['id'], $groupObjectIds)) {
$ax[] = $object;
}

return $ax;
},
[]
);

$group->setRawObjects($groupObjects);
}
}
19 changes: 17 additions & 2 deletions src/Objects/MiroGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MiroClipboard\Objects;

use MiroClipboard\Enums\ObjectType;
use MiroClipboard\MiroClipboardData;
use MiroClipboard\Utility\Makeable;

class MiroGroup extends MiroObject
Expand Down Expand Up @@ -50,11 +51,25 @@ public function getObjects(): array
}

/**
* @param MiroClipboardData $clipboardData
*
* @return void
*
* @internal
*/
public function setRawObjects(array $objects): void
public function resolveClipboardDataReferences(MiroClipboardData $clipboardData): void
{
$this->objects = $objects;
$this->objects = array_reduce(
$clipboardData->getObjects(),
function(array $ax, MiroObject $object) {
if (in_array($object->toArray()['id'], $this->ids)) {
$ax[] = $object;
}

return $ax;
},
[]
);
}

public function toArray(): array
Expand Down
58 changes: 58 additions & 0 deletions src/Objects/MiroLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace MiroClipboard\Objects;

use MiroClipboard\Enums\WidgetSide;
use MiroClipboard\Enums\WidgetType;
use MiroClipboard\MiroClipboardData;
use MiroClipboard\MiroWidget;
use MiroClipboard\Styles\MiroLineStyle;
use MiroClipboard\Utility\SetPropertiesFromArray;
Expand Down Expand Up @@ -38,6 +40,9 @@ class MiroLine extends MiroWidget
'widgetIndex' => -1,
];

protected ?MiroObject $toObject = null;
protected ?MiroObject $fromObject = null;

public function __construct()
{
parent::__construct();
Expand Down Expand Up @@ -79,6 +84,26 @@ public function end(float $x, float $y): static
return $this;
}

public function from(MiroObject $object, WidgetSide $widgetSide = WidgetSide::Right, float $offset = 0.5): static
{
$this->fromObject = $object;

$this->primary['widgetIndex'] = $object->id;
$this->primary['point'] = $widgetSide->pointForSide($offset);

return $this;
}

public function to(MiroObject $object, WidgetSide $widgetSide = WidgetSide::Left, float $offset = 0.5): static
{
$this->toObject = $object;

$this->secondary['widgetIndex'] = $object->id;
$this->secondary['point'] = $widgetSide->pointForSide($offset);

return $this;
}

public function addPoint(float $x, float $y): static
{
$this->points[] = [
Expand Down Expand Up @@ -111,6 +136,39 @@ public function addText(MiroLineText $text): static
return $this;
}

/**
* Return a list of objects that *may* need to be added to the clipboard data
*
* @return array<MiroObject>
*
* @internal
*/
public function getAdditionalObjects(): array
{
return array_filter([
$this->fromObject,
$this->toObject,
]);
}

/**
* @param MiroClipboardData $clipboardData
*
* @return void
*
* @internal
*/
public function resolveClipboardDataReferences(MiroClipboardData $clipboardData): void
{
if ($this->primary['widgetIndex'] != -1 && ($existing = $clipboardData->findObject($this->primary['widgetIndex']))) {
$this->fromObject = $existing;
}

if ($this->secondary['widgetIndex'] != -1 && ($existing = $clipboardData->findObject($this->secondary['widgetIndex']))) {
$this->toObject = $existing;
}
}

public function toArray(): array
{
return [
Expand Down
10 changes: 10 additions & 0 deletions src/Objects/MiroObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ public function initialId(string $initialId): static
return $this;
}

public function getId(): int
{
return $this->id;
}

public function getInitialId(): string
{
return $this->initialId;
}

public function toArray(): array
{
return [
Expand Down
2 changes: 1 addition & 1 deletion src/Objects/MiroShape.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MiroShape extends MiroWidget

private float $relativeRotation = 0;

public function __construct(ShapeType $shapeType)
public function __construct(ShapeType $shapeType = ShapeType::Square)
{
parent::__construct();

Expand Down
4 changes: 2 additions & 2 deletions src/Styles/MiroShapeStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MiroShapeStyle
*
* @var int
*/
private int $brc = 15132390; // Black: 15132390
private int $brc = 15132390;

/**
* The Border Thickness
Expand Down Expand Up @@ -71,7 +71,7 @@ class MiroShapeStyle
*
* @var int
*/
private int $tc = 15877926;
private int $tc = 1710618;

private int $tsc = 1;

Expand Down
2 changes: 1 addition & 1 deletion tests/.pest/snapshots/Unit/DataFormatTest/encoding.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<span data-meta="<--(miro-data-v1)tl2dqpytn4SfXXVdoKmeqp+kqaJdZ12fnK+cXXW2Xaigr5xddZaYZ12qnaWgnq+uXXWWtl2kn111a2ddpKmkr6Scp4SfXXVda11nXa+0q6BddWxvZ12ypJ+ioK9/nK+cXXW2Xa+0q6BddV2uo5yroF1nXaWuqqlddbZdrqS1oF11tl2ypJ+vo111bGtrZ12joKSio69ddWxra7hnXZqrqq6kr6SqqV11tl2qoaGuoK+Ls111tl2zXXVrZ120XXVruLhnXa6enKegXXW2Xa6enKegXXVsuGddraCnnK+ksaCOnpynoF11bGddraqvnK+kqqlddbZdraqvnK+kqqlddWu4Z12toKecr6SxoI2qr5yvpKqpXXVrZ12rqq6kr6SqqV11tl2zXXVrZ120XXVruGddmqucraCpr111qbCnp2ddr6Czr111XV1nXa6vtKegXXVdtpddrq+XXXVuZ5ddrq6XXXVtZ5ddrp6XXXVscmxrcWxzZ5ddnZ6XXXVscXJsbHFza2eXXZ2ql111bGeXXZ2tnpdddWxwbG5tbnRrZ5ddna2yl111bWeXXZ2tqpdddWxnl12dra6XXXVtZ5ddoaGpl111l12Kq6CpjpyprpddZ5ddr56XXXVscHNycnRtcWeXXa+unpdddWxnl12vnJdddZddnpddZ5ddr5yxl111l12ol11nl12hrpdddW1vZ5ddnZdddamwp6dnl12kl111qbCnp2eXXbCXXXWpsKenZ5ddrpdddamwp6dnl12drp6XXXVsZ5ddkYCNl111l11taWyXXWeXXaOnl111qbCnp7hdZ12uo5yroF11XW5duLi4mLhnXaOqrq9ddV2opK2qaZ6qqF1nXbGgra6kqqlddW1nXaSui62qr6Cer6CfXXWhnKeuoLg=(/miro-data-v1)-->"></span>
<span data-meta="<--(miro-data-v1)tl2dqpytn4SfXXVdoKmeqp+kqaJdZ12fnK+cXXW2Xaigr5xddZaYZ12qnaWgnq+uXXWWtl2kn111a2ddpKmkr6Scp4SfXXVda11nXa+0q6BddWxvZ12ypJ+ioK9/nK+cXXW2Xa+0q6BddV2uo5yroF1nXaWuqqlddbZdrqS1oF11tl2ypJ+vo111bGtrZ12joKSio69ddWxra7hnXZqrqq6kr6SqqV11tl2qoaGuoK+Ls111tl2zXXVrZ120XXVruLhnXa6enKegXXW2Xa6enKegXXVsuGddraCnnK+ksaCOnpynoF11bGddraqvnK+kqqlddbZdraqvnK+kqqlddWu4Z12toKecr6SxoI2qr5yvpKqpXXVrZ12rqq6kr6SqqV11tl2zXXVrZ120XXVruGddmqucraCpr111qbCnp2ddr6Czr111XV1nXa6vtKegXXVdtpddrq+XXXVuZ5ddrq6XXXVtZ5ddrp6XXXVscmxrcWxzZ5ddnZ6XXXVscXJsbHFza2eXXZ2ql111bGeXXZ2tnpdddWxwbG5tbnRrZ5ddna2yl111bWeXXZ2tqpdddWxnl12dra6XXXVtZ5ddoaGpl111l12Kq6CpjpyprpddZ5ddr56XXXVscmxrcWxzZ5ddr66el111bGeXXa+cl111l12el11nl12vnLGXXXWXXaiXXWeXXaGul111bW9nl12dl111qbCnp2eXXaSXXXWpsKenZ5ddsJdddamwp6dnl12ul111qbCnp2eXXZ2unpdddWxnl12RgI2XXXWXXW1pbJddZ5ddo6eXXXWpsKenuF1nXa6jnKugXXVdbl24uLiYuGddo6qur111XaikrappnqqoXWddsaCtrqSqqV11bWddpK6LraqvoJ6voJ9ddaGcp66guA==(/miro-data-v1)-->"></span>
Loading

0 comments on commit e613dde

Please sign in to comment.