Skip to content

Commit

Permalink
Merge Wistaro's generator in. Improve CSS
Browse files Browse the repository at this point in the history
  • Loading branch information
adriweb committed May 7, 2017
1 parent 3d2f764 commit 11fbed4
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 50 deletions.
154 changes: 154 additions & 0 deletions generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
Course Generator V1.0
Authors:
William "Wistaro" Romiguières (this script)
Adrien "Adriweb" Bertrand (this script + tivars_lib_cpp)
Help: tiplanet.org
*/

let newCourse = "";

let withMenu = false;

let chosenCalc = 'CE';
let lastFilePath = null;

let nbParties = 1;
let parties = [];

function isInt(value) {
return !isNaN(value) && (x => (x|0) === x)(parseFloat(value))
}

function startWizard()
{
newCourse = "0→Xmin:0→Ymin:1→∆X:1→∆Y:AxesOff:BackgroundOff:ClrDraw:Trace:\n";
$("#myText0").remove();

do {
nbParties = prompt('Combien de parties ? (1-6)');
} while (!isInt(nbParties) || nbParties < 1 || nbParties > 6);

withMenu = true;

const menuName = prompt('Titre de votre menu (sommaire des parties) ?', 'Sommaire');
newCourse += `\nLbl 0\nMenu("${menuName.toUpperCase()}"`;

for (var i = 0; i < nbParties; i++)
{
parties[i] = prompt(`Titre de la partie n°${(i+1)}`);
newCourse += `,"${parties[i].toUpperCase()}",${(i+1)}`;
$('.step2').fadeIn(1000).append(`<textarea id="myText${i}" title="Votre texte ici" name="myText${i}" placeholder="Écrivez votre texte dans cette zone">Votre cours, partie ${(i+1)}! </textarea><br />`);
}
registerPreviewHandler();

newCourse += ",\"Quitter\",Q\n";
}

function generateCourse()
{
let inputStr = "";

for (var j = 0; j < nbParties; j++)
{
newCourse += `Lbl ${(j+1)}\n`;

inputStr = $(`#myText${j}`).val();

inputStr = inputStr.replace(/"/g, "''") // replacing " by ''
.replace(//g, "->") // replacing arrow
.replace(/ /g, " ") // 2 spaces look better on screen
.replace(/\n/g, " "); //replacing return

const MIN_PAS_Y = 12;
const MIN_PAS_X = 0;
const MAX_CHAR_LINE = 33;
const LAST_LINE = 13;

let Cptlines = 0;
let CptlinesSave = 0;
let CptPages = 1;
let YText = 0;
let XText = 0;

newCourse += "\nDelVar V\n";

let currentSubStr = "";

for (var i = 0; i < inputStr.length; i++)
{
if ((i % MAX_CHAR_LINE) === 0)
{
currentSubStr = inputStr.substring(i, i + MAX_CHAR_LINE);

/*FILTERING*/
//currentSubStr = currentSubStr.replace("\"","''");
//currentSubStr = filter(currentSubStr);

//newCourse+= "Text(" + YText + "," + XText + ",\"" + currentSubStr + "\n"
newCourse += `"${currentSubStr}\nprgmZTEXT\n`;

Cptlines++;
CptlinesSave++;
YText += MIN_PAS_Y;
}
if (Cptlines === LAST_LINE)
{
newCourse += "Pause :ClrDraw:DelVar V\n";
YText = 0;
Cptlines = 0;
CptPages++;
}

}

newCourse += "Pause :ClrDraw\n";
newCourse += withMenu ? "Goto 0\n" : "Disp \"";
}

newCourse += "\nLbl Q\nClrDraw:Disp \"";

const titlePrgm = $("#prgmName").val();
//newCourse+="\n\" Titre:"+titlePrgm+"→Y₁\n";

create8xpFile(titlePrgm, newCourse);
}

function create8xpFile(title, content)
{
try
{
const lib = Module;

if (lastFilePath !== null)
{
FS.unlink(lastFilePath);
lastFilePath = null;
}

const txt = unescape(encodeURIComponent(content)); // encoding issues...

const prgm = lib.TIVarFile.createNew(lib.TIVarType.createFromName("Program"), title, lib.TIModel.createFromName(chosenCalc));

const options = new lib.options_t();
//options.set('useShortestTokens', 1);
// todo: shortestTokens on strings only

prgm.setContentFromString(txt, options);
const filePath = prgm.saveVarToFile("", title);
lastFilePath = filePath;

const file = FS.readFile(filePath, {encoding: 'binary'});
if (file)
{
const blob = new Blob([file], {type: 'application/octet-stream'});
window['saveAs'](blob, filePath.split('/').pop());
} else {
alert('Oops, something went wrong retrieving the generated .8xp file :(');
}

} catch (e)
{
alert('Hmmm. Une erreur est survenue. ' + e.toString());
}
}
76 changes: 28 additions & 48 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<script src="FileSaver.min.js" async></script>
<script src="tivars_test.js" async></script>
<script src="generator.js"></script>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
Expand All @@ -25,7 +26,17 @@
<h3>zText: convertisseur texte => 8xp</h3>

<form id="convForm">
<textarea id="myText" title="Your text here" name="myText" placeholder="Écrivez votre texte ici" required></textarea>
<section id="advancedCreator">
<p class="infos">Tapez maintenant votre cours dans chaque partie, puis cliquer sur générer.</p>
<div class="step2"></div>
<div class="infos"></div>
<p>N'oublie pas d'également transférer <a href="ressources/ZTEXT.8xp" target="_blank" download>le programme "ZTEXT"</a> sur ta calculatrice!</p>
</section>
<section id="simpleCreator">
Vous souhaitez organisez votre cours en plusieurs parties avec un menu ? <button id="toggleAdvanced" class="btn btn-success btn-xs">Cliquez-ici</button>
<br><br>
<textarea id="myText0" title="Votre texte ici" name="myText0" placeholder="Écrivez votre texte dans cette zone" required></textarea>
</section>
<div class="row">
<div class="col-xs-7">
<label>
Expand Down Expand Up @@ -78,53 +89,19 @@ <h4>Aperçu</h4>
</div>

<script>
let chosenCalc = 'CE';

$("#convForm").on('submit', function(e)

$("#toggleAdvanced").on('click', function(e)
{
e.preventDefault();
$("#simpleCreator, #advancedCreator").toggle();
setTimeout(startWizard, 100);
});

const txt = unescape(encodeURIComponent(document.getElementById("myText").value)); // encoding issues...
const name = document.getElementById("prgmName").value;

if (txt.length === 0)
{
alert('Empty text!');
return false;
}

let lastFilePath = null;
try
{
const lib = Module;

if (lastFilePath !== null) {
FS.unlink(lastFilePath);
lastFilePath = null;
}

const prgm = lib.TIVarFile.createNew(lib.TIVarType.createFromName("Program"), name, lib.TIModel.createFromName(chosenCalc));

const options = new lib.options_t();
options.set('useShortestTokens', 1);

prgm.setContentFromString(txt, options);
const filePath = prgm.saveVarToFile("", name);
lastFilePath = filePath;

const file = FS.readFile(filePath, {encoding: 'binary'});
if (file)
{
const blob = new Blob([file], {type: 'application/octet-stream'});
window['saveAs'](blob, filePath.split('/').pop());
} else {
alert('Oops, something went wrong retrieving the generated .8xp file :(');
}

} catch (e) {
alert('Hmmm. Une erreur est survenue. ' + e.toString());
}

$("#convForm").on('submit', function(e)
{
e.preventDefault();
generateCourse();
return false;
});

Expand All @@ -137,11 +114,14 @@ <h4>Aperçu</h4>
})
.filter("[value='83PCE']").click(); /* initial/default value */

$("#myText").on('keyup', function()
function registerPreviewHandler()
{
const text = $(this).val();
$("#calcPreview .content").text(text);
});
$("#myText0").on('keyup keypress blur change', function ()
{
$("#calcPreview .content").text($(this).val());
});
}
registerPreviewHandler();

</script>

Expand Down
Binary file added ressources/ZTEXT.8xp
Binary file not shown.
10 changes: 8 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
font-weight: bold;
}

#myText {
textarea {
width: 100%;
max-width: 100%;
min-height: 200px;
min-height: 180px;
}

#genButton {
Expand Down Expand Up @@ -65,7 +65,9 @@
box-shadow: 0 0 1px 1px #353535
}
#calcPreview .content {
height: 100%;
font-family: 'Geo',sans-serif;
overflow: hidden;
}

#calcPreview.calc83PCE {
Expand Down Expand Up @@ -113,4 +115,8 @@
line-height: 7px;
color: black;
font-weight: bold;
}

#advancedCreator {
display: none;
}

0 comments on commit 11fbed4

Please sign in to comment.