Skip to content

Commit 6b6bb40

Browse files
author
Wes Cossick
committed
Refactor custom status bar items
1 parent a318e44 commit 6b6bb40

File tree

5 files changed

+303
-253
lines changed

5 files changed

+303
-253
lines changed

README.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +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
103+
- **statusCustom**: An object of custom elements to add to the status bar.
104104
- **tabSize**: If set, customize the tab size. Defaults to `2`.
105105
- **toolbar**: If set to `false`, hide the toolbar. Defaults to the [array of icons](#toolbar-icons).
106106
- **toolbarTips**: If set to `false`, disable toolbar button tips. Defaults to `true`.
@@ -156,17 +156,16 @@ var simplemde = new SimpleMDE({
156156
spellChecker: false,
157157
status: false,
158158
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-
},
159+
status: ["autosave", "lines", "words", "cursor", {
160+
className: "keystrokes",
161+
defaultValue: function(el) {
162+
this.keystrokes = 0;
163+
el.innerHTML = "0 Keystrokes";
164+
},
165+
onUpdate: function(el) {
166+
el.innerHTML = ++this.keystrokes + " Keystrokes";
167+
}
168+
}], // Another optional usage, with a custom status item that counts keystrokes
170169
tabSize: 4,
171170
toolbar: false,
172171
toolbarTips: false,
@@ -175,7 +174,7 @@ var simplemde = new SimpleMDE({
175174

176175
#### Toolbar icons
177176

178-
Below are the built-in toolbar icons (only some of which are enabled by default), which can be reorganized however you like. "Name" is the name of the icon, referenced in the JS. "Action" is either a function or a URL to open. "Class" is the class given to the icon. "Tooltip" is the small tooltip that appears via the `title=""` attribute. Note that shortcut hints are added automatically and reflect the specified "action" if it has a keybind assigned to it (ie. with the value of "action" set to `bold` and that of "tootip" set to "Bold", the final text the user will see - assuming the default shortcuts are unchanged - would be "Bold (Ctrl-B)").
177+
Below are the built-in toolbar icons (only some of which are enabled by default), which can be reorganized however you like. "Name" is the name of the icon, referenced in the JS. "Action" is either a function or a URL to open. "Class" is the class given to the icon. "Tooltip" is the small tooltip that appears via the `title=""` attribute. Note that shortcut hints are added automatically and reflect the specified action if it has a keybind assigned to it (i.e. with the value of `action` set to `bold` and that of `tooltip` set to `Bold`, the final text the user will see would be "Bold (Ctrl-B)").
179178

180179
Additionally, you can add a separator between any icons by adding `"|"` to the toolbar array.
181180

debug/simplemde.debug.js

Lines changed: 96 additions & 78 deletions
Large diffs are not rendered by default.

debug/simplemde.js

Lines changed: 95 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13884,9 +13884,6 @@ function SimpleMDE(options) {
1388413884
if(!options.hasOwnProperty("status")) {
1388513885
options.status = ["autosave", "lines", "words", "cursor"];
1388613886
}
13887-
if(!options.hasOwnProperty("statusCustom")) {
13888-
options.statusCustom = {};
13889-
}
1389013887

1389113888

1389213889
// Add default preview rendering function
@@ -14040,7 +14037,7 @@ SimpleMDE.prototype.render = function(el) {
1404014037
if(options.toolbar !== false) {
1404114038
this.createToolbar();
1404214039
}
14043-
if(options.status !== false || options.statusCustom) {
14040+
if(options.status !== false) {
1404414041
this.createStatusbar();
1404514042
}
1404614043
if(options.autosave != undefined && options.autosave.enabled === true) {
@@ -14240,97 +14237,118 @@ SimpleMDE.prototype.createToolbar = function(items) {
1424014237
};
1424114238

1424214239
SimpleMDE.prototype.createStatusbar = function(status) {
14243-
var customOptions = this.options.statusCustom;
14240+
// Initialize
1424414241
status = status || this.options.status;
1424514242
var options = this.options;
14243+
var cm = this.codemirror;
14244+
14245+
14246+
// Make sure the status variable is valid
14247+
if(!status || status.length === 0)
14248+
return;
1424614249

14247-
if(!status || status.length === 0) return;
1424814250

14249-
// Copy the defaults if the status is a boolean true
14250-
if(status === true) status = ["autosave", "lines", "words", "cursor"];
14251+
// Set up the built-in items
14252+
var items = [];
14253+
var i, onUpdate, defaultValue;
1425114254

14252-
// Set up the in-built actions: autosave, lines, words, cursor
14253-
var actions = {};
14254-
var i, name, onUpdate, defaultValue;
1425514255
for(i = 0; i < status.length; i++) {
14256-
name = status[i];
14256+
// Reset some values
14257+
onUpdate = undefined;
14258+
defaultValue = undefined;
1425714259

14258-
if(name === "words") {
14259-
defaultValue = function(el) {
14260-
el.innerHTML = "0";
14261-
};
14262-
onUpdate = function(el) {
14263-
el.innerHTML = wordCount(cm.getValue());
14264-
};
14265-
} else if(name === "lines") {
14266-
defaultValue = function(el) {
14267-
el.innerHTML = "0";
14268-
};
14269-
onUpdate = function(el) {
14270-
el.innerHTML = cm.lineCount();
14271-
};
14272-
} else if(name === "cursor") {
14273-
defaultValue = function(el) {
14274-
el.innerHTML = "0:0";
14275-
};
14276-
onUpdate = function(el) {
14277-
pos = cm.getCursor();
14278-
el.innerHTML = pos.line + ":" + pos.ch;
14279-
};
14280-
} else if(name === "autosave") {
14281-
defaultValue = function(el) {
14282-
if(options.autosave != undefined && options.autosave.enabled === true) {
14283-
el.setAttribute("id", "autosaved");
14284-
}
14285-
};
14286-
}
14287-
actions[name] = {
14288-
onUpdate: onUpdate,
14289-
defaultValue: defaultValue
14290-
};
14291-
}
1429214260

14293-
// Iterate any user-provided options
14294-
for(var key in customOptions) {
14295-
if(customOptions.hasOwnProperty(key)) {
14296-
var thisOption = customOptions[key];
14261+
// Handle if custom or not
14262+
if(typeof status[i] === "object") {
14263+
items.push({
14264+
className: status[i].className,
14265+
defaultValue: status[i].defaultValue,
14266+
onUpdate: status[i].onUpdate
14267+
});
14268+
} else {
14269+
var name = status[i];
1429714270

14298-
// Copy the option into the combined actions
14299-
// This will allow the user to override the defaults
14300-
actions[key] = {
14301-
defaultValue: thisOption.defaultValue,
14302-
onUpdate: thisOption.onUpdate
14303-
};
14271+
if(name === "words") {
14272+
defaultValue = function(el) {
14273+
el.innerHTML = "0";
14274+
};
14275+
onUpdate = function(el) {
14276+
el.innerHTML = wordCount(cm.getValue());
14277+
};
14278+
} else if(name === "lines") {
14279+
defaultValue = function(el) {
14280+
el.innerHTML = "0";
14281+
};
14282+
onUpdate = function(el) {
14283+
el.innerHTML = cm.lineCount();
14284+
};
14285+
} else if(name === "cursor") {
14286+
defaultValue = function(el) {
14287+
el.innerHTML = "0:0";
14288+
};
14289+
onUpdate = function(el) {
14290+
var pos = cm.getCursor();
14291+
el.innerHTML = pos.line + ":" + pos.ch;
14292+
};
14293+
} else if(name === "autosave") {
14294+
defaultValue = function(el) {
14295+
if(options.autosave != undefined && options.autosave.enabled === true) {
14296+
el.setAttribute("id", "autosaved");
14297+
}
14298+
};
14299+
}
14300+
14301+
items.push({
14302+
className: name,
14303+
defaultValue: defaultValue,
14304+
onUpdate: onUpdate
14305+
});
1430414306
}
1430514307
}
1430614308

14309+
14310+
// Create element for the status bar
1430714311
var bar = document.createElement("div");
1430814312
bar.className = "editor-statusbar";
1430914313

14310-
var pos, cm = this.codemirror;
14311-
// Create a new span for each action
14312-
for(name in actions) {
14313-
if(actions.hasOwnProperty(name)) {
14314-
var el = document.createElement("span");
14315-
el.className = name;
14316-
// Ensure the defaultValue is a function
14317-
if(typeof actions[name].defaultValue === "function") {
14318-
actions[name].defaultValue(el);
14319-
}
14320-
// Ensure the onUpdate is a function
14321-
if(typeof actions[name].onUpdate === "function") {
14322-
// Create a closure around the span and the name
14323-
// of the current action, then execute the onUpdate handler
14324-
cm.on("update", (function(el, name) {
14325-
return function() {
14326-
actions[name].onUpdate(el);
14327-
};
14328-
}(el, name)));
14329-
}
14330-
bar.appendChild(el);
14314+
14315+
// Create a new span for each item
14316+
for(i = 0; i < items.length; i++) {
14317+
// Store in temporary variable
14318+
var item = items[i];
14319+
14320+
14321+
// Create span element
14322+
var el = document.createElement("span");
14323+
el.className = item.className;
14324+
14325+
14326+
// Ensure the defaultValue is a function
14327+
if(typeof item.defaultValue === "function") {
14328+
item.defaultValue(el);
14329+
}
14330+
14331+
14332+
// Ensure the onUpdate is a function
14333+
if(typeof item.onUpdate === "function") {
14334+
// Create a closure around the span of the current action, then execute the onUpdate handler
14335+
this.codemirror.on("update", (function(el, item) {
14336+
console.log(el);
14337+
console.log(item);
14338+
console.log("---------------");
14339+
return function() {
14340+
item.onUpdate(el);
14341+
};
14342+
}(el, item)));
1433114343
}
14344+
14345+
14346+
// Append the item to the status bar
14347+
bar.appendChild(el);
1433214348
}
1433314349

14350+
14351+
// Insert the status bar into the DOM
1433414352
var cmWrapper = this.codemirror.getWrapperElement();
1433514353
cmWrapper.parentNode.insertBefore(bar, cmWrapper.nextSibling);
1433614354
return bar;

dist/simplemde.min.js

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)