feat: Content Director engine + report diagnosis/roadmap/KPI overhaul + PDF export fix
- Content Director (contentDirector.ts): deterministic 4-week editorial calendar engine — pillar-service matrix, channel-format slots, weekly themes (브랜드 정비 → 콘텐츠 엔진 → 소셜 증거 → 전환 최적화) - transformPlan.ts: buildCalendar() delegates to Content Director with enrichment data (YouTube videos for repurposing) - transformReport.ts: buildTransformation() generates rich per-channel platform strategies; buildRoadmap() creates Foundation/Content Engine/ Optimization 3-phase plan; buildKpiDashboard() generates 10+ channel- specific metrics with targets - ProblemDiagnosis: clustered 3 core issues (brand/content/funnel) in glass cards + expandable detail list - RoadmapTimeline: Foundation/Content Engine/Optimization structure - KPIDashboard: formatKpiValue() for human-readable numbers (150K, 1.5M) - YouTubeAudit: metric-based diagnosis rows (subscriber ratio, upload freq) - ContentCalendar: week theme labels, channel symbols, compact entries - useExportPDF: triggerAllAnimations() scrolls all sections to fire whileInView before capture; isDarkSection() keeps dark sections whole; forceVisible() 2-pass opacity/transform override Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e32b8766de
commit
da267fd744
12 files changed
+1159
-340
No files matched your search
+164
-93
@@ -1,45 +1,141 @@
|
||||
import { useState, useCallback } from 'react';
|
||||
|
||||
/**
|
||||
* PDF Export Strategy:
|
||||
* Instead of capturing each section as one giant canvas and slicing mid-content,
|
||||
* we break each section into smaller "sub-blocks" (header, cards, rows, etc.)
|
||||
* and treat each sub-block atomically — never splitting a block across pages.
|
||||
* PDF Export — block-based strategy.
|
||||
*
|
||||
* For sections smaller than one page: render as a single image.
|
||||
* For sections taller than one page: find natural break points (child elements)
|
||||
* and capture each child separately, starting a new page when needed.
|
||||
* Each section is broken into atomic sub-blocks that are never split across
|
||||
* pages. Dark sections with Framer Motion animations require special handling:
|
||||
* we scroll every section into view to trigger `whileInView`, then force all
|
||||
* inline opacity/transform to their final state before html2canvas captures.
|
||||
*/
|
||||
|
||||
function isDarkSection(section: HTMLElement): boolean {
|
||||
const bg = window.getComputedStyle(section).backgroundColor;
|
||||
if (!bg || bg === 'rgba(0, 0, 0, 0)' || bg === 'transparent') return false;
|
||||
// Parse rgb(r, g, b) — dark if luminance is low
|
||||
const match = bg.match(/(\d+),\s*(\d+),\s*(\d+)/);
|
||||
if (!match) return section.classList.contains('bg-[#0A1128]');
|
||||
const [, r, g, b] = match.map(Number);
|
||||
return (r * 0.299 + g * 0.587 + b * 0.114) < 50;
|
||||
}
|
||||
|
||||
function collectSubBlocks(section: HTMLElement): HTMLElement[] {
|
||||
// If the section is short enough to fit one page (~1200px at scale 2),
|
||||
// treat the whole section as one block
|
||||
if (section.offsetHeight <= 900) {
|
||||
return [section];
|
||||
}
|
||||
// Dark sections should be captured whole — sub-blocks would lose the dark background
|
||||
if (isDarkSection(section)) return [section];
|
||||
|
||||
if (section.offsetHeight <= 900) return [section];
|
||||
|
||||
// Otherwise, break into sub-blocks:
|
||||
// Look for the section header (first child with dark bg or title)
|
||||
// and then each subsequent child
|
||||
const children = Array.from(section.children) as HTMLElement[];
|
||||
if (children.length <= 1) {
|
||||
return [section]; // Can't break further
|
||||
}
|
||||
if (children.length <= 1) return [section];
|
||||
|
||||
// Group: keep section header + subtitle together as first block,
|
||||
// then each remaining child as its own block
|
||||
const blocks: HTMLElement[] = [];
|
||||
|
||||
// Find the section-level wrapper — if it has a header area and a content area
|
||||
// For our SectionWrapper pattern: first child is usually the header area
|
||||
for (const child of children) {
|
||||
if (child.offsetHeight === 0 || child.offsetWidth === 0) continue;
|
||||
blocks.push(child);
|
||||
}
|
||||
|
||||
return blocks.length > 0 ? blocks : [section];
|
||||
}
|
||||
|
||||
/**
|
||||
* Scroll through every section to trigger all `whileInView` animations,
|
||||
* then wait for them to settle. This ensures Framer Motion has applied
|
||||
* its final inline styles before we snapshot.
|
||||
*/
|
||||
async function triggerAllAnimations(contentEl: HTMLElement): Promise<void> {
|
||||
const sections = Array.from(contentEl.children) as HTMLElement[];
|
||||
|
||||
for (const section of sections) {
|
||||
section.scrollIntoView({ behavior: 'instant' as ScrollBehavior });
|
||||
// Give framer-motion time to observe intersection and apply styles
|
||||
await new Promise((r) => setTimeout(r, 80));
|
||||
|
||||
// Also scroll sub-children into view for nested whileInView
|
||||
const motionChildren = section.querySelectorAll('[style]');
|
||||
for (let i = 0; i < Math.min(motionChildren.length, 20); i++) {
|
||||
(motionChildren[i] as HTMLElement).scrollIntoView({ behavior: 'instant' as ScrollBehavior });
|
||||
await new Promise((r) => setTimeout(r, 30));
|
||||
}
|
||||
}
|
||||
|
||||
// Scroll back to top and let everything settle
|
||||
window.scrollTo(0, 0);
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
}
|
||||
|
||||
/**
|
||||
* Force ALL elements to be fully visible — opacity 1, no transforms.
|
||||
* This catches any remaining elements that whileInView didn't reach.
|
||||
*/
|
||||
function forceVisible(contentEl: HTMLElement): (() => void) {
|
||||
document.documentElement.style.setProperty('--motion-duration', '0s');
|
||||
|
||||
const saved: { el: HTMLElement; cssText: string }[] = [];
|
||||
|
||||
contentEl.querySelectorAll('*').forEach((node) => {
|
||||
const el = node as HTMLElement;
|
||||
const s = el.style;
|
||||
|
||||
// Check both inline and computed
|
||||
const needsFix =
|
||||
(s.opacity !== '' && s.opacity !== '1') ||
|
||||
(s.transform !== '' && s.transform !== 'none') ||
|
||||
s.visibility === 'hidden';
|
||||
|
||||
if (needsFix) {
|
||||
saved.push({ el, cssText: s.cssText });
|
||||
s.opacity = '1';
|
||||
s.transform = 'none';
|
||||
s.visibility = 'visible';
|
||||
}
|
||||
});
|
||||
|
||||
// Second pass: catch computed opacity < 1 (motion might set via class)
|
||||
contentEl.querySelectorAll('*').forEach((node) => {
|
||||
const el = node as HTMLElement;
|
||||
const computed = window.getComputedStyle(el);
|
||||
if (parseFloat(computed.opacity) < 0.99) {
|
||||
if (!saved.find((s) => s.el === el)) {
|
||||
saved.push({ el, cssText: el.style.cssText });
|
||||
}
|
||||
el.style.opacity = '1';
|
||||
el.style.transform = 'none';
|
||||
}
|
||||
});
|
||||
|
||||
return () => {
|
||||
saved.forEach(({ el, cssText }) => {
|
||||
el.style.cssText = cssText;
|
||||
});
|
||||
document.documentElement.style.removeProperty('--motion-duration');
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* CSS overrides for export: unwrap overflow, prevent clipping.
|
||||
*/
|
||||
function addExportCSS(): (() => void) {
|
||||
const style = document.createElement('style');
|
||||
style.id = 'pdf-export-overrides';
|
||||
style.textContent = `
|
||||
[data-report-content] .overflow-x-auto,
|
||||
[data-plan-content] .overflow-x-auto {
|
||||
overflow: visible !important;
|
||||
flex-wrap: wrap !important;
|
||||
}
|
||||
[data-report-content] .scrollbar-thin,
|
||||
[data-plan-content] .scrollbar-thin {
|
||||
overflow: visible !important;
|
||||
}
|
||||
[data-report-content] .shrink-0,
|
||||
[data-plan-content] .shrink-0 {
|
||||
flex-shrink: 1 !important;
|
||||
min-width: 0 !important;
|
||||
}
|
||||
`;
|
||||
document.head.appendChild(style);
|
||||
return () => style.remove();
|
||||
}
|
||||
|
||||
export function useExportPDF() {
|
||||
const [isExporting, setIsExporting] = useState(false);
|
||||
|
||||
@@ -52,21 +148,21 @@ export function useExportPDF() {
|
||||
import('jspdf'),
|
||||
]);
|
||||
|
||||
// 1. Force all animations to their final state
|
||||
document.documentElement.style.setProperty('--motion-duration', '0s');
|
||||
const animatedEls: { el: HTMLElement; origOpacity: string; origTransform: string }[] = [];
|
||||
document.querySelectorAll('[style*="opacity"]').forEach((el) => {
|
||||
const htmlEl = el as HTMLElement;
|
||||
animatedEls.push({
|
||||
el: htmlEl,
|
||||
origOpacity: htmlEl.style.opacity,
|
||||
origTransform: htmlEl.style.transform,
|
||||
});
|
||||
htmlEl.style.opacity = '1';
|
||||
htmlEl.style.transform = 'none';
|
||||
});
|
||||
const contentEl =
|
||||
(document.querySelector('[data-report-content]') as HTMLElement) ||
|
||||
(document.querySelector('[data-plan-content]') as HTMLElement);
|
||||
if (!contentEl) throw new Error('Report content element not found');
|
||||
|
||||
// 2. Hide UI-only elements
|
||||
// Step 1: Scroll through all sections to trigger whileInView animations
|
||||
await triggerAllAnimations(contentEl);
|
||||
|
||||
// Step 2: Force everything visible (catch stragglers)
|
||||
const restoreVisible = forceVisible(contentEl);
|
||||
|
||||
// Step 3: CSS overrides
|
||||
const restoreCSS = addExportCSS();
|
||||
|
||||
// Step 4: Hide UI elements
|
||||
const hideSelectors = [
|
||||
'[data-report-nav]',
|
||||
'[data-plan-nav]',
|
||||
@@ -74,22 +170,18 @@ export function useExportPDF() {
|
||||
'[data-cta-card]',
|
||||
'[data-no-print]',
|
||||
];
|
||||
const hiddenEls: HTMLElement[] = [];
|
||||
const hiddenEls: { el: HTMLElement; display: string }[] = [];
|
||||
hideSelectors.forEach((sel) => {
|
||||
document.querySelectorAll(sel).forEach((el) => {
|
||||
hiddenEls.push(el as HTMLElement);
|
||||
(el as HTMLElement).style.display = 'none';
|
||||
const htmlEl = el as HTMLElement;
|
||||
hiddenEls.push({ el: htmlEl, display: htmlEl.style.display });
|
||||
htmlEl.style.display = 'none';
|
||||
});
|
||||
});
|
||||
|
||||
// 3. Find the content wrapper
|
||||
const contentEl =
|
||||
(document.querySelector('[data-report-content]') as HTMLElement) ||
|
||||
(document.querySelector('[data-plan-content]') as HTMLElement);
|
||||
await new Promise((r) => setTimeout(r, 150));
|
||||
|
||||
if (!contentEl) throw new Error('Report content element not found');
|
||||
|
||||
// 4. Setup PDF
|
||||
// Step 5: PDF generation
|
||||
const pdf = new jsPDF('p', 'mm', 'a4');
|
||||
const pageWidth = 210;
|
||||
const pageHeight = 297;
|
||||
@@ -98,55 +190,47 @@ export function useExportPDF() {
|
||||
const footerSpace = 10;
|
||||
const maxContentY = pageHeight - margin - footerSpace;
|
||||
let currentY = margin;
|
||||
let needsNewPageForNext = false;
|
||||
|
||||
const sections = Array.from(contentEl.children) as HTMLElement[];
|
||||
|
||||
for (let sIdx = 0; sIdx < sections.length; sIdx++) {
|
||||
const section = sections[sIdx];
|
||||
for (const section of sections) {
|
||||
if (section.offsetHeight === 0 || section.offsetWidth === 0) continue;
|
||||
if (window.getComputedStyle(section).display === 'none') continue;
|
||||
|
||||
// Decide: capture whole section or break into sub-blocks
|
||||
const blocks = collectSubBlocks(section);
|
||||
|
||||
for (let bIdx = 0; bIdx < blocks.length; bIdx++) {
|
||||
const block = blocks[bIdx];
|
||||
for (const block of blocks) {
|
||||
if (block.offsetHeight === 0) continue;
|
||||
|
||||
const canvas = await html2canvas(block, {
|
||||
scale: 2,
|
||||
useCORS: true,
|
||||
logging: false,
|
||||
backgroundColor: null, // Preserve dark section backgrounds
|
||||
backgroundColor: null,
|
||||
windowWidth: 1280,
|
||||
removeContainer: true,
|
||||
});
|
||||
|
||||
const blockHeightMM = (canvas.height * usableWidth) / canvas.width;
|
||||
|
||||
// Will this block fit on the current page?
|
||||
// New page if block doesn't fit
|
||||
if (currentY + blockHeightMM > maxContentY && currentY > margin + 5) {
|
||||
// Doesn't fit — start a new page
|
||||
pdf.addPage();
|
||||
currentY = margin;
|
||||
}
|
||||
|
||||
// If block is STILL taller than one full page, we must slice it
|
||||
// Tall block: slice across pages
|
||||
if (blockHeightMM > maxContentY - margin) {
|
||||
const pxPerMM = canvas.width / usableWidth;
|
||||
let srcY = 0;
|
||||
let remainPx = canvas.height;
|
||||
let isFirstSlice = true;
|
||||
let isFirst = true;
|
||||
|
||||
while (remainPx > 0) {
|
||||
if (!isFirstSlice) {
|
||||
pdf.addPage();
|
||||
currentY = margin;
|
||||
}
|
||||
isFirstSlice = false;
|
||||
if (!isFirst) { pdf.addPage(); currentY = margin; }
|
||||
isFirst = false;
|
||||
|
||||
const availMM = maxContentY - currentY;
|
||||
const availPx = availMM * pxPerMM;
|
||||
const availPx = (maxContentY - currentY) * pxPerMM;
|
||||
const sliceH = Math.min(remainPx, availPx);
|
||||
|
||||
const sliceCanvas = document.createElement('canvas');
|
||||
@@ -154,52 +238,39 @@ export function useExportPDF() {
|
||||
sliceCanvas.height = Math.ceil(sliceH);
|
||||
const ctx = sliceCanvas.getContext('2d');
|
||||
if (ctx) {
|
||||
ctx.drawImage(
|
||||
canvas,
|
||||
0, srcY, canvas.width, Math.ceil(sliceH),
|
||||
0, 0, canvas.width, Math.ceil(sliceH),
|
||||
);
|
||||
ctx.drawImage(canvas, 0, srcY, canvas.width, Math.ceil(sliceH), 0, 0, canvas.width, Math.ceil(sliceH));
|
||||
}
|
||||
const sliceData = sliceCanvas.toDataURL('image/jpeg', 0.9);
|
||||
const sliceMM = sliceH / pxPerMM;
|
||||
|
||||
pdf.addImage(sliceData, 'JPEG', margin, currentY, usableWidth, sliceMM);
|
||||
const sliceMM = sliceH / pxPerMM;
|
||||
pdf.addImage(sliceCanvas.toDataURL('image/jpeg', 0.9), 'JPEG', margin, currentY, usableWidth, sliceMM);
|
||||
currentY += sliceMM;
|
||||
srcY += sliceH;
|
||||
remainPx -= sliceH;
|
||||
}
|
||||
} else {
|
||||
// Normal case: block fits on current page
|
||||
const imgData = canvas.toDataURL('image/jpeg', 0.9);
|
||||
pdf.addImage(imgData, 'JPEG', margin, currentY, usableWidth, blockHeightMM);
|
||||
pdf.addImage(canvas.toDataURL('image/jpeg', 0.9), 'JPEG', margin, currentY, usableWidth, blockHeightMM);
|
||||
currentY += blockHeightMM;
|
||||
}
|
||||
}
|
||||
|
||||
// Small gap between sections
|
||||
currentY += 2;
|
||||
currentY += 2; // Gap between sections
|
||||
}
|
||||
|
||||
// 6. Restore hidden elements
|
||||
hiddenEls.forEach((el) => { el.style.display = ''; });
|
||||
animatedEls.forEach(({ el, origOpacity, origTransform }) => {
|
||||
el.style.opacity = origOpacity;
|
||||
el.style.transform = origTransform;
|
||||
});
|
||||
document.documentElement.style.removeProperty('--motion-duration');
|
||||
// Step 6: Restore
|
||||
hiddenEls.forEach(({ el, display }) => { el.style.display = display; });
|
||||
restoreCSS();
|
||||
restoreVisible();
|
||||
|
||||
// 7. Add page footers
|
||||
// Step 7: Footers
|
||||
const totalPages = pdf.getNumberOfPages();
|
||||
const footerLabel = filename.includes('Plan')
|
||||
? 'INFINITH Marketing Plan'
|
||||
: 'INFINITH Marketing Intelligence Report';
|
||||
for (let i = 1; i <= totalPages; i++) {
|
||||
pdf.setPage(i);
|
||||
pdf.setFontSize(7);
|
||||
pdf.setTextColor(180);
|
||||
pdf.text(
|
||||
`INFINITH Marketing Intelligence Report | Page ${i} / ${totalPages}`,
|
||||
pageWidth / 2,
|
||||
pageHeight - 5,
|
||||
{ align: 'center' },
|
||||
);
|
||||
pdf.text(`${footerLabel} | Page ${i} / ${totalPages}`, pageWidth / 2, pageHeight - 5, { align: 'center' });
|
||||
}
|
||||
|
||||
pdf.save(`${filename}.pdf`);
|
||||
|
||||
Reference in new issue
Block a user