2024-03-29 20:08:34 -04:00
|
|
|
// GIFTTemplatePreview.tsx
|
|
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
|
import Template, { ErrorTemplate } from './templates';
|
|
|
|
|
import { parse } from 'gift-pegjs';
|
|
|
|
|
import './styles.css';
|
2025-02-06 13:03:21 -05:00
|
|
|
import { FormattedTextTemplate } from './templates/TextTypeTemplate';
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
interface GIFTTemplatePreviewProps {
|
|
|
|
|
questions: string[];
|
|
|
|
|
hideAnswers?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const GIFTTemplatePreview: React.FC<GIFTTemplatePreviewProps> = ({
|
|
|
|
|
questions,
|
|
|
|
|
hideAnswers = false
|
|
|
|
|
}) => {
|
|
|
|
|
const [error, setError] = useState('');
|
|
|
|
|
const [isPreviewReady, setIsPreviewReady] = useState(false);
|
|
|
|
|
const [items, setItems] = useState('');
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
try {
|
|
|
|
|
let previewHTML = '';
|
2024-09-15 00:34:41 -04:00
|
|
|
questions.forEach((giftQuestion) => {
|
2024-09-15 10:56:37 -04:00
|
|
|
// TODO : afficher un message que les images spécifiées par <img> sont dépréciées et qu'il faut utiliser [markdown] et la syntaxe 
|
|
|
|
|
|
2024-09-15 00:34:41 -04:00
|
|
|
// const isImage = item.includes('<img');
|
|
|
|
|
// if (isImage) {
|
|
|
|
|
// const imageUrlMatch = item.match(/<img[^>]+>/i);
|
|
|
|
|
// if (imageUrlMatch) {
|
|
|
|
|
// let imageUrl = imageUrlMatch[0];
|
|
|
|
|
// imageUrl = imageUrl.replace('img', 'img style="width:10vw;" src=');
|
|
|
|
|
// item = item.replace(imageUrlMatch[0], '');
|
|
|
|
|
// previewHTML += `${imageUrl}`;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2024-03-29 20:08:34 -04:00
|
|
|
|
|
|
|
|
try {
|
2024-09-15 00:34:41 -04:00
|
|
|
const question = parse(giftQuestion);
|
|
|
|
|
previewHTML += Template(question[0], {
|
2024-03-29 20:08:34 -04:00
|
|
|
preview: true,
|
|
|
|
|
theme: 'light'
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (error instanceof Error) {
|
2024-09-15 00:34:41 -04:00
|
|
|
previewHTML += ErrorTemplate(giftQuestion + '\n' + error.message);
|
2024-03-29 20:08:34 -04:00
|
|
|
} else {
|
2024-09-15 00:34:41 -04:00
|
|
|
previewHTML += ErrorTemplate(giftQuestion + '\n' + 'Erreur inconnue');
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (hideAnswers) {
|
|
|
|
|
const svgRegex = /<svg[^>]*>([\s\S]*?)<\/svg>/gi;
|
|
|
|
|
previewHTML = previewHTML.replace(svgRegex, '');
|
|
|
|
|
const placeholderRegex = /(placeholder=")[^"]*(")/gi;
|
|
|
|
|
previewHTML = previewHTML.replace(placeholderRegex, '$1$2');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setItems(previewHTML);
|
|
|
|
|
setIsPreviewReady(true);
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
setError(error.message);
|
|
|
|
|
} else {
|
|
|
|
|
setError('Une erreur est survenue durant le chargement de la prévisualisation.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [questions]);
|
|
|
|
|
|
|
|
|
|
const PreviewComponent = () => (
|
|
|
|
|
<React.Fragment>
|
|
|
|
|
{error ? (
|
|
|
|
|
<div className="error">{error}</div>
|
|
|
|
|
) : isPreviewReady ? (
|
|
|
|
|
<div data-testid="preview-container">
|
2025-02-13 11:16:40 -05:00
|
|
|
|
2025-02-06 13:09:17 -05:00
|
|
|
<div dangerouslySetInnerHTML={{ __html: FormattedTextTemplate({ format: 'html', text: items }) }}></div>
|
2024-03-29 20:08:34 -04:00
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<div className="loading">Chargement de la prévisualisation...</div>
|
|
|
|
|
)}
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return <PreviewComponent />;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default GIFTTemplatePreview;
|