Skip to content

Commit 7636fcf

Browse files
authored
Merge pull request #1184 from jcb91/ch
[collapsible_headings] patch TextCell.set_text
2 parents 1a6b9b6 + c8739d3 commit 7636fcf

File tree

1 file changed

+30
-9
lines changed
  • src/jupyter_contrib_nbextensions/nbextensions/collapsible_headings

1 file changed

+30
-9
lines changed

src/jupyter_contrib_nbextensions/nbextensions/collapsible_headings/main.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// if here, the Jupyter namespace hasn't been specified to be loaded.
33
// This means that we're probably embedded in a page, so we need to make
44
// our definition with a specific module name
5+
"use strict";
56
return define('nbextensions/collapsible_headings/main', deps, callback);
67
})(['jquery', 'require'], function ($, requirejs) {
78
"use strict";
@@ -540,20 +541,40 @@
540541
}
541542
return orig_notebook_select.apply(this, arguments);
542543
};
544+
resolve();
545+
}, reject);
546+
}).catch(function on_reject (reason) {
547+
console.warn(log_prefix, 'error patching Notebook.protoype:', reason);
548+
});
549+
}
543550

544-
// we have to patch undelete, as there is no event to bind to. We
545-
// could bind to create.Cell, but that'd be a bit OTT
546-
var orig_notebook_undelete = notebook.Notebook.prototype.undelete;
547-
notebook.Notebook.prototype.undelete = function () {
548-
var ret = orig_notebook_undelete.apply(this, arguments);
549-
update_collapsed_headings();
551+
/**
552+
* Return a promise which resolves when the TextCell class methods have
553+
* been appropriately patched.
554+
*
555+
* Patches TextCell.set_text to update headings.
556+
* This is useful for undelete and copy/paste of cells, which don't fire
557+
* markdown.
558+
*
559+
* @return {Promise}
560+
*/
561+
function patch_TextCell () {
562+
return new Promise(function (resolve, reject) {
563+
requirejs(['notebook/js/textcell'], function on_success (textcell) {
564+
console.debug(log_prefix, 'patching TextCell.protoype');
565+
var orig_set_text = textcell.TextCell.prototype.set_text;
566+
textcell.TextCell.prototype.set_text = function (text) {
567+
var ret = orig_set_text.apply(this, arguments);
568+
if (Jupyter.notebook._fully_loaded) {
569+
update_heading_cell_status(this);
570+
update_collapsed_headings();
571+
}
550572
return ret;
551573
};
552-
553574
resolve();
554575
}, reject);
555576
}).catch(function on_reject (reason) {
556-
console.warn(log_prefix, 'error patching Notebook.protoype:', reason);
577+
console.warn(log_prefix, 'error patching TextCell.protoype:', reason);
557578
});
558579
}
559580

@@ -1028,9 +1049,9 @@
10281049
// apply all promisory things in arbitrary order
10291050
.then(patch_actions)
10301051
.then(patch_Notebook)
1052+
.then(patch_TextCell)
10311053
.then(patch_Tooltip)
10321054
.then(bind_events)
1033-
10341055
// finally add user-interaction stuff
10351056
.then(function () {
10361057
register_new_actions();

0 commit comments

Comments
 (0)