mirror of
https://github.com/jupyter/notebook.git
synced 2025-01-24 12:05:22 +08:00
Fix: split_cell doesn't always split cell (#6017)
* 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
This commit is contained in:
parent
c719e27fe7
commit
57d063cd83
@ -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;
|
||||
|
||||
@ -598,24 +602,44 @@ define([
|
||||
* @method get_split_text
|
||||
**/
|
||||
Cell.prototype.get_split_text = function () {
|
||||
var ranges = this.code_mirror.listSelections();
|
||||
|
||||
var cursors = [{line: 0, ch: 0}];
|
||||
|
||||
for (var i = 0; i < ranges.length; i++) {
|
||||
// append both to handle selections
|
||||
if (ranges[i].head.sticky == 'before') {
|
||||
cursors.push(ranges[i].anchor);
|
||||
cursors.push(ranges[i].head);
|
||||
} else {
|
||||
cursors.push(ranges[i].head);
|
||||
cursors.push(ranges[i].anchor);
|
||||
}
|
||||
}
|
||||
|
||||
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 = [start];
|
||||
|
||||
for (var i = 0; i < ranges.length; i++) {
|
||||
// append both to handle selections
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
cursors.push(end);
|
||||
|
||||
// Cursors is now sorted, but likely has duplicates due to anchor and head being the same for cursors
|
||||
@ -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;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user