Repository navigation
Expand file tree
/
Copy pathexport.js
More file actions
120 lines (105 loc) · 3.97 KB
/
Copy pathexport.js
File metadata and controls
120 lines (105 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* Scriptor - Módulo de Exportação para Word (.docx Editável)
*/
// 1. Exportar a cena atual ativa no editor
function exportCurrentSceneToDocx() {
if (!projectState || !projectState.currentWritingSceneId || !projectState.sceneDrafts) {
alert('Nenhuma cena ativa encontrada para exportar.');
return;
}
const activeId = projectState.currentWritingSceneId;
const sceneDraft = projectState.sceneDrafts[activeId];
const sceneObj = (projectState.phase4 || []).find(s => s.id === activeId);
const title = sceneDraft && sceneDraft.title ? sceneDraft.title : (sceneObj ? sceneObj.titulo : 'Cena Sem Título');
const content = sceneDraft && sceneDraft.content ? sceneDraft.content : '<p><i>[Nenhum conteúdo escrito nesta cena ainda.]</i></p>';
const htmlContent = buildWordDocumentHtml(title, `<h1>${title}</h1>${content}`);
downloadDocxFile(htmlContent, title);
}
// 2. Exportar o livro inteiro (todas as cenas do rascunho em sequência)
function exportFullBookToDocx() {
if (!projectState || !projectState.phase4 || projectState.phase4.length === 0) {
alert('Não há cenas cadastradas no roteiro para exportar.');
return;
}
let bookHtml = `
<div style="text-align: center; margin-bottom: 40px;">
<h1 style="font-size: 28pt; color: #257d0d; margin-bottom: 8px;">${projectState.title || 'Rascunho do Livro'}</h1>
<p style="font-size: 11pt; color: #555555;">Gerado pelo Scriptor — Método Story Genius</p>
</div>
`;
const scenes = projectState.phase4;
const drafts = projectState.sceneDrafts || {};
scenes.forEach((scene, index) => {
const draft = drafts[scene.id] || {};
const title = draft.title || scene.titulo || `Cena ${index + 1}`;
const content = draft.content || '<p><i>[Cena sem conteúdo cadastrado.]</i></p>';
bookHtml += `
<div style="page-break-before: always; margin-top: 30px;">
<h2 style="font-size: 18pt; color: #257d0d; border-bottom: 1px solid #257d0d; padding-bottom: 6px; margin-bottom: 16px;">
Capítulo / Cena ${index + 1}: ${title}
</h2>
${content}
</div>
`;
});
const fullHtmlContent = buildWordDocumentHtml(projectState.title || 'Livro Completo', bookHtml);
downloadDocxFile(fullHtmlContent, projectState.title || 'Livro_Completo_Scriptor');
}
// Constrói o HTML estruturado com namespaces do Microsoft Office Word
function buildWordDocumentHtml(title, bodyContent) {
return `
<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>
<head>
<meta charset='utf-8'>
<title>${title}</title>
<!--[if gte mso 9]>
<xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:Zoom>100</w:Zoom>
<w:DoNotOptimizeForBrowser/>
</w:WordDocument>
</xml>
<![endif]-->
<style>
@page {
size: A4;
margin: 2.5cm 2.5cm 2.5cm 2.5cm;
}
body {
font-family: 'Calibri', 'Arial', sans-serif;
font-size: 12pt;
line-height: 1.5;
color: #000000;
}
p {
margin-bottom: 10pt;
text-align: justify;
}
h1, h2, h3 {
font-family: 'Calibri', 'Arial', sans-serif;
font-weight: bold;
}
</style>
</head>
<body>
${bodyContent}
</body>
</html>
`;
}
// Dispara o download com a extensão .docx e MIME correto para edição imediata
function downloadDocxFile(htmlString, baseFilename) {
const blob = new Blob(['\ufeff' + htmlString], {
type: 'application/vnd.ms-word;charset=utf-8'
});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
const safeFilename = baseFilename.replace(/[^a-zA-Z0-9à-úÀ-Ú\s-_]/g, '').trim() || 'documento_scriptor';
a.download = `${safeFilename}.docx`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}