Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Bold / Italic / Strike / Underline buttons don't work correctly #76

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions bootstrap-wysiwyg.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,50 @@
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);
var commandNoArgs; // Temporarily store index, replace with command.

if ((commandNoArgs = command.indexOf(' ')) >= 0) {
commandNoArgs = command.slice(0, commandNoArgs);
} else {
commandNoArgs = command;
}
if (document.queryCommandState(command)) {
$(this).addClass(options.activeToolbarClass);
} else if (commandNoArgs + ' ' + document.queryCommandValue(commandNoArgs) === command) {
$(this).addClass(options.activeToolbarClass);
} else {
$(this).removeClass(options.activeToolbarClass);
}
Expand All @@ -40,6 +78,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 +147,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 +209,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