-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (128 loc) · 4.28 KB
/
script.js
File metadata and controls
147 lines (128 loc) · 4.28 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const editor = document.getElementById('scriptInput');
const contentsList = document.getElementById('contentsList');
const addSceneBtn = document.getElementById('addScene');
const exportPDFBtn = document.getElementById('exportPDF');
let sceneCount = 0;
let expectCharacter = true;
const scenes = [];
/* ---------- Add Scene ---------- */
addSceneBtn.addEventListener('click', () => {
sceneCount++;
const sceneEl = document.createElement('div');
sceneEl.className = 'scene-title';
sceneEl.textContent = `Scene ${sceneCount}`;
editor.appendChild(sceneEl);
scenes.push({ title: sceneEl.textContent });
refreshContents();
expectCharacter = true;
});
function refreshContents() {
contentsList.innerHTML = '';
scenes.forEach((s, i) => {
const li = document.createElement('li');
li.textContent = `${i + 1}. ${s.title}`;
contentsList.appendChild(li);
});
}
/* ---------- Editor keyboard behaviors ---------- */
editor.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.ctrlKey) {
e.preventDefault();
const block = document.createElement('div');
block.classList.add('line-block');
if (expectCharacter) {
const char = document.createElement('div');
char.classList.add('character-name');
char.contentEditable = 'true';
char.setAttribute('data-placeholder', 'Character name');
block.appendChild(char);
expectCharacter = false;
} else {
const line = document.createElement('div');
line.classList.add('dialogue-line');
line.contentEditable = 'true';
line.setAttribute('data-placeholder', 'Dialogue');
block.appendChild(line);
expectCharacter = true;
}
editor.appendChild(block);
placeCaret(block.firstChild);
}
if ((e.key === 's' || e.key === 'S') && e.ctrlKey) {
e.preventDefault();
const stage = document.createElement('div');
stage.classList.add('stage-direction');
stage.contentEditable = 'true';
stage.setAttribute('data-placeholder', 'Stage direction');
editor.appendChild(stage);
placeCaret(stage);
}
});
/* ---------- PDF export ---------- */
exportPDFBtn.addEventListener('click', () => {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
// Title page
doc.setFontSize(22);
doc.text("My Script", 105, 40, { align: "center" });
// Contents
doc.addPage();
doc.setFontSize(16);
doc.text("Contents", 105, 40, { align: "center" });
let y = 60;
scenes.forEach((s, i) => {
doc.text(`${i + 1}. ${s.title}`, 105, y, { align: "center" });
y += 14;
});
// Script body
doc.addPage();
let bodyY = 60;
[...editor.children].forEach(node => {
const text = node.textContent.trim();
if (!text) return;
if (node.classList.contains('scene-title')) {
doc.setFont("Times","Bold");
doc.setFontSize(14);
doc.text(text, 105, bodyY, { align: "center" });
bodyY += 24; // spacing after scene title
}
else if (node.classList.contains('line-block')) {
const char = node.querySelector('.character-name');
const line = node.querySelector('.dialogue-line');
if (char && char.textContent.trim()) {
doc.setFont("Times","Bold");
doc.setFontSize(12);
doc.text(char.textContent.trim(), 105, bodyY, { align: "center" });
bodyY += 0; // spacing before dialogue
}
if (line && line.textContent.trim()) {
doc.setFont("Times","Normal");
doc.setFontSize(12);
doc.text(line.textContent.trim(), 105, bodyY, { align: "center" });
bodyY += 16;
}
}
else if (node.classList.contains('stage-direction')) {
doc.setFont("Times","Italic");
doc.setFontSize(12);
doc.text(text, 105, bodyY, { align: "center" });
bodyY += 16;
}
else {
doc.setFont("Times","Normal");
doc.setFontSize(12);
doc.text(text, 105, bodyY, { align: "center" });
bodyY += 16;
}
});
doc.save("script.pdf");
});
/* ---------- Helpers ---------- */
function placeCaret(el) {
const range = document.createRange();
const sel = window.getSelection();
range.selectNodeContents(el);
range.collapse(false);
sel.removeAllRanges();
sel.addRange(range);
}