Skip to content

Commit

Permalink
Revisionable
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoungportable committed Apr 8, 2024
1 parent 18d9eab commit c44d895
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 70 deletions.
2 changes: 0 additions & 2 deletions src/Filament/Resources/AbstractContentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Portable\FilaCms\Filament\Forms\Components\StatusBadge;
use Portable\FilaCms\Filament\Resources\AbstractContentResource\Pages;
use Portable\FilaCms\Filament\Resources\AbstractContentResource\RelationManagers;
use Portable\FilaCms\Filament\Traits\IsProtectedResource;
use Portable\FilaCms\Models\Author;
use Portable\FilaCms\Models\Page;
Expand Down Expand Up @@ -200,7 +199,6 @@ public static function table(Table $table): Table
public static function getRelations(): array
{
return [
RelationManagers\RevisionsRelationManager::class,
];
}

Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion src/Filament/Traits/HasExcerpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait HasExcerpt
public function excerpt(): Attribute
{
$excerpt = $this->{$this->excerptField};
$content = $excerpt['content'];
$content = is_array($excerpt) ? $excerpt['content'] : [];

// get first content with "paragraph"
$paragraph = '';
Expand Down
3 changes: 3 additions & 0 deletions src/Models/AbstractContentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Portable\FilaCms\Filament\Traits\HasExcerpt;
use Portable\FilaCms\Filament\Traits\HasTaxonomies;
use Portable\FilaCms\Models\Scopes\PublishedScope;
use Portable\FilaCms\Versionable\FilaCmsVersion;
use RalphJSmit\Laravel\SEO\Support\HasSEO;
use RalphJSmit\Laravel\SEO\Support\SEOData;
use Str;
Expand All @@ -39,6 +40,8 @@ abstract class AbstractContentModel extends Model

protected $versionStrategy = VersionStrategy::SNAPSHOT;

public string $versionModel = FilaCmsVersion::class;

protected $versionable = [
'title',
'slug',
Expand Down
3 changes: 2 additions & 1 deletion src/Providers/FilaCmsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Portable\FilaCms\Providers;

use FilamentTiptapEditor\TiptapEditor;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
use Livewire\Livewire;
use Portable\FilaCms\Facades\FilaCms as FacadesFilaCms;
use Portable\FilaCms\FilaCms;
use FilamentTiptapEditor\TiptapEditor;
use Portable\FilaCms\Filament\Blocks\RelatedResourceBlock;

class FilaCmsServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -42,6 +42,7 @@ public function boot()
Livewire::component('portable.fila-cms.livewire.content-resource-list', \Portable\FilaCms\Livewire\ContentResourceList::class);
Livewire::component('portable.fila-cms.livewire.content-resource-show', \Portable\FilaCms\Livewire\ContentResourceShow::class);
Blade::componentNamespace('Portable\\FilaCms\\Views\\Components', 'fila-cms');
config(['versionable.user_model' => config('auth.providers.users.model')]);
}

public function register()
Expand Down
86 changes: 86 additions & 0 deletions src/Versionable/FilaCmsDiff.php
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;
}
}
32 changes: 32 additions & 0 deletions src/Versionable/FilaCmsVersion.php
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;
}
}

0 comments on commit c44d895

Please sign in to comment.