|
@@ -165,15 +165,22 @@ export namespace NotebookActions {
|
|
|
*
|
|
|
* @param notebook - The target notebook widget.
|
|
|
*
|
|
|
+ * @param mergeAbove - If only one cell is selected, indicates whether to merge it
|
|
|
+ * with the cell above (true) or below (false, default).
|
|
|
+ *
|
|
|
* #### Notes
|
|
|
* The widget mode will be preserved.
|
|
|
- * If only one cell is selected, the next cell will be selected.
|
|
|
+ * If only one cell is selected and `mergeAbove` is true, the above cell will be selected.
|
|
|
+ * If only one cell is selected and `mergeAbove` is false, the below cell will be selected.
|
|
|
* If the active cell is a code cell, its outputs will be cleared.
|
|
|
* This action can be undone.
|
|
|
* The final cell will have the same type as the active cell.
|
|
|
* If the active cell is a markdown cell, it will be unrendered.
|
|
|
*/
|
|
|
- export function mergeCells(notebook: Notebook): void {
|
|
|
+ export function mergeCells(
|
|
|
+ notebook: Notebook,
|
|
|
+ mergeAbove: boolean = false
|
|
|
+ ): void {
|
|
|
if (!notebook.model || !notebook.activeCell) {
|
|
|
return;
|
|
|
}
|
|
@@ -206,16 +213,28 @@ export namespace NotebookActions {
|
|
|
|
|
|
// Check for only a single cell selected.
|
|
|
if (toMerge.length === 1) {
|
|
|
- // Bail if it is the last cell.
|
|
|
- if (active === cells.length - 1) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // Otherwise merge with the next cell.
|
|
|
- const cellModel = cells.get(active + 1);
|
|
|
+ // Merge with the cell above when mergeAbove is true
|
|
|
+ if (mergeAbove === true) {
|
|
|
+ // Bail if it is the first cell.
|
|
|
+ if (active === 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // Otherwise merge with the previous cell.
|
|
|
+ const cellModel = cells.get(active - 1);
|
|
|
+
|
|
|
+ toMerge.unshift(cellModel.value.text);
|
|
|
+ toDelete.push(cellModel);
|
|
|
+ } else if (mergeAbove === false) {
|
|
|
+ // Bail if it is the last cell.
|
|
|
+ if (active === cells.length - 1) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // Otherwise merge with the next cell.
|
|
|
+ const cellModel = cells.get(active + 1);
|
|
|
|
|
|
- toMerge.push(cellModel.value.text);
|
|
|
- toDelete.push(cellModel);
|
|
|
+ toMerge.push(cellModel.value.text);
|
|
|
+ toDelete.push(cellModel);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
notebook.deselectAll();
|