Merge pull request #612 from jdfreder/markallthethings

Replace multi-select logic with marks
Matthias Bussonnier 11 years ago
commit 34ac57cf87

@ -173,33 +173,18 @@ define(function(require){
}
}
},
'extend-selection-previous' : {
help: 'extend selection above',
'extend-marked-previous' : {
help: 'extend marked above',
help_index : 'dc',
handler : function (env) {
var index = env.notebook.get_selected_index();
if (index !== 0 && index !== null) {
env.notebook.extend_selection('up');
env.notebook.focus_cell();
}
env.notebook.extend_marked(1);
}
},
'extend-selection-next' : {
help: 'extend selection below',
'extend-marked-next' : {
help: 'extend marked below',
help_index : 'dd',
handler : function (env) {
var index = env.notebook.get_selected_index();
if (index !== (env.notebook.ncells()-1) && index !== null) {
env.notebook.extend_selection('down');
env.notebook.focus_cell();
}
}
},
'reset-selection': {
help: 'clear selected cells',
help_index: 'de',
handler: function(env) {
env.notebook.reset_selection();
env.notebook.extend_marked(-1);
}
},
'cut-selected-cell' : {
@ -402,11 +387,11 @@ define(function(require){
env.notebook.merge_cell_below();
}
},
'merge-selected-cells' : {
help : 'merge selected cells',
'merge-marked-cells' : {
help : 'merge marked cells',
help_index: 'el',
handler: function(env) {
env.notebook.merge_selected_cells();
env.notebook.merge_marked_cells();
}
},
'close-pager' : {
@ -428,7 +413,8 @@ define(function(require){
help: 'toggle marks',
icon: 'fa-check',
handler : function(env){
env.notebook.toggle_cells_marked(env.notebook.get_selected_cells());
// Use bitwise logic to toggle the marked state.
env.notebook.get_selected_cell().marked ^= true;
}
},
};

@ -55,8 +55,6 @@ define([
this.placeholder = config.placeholder || '';
this.selected = false;
this.in_selection = false;
this.selection_anchor = false;
this.rendered = false;
this.mode = 'command';
@ -76,7 +74,7 @@ define([
// backward compat.
Object.defineProperty(this, 'cm_config', {
get: function() {
console.warn("Warning: accessing Cell.cm_config directly is deprecated.")
console.warn("Warning: accessing Cell.cm_config directly is deprecated.");
return that._options.cm_config;
},
});
@ -144,7 +142,7 @@ define([
* Call after this.element exists to initialize the css classes
* related to selected, rendered and mode.
*/
if (this.in_selection) {
if (this.selected) {
this.element.addClass('selected');
} else {
this.element.addClass('unselected');
@ -262,7 +260,6 @@ define([
this.element.addClass('selected');
this.element.removeClass('unselected');
this.selected = true;
this.in_selection = true;
return true;
} else {
return false;
@ -270,21 +267,20 @@ define([
};
/**
* handle cell level logic when the cursor moves away from a cell
* handle cell level logic when the cell is unselected
* @method unselect
* @param {bool} leave_selected - true to move cursor away and extend selection
* @return is the action being taken
*/
Cell.prototype.unselect = function (leave_selected) {
var was_selected_cell = this.selected;
this.selected = false;
if ((!leave_selected) && this.in_selection) {
this.in_selection = false;
this.selection_anchor = false;
if (this.selected) {
this.element.addClass('unselected');
this.element.removeClass('selected');
this.selected = false;
return true;
} else {
return false;
}
return was_selected_cell;
};
/**
@ -297,7 +293,10 @@ define([
},
set: function(value) {
var isMarked = this.element.hasClass('marked');
if (isMarked !== value) {
// Use a casting comparison. Allows for the caller to assign 0 or
// 1 instead of a boolean value, which in return means the caller
// can do cell.marked ^= true to toggle the mark.
if (isMarked != value) {
if (value) {
this.element.addClass('marked');
} else {

@ -535,12 +535,12 @@ define([
};
/**
* handle cell level logic when the cursor moves away from a cell
* handle cell level logic when the cell is unselected
* @method unselect
* @return is the action being taken
*/
CodeCell.prototype.unselect = function (leave_selected) {
var cont = Cell.prototype.unselect.apply(this, [leave_selected]);
CodeCell.prototype.unselect = function() {
var cont = Cell.prototype.unselect.call(this);
if (cont) {
// When a code cell is unselected, make sure that the corresponding
// tooltip and completer to that cell is closed.

@ -88,7 +88,7 @@ define([
'cmdtrl-shift-p': 'ipython.command-palette',
'shift-space': 'ipython.scroll-up',
'shift-v' : 'ipython.paste-cell-before',
'shift-m' : 'ipython.merge-selected-cells',
'shift-m' : 'ipython.merge-marked-cells',
'shift-o' : 'ipython.toggle-output-scrolling-selected-cell',
'enter' : 'ipython.enter-edit-mode',
'space' : 'ipython.scroll-down',
@ -101,10 +101,10 @@ define([
'up' : 'ipython.select-previous-cell',
'k' : 'ipython.select-previous-cell',
'j' : 'ipython.select-next-cell',
'shift-k': 'ipython.extend-selection-previous',
'shift-j': 'ipython.extend-selection-next',
'shift-up': 'ipython.extend-selection-previous',
'shift-down': 'ipython.extend-selection-next',
'shift-k': 'ipython.extend-marked-previous',
'shift-j': 'ipython.extend-marked-next',
'shift-up': 'ipython.extend-marked-previous',
'shift-down': 'ipython.extend-marked-next',
'x' : 'ipython.cut-selected-cell',
'c' : 'ipython.copy-selected-cell',
'v' : 'ipython.paste-cell-after',

@ -613,21 +613,6 @@ define(function (require) {
});
return result;
};
/**
* Get the index of the anchor cell for range selection
*
* @return {integer} The anchor cell's numeric index
*/
Notebook.prototype.get_selection_anchor = function() {
var result = null;
this.get_cell_elements().filter(function (index) {
if ($(this).data("cell").selection_anchor === true) {
result = index;
}
});
return result;
};
/**
* Toggles the marks on the cells
@ -729,29 +714,20 @@ define(function (require) {
};
/**
* Get an array of the cells in the currently selected range
*
* @return {Array} The selected cells
*/
Notebook.prototype.get_selected_cells = function () {
return this.get_cells().filter(function(cell) {
return cell.in_selection;
});
};
/**
* Get the indices of the currently selected range of cells.
* Extend the selected range
*
* @return {Array} The selected cells' numeric indices
* @param {number} offset
*/
Notebook.prototype.get_selected_indices = function () {
var result = [];
this.get_cell_elements().filter(function (index) {
if ($(this).data("cell").in_selection === true) {
result.push(index);
}
});
return result;
Notebook.prototype.extend_marked = function(offset) {
// Mark currently selected cell
this.get_selected_cell().marked = true;
// Select the cell in the offset direction. Bound index between 0 and
// the number of cells -1.
var selectedIndex = Math.min(Math.max(this.get_selected_index() + offset, 0), this.ncells()-1);
this.select(selectedIndex);
this.get_selected_cell().marked = true;
};
// Cell selection.
@ -771,27 +747,23 @@ define(function (require) {
if (this.mode !== 'command') {
this.command_mode();
}
this.get_cell(sindex).unselect();
}
var current_selection = this.get_selected_cells();
for (var i=0; i<current_selection.length; i++) {
current_selection[i].unselect();
var cell = this.get_cell(index);
cell.select();
if (cell.cell_type === 'heading') {
this.events.trigger('selected_cell_type_changed.Notebook',
{'cell_type':cell.cell_type,level:cell.level}
);
} else {
this.events.trigger('selected_cell_type_changed.Notebook',
{'cell_type':cell.cell_type}
);
}
var cell = this._select(index);
cell.selection_anchor = true;
}
return this;
};
Notebook.prototype._select = function(index) {
var cell = this.get_cell(index);
cell.select();
this.events.trigger('selected_cell_type_changed.Notebook',
{'cell_type':cell.cell_type}
);
return cell;
};
/**
* Programmatically select the next cell.
*
@ -814,42 +786,6 @@ define(function (require) {
return this;
};
/**
* Extend the selected range
*
* @param {string} direction - 'up' or 'down
*/
Notebook.prototype.extend_selection = function(direction) {
var anchor_ix = this.get_selection_anchor();
var cursor_ix = this.get_selected_index();
var range_direction = (cursor_ix > anchor_ix) ? 'down' : 'up';
var contracting = (cursor_ix !== anchor_ix) &&
(direction !== range_direction);
var ix_delta = (direction === 'up') ? -1 : 1;
var new_ix = cursor_ix + ix_delta;
if (new_ix < 0 || new_ix >= this.ncells()) {
return false;
}
if (this.mode !== 'command') {
this.command_mode();
}
this.get_cell(cursor_ix).unselect(!contracting);
this._select(new_ix);
return true;
};
/**
* Clear selection of multiple cells (except the cell at the cursor)
*/
Notebook.prototype.reset_selection = function() {
var current_selection = this.get_selected_cells();
for (var i=0; i<current_selection.length; i++) {
if (!current_selection[i].selected) {
current_selection[i].unselect();
}
}
};
// Edit/Command mode
@ -903,7 +839,6 @@ define(function (require) {
if (cell && this.mode !== 'edit') {
cell.edit_mode();
this.mode = 'edit';
this.reset_selection();
this.events.trigger('edit_mode.Notebook');
this.keyboard_manager.edit_mode();
}
@ -1021,7 +956,11 @@ define(function (require) {
*/
Notebook.prototype.delete_cells = function(indices) {
if (indices === undefined) {
indices = this.get_selected_indices();
indices = this.get_marked_indices();
if (indices.length === 0) {
indices = [this.get_selected_index()];
}
}
this.undelete_backup = [];
@ -1454,7 +1393,11 @@ define(function (require) {
* Copy cells.
*/
Notebook.prototype.copy_cell = function () {
var cells = this.get_selected_cells();
var cells = this.get_marked_cells();
if (cells.length === 0) {
cells = [this.get_selected_cell()];
}
this.clipboard = [];
var cell_json;
for (var i=0; i < cells.length; i++) {
@ -1600,8 +1543,8 @@ define(function (require) {
/**
* Merge the selected range of cells
*/
Notebook.prototype.merge_selected_cells = function() {
this.merge_cells(this.get_selected_indices());
Notebook.prototype.merge_marked_cells = function() {
this.merge_cells(this.get_marked_indices());
};
/**

@ -247,7 +247,7 @@ define(function(require){
var get_cells = function(env){
if(onlySelected()){
return env.notebook.get_selected_cells();
return env.notebook.get_marked_cells();
} else {
return env.notebook.get_cells();
}

@ -41,7 +41,7 @@ casper.notebook_test(function () {
this.test.assertEquals(this.get_cell_text(1), 'cd', 'split; Verify that cell 1 has the second half.');
this.validate_notebook_state('split', 'edit', 1);
this.select_cell(0); // Move up to cell 0
this.evaluate(function() { IPython.notebook.extend_selection('down');});
this.evaluate(function() { IPython.notebook.extend_marked(1);});
this.trigger_keydown('shift-m'); // Merge
this.validate_notebook_state('merge', 'command', 0);
this.test.assertEquals(this.get_cell_text(0), a, 'merge; Verify that cell 0 has the merged contents.');

Loading…
Cancel
Save