Skip to content

Commit

Permalink
Bold / Italic / Strike / Underline buttons don't work correctly
Browse files Browse the repository at this point in the history
* Fixes steveathon#19
* Problem: addRange or removeAllRanges kills the browser command states.
* Store cache in commandCache
* Add updateCommandCache and restoreCommandCache methods
* Add support for command-with-arg buttons to be in an active state
* Update command cache when running execCommand
* Store and then update selected commands when selecting a toolbar button
* Create the command cache by looking up the buttons that are in the DOM, set the default value to false
  • Loading branch information
alanpca committed Jun 12, 2013
1 parent 6f808e5 commit 3c6330b
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions bootstrap-wysiwyg.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,43 @@
selectedRange,
options,
toolbarBtnSelector,
commandCache = {},
updateCommandCache = function () {
var key;
for (key in commandCache) {
var commandValue = document.queryCommandValue(key);
var commandState = document.queryCommandState(key);

if (commandState) {
commandCache[key] = commandState;
} else if (commandValue.length > 0 && commandValue !== 'false') {
commandCache[key] = commandValue;
} else {
commandCache[key] = false;
}
}
},
restoreCommandCache = function() {
var key;
for (key in commandCache) {
var val = commandCache[key];
if (typeof(val) === 'boolean') {
if (val !== document.queryCommandState(key)) {
document.execCommand(key, 0, null);
}
} else if (val !== document.queryCommandValue(key)) {
document.execCommand(key, 0, val);
}
}
},
updateToolbar = function () {
if (options.activeToolbarClass) {
$(options.toolbarSelector).find(toolbarBtnSelector).each(function () {
var command = $(this).data(options.commandRole);
if (document.queryCommandState(command)) {
$(this).addClass(options.activeToolbarClass);
} else if (command.split(' ')[0] + ' ' + document.queryCommandValue(command.split(' ')[0]) === command) {
$(this).addClass(options.activeToolbarClass);
} else {
$(this).removeClass(options.activeToolbarClass);
}
Expand All @@ -40,6 +71,11 @@
command = commandArr.shift(),
args = commandArr.join(' ') + (valueArg || '');
document.execCommand(command, 0, args);
if (args.length > 0) {
commandCache[command] = document.queryCommandValue(command);
} else {
commandCache[command] = document.queryCommandState(command);
}
updateToolbar();
},
bindHotkeys = function (hotKeys) {
Expand Down Expand Up @@ -104,8 +140,10 @@
},
bindToolbar = function (toolbar, options) {
toolbar.find(toolbarBtnSelector).click(function () {
updateCommandCache();
restoreSelection();
editor.focus();
restoreCommandCache();
execCommand($(this).data(options.commandRole));
saveSelection();
});
Expand Down Expand Up @@ -164,6 +202,12 @@
saveSelection();
updateToolbar();
});

$(toolbarBtnSelector).each(function () {
var btnAttr = this.getAttribute('data-' + options.commandRole);
commandCache[btnAttr.split(' ')[0]] = false;
});

$(window).bind('touchend', function (e) {
var isInside = (editor.is(e.target) || editor.has(e.target).length > 0),
currentRange = getCurrentRange(),
Expand Down

0 comments on commit 3c6330b

Please sign in to comment.