Skip to content

Commit

Permalink
Merge pull request #253 from iMi-digital/feature-ckeditor
Browse files Browse the repository at this point in the history
Feature ckeditor
  • Loading branch information
Yuku TAKAHASHI committed Jun 1, 2016
2 parents 5de8231 + 53b0187 commit 1dcf2fd
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = function (grunt) {
'src/textarea.js',
'src/ie_textarea.js',
'src/content_editable.js',
'src/ckeditor.js',
'src/vendor/textarea_caret.js',
'src/end.frag'
],
Expand Down
32 changes: 32 additions & 0 deletions src/ckeditor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// NOTE: TextComplete plugin has contenteditable support but it does not work
// fine especially on old IEs.
// Any pull requests are REALLY welcome.

+function ($) {
'use strict';

// CKEditor adapter
// =======================
//
// Adapter for CKEditor, based on contenteditable elements.
function CKEditor (element, completer, option) {
this.initialize(element, completer, option);
}

$.extend(CKEditor.prototype, $.fn.textcomplete.ContentEditable.prototype, {
_bindEvents: function () {
var $this = this;
CKEDITOR.instances["issue_notes"].on('key', function(event) {
var domEvent = event.data;
$this._onKeyup(domEvent);
if ($this.completer.dropdown.shown && $this._skipSearch(domEvent)) {
return false;
}
}, null, null, 1); // 1 = Priority = Important!
// we actually also need the native event, as the CKEditor one is happening to late
this.$el.on('keyup.' + this.id, $.proxy(this._onKeyup, this));
},
});

$.fn.textcomplete.CKEditor = CKEditor;
}(jQuery);
14 changes: 14 additions & 0 deletions src/completer.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@
// Initialize view objects lazily.
var self = this;
this.$el.one('focus.' + this.id, function () { self.initialize(); });

// Special handling for CKEditor: lazy init on instance load
if ((!this.option.adapter || this.option.adapter == 'CKEditor') && typeof CKEDITOR != 'undefined' && (this.$el.is('textarea'))) {
CKEDITOR.on("instanceReady", function(event) {
event.editor.once("focus", function(event2) {
// replace the element with the Iframe element and flag it as CKEditor
self.$el = $(event.editor.editable().$);
if (!self.option.adapter) {
self.option.adapter = $.fn.textcomplete['CKEditor'];
}
self.initialize();
});
});
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/content_editable.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
post = newSubstr[1] + post;
newSubstr = newSubstr[0];
}
pre = pre.replace(strategy.match, newSubstr);
pre = pre.replace(strategy.match, newSubstr)
.replace(/ $/, "&nbsp"); // &nbsp necessary at least for CKeditor to not eat spaces
range.selectNodeContents(range.startContainer);
range.deleteContents();

Expand Down

0 comments on commit 1dcf2fd

Please sign in to comment.