var ghostEditor = { bindEvents: function() { this.bindDesignModeToggle(); this.bindToolbarButtons(); }, bindDesignModeToggle: function() { $('#page-content').on('click', function(e) { document.designMode = 'on'; }); $('#page-content').on('click', function(e) { var $target = $(e.target); if ($target.is('#page-content')) { document.designMode = 'off'; } }); }, bindToolbarButtons: function() { $('.footer-actions').on('mousedown', '.icon', function(e) { e.preventDefault(); var btnId = $(e.target).attr('id'); this.editStyle(btnId); }.bind(this)); }, editStyle: function(btnId) { var value = null; if (btnId === 'createLink') { if (this.isSelection()) value = prompt('Enter a link:'); } document.execCommand(btnId, true, value); }, isSelection: function() { var selection = window.getSelection(); return selection.anchorOffset !== selection.focusOffset }, init: function() { this.bindEvents(); }, } ghostEditor.init();