Skip to content

Commit

Permalink
crude image embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
gpoitch committed Jul 17, 2014
1 parent 3bdb065 commit 84a634a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 13 deletions.
10 changes: 9 additions & 1 deletion dist/content-kit-editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
padding-left: 1.05em;
color: #a0a0a0;
}

.ck-editor img {
display: block;
max-width: 100%;
margin: 0 auto;
}
/**
* Toolbar
*/
Expand Down Expand Up @@ -259,6 +263,10 @@
transform: rotate(-135deg);
}

.ck-file-input {
display:none;
}

/**
* Icons
*/
Expand Down
53 changes: 47 additions & 6 deletions dist/content-kit-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ function EmbedCommand(options) {
Command.call(this, options);
}
inherits(EmbedCommand, Command);
EmbedCommand.prototype.exec = function(value) {
EmbedCommand.prototype.exec = function() {
alert(this.name);
};

Expand All @@ -516,13 +516,49 @@ function ImageEmbedCommand(options) {
name: 'image',
button: '<i class="ck-icon-image"></i>'
});

var fileBrowser = document.createElement('input');
fileBrowser.type = 'file';
fileBrowser.accept = 'image/*';
fileBrowser.className = 'ck-file-input';
fileBrowser.addEventListener('change', this.handleFile);
document.body.appendChild(fileBrowser);
this.fileBrowser = fileBrowser;
}
inherits(ImageEmbedCommand, EmbedCommand);
ImageEmbedCommand.prototype = {
exec: function() {
var clickEvent = new MouseEvent('click', { bubbles: false });
this.fileBrowser.dispatchEvent(clickEvent);
},
handleFile: function(e) {
var target = e.target;
var file = target && target.files[0];
var reader = new FileReader();
reader.onload = function(event) {
var base64File = event.target.result;
var selectionRoot = getCurrentSelectionRootNode();
var image = document.createElement('img');
image.src = base64File;

// image needs to be placed outside of the current empty paragraph
var editorNode = selectionRoot.parentNode;
editorNode.insertBefore(image, selectionRoot);
editorNode.removeChild(selectionRoot);
};
reader.readAsDataURL(file);
target.value = null; // reset
}
};

function MediaEmbedCommand(options) {
EmbedCommand.call(this, {
name: 'media',
button: '<i class="ck-icon-embed"></i>'
button: '<i class="ck-icon-embed"></i>',
prompt: new Prompt({
command: this,
placeholder: 'Enter a twitter, or youtube url...'
})
});
}
inherits(MediaEmbedCommand, EmbedCommand);
Expand Down Expand Up @@ -813,9 +849,13 @@ var Toolbar = (function() {
},
updateForSelection: function(selection) {
var toolbar = this;
toolbar.show();
toolbar.positionToContent(selection.getRangeAt(0));
updateButtonsForSelection(toolbar.buttons, selection);
if (selection.isCollapsed) {
toolbar.hide();
} else {
toolbar.show();
toolbar.positionToContent(selection.getRangeAt(0));
updateButtonsForSelection(toolbar.buttons, selection);
}
},
positionToContent: function(content) {
var directions = ToolbarDirection;
Expand Down Expand Up @@ -999,14 +1039,15 @@ var EmbedIntent = (function() {
embedIntent.isShowing = false;
embedIntent.isActive = false;

function embedIntentHandler() {
function embedIntentHandler(e) {
var currentNode = getCurrentSelectionRootNode();
var currentNodeHTML = currentNode.innerHTML;
if (currentNodeHTML === '' || currentNodeHTML === '<br>') {
embedIntent.showAt(currentNode);
} else {
embedIntent.hide();
}
e.stopPropagation();
}

rootElement.addEventListener('keyup', embedIntentHandler);
Expand Down
5 changes: 5 additions & 0 deletions src/css/editor.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@
padding-left: 1.05em;
color: #a0a0a0;
}
.ck-editor img {
display: block;
max-width: 100%;
margin: 0 auto;
}
4 changes: 4 additions & 0 deletions src/css/embeds.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@
-webkit-transform: rotate(-135deg);
transform: rotate(-135deg);
}

.ck-file-input {
display:none;
}
40 changes: 38 additions & 2 deletions src/js/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ function EmbedCommand(options) {
Command.call(this, options);
}
inherits(EmbedCommand, Command);
EmbedCommand.prototype.exec = function(value) {
EmbedCommand.prototype.exec = function() {
alert(this.name);
};

Expand All @@ -197,13 +197,49 @@ function ImageEmbedCommand(options) {
name: 'image',
button: '<i class="ck-icon-image"></i>'
});

var fileBrowser = document.createElement('input');
fileBrowser.type = 'file';
fileBrowser.accept = 'image/*';
fileBrowser.className = 'ck-file-input';
fileBrowser.addEventListener('change', this.handleFile);
document.body.appendChild(fileBrowser);
this.fileBrowser = fileBrowser;
}
inherits(ImageEmbedCommand, EmbedCommand);
ImageEmbedCommand.prototype = {
exec: function() {
var clickEvent = new MouseEvent('click', { bubbles: false });
this.fileBrowser.dispatchEvent(clickEvent);
},
handleFile: function(e) {
var target = e.target;
var file = target && target.files[0];
var reader = new FileReader();
reader.onload = function(event) {
var base64File = event.target.result;
var selectionRoot = getCurrentSelectionRootNode();
var image = document.createElement('img');
image.src = base64File;

// image needs to be placed outside of the current empty paragraph
var editorNode = selectionRoot.parentNode;
editorNode.insertBefore(image, selectionRoot);
editorNode.removeChild(selectionRoot);
};
reader.readAsDataURL(file);
target.value = null; // reset
}
};

function MediaEmbedCommand(options) {
EmbedCommand.call(this, {
name: 'media',
button: '<i class="ck-icon-embed"></i>'
button: '<i class="ck-icon-embed"></i>',
prompt: new Prompt({
command: this,
placeholder: 'Enter a twitter, or youtube url...'
})
});
}
inherits(MediaEmbedCommand, EmbedCommand);
Expand Down
3 changes: 2 additions & 1 deletion src/js/embed-intent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ var EmbedIntent = (function() {
embedIntent.isShowing = false;
embedIntent.isActive = false;

function embedIntentHandler() {
function embedIntentHandler(e) {
var currentNode = getCurrentSelectionRootNode();
var currentNodeHTML = currentNode.innerHTML;
if (currentNodeHTML === '' || currentNodeHTML === '<br>') {
embedIntent.showAt(currentNode);
} else {
embedIntent.hide();
}
e.stopPropagation();
}

rootElement.addEventListener('keyup', embedIntentHandler);
Expand Down
10 changes: 7 additions & 3 deletions src/js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ var Toolbar = (function() {
},
updateForSelection: function(selection) {
var toolbar = this;
toolbar.show();
toolbar.positionToContent(selection.getRangeAt(0));
updateButtonsForSelection(toolbar.buttons, selection);
if (selection.isCollapsed) {
toolbar.hide();
} else {
toolbar.show();
toolbar.positionToContent(selection.getRangeAt(0));
updateButtonsForSelection(toolbar.buttons, selection);
}
},
positionToContent: function(content) {
var directions = ToolbarDirection;
Expand Down

0 comments on commit 84a634a

Please sign in to comment.