UNPKG

redactor-html

Version:

https://github.com/Magons/redactor.git email: vanyaprokopenko@gmail.com

2,073 lines (1,798 loc) 100 kB
/* Redactor v8.2.6 Updated: April 6, 2013 http://redactorjs.com/ Copyright (c) 2009-2013, Imperavi Inc. License: http://redactorjs.com/license/ Usage: $('#content').redactor(); */ var rwindow, rdocument; if (typeof RELANG === 'undefined') { var RELANG = {}; } var RLANG = { html: 'HTML', video: 'Insert Video', image: 'Insert Image', table: 'Table', link: 'Link', link_insert: 'Insert link', unlink: 'Unlink', formatting: 'Formatting', paragraph: 'Paragraph', quote: 'Quote', code: 'Code', header1: 'Header 1', header2: 'Header 2', header3: 'Header 3', header4: 'Header 4', bold: 'Bold', italic: 'Italic', fontcolor: 'Font Color', backcolor: 'Back Color', unorderedlist: 'Unordered List', orderedlist: 'Ordered List', outdent: 'Outdent', indent: 'Indent', cancel: 'Cancel', insert: 'Insert', save: 'Save', _delete: 'Delete', insert_table: 'Insert Table', insert_row_above: 'Add Row Above', insert_row_below: 'Add Row Below', insert_column_left: 'Add Column Left', insert_column_right: 'Add Column Right', delete_column: 'Delete Column', delete_row: 'Delete Row', delete_table: 'Delete Table', rows: 'Rows', columns: 'Columns', add_head: 'Add Head', delete_head: 'Delete Head', title: 'Title', image_position: 'Position', none: 'None', left: 'Left', right: 'Right', image_web_link: 'Image Web Link', text: 'Text', mailto: 'Email', web: 'URL', video_html_code: 'Video Embed Code', file: 'Insert File', upload: 'Upload', download: 'Download', choose: 'Choose', or_choose: 'Or choose', drop_file_here: 'Drop file here', align_left: 'Align text to the left', align_center: 'Center text', align_right: 'Align text to the right', align_justify: 'Justify text', horizontalrule: 'Insert Horizontal Rule', deleted: 'Deleted', anchor: 'Anchor', link_new_tab: 'Open link in new tab', underline: 'Underline', alignment: 'Alignment', lang: 'ru' }; (function($){ // Plugin jQuery.fn.redactor = function(option) { return this.each(function() { var $obj = $(this); var data = $obj.data('redactor'); if (!data) { $obj.data('redactor', (data = new Redactor(this, option))); } }); }; // Initialization var Redactor = function(element, options) { // Element this.$el = $(element); // Lang if (typeof options !== 'undefined' && typeof options.lang !== 'undefined' && options.lang !== 'en' && typeof RELANG[options.lang] !== 'undefined') { RLANG = RELANG[options.lang]; } // Options this.opts = $.extend({ iframe: false, css: false, // url lang: 'ru', direction: 'ltr', // ltr or rtl callback: false, // function keyupCallback: false, // function keydownCallback: false, // function execCommandCallback: false, // function plugins: false, cleanup: false, focus: false, tabindex: false, autoresize: false, minHeight: false, maxHeight: false, fixed: true, fixedTop: 0, // pixels fixedBox: false, source: true, shortcuts: true, mobile: true, air: false, // true or toolbar wym: false, convertLinks: false, convertDivs: false, protocol: 'http://', // for links http or https or ftp or false autosave: false, // false or url autosaveCallback: false, // function interval: 60, // seconds imageGetJson: false, // url (ex. /folder/images.json ) or false imageUpload: false, // url imageUploadCallback: false, // function imageUploadErrorCallback: false, // function fileUpload: false, // url fileUploadCallback: false, // function fileUploadErrorCallback: false, // function uploadCrossDomain: false, uploadFields: false, observeImages: true, overlay: true, // modal overlay allowedTags: ["form", "input", "button", "select", "option", "datalist", "output", "textarea", "fieldset", "legend", "section", "header", "hgroup", "aside", "footer", "article", "details", "nav", "progress", "time", "canvas", "code", "span", "div", "label", "a", "br", "p", "b", "i", "del", "strike", "u", "img", "video", "source", "track", "audio", "iframe", "object", "embed", "param", "blockquote", "mark", "cite", "small", "ul", "ol", "li", "hr", "dl", "dt", "dd", "sup", "sub", "big", "pre", "code", "figure", "figcaption", "strong", "em", "table", "tr", "td", "th", "tbody", "thead", "tfoot", "h1", "h2", "h3", "h4", "h5", "h6"], toolbarExternal: false, // ID selector buttonsCustom: {}, buttonsAdd: [], buttons: ['html', '|', 'formatting', '|', 'bold', 'italic', 'deleted', '|', 'unorderedlist', 'orderedlist', 'outdent', 'indent', '|', 'image', 'video', 'file', 'table', 'link', '|', 'fontcolor', 'backcolor', '|', 'alignment', '|', 'horizontalrule'], // 'underline', 'alignleft', 'aligncenter', 'alignright', 'justify' airButtons: ['formatting', '|', 'bold', 'italic', 'deleted', '|', 'unorderedlist', 'orderedlist', 'outdent', 'indent', '|', 'fontcolor', 'backcolor'], formattingTags: ['p', 'blockquote', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'], activeButtons: ['deleted', 'italic', 'bold', 'underline', 'unorderedlist', 'orderedlist'], // 'alignleft', 'aligncenter', 'alignright', 'justify' activeButtonsStates: { b: 'bold', strong: 'bold', i: 'italic', em: 'italic', del: 'deleted', strike: 'deleted', ul: 'unorderedlist', ol: 'orderedlist', u: 'underline' }, colors: [ '#ffffff', '#000000', '#eeece1', '#1f497d', '#4f81bd', '#c0504d', '#9bbb59', '#8064a2', '#4bacc6', '#f79646', '#ffff00', '#f2f2f2', '#7f7f7f', '#ddd9c3', '#c6d9f0', '#dbe5f1', '#f2dcdb', '#ebf1dd', '#e5e0ec', '#dbeef3', '#fdeada', '#fff2ca', '#d8d8d8', '#595959', '#c4bd97', '#8db3e2', '#b8cce4', '#e5b9b7', '#d7e3bc', '#ccc1d9', '#b7dde8', '#fbd5b5', '#ffe694', '#bfbfbf', '#3f3f3f', '#938953', '#548dd4', '#95b3d7', '#d99694', '#c3d69b', '#b2a2c7', '#b7dde8', '#fac08f', '#f2c314', '#a5a5a5', '#262626', '#494429', '#17365d', '#366092', '#953734', '#76923c', '#5f497a', '#92cddc', '#e36c09', '#c09100', '#7f7f7f', '#0c0c0c', '#1d1b10', '#0f243e', '#244061', '#632423', '#4f6128', '#3f3151', '#31859b', '#974806', '#7f6000'], // private emptyHtml: '<p><br /></p>', buffer: false, visual: true, // modal windows container modal_file: String() + '<div id="redactor_modal_content">' + '<form id="redactorUploadFileForm" method="post" action="" enctype="multipart/form-data">' + '<label>Name (optional)</label>' + '<input type="text" id="redactor_filename" class="redactor_input" />' + '<div style="margin-top: 7px;">' + '<input type="file" id="redactor_file" name="file" />' + '</div>' + '</form><br>' + '</div>', modal_image_edit: String() + '<div id="redactor_modal_content">' + '<label>' + RLANG.title + '</label>' + '<input id="redactor_file_alt" class="redactor_input" />' + '<label>' + RLANG.link + '</label>' + '<input id="redactor_file_link" class="redactor_input" />' + '<label>' + RLANG.image_position + '</label>' + '<select id="redactor_form_image_align">' + '<option value="none">' + RLANG.none + '</option>' + '<option value="left">' + RLANG.left + '</option>' + '<option value="right">' + RLANG.right + '</option>' + '</select>' + '</div>' + '<div id="redactor_modal_footer">' + '<a href="javascript:void(null);" id="redactor_image_delete_btn" class="redactor_modal_btn">' + RLANG._delete + '</a>&nbsp;&nbsp;&nbsp;' + '<a href="javascript:void(null);" class="redactor_modal_btn redactor_btn_modal_close">' + RLANG.cancel + '</a>' + '<input type="button" name="save" class="redactor_modal_btn" id="redactorSaveBtn" value="' + RLANG.save + '" />' + '</div>', modal_image: String() + '<div id="redactor_modal_content">' + '<div id="redactor_tabs">' + '<a href="javascript:void(null);" class="redactor_tabs_act">' + RLANG.upload + '</a>' + '<a href="javascript:void(null);">' + RLANG.choose + '</a>' + '<a href="javascript:void(null);">' + RLANG.link + '</a>' + '</div>' + '<form id="redactorInsertImageForm" method="post" action="" enctype="multipart/form-data">' + '<div id="redactor_tab1" class="redactor_tab">' + '<input type="file" id="redactor_file" name="file" />' + '</div>' + '<div id="redactor_tab2" class="redactor_tab" style="display: none;">' + '<div id="redactor_image_box"></div>' + '</div>' + '</form>' + '<div id="redactor_tab3" class="redactor_tab" style="display: none;">' + '<label>' + RLANG.image_web_link + '</label>' + '<input type="text" name="redactor_file_link" id="redactor_file_link" class="redactor_input" />' + '</div>' + '</div>' + '<div id="redactor_modal_footer">' + '<a href="javascript:void(null);" class="redactor_modal_btn redactor_btn_modal_close">' + RLANG.cancel + '</a>' + '<input type="button" name="upload" class="redactor_modal_btn" id="redactor_upload_btn" value="' + RLANG.insert + '" />' + '</div>', modal_link: String() + '<div id="redactor_modal_content">' + '<form id="redactorInsertLinkForm" method="post" action="">' + '<div id="redactor_tabs">' + '<a href="javascript:void(null);" class="redactor_tabs_act">URL</a>' + '<a href="javascript:void(null);">Email</a>' + '<a href="javascript:void(null);">' + RLANG.anchor + '</a>' + '</div>' + '<input type="hidden" id="redactor_tab_selected" value="1" />' + '<div class="redactor_tab" id="redactor_tab1">' + '<label>URL</label><input type="text" id="redactor_link_url" class="redactor_input" />' + '<label>' + RLANG.text + '</label><input type="text" class="redactor_input redactor_link_text" id="redactor_link_url_text" />' + '<label><input type="checkbox" id="redactor_link_blank"> ' + RLANG.link_new_tab + '</label>' + '</div>' + '<div class="redactor_tab" id="redactor_tab2" style="display: none;">' + '<label>Email</label><input type="text" id="redactor_link_mailto" class="redactor_input" />' + '<label>' + RLANG.text + '</label><input type="text" class="redactor_input redactor_link_text" id="redactor_link_mailto_text" />' + '</div>' + '<div class="redactor_tab" id="redactor_tab3" style="display: none;">' + '<label>' + RLANG.anchor + '</label><input type="text" class="redactor_input" id="redactor_link_anchor" />' + '<label>' + RLANG.text + '</label><input type="text" class="redactor_input redactor_link_text" id="redactor_link_anchor_text" />' + '</div>' + '</form>' + '</div>' + '<div id="redactor_modal_footer">' + '<a href="javascript:void(null);" class="redactor_modal_btn redactor_btn_modal_close">' + RLANG.cancel + '</a>' + '<input type="button" class="redactor_modal_btn" id="redactor_insert_link_btn" value="' + RLANG.insert + '" />' + '</div>', modal_table: String() + '<div id="redactor_modal_content">' + '<label>' + RLANG.rows + '</label>' + '<input type="text" size="5" value="2" id="redactor_table_rows" />' + '<label>' + RLANG.columns + '</label>' + '<input type="text" size="5" value="3" id="redactor_table_columns" />' + '</div>' + '<div id="redactor_modal_footer">' + '<a href="javascript:void(null);" class="redactor_modal_btn redactor_btn_modal_close">' + RLANG.cancel + '</a>' + '<input type="button" name="upload" class="redactor_modal_btn" id="redactor_insert_table_btn" value="' + RLANG.insert + '" />' + '</div>', modal_video: String() + '<div id="redactor_modal_content">' + '<form id="redactorInsertVideoForm">' + '<label>' + RLANG.video_html_code + '</label>' + '<textarea id="redactor_insert_video_area" style="width: 99%; height: 160px;"></textarea>' + '</form>' + '</div>'+ '<div id="redactor_modal_footer">' + '<a href="javascript:void(null);" class="redactor_modal_btn redactor_btn_modal_close">' + RLANG.cancel + '</a>' + '<input type="button" class="redactor_modal_btn" id="redactor_insert_video_btn" value="' + RLANG.insert + '" />' + '</div>', toolbar: { html: { title: RLANG.html, func: 'toggle' }, formatting: { title: RLANG.formatting, func: 'show', dropdown: { p: { title: RLANG.paragraph, exec: 'formatblock' }, blockquote: { title: RLANG.quote, exec: 'formatblock', className: 'redactor_format_blockquote' }, pre: { title: RLANG.code, exec: 'formatblock', className: 'redactor_format_pre' }, h1: { title: RLANG.header1, exec: 'formatblock', className: 'redactor_format_h1' }, h2: { title: RLANG.header2, exec: 'formatblock', className: 'redactor_format_h2' }, h3: { title: RLANG.header3, exec: 'formatblock', className: 'redactor_format_h3' }, h4: { title: RLANG.header4, exec: 'formatblock', className: 'redactor_format_h4' } } }, bold: { title: RLANG.bold, exec: 'bold' }, italic: { title: RLANG.italic, exec: 'italic' }, deleted: { title: RLANG.deleted, exec: 'strikethrough' }, underline: { title: RLANG.underline, exec: 'underline' }, unorderedlist: { title: '&bull; ' + RLANG.unorderedlist, exec: 'insertunorderedlist' }, orderedlist: { title: '1. ' + RLANG.orderedlist, exec: 'insertorderedlist' }, outdent: { title: '< ' + RLANG.outdent, exec: 'outdent' }, indent: { title: '> ' + RLANG.indent, exec: 'indent' }, image: { title: RLANG.image, func: 'showImage' }, video: { title: RLANG.video, func: 'showVideo' }, file: { title: RLANG.file, func: 'showFile' }, table: { title: RLANG.table, func: 'show', dropdown: { insert_table: { title: RLANG.insert_table, func: 'showTable' }, separator_drop1: { name: 'separator' }, insert_row_above: { title: RLANG.insert_row_above, func: 'insertRowAbove' }, insert_row_below: { title: RLANG.insert_row_below, func: 'insertRowBelow' }, insert_column_left: { title: RLANG.insert_column_left, func: 'insertColumnLeft' }, insert_column_right: { title: RLANG.insert_column_right, func: 'insertColumnRight' }, separator_drop2: { name: 'separator' }, add_head: { title: RLANG.add_head, func: 'addHead' }, delete_head: { title: RLANG.delete_head, func: 'deleteHead' }, separator_drop3: { name: 'separator' }, delete_column: { title: RLANG.delete_column, func: 'deleteColumn' }, delete_row: { title: RLANG.delete_row, func: 'deleteRow' }, delete_table: { title: RLANG.delete_table, func: 'deleteTable' } } }, link: { title: RLANG.link, func: 'show', dropdown: { link: { title: RLANG.link_insert, func: 'showLink' }, unlink: { title: RLANG.unlink, exec: 'unlink' } } }, fontcolor: { title: RLANG.fontcolor, func: 'show' }, backcolor: { title: RLANG.backcolor, func: 'show' }, alignment: { title: RLANG.alignment, func: 'show', dropdown: { alignleft: { title: RLANG.align_left, exec: 'JustifyLeft' }, aligncenter: { title: RLANG.align_center, exec: 'JustifyCenter' }, alignright: { title: RLANG.align_right, exec: 'JustifyRight' }, justify: { title: RLANG.align_justify, exec: 'JustifyFull' } } }, alignleft: { exec: 'JustifyLeft', title: RLANG.align_left }, aligncenter: { exec: 'JustifyCenter', title: RLANG.align_center }, alignright: { exec: 'JustifyRight', title: RLANG.align_right }, justify: { exec: 'JustifyFull', title: RLANG.align_justify }, horizontalrule: { exec: 'inserthorizontalrule', title: RLANG.horizontalrule } } }, options, this.$el.data()); this.dropdowns = []; // Init this.init(); }; // Functionality Redactor.prototype = { // Initialization init: function() { // get dimensions this.height = this.$el.css('height'); this.width = this.$el.css('width'); rdocument = this.document = document; rwindow = this.window = window; // mobile if (this.opts.mobile === false && this.isMobile()) { this.build(true); return false; } // iframe if (this.opts.iframe) { this.opts.autoresize = false; } // extend buttons if (this.opts.air) { $.extend(this.opts.toolbar, this.opts.buttonsCustom); this.opts.buttons = this.opts.airButtons; } else if (this.opts.toolbar !== false) { if (this.opts.source === false) { var index = this.opts.buttons.indexOf('html'); var next = this.opts.buttons[index+1]; this.opts.buttons.splice(index, 1); if (typeof next !== 'undefined' && next === '|') { this.opts.buttons.splice(index, 1); } } $.extend(this.opts.toolbar, this.opts.buttonsCustom); $.each(this.opts.buttonsAdd, $.proxy(function(i,s) { this.opts.buttons.push(s); }, this)); } // formatting tags if (this.opts.toolbar !== false) { $.each(this.opts.toolbar.formatting.dropdown, $.proxy(function(i,s) { if ($.inArray(i, this.opts.formattingTags) == '-1') { delete this.opts.toolbar.formatting.dropdown[i]; } }, this)); } function afterBuild() { // air enable this.enableAir(); // toolbar this.buildToolbar(); // PLUGINS if (typeof this.opts.plugins === 'object') { $.each(this.opts.plugins, $.proxy(function(i,s) { if (typeof RedactorPlugins[s] !== 'undefined') { $.extend(this, RedactorPlugins[s]); if (typeof RedactorPlugins[s].init !== 'undefined') { this.init(); } } }, this)); } // buttons response if (this.opts.activeButtons !== false && this.opts.toolbar !== false) { var observeFormatting = $.proxy(function() { this.observeFormatting(); }, this); this.$editor.click(observeFormatting).keyup(observeFormatting); } // paste var oldsafari = false; if (this.browser('webkit') && navigator.userAgent.indexOf('Chrome') === -1) { var arr = this.browser('version').split('.'); if (arr[0] < 536) oldsafari = true; } if (this.isMobile(true) === false && oldsafari === false && this.browser('opera') === false) { this.$editor.bind('paste', $.proxy(function(e) { if (this.opts.cleanup === false) { return true; } this.pasteRunning = true; this.setBuffer(); if (this.opts.autoresize === true) { this.saveScroll = this.document.body.scrollTop; } else { this.saveScroll = this.$editor.scrollTop(); } var frag = this.extractContent(); setTimeout($.proxy(function() { var pastedFrag = this.extractContent(); this.$editor.append(frag); this.restoreSelection(); var html = this.getFragmentHtml(pastedFrag); this.pasteCleanUp(html); this.pasteRunning = false; }, this), 1); }, this)); } // key handlers this.keyup(); this.keydown(); // autosave if (this.opts.autosave !== false) { this.autoSave(); } // observers setTimeout($.proxy(function() { this.observeImages(); this.observeTables(); }, this), 1); // FF fix if (this.browser('mozilla')) { this.$editor.click($.proxy(function() { this.saveSelection(); }, this)); try { this.document.execCommand('enableObjectResizing', false, false); this.document.execCommand('enableInlineTableEditing', false, false); } catch (e) {} } // focus if (this.opts.focus) { setTimeout($.proxy(function(){ this.$editor.focus(); }, this), 1); } // fixed if (this.opts.fixed) { this.observeScroll(); $(document).scroll($.proxy(this.observeScroll, this)); } // callback if (typeof this.opts.callback === 'function') { this.opts.callback(this); } if (this.opts.toolbar !== false) { this.$toolbar.find('a').attr('tabindex', '-1'); } } // construct editor this.build(false, afterBuild); }, shortcuts: function(e, cmd) { e.preventDefault(); this.execCommand(cmd, false); }, keyup: function() { this.$editor.keyup($.proxy(function(e) { var key = e.keyCode || e.which; if (this.browser('mozilla') && !this.pasteRunning) { this.saveSelection(); } // callback as you type if (typeof this.opts.keyupCallback === 'function') { this.opts.keyupCallback(this, e); } // if empty if (key === 8 || key === 46) { this.observeImages(); return this.formatEmpty(e); } // new line p if (key === 13 && !e.shiftKey && !e.ctrlKey && !e.metaKey) { if (this.browser('webkit')) { this.formatNewLine(e); } // convert links if (this.opts.convertLinks) { this.$editor.linkify(); } } this.syncCode(); }, this)); }, keydown: function() { this.$editor.keydown($.proxy(function(e) { var key = e.keyCode || e.which; var parent = this.getParentNode(); var current = this.getCurrentNode(); var pre = false; var ctrl = e.ctrlKey || e.metaKey; if ((parent || current) && ($(parent).get(0).tagName === 'PRE' || $(current).get(0).tagName === 'PRE')) { pre = true; } // callback keydown if (typeof this.opts.keydownCallback === 'function') { this.opts.keydownCallback(this, e); } if (ctrl && this.opts.shortcuts) { if (key === 90) { if (this.opts.buffer !== false) { e.preventDefault(); this.getBuffer(); } else if (e.shiftKey) { this.shortcuts(e, 'redo'); // Ctrl + Shift + z } else { this.shortcuts(e, 'undo'); // Ctrl + z } } else if (key === 77) { this.shortcuts(e, 'removeFormat'); // Ctrl + m } else if (key === 66) { this.shortcuts(e, 'bold'); // Ctrl + b } else if (key === 73) { this.shortcuts(e, 'italic'); // Ctrl + i } else if (key === 74) { this.shortcuts(e, 'insertunorderedlist'); // Ctrl + j } else if (key === 75) { this.shortcuts(e, 'insertorderedlist'); // Ctrl + k } else if (key === 76) { this.shortcuts(e, 'superscript'); // Ctrl + l } else if (key === 72) { this.shortcuts(e, 'subscript'); // Ctrl + h } } // clear undo buffer if (!ctrl && key !== 90) { this.opts.buffer = false; } // enter if (pre === true && key === 13) { e.preventDefault(); var html = $(current).parent().text(); this.insertNodeAtCaret(this.document.createTextNode('\r\n')); if (html.search(/\s$/) == -1) { this.insertNodeAtCaret(this.document.createTextNode('\r\n')); } this.syncCode(); return false; } // tab if (this.opts.shortcuts && !e.shiftKey && key === 9) { if (pre === false) { this.shortcuts(e, 'indent'); // Tab } else { e.preventDefault(); this.insertNodeAtCaret(this.document.createTextNode('\t')); this.syncCode(); return false; } } else if (this.opts.shortcuts && e.shiftKey && key === 9 ) { this.shortcuts(e, 'outdent'); // Shift + tab } // safari shift key + enter if (this.browser('webkit') && navigator.userAgent.indexOf('Chrome') === -1) { return this.safariShiftKeyEnter(e, key); } }, this)); }, build: function(mobile, whendone) { if (mobile !== true) { // container this.$box = $('<div class="redactor_box"></div>'); // air box if (this.opts.air) { this.air = $('<div class="redactor_air" style="display: none;"></div>'); } this.$content = null; function initFrame() { this.$editor = this.$content.contents().find("body").attr('contenteditable', true).attr('dir', this.opts.direction); rdocument = this.document = this.$editor[0].ownerDocument; rwindow = this.window = this.document.defaultView || window; if (this.opts.css !== false) { this.$content.contents().find('head').append('<link rel="stylesheet" href="' + this.opts.css + '" />'); } this.$editor.html(html); if (whendone) { whendone.call(this); whendone = null; } } // editor this.textareamode = true; if (this.$el.get(0).tagName === 'TEXTAREA') { if(this.opts.iframe) { var me = this; this.$content = $('<iframe style="width: 100%;" frameborder="0"></iframe>').load(function() { initFrame.call(me); }); } else { this.$content = this.$editor = $('<div></div>'); } var classlist = this.$el.get(0).className.split(/\s+/); $.each(classlist, $.proxy(function(i,s) { this.$content.addClass('redactor_' + s); }, this)); } else { this.textareamode = false; this.$content = this.$editor = this.$el; this.$el = $('<textarea name="' + this.$editor.attr('id') + '"></textarea>').css('height', this.height); } if (this.$editor) { this.$editor.addClass('redactor_editor').attr('contenteditable', true).attr('dir', this.opts.direction); } if (this.opts.tabindex !== false) { this.$content.attr('tabindex', this.opts.tabindex); } if (this.opts.minHeight !== false) { this.$content.css('min-height', this.opts.minHeight + 'px'); } if (this.opts.maxHeight !== false) { this.$content.css('max-height', this.opts.maxHeight + 'px'); } if (this.opts.wym === true) { this.$content.addClass('redactor_editor_wym'); } if (this.opts.autoresize === false) { this.$content.css('height', this.height); } // hide textarea this.$el.hide(); // append box and frame var html = ''; if (this.textareamode) { // get html html = this.$el.val(); html = this.savePreCode(html); this.$box.insertAfter(this.$el).append(this.$content).append(this.$el); } else { // get html html = this.$editor.html(); html = this.savePreCode(html); this.$box.insertAfter(this.$content).append(this.$el).append(this.$editor); } // conver newlines to p html = this.paragraphy(html); // enable if (this.$editor) { this.$editor.html(html); } if (this.textareamode === false) { this.syncCode(); } } else { if (this.$el.get(0).tagName !== 'TEXTAREA') { var html = this.$el.val(); var textarea = $('<textarea name="' + this.$editor.attr('id') + '"></textarea>').css('height', this.height).val(html); this.$el.hide(); this.$el.after(textarea); } } if (whendone && this.$editor) { whendone.call(this); } }, enableAir: function() { if (this.opts.air === false) { return false; } this.air.hide(); this.$editor.bind('textselect', $.proxy(function(e) { this.showAir(e); }, this)); this.$editor.bind('textunselect', $.proxy(function() { this.air.hide(); }, this)); }, showAir: function(e) { $('.redactor_air').hide(); var width = this.air.innerWidth(); var left = e.clientX; if ($(this.document).width() < (left + width)) { left = left - width; } var top = e.clientY + $(document).scrollTop() + 14; if (this.opts.iframe === true) { top = top + this.$box.position().top; left = left + this.$box.position().left; } this.air.css({ left: left + 'px', top: top + 'px' }).show(); }, syncCode: function() { this.$el.val(this.$editor.html()); }, // API functions setCode: function(html) { html = this.stripTags(html); this.$editor.html(html).focus(); this.syncCode(); }, getCode: function() { var html = ''; if (this.opts.visual) { html = this.$editor.html() } else { html = this.$el.val(); } return this.stripTags(html); }, insertHtml: function(html) { this.$editor.focus(); this.pasteHtmlAtCaret(html); this.observeImages(); this.syncCode(); }, pasteHtmlAtCaret: function (html) { var sel, range; if (this.document.getSelection) { sel = this.window.getSelection(); if (sel.getRangeAt && sel.rangeCount) { range = sel.getRangeAt(0); range.deleteContents(); var el = this.document.createElement("div"); el.innerHTML = html; var frag = this.document.createDocumentFragment(), node, lastNode; while (node = el.firstChild) { lastNode = frag.appendChild(node); } range.insertNode(frag); if (lastNode) { range = range.cloneRange(); range.setStartAfter(lastNode); range.collapse(true); sel.removeAllRanges(); sel.addRange(range); } } } else if (this.document.selection && this.document.selection.type != "Control") { this.document.selection.createRange().pasteHTML(html); } }, destroy: function() { var html = this.getCode(); if (this.textareamode) { this.$box.after(this.$el); this.$box.remove(); this.$el.height(this.height).val(html).show(); } else { this.$box.after(this.$editor); this.$box.remove(); this.$editor.removeClass('redactor_editor').removeClass('redactor_editor_wym').attr('contenteditable', false).html(html).show(); } if (this.opts.toolbarExternal) { $(this.opts.toolbarExternal).empty(); } $('.redactor_air').remove(); for (var i = 0; i < this.dropdowns.length; i++) { this.dropdowns[i].remove(); delete(this.dropdowns[i]); } if (this.opts.autosave !== false) { clearInterval(this.autosaveInterval); } }, // end API functions // OBSERVERS observeFormatting: function() { var parent = this.getCurrentNode(); this.inactiveAllButtons(); $.each(this.opts.activeButtonsStates, $.proxy(function(i,s) { if ($(parent).closest(i,this.$editor.get()[0]).length != 0) { this.setBtnActive(s); } }, this)); var tag = $(parent).closest(['p', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'td']); if (typeof tag[0] !== 'undefined' && typeof tag[0].elem !== 'undefined' && $(tag[0].elem).size() != 0) { var align = $(tag[0].elem).css('text-align'); switch (align) { case 'right': this.setBtnActive('alignright'); break; case 'center': this.setBtnActive('aligncenter'); break; case 'justify': this.setBtnActive('justify'); break; default: this.setBtnActive('alignleft'); break; } } }, observeImages: function() { if (this.opts.observeImages === false) { return false; } this.$editor.find('img').each($.proxy(function(i,s) { if (this.browser('msie')) { $(s).attr('unselectable', 'on'); } this.resizeImage(s); }, this)); }, observeTables: function() { this.$editor.find('table').click($.proxy(this.tableObserver, this)); }, observeScroll: function() { var scrolltop = $(this.document).scrollTop(); var boxtop = this.$box.offset().top; var left = 0; if (scrolltop > boxtop) { var width = '100%'; if (this.opts.fixedBox) { left = this.$box.offset().left; width = this.$box.innerWidth(); } this.fixed = true; this.$toolbar.css({ position: 'fixed', width: width, zIndex: 1005, top: this.opts.fixedTop + 'px', left: left }); } else { this.fixed = false; this.$toolbar.css({ position: 'relative', width: 'auto', zIndex: 1, top: 0, left: left }); } }, // BUFFER setBuffer: function() { this.saveSelection(); this.opts.buffer = this.$editor.html(); }, getBuffer: function() { if (this.opts.buffer === false) { return false; } this.$editor.html(this.opts.buffer); if (!this.browser('msie')) { this.restoreSelection(); } this.opts.buffer = false; }, // EXECCOMMAND execCommand: function(cmd, param) { if (this.opts.visual == false) { this.$el.focus(); return false; } try { var parent; if (cmd === 'inserthtml') { if (this.browser('msie')) { this.$editor.focus(); this.document.selection.createRange().pasteHTML(param); } else { this.pasteHtmlAtCaret(param); //this.execRun(cmd, param); } this.observeImages(); } else if (cmd === 'unlink') { parent = this.getParentNode(); if ($(parent).get(0).tagName === 'A') { $(parent).replaceWith($(parent).text()); } else { this.execRun(cmd, param); } } else if (cmd === 'JustifyLeft' || cmd === 'JustifyCenter' || cmd === 'JustifyRight' || cmd === 'JustifyFull') { parent = this.getCurrentNode(); var tag = $(parent).get(0).tagName; if (this.opts.iframe === false && $(parent).parents('.redactor_editor').size() == 0) { return false; } var tagsArray = ['P', 'DIV', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'BLOCKQUOTE', 'TD']; if ($.inArray(tag, tagsArray) != -1) { var align = false; if (cmd === 'JustifyCenter') { align = 'center'; } else if (cmd === 'JustifyRight') { align = 'right'; } else if (cmd === 'JustifyFull') { align = 'justify'; } if (align === false) { $(parent).css('text-align', ''); } else { $(parent).css('text-align', align); } } else { this.execRun(cmd, param); } } else if (cmd === 'formatblock' && param === 'blockquote') { parent = this.getCurrentNode(); if ($(parent).get(0).tagName === 'BLOCKQUOTE') { if (this.browser('msie')) { var node = $('<p>' + $(parent).html() + '</p>'); $(parent).replaceWith(node); } else { this.execRun(cmd, 'p'); } } else if ($(parent).get(0).tagName === 'P') { var parent2 = $(parent).parent(); if ($(parent2).get(0).tagName === 'BLOCKQUOTE') { var node = $('<p>' + $(parent).html() + '</p>'); $(parent2).replaceWith(node); this.setSelection(node[0], 0, node[0], 0); } else { if (this.browser('msie')) { var node = $('<blockquote>' + $(parent).html() + '</blockquote>'); $(parent).replaceWith(node); } else { this.execRun(cmd, param); } } } else { this.execRun(cmd, param); } } else if (cmd === 'formatblock' && (param === 'pre' || param === 'p')) { parent = this.getParentNode(); if ($(parent).get(0).tagName === 'PRE') { $(parent).replaceWith('<p>' + this.encodeEntities($(parent).text()) + '</p>'); } else { this.execRun(cmd, param); } } else { if (cmd === 'inserthorizontalrule' && this.browser('msie')) { this.$editor.focus(); } if (cmd === 'formatblock' && this.browser('mozilla')) { this.$editor.focus(); } this.execRun(cmd, param); } if (cmd === 'inserthorizontalrule') { this.$editor.find('hr').removeAttr('id'); } this.syncCode(); if (this.oldIE()) { this.$editor.focus(); } if (typeof this.opts.execCommandCallback === 'function') { this.opts.execCommandCallback(this, cmd); } if (this.opts.air) { this.air.hide(); } } catch (e) { } }, execRun: function(cmd, param) { if (cmd === 'formatblock' && this.browser('msie')) { param = '<' + param + '>'; } this.document.execCommand(cmd, false, param); }, // FORMAT NEW LINE formatNewLine: function(e) { var parent = this.getParentNode(); if (parent.nodeName === 'DIV' && parent.className === 'redactor_editor') { var element = $(this.getCurrentNode()); if (element.get(0).tagName === 'DIV' && (element.html() === '' || element.html() === '<br>')) { var newElement = $('<p>').append(element.clone().get(0).childNodes); element.replaceWith(newElement); newElement.html('<br />'); this.setSelection(newElement[0], 0, newElement[0], 0); } } }, // SAFARI SHIFT KEY + ENTER safariShiftKeyEnter: function(e, key) { if (e.shiftKey && key === 13) { e.preventDefault(); this.insertNodeAtCaret($('<span><br /></span>').get(0)); this.syncCode(); return false; } else { return true; } }, // FORMAT EMPTY formatEmpty: function(e) { var html = $.trim(this.$editor.html()); html = html.replace(/<br\s?\/?>/i, ''); var thtml = html.replace(/<p>\s?<\/p>/gi, ''); if (html === '' || thtml === '') { e.preventDefault(); var node = $(this.opts.emptyHtml).get(0); this.$editor.html(node); this.setSelection(node, 0, node, 0); this.syncCode(); return false; } else { this.syncCode(); } }, // PARAGRAPHY paragraphy: function (str) { str = $.trim(str); if (str === '' || str === '<p></p>') { return this.opts.emptyHtml; } // convert div to p if (this.opts.convertDivs) { str = str.replace(/<div(.*?)>([\w\W]*?)<\/div>/gi, '<p>$2</p>'); } // inner functions var X = function(x, a, b) { return x.replace(new RegExp(a, 'g'), b); }; var R = function(a, b) { return X(str, a, b); }; // block elements var blocks = '(table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|style|script|object|input|param|p|h[1-6])'; //str = '<p>' + str; str += '\n'; R('<br />\\s*<br />', '\n\n'); R('(<' + blocks + '[^>]*>)', '\n$1'); R('(</' + blocks + '>)', '$1\n\n'); R('\r\n|\r', '\n'); // newlines R('\n\n+', '\n\n'); // remove duplicates R('\n?((.|\n)+?)$', '<p>$1</p>\n'); // including one at the end R('<p>\\s*?</p>', ''); // remove empty p R('<p>(<div[^>]*>\\s*)', '$1<p>'); R('<p>([^<]+)\\s*?(</(div|address|form)[^>]*>)', '<p>$1</p>$2'); R('<p>\\s*(</?' + blocks + '[^>]*>)\\s*</p>', '$1'); R('<p>(<li.+?)</p>', '$1'); R('<p>\\s*(</?' + blocks + '[^>]*>)', '$1'); R('(</?' + blocks + '[^>]*>)\\s*</p>', '$1'); R('(</?' + blocks + '[^>]*>)\\s*<br />', '$1'); R('<br />(\\s*</?(p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)', '$1'); // pre if (str.indexOf('<pre') != -1) { R('(<pre(.|\n)*?>)((.|\n)*?)</pre>', function(m0, m1, m2, m3) { return X(m1, '\\\\([\'\"\\\\])', '$1') + X(X(X(m3, '<p>', '\n'), '</p>|<br />', ''), '\\\\([\'\"\\\\])', '$1') + '</pre>'; }); } return R('\n</p>$', '</p>'); }, // REMOVE TAGS stripTags: function(html) { var allowed = this.opts.allowedTags; var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi; return html.replace(tags, function ($0, $1) { return $.inArray($1.toLowerCase(), allowed) > '-1' ? $0 : ''; }); }, savePreCode: function(html) { var pre = html.match(/<pre(.*?)>([\w\W]*?)<\/pre>/gi); if (pre !== null) { $.each(pre, $.proxy(function(i,s) { var arr = s.match(/<pre(.*?)>([\w\W]*?)<\/pre>/i); arr[2] = this.encodeEntities(arr[2]); html = html.replace(s, '<pre' + arr[1] + '>' + arr[2] + '</pre>'); }, this)); } return html; }, encodeEntities: function(str) { str = String(str).replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"'); return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;'); }, cleanupPre: function(s) { s = s.replace(/<br>/gi, '\n'); s = s.replace(/<\/p>/gi, '\n'); s = s.replace(/<\/div>/gi, '\n'); var tmp = this.document.createElement("div"); tmp.innerHTML = s; return tmp.textContent||tmp.innerText; }, // PASTE CLEANUP pasteCleanUp: function(html) { var parent = this.getParentNode(); // clean up pre if ($(parent).get(0).tagName === 'PRE') { html = this.cleanupPre(html); this.pasteCleanUpInsert(html); return true; } // remove comments and php tags html = html.replace(/<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi, ''); // remove nbsp html = html.replace(/(&nbsp;){2,}/gi, '&nbsp;'); // remove google docs marker html = html.replace(/<b\sid="internal-source-marker(.*?)">([\w\W]*?)<\/b>/gi, "$2"); // strip tags html = this.stripTags(html); // prevert html = html.replace(/<td><\/td>/gi, '[td]'); html = html.replace(/<td>&nbsp;<\/td>/gi, '[td]'); html = html.replace(/<td><br><\/td>/gi, '[td]'); html = html.replace(/<a(.*?)href="(.*?)"(.*?)>([\w\W]*?)<\/a>/gi, '[a href="$2"]$4[/a]'); html = html.replace(/<iframe(.*?)>([\w\W]*?)<\/iframe>/gi, '[iframe$1]$2[/iframe]'); html = html.replace(/<video(.*?)>([\w\W]*?)<\/video>/gi, '[video$1]$2[/video]'); html = html.replace(/<audio(.*?)>([\w\W]*?)<\/audio>/gi, '[audio$1]$2[/audio]'); html = html.replace(/<embed(.*?)>([\w\W]*?)<\/embed>/gi, '[embed$1]$2[/embed]'); html = html.replace(/<object(.*?)>([\w\W]*?)<\/object>/gi, '[object$1]$2[/object]'); html = html.replace(/<param(.*?)>/gi, '[param$1]'); html = html.replace(/<img(.*?)style="(.*?)"(.*?)>/gi, '[img$1$3]'); // remove attributes html = html.replace(/<(\w+)([\w\W]*?)>/gi, '<$1>'); // remove empty html = html.replace(/<[^\/>][^>]*>(\s*|\t*|\n*|&nbsp;|<br>)<\/[^>]+>/gi, ''); html = html.replace(/<[^\/>][^>]*>(\s*|\t*|\n*|&nbsp;|<br>)<\/[^>]+>/gi, ''); // revert html = html.replace(/\[td\]/gi, '<td>&nbsp;</td>'); html = html.replace(/\[a href="(.*?)"\]([\w\W]*?)\[\/a\]/gi, '<a href="$1">$2</a>'); html = html.replace(/\[iframe(.*?)\]([\w\W]*?)\[\/iframe\]/gi, '<iframe$1>$2</iframe>'); html = html.replace(/\[video(.*?)\]([\w\W]*?)\[\/video\]/gi, '<video$1>$2</video>'); html = html.replace(/\[audio(.*?)\]([\w\W]*?)\[\/audio\]/gi, '<audio$1>$2</audio>'); html = html.replace(/\[embed(.*?)\]([\w\W]*?)\[\/embed\]/gi, '<embed$1>$2</embed>'); html = html.replace(/\[object(.*?)\]([\w\W]*?)\[\/object\]/gi, '<object$1>$2</object>'); html = html.replace(/\[param(.*?)\]/gi, '<param$1>'); html = html.replace(/\[img(.*?)\]/gi, '<img$1>'); // convert div to p if (this.opts.convertDivs) { html = html.replace(/<div(.*?)>([\w\W]*?)<\/div>/gi, '<p>$2</p>'); } // remove span html = html.replace(/<span>([\w\W]*?)<\/span>/gi, '$1'); html = html.replace(/\n{3,}/gi, '\n'); // remove dirty p html = html.replace(/<p><p>/gi, '<p>'); html = html.replace(/<\/p><\/p>/gi, '</p>'); // FF fix if (this.browser('mozilla')) { html = html.replace(/<br>$/gi, ''); } this.pasteCleanUpInsert(html); }, pasteCleanUpInsert: function(html) { this.execCommand('inserthtml', html); if (this.opts.autoresize === true) { $(this.document.body).scrollTop(this.saveScroll); } else { this.$editor.scrollTop(this.saveScroll); } }, // TEXTAREA CODE FORMATTING formattingRemove: function(html) { // save pre var prebuffer = []; var pre = html.match(/<pre(.*?)>([\w\W]*?)<\/pre>/gi); if (pre !== null) { $.each(pre, function(i,s) { html = html.replace(s, 'prebuffer_' + i); prebuffer.push(s); }); } html = html.replace(/\s{2,}/g, ' '); html = html.replace(/\n/g, ' '); html = html.replace(/[\t]*/g, ''); html = html.replace(/\n\s*\n/g, "\n"); html = html.replace(/^[\s\n]*/g, ''); html = html.replace(/[\s\n]*$/g, ''); html = html.replace(/>\s+</g, '><'); if (prebuffer) { $.each(prebuffer, function(i,s) { html = html.replace('prebuffer_' + i, s); }); prebuffer = []; } return html; }, formattingIndenting: function(html) { html = html.replace(/<li/g, "\t<li"); html = html.replace(/<tr/g, "\t<tr"); html = html.replace(/<td/g, "\t\t<td"); html = html.replace(/<\/tr>/g, "\t</tr>"); return html; }, formattingEmptyTags: function(html) { var etags = ["<pre></pre>","<blockquote>\\s*</blockquote>","<em>\\s*</em>","<ul></ul>","<ol></ol>","<li></li>","<table></table>","<tr></tr>","<span>\\s*<span>", "<span>&nbsp;<span>", "<b>\\s*</b>", "<b>&nbsp;</b>", "<p>\\s*</p>", "<p>&nbsp;</p>", "<p>\\s*<br>\\s*</p>", "<div>\\s*</div>", "<div>\\s*<br>\\s*</div>"]; for (var i = 0; i < etags.length; ++i) { var bbb = etags[i]; html = html.replace(new RegExp(bbb,'gi'), ""); } return html; }, formattingAddBefore: function(html) { var lb = '\r\n'; var btags = ["<p", "<form","</ul>", '</ol>', "<fieldset","<legend","<object","<embed","<select","<option","<input","<textarea","<pre","<blockquote","<ul","<ol","<li","<dl","<dt","<dd","<table", "<thead","<tbody","<caption","</caption>","<th","<tr","<td","<figure"]; for (var i = 0; i < btags.length; ++i) { var eee = btags[i]; html = html.replace(new RegExp(eee,'gi'),lb+eee); } return html; }, formattingAddAfter: function(html) { var lb = '\r\n'; var atags = ['</p>', '</div>', '</h1>', '</h2>', '</h3>', '</h4>', '</h5>', '</h6>', '<br>', '<br />', '</dl>', '</dt>', '</dd>', '</form>', '</blockquote>', '</pre>', '</legend>', '</fieldset>', '</object>', '</embed>', '</textarea>', '</select>', '</option>', '</table>', '</thead>', '</tbody>', '</tr>', '</td>', '</th>', '</figure>']; for (var i = 0; i < atags.length; ++i) { var aaa = atags[i]; html = html.replace(new RegExp(aaa,'gi'),aaa+lb); } return html; }, formatting: function(html) { html = this.formattingRemove(html); // empty tags html = this.formattingEmptyTags(html); // add formatting before html = this.formattingAddBefore(html); // add formatting after html = this.formattingAddAfter(html); // indenting html = this.formattingIndenting(html); return html; }, // TOGGLE toggle: function() { var html; if (this.opts.visual) { var height = this.$editor.innerHeight(); this.$editor.hide(); this.$content.hide(); html = this.$editor.html(); //html = $.trim(this.formatting(html)); this.$el.height(height).val(html).show().focus(); this.setBtnActive('html'); this.opts.visual = false; } else { this.$el.hide(); var html = this.$el.val(); //html = this.savePreCode(html); // clean up //html = this.stripTags(html); // set code this.$editor.html(html).show(); this.$content.show(); if (this.$editor.html() === '') { this.setCode(this.opts.emptyHtml); } this.$editor.focus(); this.setBtnInactive('html'); this.opts.visual = true; this.observeImages(); this.observeTables(); } }, // AUTOSAVE autoSave: function() { this.autosaveInterval = setInterval($.proxy(function() { $.ajax({ url: this.opts.autosave, type: 'post', data: this.$el.attr('name') + '=' + escape(encodeURIComponent(this.getCode())), success: $.proxy(function(data) { // callback if (typeof this.opts.autosaveCallback === 'function') { this.opts.autosaveCallback(data, this); } }, this) }); }, this), this.opts.interval*1000); }, // TOOLBAR buildToolbar: function() { if (this.opts.toolbar === false) { return false; } this.$toolbar = $('<ul>').addClass('redactor_toolbar'); if (this.opts.air) { $(this.air).append(this.$toolbar); $('body').append(this.air); } else { if (this.opts.toolbarExternal === false) { this.$box.prepend(this.$toolbar); } else { $(this.opts.toolbarExternal).html(this.$toolbar); } } $.each(this.opts.buttons, $.proxy(function(i,key) { if (key !== '|' && typeof this.opts.toolbar[key] !== 'undefined') { var s = this.opts.toolbar[key]; if (this.opts.fileUpload === false && key === 'file') { return true; } this.$toolbar.append($('<li>').append(this.buildButton(key, s))); } if (key === '|') { this.$toolbar.append($('<li class="redactor_separator"></li>')); } }, this)); }, buildButton: function(key, s) { var button = $('<a href="javascript:void(null);" title="' + s.title + '" class="redactor_btn_' + key + '"></a>'); if (typeof s.func === 'undefined') { button.click($.proxy(function() { if ($.inArray(key, this.opts.activeButtons) != -1) { this.inactiveAllButtons(); this.setBtnActive(key); } if (this.browse