Skip to content

Commit

Permalink
Don't observe the contextmenu in safari since it doesn't contain undo…
Browse files Browse the repository at this point in the history
…/redo options (fixes tiff#27)
  • Loading branch information
Christopher Blum committed Mar 23, 2012
1 parent ed21963 commit d05f02c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
5 changes: 5 additions & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ wysihtml5.browser = (function() {
isIE = userAgent.indexOf("MSIE") !== -1 && userAgent.indexOf("Opera") === -1,
isGecko = userAgent.indexOf("Gecko") !== -1 && userAgent.indexOf("KHTML") === -1,
isWebKit = userAgent.indexOf("AppleWebKit/") !== -1,
isChrome = userAgent.indexOf("Chrome/") !== -1,
isOpera = userAgent.indexOf("Opera/") !== -1;

function iosVersion(userAgent) {
Expand Down Expand Up @@ -347,6 +348,10 @@ wysihtml5.browser = (function() {
*/
hasProblemsSettingCaretAfterImg: function() {
return isIE;
},

hasUndoInContextMenu: function() {
return isGecko || isChrome || isOpera;
}
};
})();
69 changes: 35 additions & 34 deletions src/undo_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,49 +71,50 @@
}
});

var interval, observed, cleanUp = function() {
cleanTempElements(doc);
clearInterval(interval);
};


// Now this is very hacky:
// These days browsers don't offer a undo/redo event which we could hook into
// to be notified when the user hits undo/redo in the contextmenu.
// Therefore we simply insert two elements as soon as the contextmenu gets opened.
// The last element being inserted will be immediately be removed again by a exexCommand("undo")
// => When the second element appears in the dom tree then we know the user clicked "redo" in the context menu
// => When the first element disappears from the dom tree then we know the user clicked "undo" in the context menu
dom.observe(this.composerElement, "contextmenu", function() {
cleanUp();
wysihtml5.selection.executeAndRestoreSimple(function() {
if (that.composerElement.lastChild) {
wysihtml5.selection.setAfter(that.composerElement.lastChild);
}

// enable undo button in context menu
doc.execCommand("insertHTML", false, UNDO_HTML);
// enable redo button in context menu
doc.execCommand("insertHTML", false, REDO_HTML);
doc.execCommand("undo", false, null);
});
if (wysihtml5.browser.hasUndoInContextMenu()) {
var interval, observed, cleanUp = function() {
cleanTempElements(doc);
clearInterval(interval);
};

interval = setInterval(function() {
if (doc.getElementById("_wysihtml5-redo")) {
cleanUp();
that.redo();
} else if (!doc.getElementById("_wysihtml5-undo")) {
cleanUp();
that.undo();
dom.observe(this.composerElement, "contextmenu", function() {
cleanUp();
wysihtml5.selection.executeAndRestoreSimple(function() {
if (that.composerElement.lastChild) {
wysihtml5.selection.setAfter(that.composerElement.lastChild);
}

// enable undo button in context menu
doc.execCommand("insertHTML", false, UNDO_HTML);
// enable redo button in context menu
doc.execCommand("insertHTML", false, REDO_HTML);
doc.execCommand("undo", false, null);
});

interval = setInterval(function() {
if (doc.getElementById("_wysihtml5-redo")) {
cleanUp();
that.redo();
} else if (!doc.getElementById("_wysihtml5-undo")) {
cleanUp();
that.undo();
}
}, 400);

if (!observed) {
observed = true;
dom.observe(document, "mousedown", cleanUp);
dom.observe(doc, ["mousedown", "paste", "cut", "copy"], cleanUp);
}
}, 400);

if (!observed) {
observed = true;
dom.observe(document, "mousedown", cleanUp);
dom.observe(doc, ["mousedown", "paste", "cut", "copy"], cleanUp);
}
});
});
}

this.editor
.observe("newword:composer", function() {
Expand Down

0 comments on commit d05f02c

Please sign in to comment.