Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[collapsible_headings] add new (un)collapse-all actions, shortcuts, button #1025

Merged
merged 5 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ def __init__(self, *args, **kwargs):
'collapsible_headings')

with open(os.path.join(ch_dir, 'main.css'), 'r') as f:
main_css = f.read()
self.inliner_resources['css'].append(main_css)
self.inliner_resources['css'].append(f.read())

self.inliner_resources['css'].append("""
/* no local copies of fontawesome fonts from basic templates, so get them from cdn */
Expand All @@ -50,11 +49,7 @@ def __init__(self, *args, **kwargs):
""") # noqa: E501

with open(os.path.join(ch_dir, 'main.js'), 'r') as f:
self.inliner_resources['js'].append(
f.read().replace(
"define([",
"define('nbextensions/collapsible_headings/main', [")
)
self.inliner_resources['js'].append(f.read())

cm = ConfigManager()
collapsible_headings_options = cm.get('notebook').get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def default_config(self):
contrib_templates_dir = templates_directory()

template_path = c.TemplateExporter.setdefault('template_path', [])
if templates_directory not in template_path:
if contrib_templates_dir not in template_path:
template_path.append(contrib_templates_dir)

return c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Parameters:
description: Add a toolbar button to collapse the closest header cell
input_type: checkbox
default: false
- name: collapsible_headings.add_all_cells_button
description: Add a toolbar button to collapse/uncollapse all header cells
input_type: checkbox
default: false
- name: collapsible_headings.add_insert_header_buttons
description: Add toolbar buttons to insert heading cells above/below the selected cell
input_type: checkbox
Expand Down Expand Up @@ -62,6 +66,14 @@ Parameters:
description: Command-mode shortcut to uncollapse (expand) the selected heading cell
input_type: hotkey
default: right
- name: collapsible_headings.shortcuts.collapse_all
description: Command-mode shortcut to collapse all heading cells
input_type: hotkey
default: ctrl-shift-left
- name: collapsible_headings.shortcuts.uncollapse_all
description: Command-mode shortcut to uncollapse (expand) all heading cells
input_type: hotkey
default: ctrl-shift-right
- name: collapsible_headings.shortcuts.select
description: Command-mode shortcut to select all cells in the selected heading cell's section
input_type: hotkey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
define(['jquery', 'require'], function ($, require) {
(require.specified('base/js/namespace') ? define : function (deps, callback) {
// if here, the Jupyter namespace hasn't been specified to be loaded.
// This means that we're probably embedded in a page, so we need to make
// our definition with a specific module name
return define('nbextensions/collapsible_headings/main', deps, callback);
})(['jquery', 'require'], function ($, require) {
"use strict";

var mod_name = 'collapsible_headings';
Expand All @@ -15,6 +20,7 @@ define(['jquery', 'require'], function ($, require) {
// define default values for config parameters
var params = {
add_button : false,
add_all_cells_button: false,
add_insert_header_buttons: false,
use_toggle_controls : true,
make_toggle_controls_buttons : false,
Expand All @@ -25,7 +31,9 @@ define(['jquery', 'require'], function ($, require) {
use_shortcuts : true,
shortcuts: {
collapse: 'left',
collapse_all: 'ctrl-shift-left',
uncollapse: 'right',
uncollapse_all: 'ctrl-shift-right',
select: 'shift-right',
insert_above: 'shift-a',
insert_below: 'shift-b',
Expand Down Expand Up @@ -171,6 +179,11 @@ define(['jquery', 'require'], function ($, require) {
// Restrict the search to cells that are of the same level and lower
// than the currently selected cell by index.
var ref_cell = _get_cell_at_index(index);
// ref_cell may be null, if we've attempted to extend selection beyond
// the existing cells
if (!ref_cell) {
return;
}
var pivot_level = get_cell_level(ref_cell);
var cells = _get_cells();
while (index > 0 && pivot_level > 1) {
Expand Down Expand Up @@ -629,7 +642,7 @@ define(['jquery', 'require'], function ($, require) {
var filter_func;
if (is_h) {
var lvl = get_cell_level(cell);
filter_func = function (c) { return get_cell_level(c) < lvl; }
filter_func = function (c) { return get_cell_level(c) < lvl; };
}
cell = find_header_cell(cell, filter_func);
if (cell !== undefined) {
Expand All @@ -644,6 +657,27 @@ define(['jquery', 'require'], function ($, require) {
'collapse_heading', mod_name
);

action_names.collapse_all = Jupyter.keyboard_manager.actions.register({
handler : function (env) {
env.notebook.get_cells().forEach(function (c, idx, arr) {
toggle_heading(c, true);
});
var cell = env.notebook.get_selected_cell();
if (cell.element.is(':hidden')) {
cell = find_header_cell(cell, function (c) { return c.element.is(':visible'); });
if (cell !== undefined) {
Jupyter.notebook.select(Jupyter.notebook.find_cell_index(cell));
cell.focus_cell();
}
}
},
help : "Collapse all heading cells' sections",
icon : params.toggle_closed_icon,
help_index: 'c2'
},
'collapse_all_headings', mod_name
);

action_names.uncollapse = Jupyter.keyboard_manager.actions.register({
handler : function (env) {
var cell = env.notebook.get_selected_cell();
Expand All @@ -664,11 +698,25 @@ define(['jquery', 'require'], function ($, require) {
},
help : "Un-collapse (expand) the selected heading cell's section",
icon : params.toggle_open_icon,
help_index: 'c2'
help_index: 'c3'
},
'uncollapse_heading', mod_name
);

action_names.uncollapse_all = Jupyter.keyboard_manager.actions.register({
handler : function (env) {
env.notebook.get_cells().forEach(function (c, idx, arr) {
toggle_heading(c, false);
});
env.notebook.get_selected_cell().focus_cell();
},
help : "Un-collapse (expand) all heading cells' sections",
icon : params.toggle_open_icon,
help_index: 'c4'
},
'uncollapse_all_headings', mod_name
);

action_names.select = Jupyter.keyboard_manager.actions.register({
handler : function (env) {
var cell = env.notebook.get_selected_cell();
Expand Down Expand Up @@ -807,6 +855,25 @@ define(['jquery', 'require'], function ($, require) {
}
}]);
}
if (params.add_all_cells_button) {
Jupyter.toolbar.add_buttons_group([{
label: 'toggle all headings',
icon: 'fa-angle-double-up',
callback: function () {
/**
* Collapse/uncollapse all heading cells based on status of first
*/
var cells = Jupyter.notebook.get_cells();
for (var ii = 0; ii < cells.length; ii++) {
if (is_heading(cells[ii])) {
Jupyter.keyboard_manager.actions.call(action_names[
is_collapsed_heading(cells[ii]) ? 'uncollapse_all' : 'collapse_all']);
return;
}
}
}
}]);
}
if (params.add_insert_header_buttons) {
Jupyter.toolbar.add_buttons_group([
action_names.insert_above, action_names.insert_below
Expand Down Expand Up @@ -968,7 +1035,7 @@ define(['jquery', 'require'], function ($, require) {
add_buttons_and_shortcuts();
})
.catch(function on_reject (reason) {
console.error(log_prefix, 'error:', reason)
console.error(log_prefix, 'error:', reason);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ the nbextensions config page:

* Command-mode keyboard shortcuts, (enabled by default, and set to left and
right arrow keys to collapse/expand sections, or go to the previous/next
heading, plus shift-right to select a heading cell's section).
heading, plus shift-right to select a heading cell's section, shift-a/b to
insert a heading above/below the current cell, ctrl-shift-left and
ctrl-shift-right to collapse/uncollapse all headings).
Bindings are also configurable from the config page
* A toggle control in the input prompt area of each heading cell (as seen in
the screenshot below, enabled by default)
Expand All @@ -38,6 +40,7 @@ the nbextensions config page:
indicating hidden content (disabled by default)
* A toolbar button to collapse the nearest heading to the curently selected
cell (disabled by default)
* A toolbar button to collapse/uncollapse all headings (disabled by default)


css
Expand Down