Skip to content

Commit 5a91598

Browse files
author
Kinon
committed
Save LaTeX in localStorage
1 parent 882850e commit 5a91598

2 files changed

Lines changed: 33 additions & 12 deletions

File tree

TypeMath/app.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class Application
137137
};
138138
private importMode: boolean;
139139
private proofMode: boolean;
140+
private autosave: boolean;
140141
private clipboard: Token[] = [];
141142
private outputCurrentStyle: FontStyle[];
142143
private afterLayout: Token[];
@@ -197,7 +198,7 @@ class Application
197198
return this.macroOption.field !== null;
198199
}
199200

200-
constructor(field: JQuery, latex: JQuery, importer: JQuery, candy: JQuery, ghost: JQuery, select: JQuery, proof: JQuery)
201+
constructor(field: JQuery, latex: JQuery, importer: JQuery, candy: JQuery, ghost: JQuery, select: JQuery, autosave: JQuery, proof: JQuery)
201202
{
202203
this.field = field;
203204
this.importer = importer;
@@ -214,6 +215,12 @@ class Application
214215
{
215216
this.proofMode = proof.prop("checked");
216217
});
218+
autosave.change(e =>
219+
{
220+
this.autosave = autosave.prop("checked");
221+
if (!this.autosave)
222+
localStorage.setItem("latex", "");
223+
});
217224
proof.change();
218225
ghost.mousedown((e) => { this.dragFrom = { x: e.pageX, y: e.pageY }; this.jumpTo(this.dragFrom); });
219226
ghost.mousemove((e) => { this.dragSelect(e); });
@@ -222,9 +229,20 @@ class Application
222229
$(document.body).append(this._status = $("<pre/>").css("font-size", "9pt"));
223230
$(document.body).append(this._log = $("<pre/>").css("font-size", "9pt"));
224231

225-
this.render();
226-
227232
this.glyph = new GlyphFactory();
233+
234+
var src: string;
235+
if (src = localStorage.getItem("latex"))
236+
{
237+
console.log("load localStorage");
238+
var code = LaTeXReader.parse(src);
239+
this.interpretLaTeX(code);
240+
autosave.prop("checked", true);
241+
}
242+
else
243+
autosave.change();
244+
245+
this.render();
228246
}
229247
private enrichKeywords(): void
230248
{
@@ -281,20 +299,22 @@ class Application
281299
else
282300
this.selectedArea.css("visibility", "hidden");
283301

284-
var latex = [];
302+
var latex = "";
285303
for (var macro in this.macroTable)
286304
{
287305
var m = this.macroTable[macro];
288-
var s = ["\\newcommand{\\", macro, "}"];
306+
var s = "\\newcommand{\\" + macro + "}";
289307
if (m.argc > 0)
290-
s.push("[", m.argc.toString(), "]");
291-
s.push("{", LaTeX.trans(m.content), "}");
292-
latex.push(s.join(""));
308+
s += "[" + m.argc.toString() + "]";
309+
s += "{" + LaTeX.trans(m.content) + "}";
310+
latex += s + "\n";
293311
}
294312
if (latex.length > 0)
295-
latex.push("\n");
296-
latex.push(LaTeX.trans(this.formula, "", this.proofMode));
297-
this.latex.text(latex.join("\n"));
313+
latex += "\n";
314+
latex += LaTeX.trans(this.formula, "", this.proofMode);
315+
this.latex.text(latex);
316+
if (this.autosave)
317+
localStorage.setItem("latex", latex);
298318

299319
if (this._enableStatus)
300320
this._status.text([
@@ -2550,5 +2570,5 @@ var app;
25502570

25512571
window.onload = () =>
25522572
{
2553-
app = new Application($("#field"), $("#latex"), $("#importer"), $("#candy"), $("#ghost"), $("#select"), $("#proofMode"));
2573+
app = new Application($("#field"), $("#latex"), $("#importer"), $("#candy"), $("#ghost"), $("#select"), $("#autosave"), $("#proofMode"));
25542574
};

TypeMath/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
</head>
2121
<body>
2222
<div class="panel">
23+
<input id="autosave" type="checkbox"/> autosave
2324
<input id="proofMode" type="checkbox"/> proof mode
2425
| <a href="usage.html">usage</a>
2526
</div>

0 commit comments

Comments
 (0)