Skip to content

Commit 6ba5626

Browse files
authored
Merge pull request #1028 from juhasch/fix/codefolding
Add configurable delay for initialization
2 parents 33546ef + 8ea9f98 commit 6ba5626

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/jupyter_contrib_nbextensions/nbextensions/codefolding/codefolding.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ Parameters:
1010
description: Hotkey to fold/unfold code
1111
input_type: hotkey
1212
default: Alt-F
13+
- name: init_delay
14+
description: Add a delay before initializing the extension. Useful when the gutter is not being initialized correctly.
15+
input_type: number
16+
min: 0
17+
default: 0

src/jupyter_contrib_nbextensions/nbextensions/codefolding/codefolding_editor.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ Link: readme.md
99
Icon: codefolding_editor.png
1010
Main: edit.js
1111
Compatibility: 4.x, 5.x
12+
Parameters:
13+
- name: init_delay
14+
description: Add a delay before initializing the extension. Useful when the gutter is not being initialized correctly.
15+
input_type: number
16+
min: 0
17+
default: 1000
1218
Section: edit

src/jupyter_contrib_nbextensions/nbextensions/codefolding/main.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ define([
2727
// define default config parameter values
2828
var params = {
2929
codefolding_hotkey : 'Alt-f',
30+
init_delay : 1000
3031
};
3132

3233
// updates default params with any specified in the provided config data
@@ -109,12 +110,12 @@ define([
109110
}
110111
/* User can click on gutter of unselected cells, so make sure we store metadata in the correct cell */
111112
var cell = Jupyter.notebook.get_selected_cell();
112-
if (cell.code_mirror != cm) {
113+
if (cell.code_mirror !== cm) {
113114
var cells = Jupyter.notebook.get_cells();
114115
var ncells = Jupyter.notebook.ncells();
115116
for (var k = 0; k < ncells; k++) {
116117
var _cell = cells[k];
117-
if (_cell.code_mirror == cm ) { cell = _cell; break; }
118+
if (_cell.code_mirror === cm ) { cell = _cell; break; }
118119
}
119120
}
120121
cell.metadata.code_folding = lines;
@@ -123,12 +124,12 @@ define([
123124
/**
124125
* Activate codefolding in CodeMirror options, don't overwrite other settings
125126
*
126-
* @param cell {codecell.CodeCell} code cell to activate folding gutter
127+
* @param cm codemirror instance
127128
*/
128129
function activate_cm_folding (cm) {
129130
var gutters = cm.getOption('gutters').slice();
130131
if ( $.inArray("CodeMirror-foldgutter", gutters) < 0) {
131-
gutters.push('CodeMirror-foldgutter')
132+
gutters.push('CodeMirror-foldgutter');
132133
cm.setOption('gutters', gutters);
133134
}
134135

@@ -242,7 +243,10 @@ define([
242243
/* require our additional custom codefolding modes before initialising fully */
243244
require(['./firstline-fold', './magic-fold'], function () {
244245
if (Jupyter.notebook._fully_loaded) {
245-
initExistingCells();
246+
setTimeout(function () {
247+
console.log('Codefolding: Wait for', params.init_delay, 'ms');
248+
initExistingCells();
249+
}, params.init_delay);
246250
}
247251
else {
248252
events.one('notebook_loaded.Notebook', initExistingCells);
@@ -252,8 +256,9 @@ define([
252256
else {
253257
activate_cm_folding(Jupyter.editor.codemirror);
254258
setTimeout(function () {
259+
console.log('Codefolding: Wait for', params.init_delay, 'ms');
255260
Jupyter.editor.codemirror.refresh();
256-
}, 1000);
261+
}, params.init_delay);
257262
}
258263
};
259264

0 commit comments

Comments
 (0)