Skip to content

Commit

Permalink
Fix: split_cell doesn't always split cell (#6017)
Browse files Browse the repository at this point in the history
* Split cells if a single cursor is positioned at the beginning or end of a cell

* Duplicate cell if full cell is selected

* fix null replace
  • Loading branch information
gamestrRUS authored Mar 25, 2021
1 parent c719e27 commit 57d063c
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions notebook/static/notebook/js/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ define([
'services/config',
], function($, utils, i18n, CodeMirror, cm_match, cm_closeb, cm_comment, configmod) {
"use strict";

function is_single_cursor(dict1, dict2) {
return ((dict1.line == dict2.line) && (dict1.ch == dict2.ch));
};

var overlayHack = CodeMirror.scrollbarModel.native.prototype.overlayHack;

Expand Down Expand Up @@ -598,24 +602,44 @@ define([
* @method get_split_text
**/
Cell.prototype.get_split_text = function () {
var start = {line:0, ch:0};
var last_line_num = this.code_mirror.lineCount()-1;
var last_line_len = this.code_mirror.getLine(last_line_num).length;
var end = {line:last_line_num, ch:last_line_len};

var flag_empty_cell = is_single_cursor(start, end);
var flag_first_position = false;
var flag_last_position = false;
var flag_all_select = false;

var ranges = this.code_mirror.listSelections();

var cursors = [{line: 0, ch: 0}];
var cursors = [start];

for (var i = 0; i < ranges.length; i++) {
// append both to handle selections
if (ranges[i].head.sticky == 'before') {
// ranges[i].head.sticky is null if ctrl-a select
if ((ranges[i].head.sticky == 'before') || (ranges[i].head.sticky === null )) {
cursors.push(ranges[i].anchor);
cursors.push(ranges[i].head);
if (is_single_cursor(ranges[i].anchor, start) &&
is_single_cursor(ranges[i].head, end)) {
flag_all_select = true;
}
} else {
cursors.push(ranges[i].head);
cursors.push(ranges[i].anchor);
if (is_single_cursor(ranges[i].head, start) &&
is_single_cursor(ranges[i].anchor, end)) {
flag_all_select = true;
}
}
// single cursor at beginning or end of cell
if (is_single_cursor(ranges[i].head, ranges[i].anchor)) {
if (is_single_cursor(ranges[i].head, start)) flag_first_position = true;
if (is_single_cursor(ranges[i].head, end)) flag_last_position = true;
}
}

var last_line_num = this.code_mirror.lineCount()-1;
var last_line_len = this.code_mirror.getLine(last_line_num).length;
var end = {line:last_line_num, ch:last_line_len};
cursors.push(end);

// Cursors is now sorted, but likely has duplicates due to anchor and head being the same for cursors
Expand All @@ -630,11 +654,19 @@ define([

// Split text
var text_list = [];
// Split single cursors at first position
if (flag_empty_cell || flag_first_position) text_list.push('');
for (var i = 1; i < locations.length; i++) {
var text = this.code_mirror.getRange(locations[i-1], locations[i]);
text = text.replace(/^\n+/, '').replace(/\n+$/, ''); // removes newlines at beginning and end
text_list.push(text);
}
// Split single cursors at last position
if (flag_last_position) text_list.push('');
// Duplicate cell if full cell is selected
if ((text_list.length == 1) && flag_all_select && !flag_empty_cell) {
text_list = text_list.concat(text_list);
}
return text_list;
};

Expand Down

0 comments on commit 57d063c

Please sign in to comment.