chahuadev-markdown-presenter / src /shared /slide-renderer.js
chahuadev's picture
Upload 51 files
f462b1c verified
// ══════════════════════════════════════════════════════════════════════════════
// Chahua Markdown Presenter - Slide Renderer
// Render parsed slides as HTML presentation
// ══════════════════════════════════════════════════════════════════════════════
// Company: Chahua Development Co., Ltd.
// Version: 2.0.0
// License: MIT
// ══════════════════════════════════════════════════════════════════════════════
export class SlideRenderer {
constructor(container, options = {}) {
this.container = container;
this.options = {
transition: options.transition || 'fade', // fade, slide, zoom
transitionDuration: options.transitionDuration || 600,
autoAnimate: options.autoAnimate !== false,
...options
};
this.slides = [];
this.currentIndex = 0;
this.isAnimating = false;
}
/**
* Load slides into renderer
*/
loadSlides(slides) {
this.slides = slides;
this.currentIndex = 0;
this.renderAllSlides();
}
/**
* Render all slides as HTML
*/
renderAllSlides() {
this.container.innerHTML = '';
this.slides.forEach((slide, index) => {
const slideElement = this.createSlideElement(slide, index);
this.container.appendChild(slideElement);
});
// Show first slide
if (this.slides.length > 0) {
this.showSlide(0);
}
}
/**
* Create HTML element for a slide
*/
createSlideElement(slide, index) {
const slideDiv = document.createElement('div');
slideDiv.className = 'slide';
slideDiv.dataset.index = index;
slideDiv.style.display = 'none';
// Create slide content container
const contentDiv = document.createElement('div');
contentDiv.className = 'slide-content';
// Render each content item
slide.content.forEach((item, itemIndex) => {
const element = this.renderContentItem(item, itemIndex);
if (element) {
contentDiv.appendChild(element);
}
});
slideDiv.appendChild(contentDiv);
// Add slide number
const slideNumber = document.createElement('div');
slideNumber.className = 'slide-number';
slideNumber.textContent = `${index + 1} / ${this.slides.length}`;
slideDiv.appendChild(slideNumber);
return slideDiv;
}
/**
* Render individual content item
*/
renderContentItem(item, index) {
const element = document.createElement('div');
element.className = `content-item content-${item.type}`;
if (this.options.autoAnimate) {
element.style.opacity = '0';
element.style.transform = 'translateY(20px)';
element.dataset.animationDelay = index * 150;
}
switch (item.type) {
case 'h1':
element.innerHTML = `<h1>${this.parseInline(item.content)}</h1>`;
break;
case 'h2':
element.innerHTML = `<h2>${this.parseInline(item.content)}</h2>`;
break;
case 'h3':
element.innerHTML = `<h3>${this.parseInline(item.content)}</h3>`;
break;
case 'h4':
element.innerHTML = `<h4>${this.parseInline(item.content)}</h4>`;
break;
case 'text':
element.innerHTML = `<p>${this.parseInline(item.content)}</p>`;
break;
case 'list-item':
const li = document.createElement('li');
li.innerHTML = this.parseInline(item.content);
const ul = document.createElement('ul');
ul.appendChild(li);
element.appendChild(ul);
break;
case 'numbered-item':
const nli = document.createElement('li');
nli.innerHTML = this.parseInline(item.content);
const ol = document.createElement('ol');
ol.appendChild(nli);
element.appendChild(ol);
break;
case 'code':
const pre = document.createElement('pre');
const code = document.createElement('code');
code.className = `language-${item.language}`;
code.textContent = item.content;
pre.appendChild(code);
element.appendChild(pre);
break;
case 'blockquote':
element.innerHTML = `<blockquote>${this.parseInline(item.content)}</blockquote>`;
break;
case 'image':
element.innerHTML = `<img src="${item.src}" alt="${item.alt || ''}" />`;
break;
case 'link':
element.innerHTML = `<a href="${item.url}" target="_blank">${item.text}</a>`;
break;
default:
return null;
}
return element;
}
/**
* Parse inline markdown
*/
parseInline(text) {
if (!text) return '';
// Bold
text = text.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
text = text.replace(/__([^_]+)__/g, '<strong>$1</strong>');
// Italic
text = text.replace(/\*([^*]+)\*/g, '<em>$1</em>');
text = text.replace(/_([^_]+)_/g, '<em>$1</em>');
// Inline code
text = text.replace(/`([^`]+)`/g, '<code>$1</code>');
return text;
}
/**
* Show specific slide
*/
async showSlide(index, direction = 'next') {
if (this.isAnimating || index < 0 || index >= this.slides.length) {
return;
}
this.isAnimating = true;
const currentSlide = this.container.querySelector(`[data-index="${this.currentIndex}"]`);
const nextSlide = this.container.querySelector(`[data-index="${index}"]`);
if (!nextSlide) {
this.isAnimating = false;
return;
}
// Apply transition
await this.applyTransition(currentSlide, nextSlide, direction);
this.currentIndex = index;
this.isAnimating = false;
// Animate content items
if (this.options.autoAnimate) {
this.animateSlideContent(nextSlide);
}
}
/**
* Apply transition between slides
*/
async applyTransition(currentSlide, nextSlide, direction) {
const duration = this.options.transitionDuration;
switch (this.options.transition) {
case 'fade':
await this.fadeTransition(currentSlide, nextSlide, duration);
break;
case 'slide':
await this.slideTransition(currentSlide, nextSlide, direction, duration);
break;
case 'zoom':
await this.zoomTransition(currentSlide, nextSlide, duration);
break;
case 'glitch':
await this.glitchTransition(currentSlide, nextSlide, duration);
break;
case 'matrix':
await this.matrixTransition(currentSlide, nextSlide, duration);
break;
case 'cube':
await this.cubeTransition(currentSlide, nextSlide, direction, duration);
break;
case 'pixelate':
await this.pixelateTransition(currentSlide, nextSlide, duration);
break;
default:
// Instant transition
if (currentSlide) currentSlide.style.display = 'none';
nextSlide.style.display = 'flex';
}
}
/**
* Fade transition
*/
async fadeTransition(currentSlide, nextSlide, duration) {
nextSlide.style.display = 'flex';
nextSlide.style.opacity = '0';
await this.wait(50);
if (currentSlide) {
currentSlide.style.transition = `opacity ${duration}ms ease`;
currentSlide.style.opacity = '0';
}
nextSlide.style.transition = `opacity ${duration}ms ease`;
nextSlide.style.opacity = '1';
await this.wait(duration);
if (currentSlide) {
currentSlide.style.display = 'none';
currentSlide.style.opacity = '1';
}
}
/**
* Slide transition
*/
async slideTransition(currentSlide, nextSlide, direction, duration) {
const offset = direction === 'next' ? '100%' : '-100%';
nextSlide.style.display = 'flex';
nextSlide.style.transform = `translateX(${offset})`;
await this.wait(50);
if (currentSlide) {
currentSlide.style.transition = `transform ${duration}ms ease`;
currentSlide.style.transform = `translateX(${direction === 'next' ? '-100%' : '100%'})`;
}
nextSlide.style.transition = `transform ${duration}ms ease`;
nextSlide.style.transform = 'translateX(0)';
await this.wait(duration);
if (currentSlide) {
currentSlide.style.display = 'none';
currentSlide.style.transform = 'translateX(0)';
}
}
/**
* Zoom transition
*/
async zoomTransition(currentSlide, nextSlide, duration) {
nextSlide.style.display = 'flex';
nextSlide.style.transform = 'scale(0.8)';
nextSlide.style.opacity = '0';
await this.wait(50);
if (currentSlide) {
currentSlide.style.transition = `all ${duration}ms ease`;
currentSlide.style.transform = 'scale(1.2)';
currentSlide.style.opacity = '0';
}
nextSlide.style.transition = `all ${duration}ms ease`;
nextSlide.style.transform = 'scale(1)';
nextSlide.style.opacity = '1';
await this.wait(duration);
if (currentSlide) {
currentSlide.style.display = 'none';
currentSlide.style.transform = 'scale(1)';
currentSlide.style.opacity = '1';
}
}
/**
* Animate slide content items
*/
animateSlideContent(slideElement) {
const items = slideElement.querySelectorAll('.content-item');
items.forEach((item, index) => {
const delay = parseInt(item.dataset.animationDelay) || 0;
setTimeout(() => {
item.style.transition = 'all 400ms ease';
item.style.opacity = '1';
item.style.transform = 'translateY(0)';
}, delay);
});
}
/**
* Navigate to next slide
*/
async next() {
if (this.currentIndex < this.slides.length - 1) {
await this.showSlide(this.currentIndex + 1, 'next');
}
}
/**
* Navigate to previous slide
*/
async previous() {
if (this.currentIndex > 0) {
await this.showSlide(this.currentIndex - 1, 'prev');
}
}
/**
* Go to first slide
*/
async goToFirst() {
await this.showSlide(0, 'next');
}
/**
* Go to last slide
*/
async goToLast() {
await this.showSlide(this.slides.length - 1, 'prev');
}
/**
* Glitch transition - Digital glitch effect
*/
async glitchTransition(currentSlide, nextSlide, duration) {
nextSlide.style.display = 'flex';
nextSlide.style.opacity = '0';
const glitchSteps = 8;
const stepDuration = duration / glitchSteps;
for (let i = 0; i < glitchSteps; i++) {
const offset = Math.random() * 20 - 10;
const opacity = i / glitchSteps;
if (currentSlide) {
currentSlide.style.transform = `translateX(${offset}px)`;
currentSlide.style.opacity = String(1 - opacity);
currentSlide.style.filter = `hue-rotate(${Math.random() * 360}deg)`;
}
nextSlide.style.transform = `translateX(${-offset}px)`;
nextSlide.style.opacity = String(opacity);
nextSlide.style.filter = `hue-rotate(${Math.random() * 360}deg)`;
await this.wait(stepDuration);
}
if (currentSlide) {
currentSlide.style.display = 'none';
currentSlide.style.transform = '';
currentSlide.style.filter = '';
}
nextSlide.style.transform = '';
nextSlide.style.filter = '';
nextSlide.style.opacity = '1';
}
/**
* Matrix transition - Digital rain effect simulation
*/
async matrixTransition(currentSlide, nextSlide, duration) {
nextSlide.style.display = 'flex';
// Create matrix overlay
const matrix = document.createElement('div');
matrix.style.position = 'absolute';
matrix.style.inset = '0';
matrix.style.background = 'linear-gradient(0deg, transparent 0%, rgba(0, 255, 0, 0.8) 50%, transparent 100%)';
matrix.style.backgroundSize = '100% 200%';
matrix.style.animation = `matrixScroll ${duration}ms linear`;
matrix.style.zIndex = '1000';
matrix.style.pointerEvents = 'none';
this.container.appendChild(matrix);
// Fade out current, fade in next
if (currentSlide) {
currentSlide.style.transition = `opacity ${duration * 0.7}ms ease`;
currentSlide.style.opacity = '0';
}
await this.wait(duration * 0.3);
nextSlide.style.opacity = '0';
nextSlide.style.transition = `opacity ${duration * 0.7}ms ease`;
nextSlide.style.opacity = '1';
await this.wait(duration * 0.7);
if (currentSlide) {
currentSlide.style.display = 'none';
currentSlide.style.opacity = '1';
}
matrix.remove();
}
/**
* Cube transition - 3D cube rotation
*/
async cubeTransition(currentSlide, nextSlide, direction, duration) {
const perspective = '1200px';
const rotateY = direction === 'next' ? '-90deg' : '90deg';
this.container.style.perspective = perspective;
this.container.style.perspectiveOrigin = 'center center';
nextSlide.style.display = 'flex';
nextSlide.style.transform = `rotateY(${direction === 'next' ? '90deg' : '-90deg'})`;
nextSlide.style.transformOrigin = direction === 'next' ? 'right center' : 'left center';
if (currentSlide) {
currentSlide.style.transformOrigin = direction === 'next' ? 'left center' : 'right center';
}
await this.wait(50);
if (currentSlide) {
currentSlide.style.transition = `transform ${duration}ms cubic-bezier(0.645, 0.045, 0.355, 1)`;
currentSlide.style.transform = `rotateY(${rotateY})`;
}
nextSlide.style.transition = `transform ${duration}ms cubic-bezier(0.645, 0.045, 0.355, 1)`;
nextSlide.style.transform = 'rotateY(0deg)';
await this.wait(duration);
if (currentSlide) {
currentSlide.style.display = 'none';
currentSlide.style.transform = '';
}
nextSlide.style.transform = '';
this.container.style.perspective = '';
}
/**
* Pixelate transition - Pixelation effect
*/
async pixelateTransition(currentSlide, nextSlide, duration) {
nextSlide.style.display = 'flex';
nextSlide.style.opacity = '0';
const steps = 10;
const stepDuration = duration / steps;
// Pixelate out
for (let i = 0; i <= steps / 2; i++) {
const pixelSize = i * 2;
if (currentSlide) {
currentSlide.style.filter = `blur(${pixelSize}px)`;
currentSlide.style.opacity = String(1 - (i / (steps / 2)));
}
await this.wait(stepDuration);
}
if (currentSlide) {
currentSlide.style.display = 'none';
}
// Pixelate in
for (let i = steps / 2; i >= 0; i--) {
const pixelSize = i * 2;
nextSlide.style.filter = `blur(${pixelSize}px)`;
nextSlide.style.opacity = String(1 - (i / (steps / 2)));
await this.wait(stepDuration);
}
if (currentSlide) {
currentSlide.style.filter = '';
currentSlide.style.opacity = '1';
}
nextSlide.style.filter = '';
nextSlide.style.opacity = '1';
}
/**
* Utility: wait for milliseconds
*/
wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Get current slide info
*/
getCurrentSlide() {
return this.slides[this.currentIndex];
}
/**
* Get slide count
*/
getSlideCount() {
return this.slides.length;
}
}
// Export helper function
export function createSlideRenderer(container, options = {}) {
return new SlideRenderer(container, options);
}