mirror of
https://github.com/ets-cfuhrman-pfe/EvalueTonSavoir.git
synced 2025-08-11 21:23:54 -04:00
Merge 85988697d4 into ee7a7a0544
This commit is contained in:
commit
eae75953be
2 changed files with 100 additions and 6 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import marked from 'src/markedConfig';
|
import marked, {attachImageModalListeners} from 'src/markedConfig';
|
||||||
|
|
||||||
import katex from 'katex';
|
import katex from 'katex';
|
||||||
import { TextFormat } from 'gift-pegjs';
|
import { TextFormat } from 'gift-pegjs';
|
||||||
|
|
@ -53,6 +53,7 @@ export function FormattedTextTemplate(formattedText: TextFormat): string {
|
||||||
break;
|
break;
|
||||||
case 'markdown':
|
case 'markdown':
|
||||||
parsedText = marked.parse(formatText, { breaks: true, gfm: true }) as string; // <br> for newlines
|
parsedText = marked.parse(formatText, { breaks: true, gfm: true }) as string; // <br> for newlines
|
||||||
|
attachImageModalListeners();
|
||||||
result = parsedText.replace(/(^<p>)(.*?)(<\/p>)$/gm, '$2');
|
result = parsedText.replace(/(^<p>)(.*?)(<\/p>)$/gm, '$2');
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
|
||||||
|
|
@ -2,15 +2,108 @@ import { marked, Renderer } from 'marked';
|
||||||
|
|
||||||
// Customized renderer to support image width and height
|
// Customized renderer to support image width and height
|
||||||
// see https://github.com/markedjs/marked/issues/339#issuecomment-1146363560
|
// see https://github.com/markedjs/marked/issues/339#issuecomment-1146363560
|
||||||
const renderer = new Renderer();
|
declare global {
|
||||||
|
interface Window {
|
||||||
renderer.image = ({href, title, text}) => {
|
showImageInModal: (src: string, alt: string) => void;
|
||||||
const [width, height] = title?.startsWith('=') ? title.slice(1).split('x').map(v => v.trim()).filter(Boolean) : [];
|
}
|
||||||
return `<img src="${href}" alt="${text}"${width ? ` width="${width}"` : ''}${height ? ` height="${height}"` : ''}>`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const renderer = new Renderer();
|
||||||
|
|
||||||
|
renderer.image = ({ href, title, text }) => {
|
||||||
|
const [width, height] = title?.startsWith('=') ? title.slice(1).split('x').map(v => v.trim()).filter(Boolean) : [];
|
||||||
|
const maxHeight = '15rem'; // Limite maximale de hauteur
|
||||||
|
|
||||||
|
return `
|
||||||
|
<div style="max-height: ${maxHeight}; width: ${width || 'auto'}; height: ${height || 'auto'}; text-align: center; display: flex; justify-content: center; align-items: center">
|
||||||
|
<img
|
||||||
|
src="${href}"
|
||||||
|
alt="${text}"
|
||||||
|
class="modal-image"
|
||||||
|
style="max-height: ${maxHeight}; width: ${width || 'auto'}; height: ${height || 'auto'}; cursor: zoom-in;"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
`;
|
||||||
|
};
|
||||||
marked.use({
|
marked.use({
|
||||||
renderer: renderer
|
renderer: renderer
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
window.showImageInModal = (src, alt) => {
|
||||||
|
console.log('showImageInModal called with:', src, alt);
|
||||||
|
|
||||||
|
// Check if a modal already exists
|
||||||
|
const existingModal = document.querySelector('.image-modal');
|
||||||
|
if (existingModal) {
|
||||||
|
// If the modal exists, remove it (close the modal)
|
||||||
|
document.body.removeChild(existingModal);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the modal container
|
||||||
|
const modal = document.createElement('div');
|
||||||
|
modal.className = 'image-modal'; // Add a class to identify the modal
|
||||||
|
modal.style.position = 'fixed';
|
||||||
|
modal.style.top = '0';
|
||||||
|
modal.style.left = '0';
|
||||||
|
modal.style.width = '100vw';
|
||||||
|
modal.style.height = '100vh';
|
||||||
|
modal.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
|
||||||
|
modal.style.display = 'flex';
|
||||||
|
modal.style.justifyContent = 'center';
|
||||||
|
modal.style.alignItems = 'center';
|
||||||
|
modal.style.zIndex = '1000';
|
||||||
|
|
||||||
|
// Close the modal when the modal itself is clicked
|
||||||
|
modal.onclick = () => {
|
||||||
|
document.body.removeChild(modal);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create the image element
|
||||||
|
const img = document.createElement('img');
|
||||||
|
img.src = src;
|
||||||
|
img.alt = alt;
|
||||||
|
|
||||||
|
img.style.minWidth = '40%';
|
||||||
|
img.style.minHeight = '40%';
|
||||||
|
|
||||||
|
img.style.cursor = 'zoom-out';
|
||||||
|
|
||||||
|
// Allow the modal to close when clicking on the image
|
||||||
|
img.onclick = () => {
|
||||||
|
document.body.removeChild(modal);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Append the image to the modal and the modal to the body
|
||||||
|
modal.appendChild(img);
|
||||||
|
document.body.appendChild(modal);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const attachImageModalListeners = () => {
|
||||||
|
const images = document.querySelectorAll('.modal-image');
|
||||||
|
|
||||||
|
images.forEach((image) => {
|
||||||
|
// Remove any existing event listeners by cloning the element
|
||||||
|
const newImage = image.cloneNode(true) as HTMLElement;
|
||||||
|
image.parentNode?.replaceChild(newImage, image);
|
||||||
|
|
||||||
|
// Attach a new event listener
|
||||||
|
newImage.addEventListener('click', (event) => {
|
||||||
|
const target = event.target as HTMLImageElement;
|
||||||
|
const src = target.getAttribute('src');
|
||||||
|
const alt = target.getAttribute('alt');
|
||||||
|
if (src) {
|
||||||
|
window.showImageInModal(src, alt || '');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
marked.use({
|
||||||
|
renderer: renderer,
|
||||||
|
});
|
||||||
|
|
||||||
export default marked;
|
export default marked;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue