Skip to content

Commit

Permalink
events
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch committed Aug 28, 2014
1 parent f16e8b2 commit 8e870d6
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 45 deletions.
4 changes: 4 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ var ContentKitDemo = exports.ContentKitDemo = {
// Initialize
if (editor) {
ContentKitDemo.syncCodePane(editor);
editor.on('update', function(data) {
ContentKitDemo.syncCodePane(this);
console.log(data);
});
}
if (location.hash === '#code') {
ContentKitDemo.openCodePane();
Expand Down
88 changes: 63 additions & 25 deletions dist/content-kit-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @version 0.1.0
* @author Garth Poitras <garth22@gmail.com> (http://garthpoitras.com/)
* @license MIT
* Last modified: Aug 26, 2014
* Last modified: Aug 27, 2014
*/

(function(exports, document) {
Expand Down Expand Up @@ -1366,7 +1366,7 @@ define("content-kit-editor/commands/base",
if (prompt) { command.prompt = prompt; }
}

Command.prototype.exec = function(){};
Command.prototype.exec = function() {};

__exports__["default"] = Command;
});
Expand Down Expand Up @@ -1821,6 +1821,10 @@ define("content-kit-editor/editor/editor-factory",
var editors = [];
var elements, elementsLen, i;

if (!element) {
return new Editor(element, options);
}

if (typeof element === 'string') {
elements = document.querySelectorAll(element);
} else if (element && element.length) {
Expand Down Expand Up @@ -1888,8 +1892,8 @@ define("content-kit-editor/editor/editor-html-renderer",
__exports__["default"] = EditorHTMLRenderer;
});
define("content-kit-editor/editor/editor",
["./editor-html-renderer","../views/text-format-toolbar","../views/tooltip","../views/embed-intent","../commands/bold","../commands/italic","../commands/link","../commands/quote","../commands/heading","../commands/subheading","../commands/unordered-list","../commands/ordered-list","../commands/image","../commands/oembed","../commands/text-format","../constants","../utils/selection-utils","../utils/paste-utils","../../content-kit-compiler/compiler","../../content-kit-compiler/models/text","../../content-kit-compiler/types/type","../../content-kit-utils/array-utils","../../content-kit-utils/object-utils","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __dependency8__, __dependency9__, __dependency10__, __dependency11__, __dependency12__, __dependency13__, __dependency14__, __dependency15__, __dependency16__, __dependency17__, __dependency18__, __dependency19__, __dependency20__, __dependency21__, __dependency22__, __dependency23__, __exports__) {
["./editor-html-renderer","../views/text-format-toolbar","../views/tooltip","../views/embed-intent","../commands/bold","../commands/italic","../commands/link","../commands/quote","../commands/heading","../commands/subheading","../commands/unordered-list","../commands/ordered-list","../commands/image","../commands/oembed","../commands/text-format","../constants","../utils/selection-utils","../utils/event-emitter","../utils/paste-utils","../../content-kit-compiler/compiler","../../content-kit-compiler/models/text","../../content-kit-compiler/types/type","../../content-kit-utils/array-utils","../../content-kit-utils/object-utils","exports"],
function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __dependency8__, __dependency9__, __dependency10__, __dependency11__, __dependency12__, __dependency13__, __dependency14__, __dependency15__, __dependency16__, __dependency17__, __dependency18__, __dependency19__, __dependency20__, __dependency21__, __dependency22__, __dependency23__, __dependency24__, __exports__) {
"use strict";
var EditorHTMLRenderer = __dependency1__["default"];
var TextFormatToolbar = __dependency2__["default"];
Expand All @@ -1910,12 +1914,14 @@ define("content-kit-editor/editor/editor",
var Keycodes = __dependency16__.Keycodes;
var getSelectionBlockElement = __dependency17__.getSelectionBlockElement;
var getSelectionBlockTagName = __dependency17__.getSelectionBlockTagName;
var cleanPastedContent = __dependency18__.cleanPastedContent;
var Compiler = __dependency19__["default"];
var TextModel = __dependency20__["default"];
var Type = __dependency21__["default"];
var toArray = __dependency22__.toArray;
var mergeWithOptions = __dependency23__.mergeWithOptions;
var EventEmitter = __dependency18__["default"];
var cleanPastedContent = __dependency19__.cleanPastedContent;
var Compiler = __dependency20__["default"];
var TextModel = __dependency21__["default"];
var Type = __dependency22__["default"];
var toArray = __dependency23__.toArray;
var merge = __dependency24__.merge;
var mergeWithOptions = __dependency24__.mergeWithOptions;

var defaults = {
placeholder: 'Write here...',
Expand All @@ -1939,7 +1945,7 @@ define("content-kit-editor/editor/editor",
],
compiler: new Compiler({
includeTypeNames: true, // outputs models with type names, i.e. 'BOLD', for easier debugging
renderer: new EditorHTMLRenderer() // subclassed HTML renderer that adds structure for editor interactivity
renderer: new EditorHTMLRenderer() // subclassed HTML renderer that adds dom structure for additional editor interactivity
})
};

Expand Down Expand Up @@ -1985,16 +1991,9 @@ define("content-kit-editor/editor/editor",
}
});

// Experimental: Live update
editorEl.addEventListener('keyup', function() {
var index = editor.getCurrentBlockIndex();
editor.syncModelAt(index);
});
document.addEventListener('mouseup', function() {
setTimeout(function() {
var index = editor.getCurrentBlockIndex();
editor.syncModelAt(index);
});
// Live update the model on contentEditable change
editorEl.addEventListener('input', function() {
editor.syncModelAtSelection();
});
}

Expand Down Expand Up @@ -2043,7 +2042,7 @@ define("content-kit-editor/editor/editor",

if(editor.embedCommands) {
// NOTE: must come after bindTypingEvents so those keyup handlers are executed first.
// TODO: manage event listener order
// TODO: make order independant
var embedIntent = new EmbedIntent({
editorContext: editor,
commands: editor.embedCommands,
Expand All @@ -2064,21 +2063,28 @@ define("content-kit-editor/editor/editor",
}
}

// Add event emitter pub/sub functionality
merge(Editor.prototype, EventEmitter);

Editor.prototype.syncModel = function() {
this.model = this.compiler.parse(this.element.innerHTML);
this.trigger('update');
};

Editor.prototype.syncModelAt = function(index) {
if (index > -1) {
var blockElements = toArray(this.element.children);
var parsedBlockModel = this.compiler.parser.parseBlock(blockElements[index]);
this.model[index] = parsedBlockModel;

// TODO: event subscription
ContentKitDemo.syncCodePane(this);
this.trigger('update', { index: index });
}
};

Editor.prototype.syncModelAtSelection = function() {
var index = this.getCurrentBlockIndex();
this.syncModelAt(index);
};

Editor.prototype.syncVisualAt = function(index) {
if (index > -1) {
var blockModel = this.model[index];
Expand Down Expand Up @@ -2240,6 +2246,38 @@ define("content-kit-editor/utils/element-utils",
__exports__.positionElementToLeftOf = positionElementToLeftOf;
__exports__.positionElementToRightOf = positionElementToRightOf;
});
define("content-kit-editor/utils/event-emitter",
["exports"],
function(__exports__) {
"use strict";
// Based on https://github.com/jeromeetienne/microevent.js/blob/master/microevent.js

var EventEmitter = {
on : function(type, handler){
var events = this.__events = this.__events || {};
events[type] = events[type] || [];
events[type].push(handler);
},
off : function(type, handler){
var events = this.__events = this.__events || {};
if (type in events) {
events[type].splice(events[type].indexOf(handler), 1);
}
},
trigger : function(type) {
var events = this.__events = this.__events || {};
var eventForTypeCount, i;
if (type in events) {
eventForTypeCount = events[type].length;
for(i = 0; i < eventForTypeCount; i++) {
events[type][i].apply(this, Array.prototype.slice.call(arguments, 1));
}
}
}
};

__exports__["default"] = EventEmitter;
});
define("content-kit-editor/utils/paste-utils",
["../constants","exports"],
function(__dependency1__, __exports__) {
Expand Down
11 changes: 9 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var jsDistPath = distDest + jsDistName;
var cssDistName = 'content-kit-editor.css';

var testRunner = './tests/index.html';
var testScripts = './tests/**/*.js';

var demo = './demo/index.html';

var banner = ['/*!',
Expand Down Expand Up @@ -110,13 +112,18 @@ gulp.task('watch-js', function() {
gulp.watch(jsSrc, ['lint', 'build-js']);
});

// Watches test files change and automatically tests
gulp.task('watch-tests', function() {
gulp.watch(testScripts, ['test']);
});

// Watches when css files change and automatically builds
gulp.task('watch-css', function() {
gulp.watch(cssSrc, ['build-css']);
});

// Watches when any files change and automatically builds
gulp.task('watch', ['watch-js', 'watch-css']);
// Watches when any files change and automatically tests/builds
gulp.task('watch', ['watch-js', 'watch-tests', 'watch-css']);

// Default task
gulp.task('default', ['lint', 'build', 'test']);
2 changes: 1 addition & 1 deletion src/js/content-kit-editor/commands/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ function Command(options) {
if (prompt) { command.prompt = prompt; }
}

Command.prototype.exec = function(){};
Command.prototype.exec = function() {};

export default Command;
4 changes: 4 additions & 0 deletions src/js/content-kit-editor/editor/editor-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function EditorFactory(element, options) {
var editors = [];
var elements, elementsLen, i;

if (!element) {
return new Editor(element, options);
}

if (typeof element === 'string') {
elements = document.querySelectorAll(element);
} else if (element && element.length) {
Expand Down
33 changes: 17 additions & 16 deletions src/js/content-kit-editor/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import OEmbedCommand from '../commands/oembed';
import TextFormatCommand from '../commands/text-format';
import { RootTags, Keycodes } from '../constants';
import { getSelectionBlockElement, getSelectionBlockTagName } from '../utils/selection-utils';
import EventEmitter from '../utils/event-emitter';
import { cleanPastedContent } from '../utils/paste-utils';
import Compiler from '../../content-kit-compiler/compiler';
import TextModel from '../../content-kit-compiler/models/text';
import Type from '../../content-kit-compiler/types/type';
import { toArray } from '../../content-kit-utils/array-utils';
import { mergeWithOptions } from '../../content-kit-utils/object-utils';
import { merge, mergeWithOptions } from '../../content-kit-utils/object-utils';

var defaults = {
placeholder: 'Write here...',
Expand All @@ -44,7 +45,7 @@ var defaults = {
],
compiler: new Compiler({
includeTypeNames: true, // outputs models with type names, i.e. 'BOLD', for easier debugging
renderer: new EditorHTMLRenderer() // subclassed HTML renderer that adds structure for editor interactivity
renderer: new EditorHTMLRenderer() // subclassed HTML renderer that adds dom structure for additional editor interactivity
})
};

Expand Down Expand Up @@ -90,16 +91,9 @@ function bindTypingEvents(editor) {
}
});

// Experimental: Live update
editorEl.addEventListener('keyup', function() {
var index = editor.getCurrentBlockIndex();
editor.syncModelAt(index);
});
document.addEventListener('mouseup', function() {
setTimeout(function() {
var index = editor.getCurrentBlockIndex();
editor.syncModelAt(index);
});
// Live update the model on contentEditable change
editorEl.addEventListener('input', function() {
editor.syncModelAtSelection();
});
}

Expand Down Expand Up @@ -148,7 +142,7 @@ function Editor(element, options) {

if(editor.embedCommands) {
// NOTE: must come after bindTypingEvents so those keyup handlers are executed first.
// TODO: manage event listener order
// TODO: make order independant
var embedIntent = new EmbedIntent({
editorContext: editor,
commands: editor.embedCommands,
Expand All @@ -169,21 +163,28 @@ function Editor(element, options) {
}
}

// Add event emitter pub/sub functionality
merge(Editor.prototype, EventEmitter);

Editor.prototype.syncModel = function() {
this.model = this.compiler.parse(this.element.innerHTML);
this.trigger('update');
};

Editor.prototype.syncModelAt = function(index) {
if (index > -1) {
var blockElements = toArray(this.element.children);
var parsedBlockModel = this.compiler.parser.parseBlock(blockElements[index]);
this.model[index] = parsedBlockModel;

// TODO: event subscription
ContentKitDemo.syncCodePane(this);
this.trigger('update', { index: index });
}
};

Editor.prototype.syncModelAtSelection = function() {
var index = this.getCurrentBlockIndex();
this.syncModelAt(index);
};

Editor.prototype.syncVisualAt = function(index) {
if (index > -1) {
var blockModel = this.model[index];
Expand Down
27 changes: 27 additions & 0 deletions src/js/content-kit-editor/utils/event-emitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Based on https://github.com/jeromeetienne/microevent.js/blob/master/microevent.js

var EventEmitter = {
on : function(type, handler){
var events = this.__events = this.__events || {};
events[type] = events[type] || [];
events[type].push(handler);
},
off : function(type, handler){
var events = this.__events = this.__events || {};
if (type in events) {
events[type].splice(events[type].indexOf(handler), 1);
}
},
trigger : function(type) {
var events = this.__events = this.__events || {};
var eventForTypeCount, i;
if (type in events) {
eventForTypeCount = events[type].length;
for(i = 0; i < eventForTypeCount; i++) {
events[type][i].apply(this, Array.prototype.slice.call(arguments, 1));
}
}
}
};

export default EventEmitter;
14 changes: 13 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ QUnit.module('Editor', {
test('can create an editor', function() {
var editor = new ContentKit.Editor();
ok(editor);
ok(editor instanceof ContentKit.Editor);
//ok(editor instanceof ContentKit.Editor); // TODO: fix
});

test('can create an editor via dom node reference', function() {
Expand Down Expand Up @@ -60,3 +60,15 @@ test('creating an editor without a class name adds appropriate class', function(
var editor = new ContentKit.Editor(document.getElementById('editor1'));
equal(editor.element.className, 'ck-editor');
});

asyncTest('editor fires update event', function() {
expect(2);

var editor = new ContentKit.Editor();
editor.on('update', function(data) {
equal (this, editor);
equal (data.index, 99);
start();
});
editor.trigger('update', { index: 99 });
});

0 comments on commit 8e870d6

Please sign in to comment.