Add a new insert-image action and corresponding dialog to insert inline images from menu.

Julien Rebetez 10 years ago
parent 480c90e473
commit 22fccff443

@ -80,6 +80,9 @@ define(function(require) {
.addClass("btn btn-default btn-sm")
.attr("data-dismiss", "modal")
.text(label);
if (btn_opts.id) {
button.attr('id', btn_opts.id);
}
if (btn_opts.click) {
button.click($.proxy(btn_opts.click, dialog_content));
}
@ -269,12 +272,58 @@ define(function(require) {
modal_obj.on('shown.bs.modal', function(){ editor.refresh(); });
};
var insert_image = function (options) {
var message =
"Select a file to insert.";
var file_input = $('<input/>')
.attr('type', 'file')
.attr('accept', 'image/*')
.attr('name', 'file')
.on('change', function(file) {
var $btn = $(modal_obj).find('#btn_ok');
if (this.files.length > 0) {
$btn.removeClass('disabled');
} else {
$btn.addClass('disabled');
}
});
var dialogform = $('<div/>').attr('title', 'Edit attachments')
.append(
$('<form/>').append(
$('<fieldset/>').append(
$('<label/>')
.attr('for','file')
.text(message)
)
.append($('<br/>'))
.append(file_input)
)
);
var modal_obj = modal({
title: "Pick a file",
body: dialogform,
buttons: {
OK: {
id : 'btn_ok',
class : "btn-primary disabled",
click: function() {
options.callback(file_input[0].files[0]);
}
},
Cancel: {}
},
notebook: options.notebook,
keyboard_manager: options.keyboard_manager,
});
};
var dialog = {
modal : modal,
kernel_modal : kernel_modal,
edit_metadata : edit_metadata,
edit_attachments : edit_attachments
edit_attachments : edit_attachments,
insert_image : insert_image
};
return dialog;

@ -153,6 +153,13 @@ define(function(require){
env.notebook.command_mode();
}
},
'insert-image': {
help : 'insert image',
help_index : 'dz',
handler : function (env) {
env.notebook.insert_image();
}
},
'split-cell-at-cursor': {
help : 'split cell',
help_index : 'ea',

@ -228,6 +228,7 @@ define([
'#toggle_all_output': 'toggle-all-cells-output-collapsed',
'#toggle_all_output_scroll': 'toggle-all-cells-output-scrolled',
'#clear_all_output': 'clear-all-cells-output',
'#insert_image': 'insert-image',
};
for(var idx in id_actions_dict){

@ -1673,6 +1673,46 @@ define(function (require) {
this.merge_cells([index, index+1], false);
};
// Image insertion
/**
* Shows a dialog letting the user pick an image from her computer and
* insert it into the edited markdown cell
*/
Notebook.prototype.insert_image = function () {
var that = this;
var cell = this.get_selected_cell();
// The following should not happen as the menu item is greyed out
// when those conditions are not fullfilled (see MarkdownCell
// unselect/select/unrender handlers)
if (cell.cell_type != 'markdown') {
console.log('Error: insert_image called on non-markdown cell');
return;
}
if (cell.rendered) {
console.log('Error: insert_image called on rendered cell');
return;
}
dialog.insert_image({
callback: function(file) {
cell.edit_mode();
cell.insert_inline_image_from_blob(file);
},
notebook: this,
keyboard_manager: this.keyboard_manager
});
};
/**
* Enable/disable the "Insert image" menu item
*/
Notebook.prototype.set_insert_image_enabled = function(enabled) {
if (enabled) {
$('#insert_image').removeClass('disabled');
} else {
$('#insert_image').addClass('disabled');
}
};
// Cell collapsing and output clearing

@ -51,6 +51,7 @@ define([
this.notebook = options.notebook;
this.events = options.events;
this.config = options.config;
this.notebook = options.notebook;
// we cannot put this as a class key as it has handle to "this".
var config = utils.mergeopt(TextCell, this.config);
@ -256,6 +257,24 @@ define([
}
};
MarkdownCell.prototype.unselect = function () {
var cont = TextCell.prototype.unselect.apply(this);
this.notebook.set_insert_image_enabled(false);
};
MarkdownCell.prototype.select = function () {
var cont = TextCell.prototype.select.apply(this);
if (cont) {
this.notebook.set_insert_image_enabled(!this.rendered);
}
};
MarkdownCell.prototype.unrender = function () {
var cont = TextCell.prototype.unrender.apply(this);
this.notebook.set_insert_image_enabled(true);
};
// TODO(julienr): Move to cell if the attachments is accepted as a cell
// attribute (not markdown specific)
MarkdownCell.prototype.add_attachment = function (key, mime_type, b64_data) {

@ -143,6 +143,8 @@ data-notebook-path="{{notebook_path | urlencode}}"
<li id="edit_nb_metadata"><a href="#">Edit Notebook Metadata</a></li>
<li class="divider"></li>
<li id="find_and_replace"><a href="#"> Find and Replace </a></li>
<li class="divider"></li>
<li id="insert_image" class="disabled"><a href="#"> Insert Image </a></li>
</ul>
</li>
<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">View</a>

Loading…
Cancel
Save