Quellcode durchsuchen

Merge cell attachments when merging markdown cells

Jeremy Tuloup vor 5 Jahren
Ursprung
Commit
5e2b8e5e3a
2 geänderte Dateien mit 43 neuen und 11 gelöschten Zeilen
  1. 14 3
      packages/notebook/src/actions.tsx
  2. 29 8
      packages/notebook/test/actions.spec.ts

+ 14 - 3
packages/notebook/src/actions.tsx

@@ -1,8 +1,6 @@
 // Copyright (c) Jupyter Development Team.
 // Distributed under the terms of the Modified BSD License.
 
-import { KernelMessage } from '@jupyterlab/services';
-
 import {
   ISessionContext,
   Clipboard,
@@ -15,11 +13,14 @@ import {
   ICodeCellModel,
   CodeCell,
   Cell,
-  MarkdownCell
+  MarkdownCell,
+  IMarkdownCellModel
 } from '@jupyterlab/cells';
 
 import * as nbformat from '@jupyterlab/nbformat';
 
+import { KernelMessage } from '@jupyterlab/services';
+
 import { ArrayExt, each, toArray } from '@lumino/algorithm';
 
 import { JSONObject, JSONExt } from '@lumino/coreutils';
@@ -196,6 +197,7 @@ export namespace NotebookActions {
     const cells = model.cells;
     const primary = notebook.activeCell;
     const active = notebook.activeCellIndex;
+    const attachments: nbformat.IAttachments = {};
 
     // Get the cells to merge.
     notebook.widgets.forEach((child, index) => {
@@ -204,6 +206,13 @@ export namespace NotebookActions {
         if (index !== active) {
           toDelete.push(child.model);
         }
+        // Merge attachments if the cell is a markdown cell
+        if (child.model.type === 'markdown') {
+          const model = (child as MarkdownCell).model;
+          for (const key of model.attachments.keys) {
+            attachments[key] = model.attachments.get(key)!.toJSON();
+          }
+        }
       }
     });
 
@@ -229,6 +238,8 @@ export namespace NotebookActions {
     newModel.value.text = toMerge.join('\n\n');
     if (newModel.type === 'code') {
       (newModel as ICodeCellModel).outputs.clear();
+    } else if (newModel.type === 'markdown') {
+      (newModel as IMarkdownCellModel).attachments.fromJSON(attachments);
     }
 
     // Make the changes while preserving history.

+ 29 - 8
packages/notebook/test/actions.spec.ts

@@ -5,15 +5,11 @@ import 'jest';
 
 import { ISessionContext, SessionContext } from '@jupyterlab/apputils';
 
-import { each } from '@lumino/algorithm';
-
 import { CodeCell, MarkdownCell, RawCell } from '@jupyterlab/cells';
 
-import { NotebookModel } from '../src';
-
-import { NotebookActions } from '../src';
+import { IMimeBundle } from '@jupyterlab/nbformat';
 
-import { Notebook } from '../src';
+import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
 
 import {
   acceptDialog,
@@ -21,10 +17,18 @@ import {
   dismissDialog,
   sleep
 } from '@jupyterlab/testutils';
-import { JSONObject, JSONArray } from '@lumino/coreutils';
 
 import { JupyterServer } from '@jupyterlab/testutils/lib/start_jupyter_server';
-import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
+
+import { each } from '@lumino/algorithm';
+
+import { JSONObject, JSONArray, UUID } from '@lumino/coreutils';
+
+import { NotebookModel } from '../src';
+
+import { NotebookActions } from '../src';
+
+import { Notebook } from '../src';
 
 import * as utils from './utils';
 
@@ -265,6 +269,23 @@ describe('@jupyterlab/notebook', () => {
         expect(widget.activeCell).toBeInstanceOf(RawCell);
         expect(widget.mode).toBe('command');
       });
+
+      it('should merge attachments if the last selected cell is a markdown cell', () => {
+        for (let i = 0; i < 2; i++) {
+          NotebookActions.changeCellType(widget, 'markdown');
+          const markdownCell = widget.activeCell as MarkdownCell;
+          const attachment: IMimeBundle = { 'text/plain': 'test' };
+          markdownCell.model.attachments.set(UUID.uuid4(), attachment);
+          widget.select(markdownCell);
+        }
+        NotebookActions.mergeCells(widget);
+        const model = (widget.activeCell as MarkdownCell).model;
+        expect(model.attachments.length).toBe(2);
+      });
+
+      it('should drop attachments if the last selected cell is a code cell', () => {
+        // TODO
+      });
     });
 
     describe('#deleteCells()', () => {