Skip to content

Commit 120d7e5

Browse files
committed
Merge pull request sparksuite#233 from ryan-codingintrigue/development
Add support for custom counts in statusbar
2 parents 91a27e3 + 7904c44 commit 120d7e5

File tree

2 files changed

+91
-28
lines changed

2 files changed

+91
-28
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ simplemde.value("This text will appear in the editor");
100100
- **spellChecker**: If set to `false`, disable the spell checker. Defaults to `true`.
101101
- **status**: If set to `false`, hide the status bar. Defaults to `true`.
102102
- Optionally, you can set an array of status bar elements to include, and in what order.
103+
- **statusCustom**: An object of custom elements to add to the statusbar
103104
- **tabSize**: If set, customize the tab size. Defaults to `2`.
104105
- **toolbar**: If set to `false`, hide the toolbar. Defaults to the [array of icons](#toolbar-icons).
105106
- **toolbarTips**: If set to `false`, disable toolbar button tips. Defaults to `true`.
@@ -155,6 +156,17 @@ var simplemde = new SimpleMDE({
155156
spellChecker: false,
156157
status: false,
157158
status: ["autosave", "lines", "words", "cursor"], // Optional usage
159+
statusCustom: {
160+
countKeyStrokes: { // Counts the total number of keystrokes
161+
defaultValue: function(span) {
162+
this.keystrokes = 0;
163+
span.innerHTML = "0 Keystrokes";
164+
},
165+
onUpdate: function(span) {
166+
span.innerHTML = ++this.keystrokes + " Keystrokes";
167+
}
168+
}
169+
},
158170
tabSize: 4,
159171
toolbar: false,
160172
toolbarTips: false,

src/js/simplemde.js

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,9 @@ function SimpleMDE(options) {
10191019
if(!options.hasOwnProperty("status")) {
10201020
options.status = ["autosave", "lines", "words", "cursor"];
10211021
}
1022+
if(!options.hasOwnProperty("statusCustom")) {
1023+
options.statusCustom = {};
1024+
}
10221025

10231026

10241027
// Add default preview rendering function
@@ -1172,7 +1175,7 @@ SimpleMDE.prototype.render = function(el) {
11721175
if(options.toolbar !== false) {
11731176
this.createToolbar();
11741177
}
1175-
if(options.status !== false) {
1178+
if(options.status !== false || options.statusCustom) {
11761179
this.createStatusbar();
11771180
}
11781181
if(options.autosave != undefined && options.autosave.enabled === true) {
@@ -1372,47 +1375,95 @@ SimpleMDE.prototype.createToolbar = function(items) {
13721375
};
13731376

13741377
SimpleMDE.prototype.createStatusbar = function(status) {
1378+
var customOptions = this.options.statusCustom;
13751379
status = status || this.options.status;
13761380
var options = this.options;
13771381

13781382
if(!status || status.length === 0) return;
13791383

1380-
var bar = document.createElement("div");
1381-
bar.className = "editor-statusbar";
1384+
// Copy the defaults if the status is a boolean true
1385+
if(status === true) status = ["autosave", "lines", "words", "cursor"];
13821386

1383-
var pos, cm = this.codemirror;
1384-
for(var i = 0; i < status.length; i++) {
1385-
(function(name) {
1386-
var el = document.createElement("span");
1387-
el.className = name;
1388-
if(name === "words") {
1389-
el.innerHTML = "0";
1390-
cm.on("update", function() {
1391-
el.innerHTML = wordCount(cm.getValue());
1392-
});
1393-
} else if(name == "characters") {
1387+
// Set up the in-built actions: autosave, lines, words, cursor
1388+
var actions = {};
1389+
var i, name, onUpdate, defaultValue;
1390+
for(i = 0; i < status.length; i++) {
1391+
name = status[i];
1392+
1393+
if(name === "words") {
1394+
defaultValue = function(el) {
13941395
el.innerHTML = "0";
1395-
cm.on("update", function() {
1396-
el.innerHTML = cm.getValue().length;
1397-
});
1398-
} else if(name === "lines") {
1396+
};
1397+
onUpdate = function(el) {
1398+
el.innerHTML = wordCount(cm.getValue());
1399+
};
1400+
} else if(name === "lines") {
1401+
defaultValue = function(el) {
13991402
el.innerHTML = "0";
1400-
cm.on("update", function() {
1401-
el.innerHTML = cm.lineCount();
1402-
});
1403-
} else if(name === "cursor") {
1403+
};
1404+
onUpdate = function(el) {
1405+
el.innerHTML = cm.lineCount();
1406+
};
1407+
} else if(name === "cursor") {
1408+
defaultValue = function(el) {
14041409
el.innerHTML = "0:0";
1405-
cm.on("cursorActivity", function() {
1406-
pos = cm.getCursor();
1407-
el.innerHTML = pos.line + ":" + pos.ch;
1408-
});
1409-
} else if(name === "autosave") {
1410+
};
1411+
onUpdate = function(el) {
1412+
pos = cm.getCursor();
1413+
el.innerHTML = pos.line + ":" + pos.ch;
1414+
};
1415+
} else if(name === "autosave") {
1416+
defaultValue = function(el) {
14101417
if(options.autosave != undefined && options.autosave.enabled === true) {
14111418
el.setAttribute("id", "autosaved");
14121419
}
1420+
};
1421+
}
1422+
actions[name] = {
1423+
onUpdate: onUpdate,
1424+
defaultValue: defaultValue
1425+
};
1426+
}
1427+
1428+
// Iterate any user-provided options
1429+
for(var key in customOptions) {
1430+
if(customOptions.hasOwnProperty(key)) {
1431+
var thisOption = customOptions[key];
1432+
1433+
// Copy the option into the combined actions
1434+
// This will allow the user to override the defaults
1435+
actions[key] = {
1436+
defaultValue: thisOption.defaultValue,
1437+
onUpdate: thisOption.onUpdate
1438+
};
1439+
}
1440+
}
1441+
1442+
var bar = document.createElement("div");
1443+
bar.className = "editor-statusbar";
1444+
1445+
var pos, cm = this.codemirror;
1446+
// Create a new span for each action
1447+
for(name in actions) {
1448+
if(actions.hasOwnProperty(name)) {
1449+
var el = document.createElement("span");
1450+
el.className = name;
1451+
// Ensure the defaultValue is a function
1452+
if(typeof actions[name].defaultValue === "function") {
1453+
actions[name].defaultValue(el);
1454+
}
1455+
// Ensure the onUpdate is a function
1456+
if(typeof actions[name].onUpdate === "function") {
1457+
// Create a closure around the span and the name
1458+
// of the current action, then execute the onUpdate handler
1459+
cm.on("update", (function(el, name) {
1460+
return function() {
1461+
actions[name].onUpdate(el);
1462+
};
1463+
}(el, name)));
14131464
}
14141465
bar.appendChild(el);
1415-
})(status[i]);
1466+
}
14161467
}
14171468

14181469
var cmWrapper = this.codemirror.getWrapperElement();

0 commit comments

Comments
 (0)