Skip to content

Commit

Permalink
Indicate when Firepad believes it has unsaved changes (#56).
Browse files Browse the repository at this point in the history
Also initial "loading..." indication.

Not sure it's water-tight wrt bugs like #85.
  • Loading branch information
cben committed May 19, 2015
1 parent 2d84da6 commit 2155569
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ <h1 style="display: inline">
</span>
</h1>
</div>
<div id="status_container">
<span id="status">loading...</span>
</div>

<form id="content"><textarea id="code" name="code"></textarea></form>
<script src="mathdown.js"></script>
Expand Down
12 changes: 12 additions & 0 deletions mathdown.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
}
.warning { color: orange; }

/* Horizontally center http://stackoverflow.com/a/10352525/239657 */
#status_container {
position: fixed; top: 1ex; left: 0; right: 0;
text-align: center;
}
#status {
display: inline-block;
padding: 0 0.5em; border-radius: 1ex;
color: black; background-color: yellow; opacity: 0.6;
font-size: 150%; font-style: italic;
}

@media print { #header { display: none; } }

#content { clear: both; }
Expand Down
24 changes: 24 additions & 0 deletions mathdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

"use strict";

// Logging
// =======

// Prevent errors on IE (but do strive to log somehow if IE Dev Tools are open).
function log() {
try {
Expand All @@ -27,6 +30,18 @@ MathJax.Message.filterText = function(text, n, msgType) {
return origFilterText(text, n, msgType);
}

var statusElement = document.getElementById("status");
// Show "flash" message (remove if "")
function setStatus(text) {
statusElement.innerHTML = "";
if(text) {
statusElement.appendChild(document.createTextNode(text));
}
}

// Random pad IDs
// ==============

// Return an int in [0, 62).
var random62 = (window.crypto && window.crypto.getRandomValues) ?
function() {
Expand Down Expand Up @@ -203,6 +218,8 @@ CodeMirror.hookMath(editor, MathJax);

var firepad = Firepad.fromCodeMirror(firepadRef, editor);
firepad.on("ready", function() {
setStatus("");

if (firepad.isHistoryEmpty()) {
firepad.setText(
"# Untitled\n" +
Expand Down Expand Up @@ -232,5 +249,12 @@ firepad.on("ready", function() {
editor.renderAllMath();
});
});
firepad.on("synced", function(isSynced) {
if(!isSynced) {
setStatus("unsaved");
} else {
setStatus("");
}
})

})()

0 comments on commit 2155569

Please sign in to comment.