Skip to content

Commit

Permalink
crude oEmbed working
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch committed Jul 29, 2014
1 parent 0396d21 commit 7d523d4
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 15 deletions.
6 changes: 5 additions & 1 deletion dist/content-kit-editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
max-width: 100%;
margin: 0 auto;
}
.ck-editor div,
.ck-editor iframe {
max-width: 100%;
}
/**
* Toolbar
*/
Expand Down Expand Up @@ -123,7 +127,7 @@
border: none;
color: #f5f5f5;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 15px;
font-size: 14px;
padding: 0 16px;
width: 288px;
height: 44px;
Expand Down
59 changes: 52 additions & 7 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: Jul 25, 2014
* Last modified: Jul 29, 2014
*/

(function(exports, document) {
Expand Down Expand Up @@ -258,6 +258,37 @@ function selectNode(node) {
selection.addRange(range);
}

var HTTP = (function() {

var head = document.head;
var uuid = 0;

return {
get: function(url, callback) {
var request = new XMLHttpRequest();
request.onload = function() {
callback(this.responseText);
};
request.open('GET', url);
request.send();
},

jsonp: function(url, callback) {
var script = document.createElement('script');
var name = '_jsonp_' + uuid++;
url += ( url.match(/\?/) ? '&' : '?' ) + 'callback=' + name;
script.src = url;
exports[name] = function(response) {
callback(JSON.parse(response));
head.removeChild(script);
delete exports[name];
};
head.appendChild(script);
}
};

}());

function View(options) {
this.tagName = options.tagName || 'div';
this.classNames = options.classNames || [];
Expand Down Expand Up @@ -574,11 +605,11 @@ ImageEmbedCommand.prototype = {
reader.onload = function(event) {
var base64File = event.target.result;
var blockElement = getSelectionBlockElement();
var editorNode = blockElement.parentNode;
var image = document.createElement('img');
image.src = base64File;

// image needs to be placed outside of the current empty paragraph
var editorNode = blockElement.parentNode;
editorNode.insertBefore(image, blockElement);
editorNode.removeChild(blockElement);
};
Expand All @@ -587,21 +618,35 @@ ImageEmbedCommand.prototype = {
}
};

function MediaEmbedCommand(options) {
function OEmbedCommand(options) {
EmbedCommand.call(this, {
name: 'media',
name: 'oEmbed',
button: '<i class="ck-icon-embed"></i>',
prompt: new Prompt({
command: this,
placeholder: 'Enter a twitter, or youtube url...'
placeholder: 'Enter a youtube, twitter, instagram url...'
})
});
}
inherits(MediaEmbedCommand, EmbedCommand);
inherits(OEmbedCommand, EmbedCommand);
OEmbedCommand.prototype.exec = function(url) {
HTTP.get('http://noembed.com/embed?url=' + url, function(responseText) {
var json = JSON.parse(responseText);
console.log(json);
if (json.html) {
var blockElement = getSelectionBlockElement();
var editorNode = blockElement.parentNode;
var embed = createDiv();
embed.innerHTML = json.html;
editorNode.insertBefore(embed, blockElement);
editorNode.removeChild(blockElement);
}
});
};

EmbedCommand.all = [
new ImageEmbedCommand(),
new MediaEmbedCommand()
new OEmbedCommand()
];

EmbedCommand.index = createCommandIndex(EmbedCommand.all);
Expand Down
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var jsSrc = [
'./src/js/utils/object-utils.js',
'./src/js/utils/element-utils.js',
'./src/js/utils/selection-utils.js',
'./src/js/utils/http-utils.js',
'./src/js/view.js',
'./src/js/prompt.js',
'./src/js/commands.js',
Expand Down
4 changes: 4 additions & 0 deletions src/css/editor.less
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
display: block;
max-width: 100%;
margin: 0 auto;
}
.ck-editor div,
.ck-editor iframe {
max-width: 100%;
}
2 changes: 1 addition & 1 deletion src/css/toolbar.less
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
border: none;
color: #f5f5f5;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 15px;
font-size: 14px;
padding: 0 16px;
width: 288px;
height: 44px;
Expand Down
26 changes: 20 additions & 6 deletions src/js/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ ImageEmbedCommand.prototype = {
reader.onload = function(event) {
var base64File = event.target.result;
var blockElement = getSelectionBlockElement();
var editorNode = blockElement.parentNode;
var image = document.createElement('img');
image.src = base64File;

// image needs to be placed outside of the current empty paragraph
var editorNode = blockElement.parentNode;
editorNode.insertBefore(image, blockElement);
editorNode.removeChild(blockElement);
};
Expand All @@ -232,21 +232,35 @@ ImageEmbedCommand.prototype = {
}
};

function MediaEmbedCommand(options) {
function OEmbedCommand(options) {
EmbedCommand.call(this, {
name: 'media',
name: 'oEmbed',
button: '<i class="ck-icon-embed"></i>',
prompt: new Prompt({
command: this,
placeholder: 'Enter a twitter, or youtube url...'
placeholder: 'Enter a youtube, twitter, instagram url...'
})
});
}
inherits(MediaEmbedCommand, EmbedCommand);
inherits(OEmbedCommand, EmbedCommand);
OEmbedCommand.prototype.exec = function(url) {
HTTP.get('http://noembed.com/embed?url=' + url, function(responseText) {
var json = JSON.parse(responseText);
console.log(json);
if (json.html) {
var blockElement = getSelectionBlockElement();
var editorNode = blockElement.parentNode;
var embed = createDiv();
embed.innerHTML = json.html;
editorNode.insertBefore(embed, blockElement);
editorNode.removeChild(blockElement);
}
});
};

EmbedCommand.all = [
new ImageEmbedCommand(),
new MediaEmbedCommand()
new OEmbedCommand()
];

EmbedCommand.index = createCommandIndex(EmbedCommand.all);
30 changes: 30 additions & 0 deletions src/js/utils/http-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var HTTP = (function() {

var head = document.head;
var uuid = 0;

return {
get: function(url, callback) {
var request = new XMLHttpRequest();
request.onload = function() {
callback(this.responseText);
};
request.open('GET', url);
request.send();
},

jsonp: function(url, callback) {
var script = document.createElement('script');
var name = '_jsonp_' + uuid++;
url += ( url.match(/\?/) ? '&' : '?' ) + 'callback=' + name;
script.src = url;
exports[name] = function(response) {
callback(JSON.parse(response));
head.removeChild(script);
delete exports[name];
};
head.appendChild(script);
}
};

}());

0 comments on commit 7d523d4

Please sign in to comment.