Skip to content

Commit

Permalink
Introduce cards
Browse files Browse the repository at this point in the history
  • Loading branch information
mixonic committed Jun 24, 2015
1 parent 278e02c commit ca43198
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 17 deletions.
8 changes: 7 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ <h3>Keyboard shortcuts:</h3>

<script>
// Initialize a new Editor
var editor = new ContentKit.Editor('.editor');
var editor = new ContentKit.Editor('.editor', {
cards: {
'pick-color': function renderPickColor(payload) {
return 'PICK A COLOR: '+payload.options.join(', ');
}
}
});
</script>

<!-- JS just for the demo page -->
Expand Down
38 changes: 38 additions & 0 deletions src/js/commands/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Command from './base';
import { inherit } from 'node_modules/content-kit-utils/src/object-utils';
import BlockModel from 'node_modules/content-kit-compiler/src/models/block';
import Type from 'node_modules/content-kit-compiler/src/types/type';

function injectCardBlock(cardName, cardPayload, editor, index) {
// FIXME: Do we change the block model internal representation here?
var cardBlock = BlockModel.createWithType(Type.CARD, {
attributes: {
name: cardName,
payload: cardPayload
}
});
editor.replaceBlock(cardBlock, index);
}

function CardCommand() {
Command.call(this, {
name: 'card',
button: '<i>CA</i>'
});
}
inherit(CardCommand, Command);

CardCommand.prototype = {
exec: function() {
CardCommand._super.prototype.exec.call(this);
var editor = this.editorContext;
var currentEditingIndex = editor.getCurrentBlockIndex();

var cardName = 'pick-color';
var cardPayload = { options: ['red', 'blue'] };
injectCardBlock(cardName, cardPayload, editor, currentEditingIndex);
editor.renderBlockAt(currentEditingIndex, true);
}
};

export default CardCommand;
2 changes: 1 addition & 1 deletion src/js/commands/oembed.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ OEmbedCommand.prototype.exec = function(url) {
var editorContext = command.editorContext;
var embedIntent = command.embedIntent;
var index = editorContext.getCurrentBlockIndex();

embedIntent.showLoading();
this.embedService.fetch({
url: url,
Expand Down
26 changes: 14 additions & 12 deletions src/js/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import UnorderedListCommand from '../commands/unordered-list';
import OrderedListCommand from '../commands/ordered-list';
import ImageCommand from '../commands/image';
import OEmbedCommand from '../commands/oembed';
import CardCommand from '../commands/card';
import Keycodes from '../utils/keycodes';
import { getSelectionBlockElement, getCursorOffsetInElement } from '../utils/selection-utils';
import EventEmitter from '../utils/event-emitter';
Expand All @@ -36,17 +37,16 @@ var defaults = {
new SubheadingCommand()
],
embedCommands: [
new ImageCommand({ serviceUrl: '/upload' }),
new OEmbedCommand({ serviceUrl: '/embed' })
new ImageCommand({ serviceUrl: '/upload' }),
new OEmbedCommand({ serviceUrl: '/embed' }),
new CardCommand()
],
autoTypingCommands: [
new UnorderedListCommand(),
new OrderedListCommand()
],
compiler: new Compiler({
includeTypeNames: true, // outputs models with type names, i.e. 'BOLD', for easier debugging
renderer: new EditorHTMLRenderer() // subclassed HTML renderer that adds dom structure for additional editor interactivity
})
compiler: null,
cards: {}
};

function bindContentEditableTypingListeners(editor) {
Expand Down Expand Up @@ -169,12 +169,14 @@ function getNonTextBlocks(blockTypeSet, model) {
function Editor(element, options) {
var editor = this;
mergeWithOptions(editor, defaults, options);

// Update embed commands by prepending the serverHost
editor.embedCommands = [
new ImageCommand({ serviceUrl: editor.serverHost + '/upload' }),
new OEmbedCommand({ serviceUrl: editor.serverHost + '/embed' })
];
if (!editor.compiler) {
editor.compiler = new Compiler({
includeTypeNames: true, // outputs models with type names, i.e. 'BOLD', for easier debugging
renderer: new EditorHTMLRenderer({
cards: editor.cards
}) // subclassed HTML renderer that adds dom structure for additional editor interactivity
});
}

if (element) {
applyClassName(element);
Expand Down
9 changes: 6 additions & 3 deletions src/js/renderers/editor-html-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import YouTubeRenderer from './youtube';
import TwitterRenderer from './twitter';
import InstagramRenderer from './instagram';
import LinkImageRenderer from './link-image-renderer';
import { merge } from 'node_modules/content-kit-utils/src/object-utils';

/**
* A dictionary of supported embeds types that we'll custom render
Expand Down Expand Up @@ -55,10 +56,12 @@ typeRenderers[Type.IMAGE.id] = imageRenderer;
* Subclass of HTMLRenderer specifically for the Editor
* Wraps interactive elements to add functionality
*/
function EditorHTMLRenderer() {
HTMLRenderer.call(this, {
function EditorHTMLRenderer(options) {
var rendererOptions = {
typeRenderers: typeRenderers
});
};
merge(rendererOptions, options);
HTMLRenderer.call(this, rendererOptions);
}
inherit(EditorHTMLRenderer, HTMLRenderer);

Expand Down

0 comments on commit ca43198

Please sign in to comment.