correction de l'affichage de la mise en forme du texte (partie 2)
This commit is contained in:
parent
27076900bb
commit
38b7596569
@ -2,13 +2,40 @@
|
||||
|
||||
class DeltaConverter {
|
||||
private static $blockAttrs = ['align', 'header', 'blockquote', 'code-block', 'list'];
|
||||
private static $inlineAttrs = ['bold', 'italic', 'underline', 'color', 'background', 'link', 'font', 'script'];
|
||||
private static $inlineAttrs = ['bold', 'italic', 'underline', 'strike', 'color', 'background', 'link', 'font', 'script'];
|
||||
|
||||
public static function toHtml($content) {
|
||||
if (empty($content)) return '';
|
||||
|
||||
// Si le contenu est déjà en HTML
|
||||
if (is_string($content) && !self::isJson($content)) {
|
||||
// Convertir les classes ql-align-* en styles d'alignement
|
||||
$content = preg_replace_callback(
|
||||
'/class="ql-align-(justify|center|right)"/',
|
||||
function($matches) {
|
||||
return 'style="text-align: ' . $matches[1] . ' !important"';
|
||||
},
|
||||
$content
|
||||
);
|
||||
|
||||
// Convertir les classes ql-font-* en classes de police
|
||||
$content = preg_replace_callback(
|
||||
'/class="ql-font-(serif|monospace|sans-serif)"/',
|
||||
function($matches) {
|
||||
switch($matches[1]) {
|
||||
case 'serif':
|
||||
return 'class="font-serif"';
|
||||
case 'monospace':
|
||||
return 'class="font-mono"';
|
||||
case 'sans-serif':
|
||||
return 'class="font-sans"';
|
||||
default:
|
||||
return '';
|
||||
}
|
||||
},
|
||||
$content
|
||||
);
|
||||
|
||||
return self::cleanImageUrls($content);
|
||||
}
|
||||
|
||||
@ -37,10 +64,17 @@ class DeltaConverter {
|
||||
// Si c'est un saut de ligne, on finalise le bloc
|
||||
if ($text === "\n") {
|
||||
// Vérifier les attributs de bloc pour ce saut de ligne
|
||||
foreach ($attrs as $key => $value) {
|
||||
if (in_array($key, self::$blockAttrs)) {
|
||||
$currentBlock['attrs'][$key] = $value;
|
||||
}
|
||||
if (!empty($attrs['align'])) {
|
||||
$currentBlock['attrs']['text-align'] = $attrs['align'];
|
||||
}
|
||||
if (!empty($attrs['header'])) {
|
||||
$currentBlock['attrs']['header'] = $attrs['header'];
|
||||
}
|
||||
if (!empty($attrs['blockquote'])) {
|
||||
$currentBlock['attrs']['blockquote'] = true;
|
||||
}
|
||||
if (!empty($attrs['code-block'])) {
|
||||
$currentBlock['attrs']['code-block'] = true;
|
||||
}
|
||||
|
||||
// Finaliser le bloc courant
|
||||
@ -111,16 +145,12 @@ class DeltaConverter {
|
||||
$tag = 'blockquote';
|
||||
} elseif (!empty($block['attrs']['code-block'])) {
|
||||
$tag = 'pre';
|
||||
$classes[] = 'code-block';
|
||||
}
|
||||
|
||||
// Appliquer l'alignement au niveau du bloc
|
||||
if (!empty($block['attrs']['align'])) {
|
||||
$styles[] = "text-align: " . $block['attrs']['align'] . " !important";
|
||||
}
|
||||
|
||||
// Appliquer la police au niveau du bloc si présente
|
||||
if (!empty($block['attrs']['font'])) {
|
||||
$classes[] = 'font-' . strtolower($block['attrs']['font']);
|
||||
if (!empty($block['attrs']['text-align'])) {
|
||||
$styles[] = "text-align: " . $block['attrs']['text-align'] . " !important";
|
||||
}
|
||||
|
||||
// Construire le contenu avec les attributs inline
|
||||
@ -153,10 +183,10 @@ class DeltaConverter {
|
||||
// Construire les attributs HTML finaux
|
||||
$attributes = '';
|
||||
if (!empty($styles)) {
|
||||
$attributes .= ' style="' . implode('; ', $styles) . '"';
|
||||
$attributes .= ' style="' . implode('; ', array_unique($styles)) . '"';
|
||||
}
|
||||
if (!empty($classes)) {
|
||||
$attributes .= ' class="' . implode(' ', $classes) . '"';
|
||||
$attributes .= ' class="' . implode(' ', array_unique($classes)) . '"';
|
||||
}
|
||||
|
||||
return "<$tag$attributes>$content</$tag>";
|
||||
@ -174,17 +204,30 @@ class DeltaConverter {
|
||||
if (!empty($attrs['underline'])) {
|
||||
$text = "<u>$text</u>";
|
||||
}
|
||||
if (!empty($attrs['strike'])) {
|
||||
$text = "<s>$text</s>";
|
||||
}
|
||||
if (!empty($attrs['color'])) {
|
||||
$text = "<span style=\"color: {$attrs['color']}\">$text</span>";
|
||||
$text = "<span style=\"color: {$attrs['color']} !important\">$text</span>";
|
||||
}
|
||||
if (!empty($attrs['background'])) {
|
||||
$text = "<span style=\"background-color: {$attrs['background']}\">$text</span>";
|
||||
$text = "<span style=\"background-color: {$attrs['background']} !important\">$text</span>";
|
||||
}
|
||||
if (!empty($attrs['link'])) {
|
||||
$text = "<a href=\"{$attrs['link']}\" target=\"_blank\">$text</a>";
|
||||
}
|
||||
if (!empty($attrs['font'])) {
|
||||
$text = "<span class=\"font-" . strtolower($attrs['font']) . "\">$text</span>";
|
||||
switch($attrs['font']) {
|
||||
case 'serif':
|
||||
$text = "<span class=\"font-serif\">$text</span>";
|
||||
break;
|
||||
case 'monospace':
|
||||
$text = "<span class=\"font-mono\">$text</span>";
|
||||
break;
|
||||
case 'sans-serif':
|
||||
$text = "<span class=\"font-sans\">$text</span>";
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!empty($attrs['script'])) {
|
||||
if ($attrs['script'] === 'super') $text = "<sup>$text</sup>";
|
||||
|
Loading…
x
Reference in New Issue
Block a user