(function($) { $.fn.editor = function() { return this.each(function() { if($(this).data('editor')) { return $(this); } var textarea = $(this); var buttons = textarea.parent().find('.field-buttons'); // start autosizing textarea.autosize(); buttons.find('.btn').on('click.editorButton', function(e) { textarea.focus(); var button = $(this); if(button.data('action')) { app.modal.open(button.data('action'), window.location.href); } else { var sel = textarea.getSelection(); var tpl = button.data('tpl'); var text = button.data('text'); if(sel.length > 0) text = sel; var tag = tpl.replace('{text}', text); textarea.insertAtCursor(tag); textarea.trigger('autosize.resize'); } return false; }); buttons.find('[data-editor-shortcut]').each(function(i, el) { var key = $(this).data('editor-shortcut'); var action = function(e) { $(el).trigger('click'); return false; }; textarea.bind('keydown', key, action); if(key.match(/meta\+/)) { textarea.bind('keydown', key.replace('meta+', 'ctrl+'), action); } }); textarea.data('editor', true); }); }; })(jQuery);