Skip to content

Commit

Permalink
Moved image and oembed controls from inline to media module.
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-m committed Jun 2, 2014
1 parent 442559b commit e0efd05
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 104 deletions.
101 changes: 0 additions & 101 deletions src/lib/controls/inline.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,104 +243,3 @@ exports.Link = Class(Button, {
return node;
}
});


var ImageDialog = Class(Dialog, {
show: function(node) {
var control = this.control,
editor = this.toolbar.editor,
selection = rangy.saveSelection();

editor.showDialog(function() {
input.focus();
});

var input = this.addTextField('URL: ', {
escape: function() {
editor.restoreSelection(selection);
},
enter: function(evt) {
editor.restoreSelection(selection);
control.saveImage(node, evt.target.value);
}
});

this.addButton('Save', {
fontAwesomeID: 'check',
click: function(evt) {
evt.stopImmediatePropagation();
editor.restoreSelection(selection);
control.saveImage(node, input.value);
}
});

if (node) {
input.value = node.getAttribute('src');
}
}
});

exports.Image = Class(Button, {
defaults: extend({}, Button.prototype.defaults, {
label: 'IMG',
title: 'Image',
fontAwesomeID: 'picture-o'
}),

init: function() {
Button.prototype.init.apply(this, arguments);

// Select image when clicked
var editor = this.toolbar.editor;
editor.on('click', function(evt) {
if (evt.target.tagName.toLowerCase() === 'img') {
editor.setRange(evt.target);
editor.showToolbar();
}
});
},

isHighlighted: function() {
return this.toolbar.editor.filterTopNodeNames('img').length > 0;
},

click: function() {
var editor = this.toolbar.editor,
node = editor.filterTopNodeNames('img');

node = node.length === 0 ? null : node[0];
(new ImageDialog(this)).show(node);
},

saveImage: function(node, url) {
var editor = this.toolbar.editor,
range = editor.getRange();

if (node && url) {
node.setAttribute('src', url);
editor.setRange(node);
} else if (url) {
node = document.createElement('img');
node.setAttribute('src', url);
range.deleteContents();
range.insertNode(node);
editor.cleanBlock(node.parentNode);
editor.setRange(node);
}
editor.showToolbar();
return node;
}
});


exports.Oembed = Class(Button, {
defaults: extend({}, Button.prototype.defaults, {
label: 'Embeded',
title: 'Embeded content',
fontAwesomeID: 'youtube-play'
}),

click: function() {
window.alert('No implemented yet :)');
}
});
112 changes: 112 additions & 0 deletions src/lib/controls/media.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* jshint newcap:false */
/* global exports, require */

var _util = require('../util'),
_controls = require('../controls'),
rangy = require('../../vendor/rangy/core').api,

Class = _util.Class,
extend = _util.extend,
Button = _controls.Button,
Dialog = _controls.Dialog;


var ImageDialog = Class(Dialog, {
show: function(node) {
var control = this.control,
editor = this.toolbar.editor,
selection = rangy.saveSelection();

editor.showDialog(function() {
input.focus();
});

var input = this.addTextField('URL: ', {
escape: function() {
editor.restoreSelection(selection);
},
enter: function(evt) {
editor.restoreSelection(selection);
control.saveImage(node, evt.target.value);
}
});

this.addButton('Save', {
fontAwesomeID: 'check',
click: function(evt) {
evt.stopImmediatePropagation();
editor.restoreSelection(selection);
control.saveImage(node, input.value);
}
});

if (node) {
input.value = node.getAttribute('src');
}
}
});

exports.Image = Class(Button, {
defaults: extend({}, Button.prototype.defaults, {
label: 'IMG',
title: 'Image',
fontAwesomeID: 'picture-o'
}),

init: function() {
Button.prototype.init.apply(this, arguments);

// Select image when clicked
var editor = this.toolbar.editor;
editor.on('click', function(evt) {
if (evt.target.tagName.toLowerCase() === 'img') {
editor.setRange(evt.target);
editor.showToolbar();
}
});
},

isHighlighted: function() {
return this.toolbar.editor.filterTopNodeNames('img').length > 0;
},

click: function() {
var editor = this.toolbar.editor,
node = editor.filterTopNodeNames('img');

node = node.length === 0 ? null : node[0];
(new ImageDialog(this)).show(node);
},

saveImage: function(node, url) {
var editor = this.toolbar.editor,
range = editor.getRange();

if (node && url) {
node.setAttribute('src', url);
editor.setRange(node);
} else if (url) {
node = document.createElement('img');
node.setAttribute('src', url);
range.deleteContents();
range.insertNode(node);
editor.cleanBlock(node.parentNode);
editor.setRange(node);
}
editor.showToolbar();
return node;
}
});


exports.Oembed = Class(Button, {
defaults: extend({}, Button.prototype.defaults, {
label: 'Embeded',
title: 'Embeded content',
fontAwesomeID: 'youtube-play'
}),

click: function() {
window.alert('No implemented yet :)');
}
});
7 changes: 4 additions & 3 deletions src/lib/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ exports.Editor = Editor;
var controls = {
Menu: require('./controls').Menu,
inline: require('./controls/inline'),
block: require('./controls/block')
block: require('./controls/block'),
media: require('./controls/media')
};

exports.simpleEditor = Class(Editor, {
Expand Down Expand Up @@ -394,7 +395,7 @@ exports.simpleEditor = Class(Editor, {
this.toolbar.addControl(controls.inline.Underline, 'underline');
this.toolbar.addControl(controls.inline.StrikeThrough, 'strike');
this.toolbar.addControl(controls.inline.Link, 'link');
this.toolbar.addControl(controls.inline.Image, 'image');
this.toolbar.addControl(controls.inline.Oembed, 'oembed');
this.toolbar.addControl(controls.media.Image, 'image');
this.toolbar.addControl(controls.media.Oembed, 'oembed');
}
});

0 comments on commit e0efd05

Please sign in to comment.