Last active
February 28, 2023 14:39
-
-
Save jacksleight/9495628eab586117e5d3b3bfb33111c3 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 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 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; | |
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
@jacksleight Just installed this, it appears to working like a champ! Thanks!!!