feat: make the temporary html generated usable

This commit is contained in:
KyleUltimate 2025-04-24 21:14:45 +08:00
parent 54bddaf017
commit d0ea46ce85
2 changed files with 64 additions and 3 deletions

View File

@ -40,7 +40,12 @@ impl HtmlSlide {
fn new(grid: TerminalGrid, content_manager: &mut ContentManager) -> Result<Self, ExportError> {
let mut rows = Vec::new();
for (y, row) in grid.rows.into_iter().enumerate() {
let mut finalized_row = "<div class=\"content-line\"><pre>".to_string();
let mut finalized_row = if y == 0 {
"<div class=\"content-line force-page-break\"><pre>"
} else {
"<div class=\"content-line\"><pre>"
}
.to_string();
let mut current_style = row.first().map(|c| c.style).unwrap_or_default();
let mut current_string = String::new();
let mut x = 0;
@ -154,12 +159,20 @@ impl PdfRender {
pub(crate) fn generate(self, pdf_path: &Path) -> Result<(), ExportError> {
let html_body = &self.html_body;
let script = include_str!("script.js");
let html = format!(
r#"<html>
r#"
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
{html_body}</body>
{html_body}
<script>
{script}
</script>
</body>
</html>"#
);
let width = (self.dimensions.columns as f64 * FONT_SIZE as f64 * FONT_SIZE_WIDTH).ceil();
@ -167,6 +180,9 @@ impl PdfRender {
let background_color = self.background_color.unwrap_or_else(|| "black".into());
let css = format!(
r"
.force-page-break {{
page-break-before: always
}}
pre {{
margin: 0;
padding: 0;
@ -191,6 +207,10 @@ impl PdfRender {
width: {width}px;
}}
.hidden {{
display: none;
}}
@page {{
margin: 0;
height: {height}px;

41
src/export/script.js Normal file
View File

@ -0,0 +1,41 @@
document.addEventListener('DOMContentLoaded', function() {
const allLines = document.querySelectorAll('body > div');
const pageBreakMarkers = document.querySelectorAll('.force-page-break');
let currentPageIndex = 0;
function showCurrentPage() {
const startIndexOfCurrentPage = Array.from(allLines).indexOf(pageBreakMarkers[currentPageIndex]);
let endIndexOfCurrentPage = allLines.length;
if (currentPageIndex < pageBreakMarkers.length - 1) {
endIndexOfCurrentPage = Array.from(allLines).indexOf(pageBreakMarkers[currentPageIndex + 1]);
}
allLines.forEach((line, index) => {
if (startIndexOfCurrentPage <= index && index < endIndexOfCurrentPage) {
line.classList.remove('hidden');
} else {
line.classList.add('hidden');
}
});
}
function handleKeyPress(event) {
if (event.key === 'ArrowLeft') {
if (currentPageIndex > 0) {
currentPageIndex--;
showCurrentPage();
}
} else if (event.key === 'ArrowRight') {
if (currentPageIndex < pageBreakMarkers.length - 1) {
currentPageIndex++;
showCurrentPage();
}
}
}
document.addEventListener('keydown', handleKeyPress);
showCurrentPage();
});