vendor/assets/javascripts/simditor/simditor.js in simditor-2.2.2 vs vendor/assets/javascripts/simditor/simditor.js in simditor-2.2.3

- old
+ new

@@ -1,10 +1,5 @@ -/*! -* Simditor v2.2.2 -* http://simditor.tower.im/ -* 2015-07-31 -*/ (function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module unless amdModuleId is set define('simditor', ["jquery","simple-module","simple-hotkeys","simple-uploader"], function ($, SimpleModule, simpleHotkeys, simpleUploader) { return (root['Simditor'] = factory($, SimpleModule, simpleHotkeys, simpleUploader)); @@ -1575,10 +1570,13 @@ currentState = this.currentState(); if (!currentState) { return; } html = this.editor.body.html(); + if (html !== currentState.html) { + return; + } currentState.html = html; return currentState.caret = this.caretPosition(); }; UndoManager.prototype._getNodeOffset = function(node, index) { @@ -1667,15 +1665,11 @@ end: this.endPosition(), collapsed: range.collapsed } : {}; return caret; } else { - if (!this.editor.inputManager.focused) { - this.editor.body.focus(); - } if (!caret.start) { - this.editor.body.blur(); return; } startContainer = this._getNodeByPosition(caret.start); startOffset = caret.start[caret.start.length - 1]; if (caret.collapsed) { @@ -1983,11 +1977,11 @@ wrapper: '<div class="simditor-toolbar"><ul></ul></div>', separator: '<li><span class="separator"></span></li>' }; Toolbar.prototype._init = function() { - var toolbarHeight; + var floatInitialized, initToolbarFloat, toolbarHeight; this.editor = this._module; if (!this.opts.toolbar) { return; } if (!$.isArray(this.opts.toolbar)) { @@ -2008,34 +2002,44 @@ }; })(this)); if (!this.opts.toolbarHidden && this.opts.toolbarFloat) { this.wrapper.css('top', this.opts.toolbarFloatOffset); toolbarHeight = 0; - $(window).on('resize.simditor-' + this.editor.id, (function(_this) { - return function(e) { + initToolbarFloat = (function(_this) { + return function() { _this.wrapper.css('position', 'static'); _this.wrapper.width('auto'); _this.editor.util.reflow(_this.wrapper); _this.wrapper.width(_this.wrapper.outerWidth()); - _this.wrapper.css('left', _this.wrapper.offset().left); + _this.wrapper.css('left', _this.editor.util.os.mobile ? _this.wrapper.position().left : _this.wrapper.offset().left); _this.wrapper.css('position', ''); toolbarHeight = _this.wrapper.outerHeight(); - return _this.editor.placeholderEl.css('top', toolbarHeight); + _this.editor.placeholderEl.css('top', toolbarHeight); + return true; }; - })(this)).resize(); + })(this); + $(window).on('resize.simditor-' + this.editor.id, function(e) { + var floatInitialized; + return floatInitialized = null; + }); + floatInitialized = null; $(window).on('scroll.simditor-' + this.editor.id, (function(_this) { return function(e) { var bottomEdge, scrollTop, topEdge; + if (!_this.wrapper.is(':visible')) { + return; + } topEdge = _this.editor.wrapper.offset().top; bottomEdge = topEdge + _this.editor.wrapper.outerHeight() - 80; scrollTop = $(document).scrollTop() + _this.opts.toolbarFloatOffset; if (scrollTop <= topEdge || scrollTop >= bottomEdge) { _this.editor.wrapper.removeClass('toolbar-floating').css('padding-top', ''); if (_this.editor.util.os.mobile) { return _this.wrapper.css('top', _this.opts.toolbarFloatOffset); } } else { + floatInitialized || (floatInitialized = initToolbarFloat()); _this.editor.wrapper.addClass('toolbar-floating').css('padding-top', toolbarHeight); if (_this.editor.util.os.mobile) { return _this.wrapper.css('top', scrollTop - topEdge + _this.opts.toolbarFloatOffset); } } @@ -2459,24 +2463,17 @@ this.textarea.val(val); return val; }; Simditor.prototype.focus = function() { - var $blockEl, range; if (!(this.body.is(':visible') && this.body.is('[contenteditable]'))) { this.el.find('textarea:visible').focus(); return; } if (this.inputManager.lastCaretPosition) { return this.undoManager.caretPosition(this.inputManager.lastCaretPosition); } else { - $blockEl = this.body.find('p').last(); - if (!($blockEl.length > 0)) { - $blockEl = $('<p/>').append(this.util.phBr).appendTo(this.body); - } - range = document.createRange(); - this.selection.setRangeAtEndOf($blockEl, range); return this.body.focus(); } }; Simditor.prototype.blur = function() { @@ -2516,10 +2513,11 @@ 'zh-CN': { 'blockquote': '引用', 'bold': '加粗文字', 'code': '插入代码', 'color': '文字颜色', + 'coloredText': '彩色文字', 'hr': '分隔线', 'image': '插入图片', 'externalImage': '外链图片', 'uploadImage': '上传图片', 'uploadFailed': '上传失败了', @@ -2560,10 +2558,11 @@ 'en-US': { 'blockquote': 'Block Quote', 'bold': 'Bold', 'code': 'Code', 'color': 'Text Color', + 'coloredText': 'Colored Text', 'hr': 'Horizontal Line', 'image': 'Insert Image', 'externalImage': 'External Image', 'uploadImage': 'Upload Image', 'uploadFailed': 'Upload failed', @@ -3220,11 +3219,11 @@ this.menuWrapper.on('mousedown', '.color-list', function(e) { return false; }); return this.menuWrapper.on('click', '.font-color', (function(_this) { return function(e) { - var $link, $p, hex, rgb; + var $link, $p, hex, range, rgb, textNode; _this.wrapper.removeClass('menu-on'); $link = $(e.currentTarget); if ($link.hasClass('font-color-default')) { $p = _this.editor.body.find('p, li'); if (!($p.length > 0)) { @@ -3237,10 +3236,17 @@ hex = _this._convertRgbToHex(rgb); } if (!hex) { return; } + range = _this.editor.selection.range(); + if (!$link.hasClass('font-color-default') && range.collapsed) { + textNode = document.createTextNode(_this._t('coloredText')); + range.insertNode(textNode); + range.selectNodeContents(textNode); + _this.editor.selection.range(range); + } document.execCommand('styleWithCSS', false, true); document.execCommand('foreColor', false, hex); document.execCommand('styleWithCSS', false, false); if (!_this.editor.util.support.oninput) { return _this.editor.trigger('valuechanged'); @@ -4268,16 +4274,17 @@ this.widthEl = this.el.find('#image-width'); this.heightEl = this.el.find('#image-height'); this.altEl = this.el.find('#image-alt'); this.srcEl.on('keydown', (function(_this) { return function(e) { + var range; if (!(e.which === 13 && !_this.target.hasClass('uploading'))) { return; } e.preventDefault(); - _this.button.editor.body.focus(); - _this.button.editor.selection.setRangeAfter(_this.target); + range = document.createRange(); + _this.button.editor.selection.setRangeAfter(_this.target, range); return _this.hide(); }; })(this)); this.srcEl.on('blur', (function(_this) { return function(e) { @@ -4299,33 +4306,35 @@ } }; })(this)); this.el.find('.image-size').on('keydown', (function(_this) { return function(e) { - var inputEl; + var $img, inputEl, range; inputEl = $(e.currentTarget); if (e.which === 13 || e.which === 27) { e.preventDefault(); if (e.which === 13) { _this._resizeImg(inputEl); } else { _this._restoreImg(); } - _this.button.editor.body.focus(); - _this.button.editor.selection.setRangeAfter(_this.target); - return _this.hide(); + $img = _this.target; + _this.hide(); + range = document.createRange(); + return _this.button.editor.selection.setRangeAfter($img, range); } else if (e.which === 9) { return _this.el.data('popover').refresh(); } }; })(this)); this.altEl.on('keydown', (function(_this) { return function(e) { + var range; if (e.which === 13) { e.preventDefault(); - _this.button.editor.body.focus(); - _this.button.editor.selection.setRangeAfter(_this.target); + range = document.createRange(); + _this.button.editor.selection.setRangeAfter(_this.target, range); return _this.hide(); } }; })(this)); this.altEl.on('keyup', (function(_this) { @@ -4391,11 +4400,11 @@ var height, value, width; if (onlySetVal == null) { onlySetVal = false; } value = inputEl.val() * 1; - if (!($.isNumeric(value) || value < 0)) { + if (!(this.target && ($.isNumeric(value) || value < 0))) { return; } if (inputEl.is(this.widthEl)) { width = value; height = this.height * value / this.width; @@ -4408,11 +4417,11 @@ if (!onlySetVal) { this.target.attr({ width: width, height: height }); + return this.editor.trigger('valuechanged'); } - return this.editor.trigger('valuechanged'); }; ImagePopover.prototype._restoreImg = function() { var ref, size; size = ((ref = this.target.data('image-size')) != null ? ref.split(",") : void 0) || [this.width, this.height];