Forked from jacksleight/AppServiceProvider.php
Last active
February 17, 2023 07:58
-
-
Save MrMooky/0f9e28648aa5c2ac6c787063596d086e to your computer and use it in GitHub Desktop.
Convert legacy Bard Text Align mark values to Tiptap 2 text align values
This file contains hidden or 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
<?php | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
public function register() | |
{ | |
$this->app->bind(\Statamic\Fieldtypes\Bard::class, \App\Fieldtypes\Bard::class); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace App\Fieldtypes; | |
class Bard extends \Statamic\Fieldtypes\Bard | |
{ | |
protected function convertLegacyTiptap($value) | |
{ | |
return parent::convertLegacyTiptap($this->convertLegacyTextAlign($value)); | |
} | |
protected function convertLegacyTextAlign($value) | |
{ | |
if (is_string($value)) { | |
return $value; | |
} | |
if (! in_array($value['type'] ?? null, ['heading', 'paragraph'])) { | |
return $value; | |
} | |
$found = null; | |
if (! array_key_exists('content', $value)) { | |
return $value; | |
} | |
foreach ($value['content'] as $i => $node) { | |
if (! isset($node['marks'])) { | |
continue; | |
} | |
foreach ($node['marks'] as $j => $mark) { | |
if ($mark['type'] === 'textAlign') { | |
unset($value['content'][$i]['marks'][$j]); | |
if (! $found) { | |
$found = $mark; | |
} | |
} | |
} | |
if (! count($value['content'][$i]['marks'])) { | |
unset($value['content'][$i]['marks']); | |
} else { | |
$value['content'][$i]['marks'] = array_values($value['content'][$i]['marks']); | |
} | |
} | |
if (! $found) { | |
return $value; | |
} | |
$value['attrs']['textAlign'] = $found['attrs']['align']; | |
return $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment