-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
168 lines (133 loc) · 5.37 KB
/
main.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
var Config = {};
Config.pixelsPerInch = 96;
Config.pageHeightInCentimeter = 29.7; // must match 'min-height' from 'css/sheets-of-paper-*.css' being used
Config.pageMarginBottomInCentimeter = 2; // must match 'padding-bottom' and 'margin-bottom' from 'css/sheets-of-paper-*.css' being used
var tgpy = 0; //This is the variable that will be used to determine which typography to use
var editor = document.getElementById("document");
var currentFormat = document.getElementById("currentFormat");
const timer = new Date();
var hh = timer.getHours();
var mm = timer.getMinutes();
window.addEventListener("DOMContentLoaded", function () {;
//Default value
document.getElementById("input").value = "title: Script name... \n subtitle: Author name... \n <<< \n # scene header \n = this is an action \n -- character \n _ this is a dialogue";
setTime();
updateDivContent();
});
//ScriptDown syntax
var syntax = {
"_":"<p style='margin-left:6.8cm; text-decoration:none; padding:0; margin-right:6.1cm;'>",
"---":"<p style='margin-left:6.8cm; text-decoration:none; padding:0; margin-right:6.1cm;'>",
"--":"<p style='margin-left:10.4cm;'><u class='character'>",
"=": "<p style='margin-left:4.3cm; margin-right:3.3cm;'>",
"#": "<p style='margin-left:2.5cm;'> <strong><u class='scenes'>",
"title:": "<p style='text-align:center;' class='cover'>",
"subtitle:": "<p style='text-align:center;'>",
"&" : "<p style='margin-left:15.2cm;'>",
"{": "<p style='margin-left:8.6cm;'>(",
"}": ")</p>",
"<<<": "<div class='pagebreak'></div>",
"\n": "</strong></u><br>"
};
//ScriptDown interpreter
var docContent;
var scenesCounter;
// Using setTimeout for improving the preview performance
function updateDivContent() {
setTimeout(()=>{
updatePreview();
}, 60);
}
function updatePreview (){
docContent = document.querySelector('textarea').value;
var t = docContent.replace(/_|---|--|=|#|title:|subtitle:|&|{|}|<<<|\n/gi, function(matched){
return syntax[matched];
});
document.getElementById('result').innerHTML = t;
//upper case characters
let elements = document.getElementsByClassName("character");
for (let i = 0; i < elements.length; i++) {
let text = elements[i].innerHTML;
let result = text.toUpperCase();
elements[i].innerHTML = result;
}
//upper case characters
let scenes = document.getElementsByClassName("scenes");
for (let i = 0; i < scenes.length; i++) {
let text = scenes[i].innerHTML;
let result = text.toUpperCase();
let n = i + 1;
scenes[i].innerHTML = n + ". " + result;
}
}
//Consideration clock
function setTime () {
const today = new Date();
let h = today.getHours() - hh;
let m = today.getMinutes() - mm;
let s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
h = checkTime(h);
document.getElementById("time").innerHTML = h + ":" + m + ":" + s;
setTimeout(() => {
setTime();
}, 1000);
}
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
//Header methods
// This sets the font for the scripting div
// (change the name of the method to "setFont" in the future)
function setFormat (index) {
var fontName = "";
switch (index){
case 0: fontName = "Consolas"; break;
case 1: fontName = "Courier new"; break;
case 2: fontName = "Hack";break;
case 3: fontName = "Arial"; break;
}
currentFormat.innerHTML = fontName;
document.getElementById("input").style = "font-family:" + fontName;
}
//File handling
function exportPDF () {
var content = document.getElementById("result").innerHTML;
// Result HTML
var elementHTML = '<head><title>LibreScript</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous"><link href="css/sheet.css" rel="stylesheet"></head><body><div style="margin-top:2.5cm;">'+content+'</div><script>window.print();window.close();</script><body/>';
const newWindow = window.open();
// write content to the new window's document.
newWindow.document.write(elementHTML);
}
function saveFile (){
const data = document.getElementById("input").value;
const blob = new Blob([data], { type: 'text/plain' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'NewScript.sd';
// Append the anchor element to the body
document.body.appendChild(a);
// Trigger a click event on the anchor element
a.click();
// Clean up by removing the anchor element
document.body.removeChild(a);
}
function openFile (){
let input = document.createElement('input');
input.type = 'file';
input.accept = '.sd' , '.anel , .txt , .html';
input.onchange = function() {
let file = input.files[0];
let reader = new FileReader();
reader.onload = function(e) {
// The contents of the file are accessible here
let fileContents = reader.result;
document.getElementById("input").value = fileContents;
// Perform operations with the file contents
};
reader.readAsText(file); // Read the file as text
};
input.click();
}