-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18d9eab
commit c44d895
Showing
7 changed files
with
124 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 0 additions & 66 deletions
66
src/Filament/Resources/AbstractContentResource/RelationManagers/RevisionsRelationManager.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
namespace Portable\FilaCms\Versionable; | ||
|
||
use Illuminate\Support\Arr; | ||
use Illuminate\Support\Collection; | ||
use Jfcherng\Diff\Differ; | ||
use Jfcherng\Diff\DiffHelper; | ||
use Overtrue\LaravelVersionable\Diff; | ||
|
||
class FilaCmsDiff extends Diff | ||
{ | ||
public function getStatistics(array $differOptions = []): array | ||
{ | ||
if (empty($differOptions)) { | ||
$differOptions = $this->differOptions; | ||
} | ||
|
||
$oldContents = $this->fromVersion->contents; | ||
$newContents = $this->toVersion->contents; | ||
|
||
$diffStats = new Collection(); | ||
|
||
foreach ($oldContents as $key => $value) { | ||
if ($newContents[$key] !== $oldContents[$key]) { | ||
$newData = $newContents[$key]; | ||
$oldData = $oldContents[$key]; | ||
|
||
if(is_array($newData) || is_array($oldData)) { | ||
$newData = tiptap_converter()->asText($newData); | ||
$oldData = tiptap_converter()->asText($oldData); | ||
} | ||
|
||
$diffStats->push( | ||
(new Differ( | ||
explode("\n", $newData), | ||
explode("\n", $oldData), | ||
))->getStatistics() | ||
); | ||
} | ||
} | ||
|
||
return [ | ||
'inserted' => $diffStats->sum('inserted'), | ||
'deleted' => $diffStats->sum('deleted'), | ||
'unmodified' => $diffStats->sum('unmodified'), | ||
'changedRatio' => $diffStats->sum('changedRatio'), | ||
]; | ||
} | ||
|
||
|
||
public function render(?string $renderer = null, array $differOptions = [], array $renderOptions = []): array | ||
{ | ||
if (empty($differOptions)) { | ||
$differOptions = $this->differOptions; | ||
} | ||
|
||
if (empty($renderOptions)) { | ||
$renderOptions = $this->renderOptions; | ||
} | ||
|
||
$oldContents = $this->fromVersion->contents; | ||
$newContents = $this->toVersion->contents; | ||
|
||
$diff = []; | ||
$createDiff = function ($key, $old, $new) use (&$diff, $renderer, $differOptions, $renderOptions) { | ||
if ($renderer) { | ||
$old = is_string($old) ? $old : (is_array($old) ? tiptap_converter()->asText($old) : json_encode($old)); | ||
$new = is_string($new) ? $new : (is_array($new) ? tiptap_converter()->asText($new) : json_encode($new)); | ||
$diff[$key] = str_replace('\n No newline at end of file', '', DiffHelper::calculate($old, $new, $renderer, $differOptions, $renderOptions)); | ||
} else { | ||
$diff[$key] = compact('old', 'new'); | ||
} | ||
}; | ||
|
||
foreach ($oldContents as $key => $value) { | ||
$createDiff($key, Arr::get($newContents, $key), Arr::get($oldContents, $key)); | ||
} | ||
|
||
foreach (array_diff_key($oldContents, $newContents) as $key => $value) { | ||
$createDiff($key, null, $value); | ||
} | ||
|
||
return $diff; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Portable\FilaCms\Versionable; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Overtrue\LaravelVersionable\Diff; | ||
use Overtrue\LaravelVersionable\Version; | ||
|
||
class FilaCmsVersion extends \Overtrue\LaravelVersionable\Version | ||
{ | ||
protected $table = 'versions'; | ||
|
||
public function diff(?Version $toVersion = null, array $differOptions = [], array $renderOptions = []): Diff | ||
{ | ||
if (! $toVersion) { | ||
$toVersion = $this->previousVersion() ?? new static(); | ||
} | ||
|
||
return new FilaCmsDiff($this, $toVersion, $differOptions, $renderOptions); | ||
} | ||
|
||
public function revertWithoutSaving(): ?Model | ||
{ | ||
$oldContents = $newContents = $this->contents; | ||
$newContents['contents'] = json_encode($this->contents['contents']); | ||
$this->contents = $newContents; | ||
$result = parent::revertWithoutSaving(); | ||
$this->contents = $oldContents; | ||
|
||
return $result; | ||
} | ||
} |