From 57d063cd83e867ce77cf2ae53d286d70b86695c6 Mon Sep 17 00:00:00 2001 From: gamestrRUS Date: Thu, 25 Mar 2021 10:30:25 +0300 Subject: [PATCH] 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 --- notebook/static/notebook/js/cell.js | 44 +++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/notebook/static/notebook/js/cell.js b/notebook/static/notebook/js/cell.js index dbe031359f..4fd4ec442a 100644 --- a/notebook/static/notebook/js/cell.js +++ b/notebook/static/notebook/js/cell.js @@ -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 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 @@ -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; };