mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-24 12:05:22 +08:00
Use the cell ‘attachments’ property instead of ‘metadata.attachments’.
Use the mime-bundle storage format to store the attachments
This commit is contained in:
parent
3462b34fca
commit
398c90b0aa
@ -788,6 +788,23 @@ define([
|
||||
return MathJax.Hub.Queue(["Typeset", MathJax.Hub, this]);
|
||||
});
|
||||
};
|
||||
|
||||
var parse_b64_data_uri = function(uri) {
|
||||
/**
|
||||
* Parses a base64 encoded data-uri to extract mimetype and the
|
||||
* base64 string.
|
||||
*
|
||||
* For example, given 'data:image/png;base64,iVBORw', it will return
|
||||
* ["image/png", "iVBORw"]
|
||||
*
|
||||
* Parameters
|
||||
*/
|
||||
var regex = /^data:(.+\/.+);base64,(.*)$/;
|
||||
var matches = uri.match(regex);
|
||||
var mime = matches[1];
|
||||
var b64_data = matches[2];
|
||||
return [mime, b64_data];
|
||||
};
|
||||
|
||||
var time = {};
|
||||
time.milliseconds = {};
|
||||
@ -877,6 +894,7 @@ define([
|
||||
resolve_promises_dict: resolve_promises_dict,
|
||||
reject: reject,
|
||||
typeset: typeset,
|
||||
parse_b64_data_uri: parse_b64_data_uri,
|
||||
time: time,
|
||||
format_datetime: format_datetime,
|
||||
datetime_sort_helper: datetime_sort_helper,
|
||||
|
@ -70,6 +70,8 @@ define([
|
||||
}
|
||||
});
|
||||
|
||||
this.attachments = {};
|
||||
|
||||
// backward compat.
|
||||
Object.defineProperty(this, 'cm_config', {
|
||||
get: function() {
|
||||
@ -472,6 +474,8 @@ define([
|
||||
var data = {};
|
||||
// deepcopy the metadata so copied cells don't share the same object
|
||||
data.metadata = JSON.parse(JSON.stringify(this.metadata));
|
||||
// same for attachments
|
||||
data.attachments = JSON.parse(JSON.stringify(this.attachments));
|
||||
data.cell_type = this.cell_type;
|
||||
return data;
|
||||
};
|
||||
@ -484,6 +488,9 @@ define([
|
||||
if (data.metadata !== undefined) {
|
||||
this.metadata = data.metadata;
|
||||
}
|
||||
if (data.attachments !== undefined) {
|
||||
this.attachments = data.attachments;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
@ -263,8 +263,8 @@ define([
|
||||
|
||||
// Inline images insertion. When a user drops an image in a markdown
|
||||
// cell, we do the following :
|
||||
// - We insert the base64-encoded image into the cell metadata
|
||||
// attachments directory, keyed by the filename.
|
||||
// - We insert the base64-encoded image into the cell attachments
|
||||
// directory, keyed by the filename.
|
||||
// - We insert an img tag with a 'nbdata' src that refers to the
|
||||
// attachments entry.
|
||||
//
|
||||
@ -284,14 +284,19 @@ define([
|
||||
var reader = new FileReader;
|
||||
reader.onloadend = function() {
|
||||
var img_md = '<img width="200px" height="200px" src="nbdata:' + key + '" />';
|
||||
if (that.metadata.attachments === undefined) {
|
||||
that.metadata.attachments = {};
|
||||
if (that.attachments === undefined) {
|
||||
that.attachments = {};
|
||||
}
|
||||
that.metadata.attachments[key] = {
|
||||
'data': reader.result,
|
||||
'mime': file.type
|
||||
that.attachments[key] = {};
|
||||
// Strip the "data:image/png;base64," prefix from the data-url
|
||||
// to turn it into a base64 encoded string
|
||||
var d = utils.parse_b64_data_uri(reader.result);
|
||||
if (file.type != d[0]) {
|
||||
// TODO(julienr): Not sure what we should do in this case
|
||||
console.log('File type (' + file.type + ') != data-uri ' +
|
||||
'type (' + d[0] + ')');
|
||||
}
|
||||
//var img_md = '<img height="200px" src="' + reader.result + '" />';
|
||||
that.attachments[key][file.type] = [d[1]];
|
||||
that.code_mirror.replaceRange(img_md, pos);
|
||||
}
|
||||
reader.readAsDataURL(file);
|
||||
@ -339,10 +344,12 @@ define([
|
||||
html.find('img[src^="nbdata:"]').each(function (i, h) {
|
||||
h = $(h);
|
||||
var key = h.attr('src').replace(/^nbdata:/, '');
|
||||
if (that.metadata.attachments !== undefined &&
|
||||
key in that.metadata.attachments) {
|
||||
var att = that.metadata.attachments[key];
|
||||
h.attr('src', att['data']);
|
||||
|
||||
if (that.attachments !== undefined &&
|
||||
key in that.attachments) {
|
||||
var att = that.attachments[key];
|
||||
var mime = Object.keys(att)[0];
|
||||
h.attr('src', 'data:' + mime + ';base64,' + att[mime][0]);
|
||||
}
|
||||
});
|
||||
that.set_rendered(html);
|
||||
|
Loading…
Reference in New Issue
Block a user