Browse Source

Add markdown header level changing

Steven Silvester 8 years ago
parent
commit
e85e84ecb7

+ 17 - 19
src/notebook/notebook/actions.ts

@@ -280,21 +280,20 @@ namespace NotebookActions {
       if (!widget.isSelected(child)) {
         continue;
       }
-      if (child.model.type === value) {
-        continue;
-      }
-      let newCell: ICellModel;
-      switch (value) {
-      case 'code':
-        newCell = model.factory.createCodeCell(child.model.toJSON());
-        break;
-      case 'markdown':
-        newCell = model.factory.createMarkdownCell(child.model.toJSON());
-        break;
-      default:
-        newCell = model.factory.createRawCell(child.model.toJSON());
+      if (child.model.type !== value) {
+        let newCell: ICellModel;
+        switch (value) {
+        case 'code':
+          newCell = model.factory.createCodeCell(child.model.toJSON());
+          break;
+        case 'markdown':
+          newCell = model.factory.createMarkdownCell(child.model.toJSON());
+          break;
+        default:
+          newCell = model.factory.createRawCell(child.model.toJSON());
+        }
+        cells.set(i, newCell);
       }
-      cells.set(i, newCell);
       if (value === 'markdown') {
         // Fetch the new widget and unrender it.
         child = widget.childAt(i);
@@ -803,16 +802,14 @@ namespace NotebookActions {
       return;
     }
     level = Math.min(Math.max(level, 1), 6);
-    changeCellType(widget, 'markdown');
     let cells = widget.model.cells;
     for (let i = 0; i < cells.length; i++) {
-      let cell = cells.get(i) as CodeCellModel;
-      let child = widget.childAt(i);
+      let child = widget.childAt(i) as MarkdownCellWidget;
       if (widget.isSelected(child)) {
-        Private.setMarkdownHeader(cell, level);
-        widget.rendered = false;
+        Private.setMarkdownHeader(cells.get(i), level);
       }
     }
+    changeCellType(widget, 'markdown');
   }
 }
 
@@ -876,6 +873,7 @@ namespace Private {
   export
   function setMarkdownHeader(cell: ICellModel, level: number) {
     let source = cell.source;
+    debugger;
     let newHeader = Array(level + 1).join('#') + ' ';
     // Remove existing header or leading white space.
     let regex = /^(#+\s*)|^(\s*)/;

+ 85 - 2
src/notebook/plugin.ts

@@ -66,11 +66,16 @@ const cmdIds = {
   merge: 'notebook-cells:merge',
   split: 'notebook-cells:split',
   commandMode: 'notebook:commandMode',
-  newNotebook: 'notebook:create-new',
   toggleLines: 'notebook-cells:toggle-lineNumbers',
   toggleAllLines: 'notebook-cells:toggle-allLineNumbers',
   undo: 'notebook-cells:undo',
-  redo: 'notebook-cells:redo'
+  redo: 'notebook-cells:redo',
+  markdown1: 'notebook-cells:markdown-header1',
+  markdown2: 'notebook-cells:markdown-header2',
+  markdown3: 'notebook-cells:markdown-header3',
+  markdown4: 'notebook-cells:markdown-header4',
+  markdown5: 'notebook-cells:markdown-header5',
+  markdown6: 'notebook-cells:markdown-header6',
 };
 
 
@@ -451,6 +456,54 @@ function activateNotebookHandler(app: Application, registry: DocumentRegistry, s
         selectKernelForContext(activeNotebook.context, activeNotebook.node);
       }
     }
+  },
+  {
+    id: cmdIds['markdown1'],
+    handler: () => {
+      if (activeNotebook) {
+        NotebookActions.setMarkdownHeader(activeNotebook.content, 1);
+      }
+    }
+  },
+  {
+    id: cmdIds['markdown2'],
+    handler: () => {
+      if (activeNotebook) {
+        NotebookActions.setMarkdownHeader(activeNotebook.content, 2);
+      }
+    }
+  },
+  {
+    id: cmdIds['markdown3'],
+    handler: () => {
+      if (activeNotebook) {
+        NotebookActions.setMarkdownHeader(activeNotebook.content, 3);
+      }
+    }
+  },
+  {
+    id: cmdIds['markdown4'],
+    handler: () => {
+      if (activeNotebook) {
+        NotebookActions.setMarkdownHeader(activeNotebook.content, 4);
+      }
+    }
+  },
+  {
+    id: cmdIds['markdown5'],
+    handler: () => {
+      if (activeNotebook) {
+        NotebookActions.setMarkdownHeader(activeNotebook.content, 5);
+      }
+    }
+  },
+  {
+    id: cmdIds['markdown6'],
+    handler: () => {
+      if (activeNotebook) {
+        NotebookActions.setMarkdownHeader(activeNotebook.content, 6);
+      }
+    }
   }
   ]);
   app.palette.add([
@@ -613,6 +666,36 @@ function activateNotebookHandler(app: Application, registry: DocumentRegistry, s
     command: cmdIds['redo'],
     category: 'Notebook Cell Operations',
     text: 'Redo Cell Operation'
+  },
+  {
+    command: cmdIds['markdown1'],
+    category: 'Notebook Cell Operations',
+    text: 'Markdown Header 1'
+  },
+  {
+    command: cmdIds['markdown1'],
+    category: 'Notebook Cell Operations',
+    text: 'Markdown Header 2'
+  },
+  {
+    command: cmdIds['markdown1'],
+    category: 'Notebook Cell Operations',
+    text: 'Markdown Header 3'
+  },
+  {
+    command: cmdIds['markdown1'],
+    category: 'Notebook Cell Operations',
+    text: 'Markdown Header 4'
+  },
+  {
+    command: cmdIds['markdown1'],
+    category: 'Notebook Cell Operations',
+    text: 'Markdown Header 5'
+  },
+  {
+    command: cmdIds['markdown1'],
+    category: 'Notebook Cell Operations',
+    text: 'Markdown Header 6'
   }
   ]);
 }

+ 30 - 0
src/shortcuts/plugin.ts

@@ -210,6 +210,36 @@ const SHORTCUTS = [
     selector: '.jp-Notebook.jp-mod-commandMode',
     sequence: ['L']
   },
+  {
+    command: 'notebook-cells:markdown-header1',
+    selector: '.jp-Notebook.jp-mod-commandMode',
+    sequence: ['1']
+  },
+  {
+    command: 'notebook-cells:markdown-header2',
+    selector: '.jp-Notebook.jp-mod-commandMode',
+    sequence: ['2']
+  },
+  {
+    command: 'notebook-cells:markdown-header3',
+    selector: '.jp-Notebook.jp-mod-commandMode',
+    sequence: ['3']
+  },
+  {
+    command: 'notebook-cells:markdown-header4',
+    selector: '.jp-Notebook.jp-mod-commandMode',
+    sequence: ['4']
+  },
+  {
+    command: 'notebook-cells:markdown-header5',
+    selector: '.jp-Notebook.jp-mod-commandMode',
+    sequence: ['5']
+  },
+  {
+    command: 'notebook-cells:markdown-header6',
+    selector: '.jp-Notebook.jp-mod-commandMode',
+    sequence: ['6']
+  },
   {
     command: 'notebook:editMode',
     selector: '.jp-Notebook.jp-mod-commandMode',

+ 37 - 21
test/src/notebook/notebook/actions.spec.ts

@@ -1225,37 +1225,53 @@ describe('notebook/notebook/actions', () => {
         NotebookActions.clearAllOutputs(widget);
         expect(widget.activeCellIndex).to.be(-1);
       });
-    });
-
-  });
 
-  describe('#setMarkdownHeader()', () => {
-
-    it('should set the markdown header level of selected cells', () => {
-      let next = widget.childAt(1);
-      widget.select(next);
-      NotebookActions.setMarkdownHeader(widget, 2);
-      expect(widget.activeCell.model.source.slice(0, 3)).to.be('## ');
-      expect(next.activeCell.model.source.slice(0, 3)).to.be('## ');
     });
 
-    it('should convert the cells to markdown type', () => {
-
-    });
+    describe('#setMarkdownHeader()', () => {
 
-    it('should be clamped between 1 and 6', () => {
+      it('should set the markdown header level of selected cells', () => {
+        let next = widget.childAt(1);
+        widget.select(next);
+        NotebookActions.setMarkdownHeader(widget, 2);
+        expect(widget.activeCell.model.source.slice(0, 3)).to.be('## ');
+        expect(next.model.source.slice(0, 3)).to.be('## ');
+      });
 
-    });
+      it('should convert the cells to markdown type', () => {
+        NotebookActions.setMarkdownHeader(widget, 2);
+        expect(widget.activeCell).to.be.a(MarkdownCellWidget);
+      });
 
-    it('should replace an existing header', () => {
+      it('should be clamped between 1 and 6', () => {
+        NotebookActions.setMarkdownHeader(widget, -1);
+        expect(widget.activeCell.model.source.slice(0, 2)).to.be('# ');
+        NotebookActions.setMarkdownHeader(widget, 10);
+        expect(widget.activeCell.model.source.slice(0, 7)).to.be('###### ');
+      });
 
-    });
+      it('should be a no-op if there is no model', () => {
+        widget.model = null;
+        NotebookActions.setMarkdownHeader(widget, 1);
+        expect(widget.activeCellIndex).to.be(-1);
+      });
 
-    it('should replace leading white space', () => {
+      it('should replace an existing header', () => {
+        widget.activeCell.model.source = '# foo';
+        NotebookActions.setMarkdownHeader(widget, 2);
+        expect(widget.activeCell.model.source).to.be('## foo');
+      });
 
-    });
+      it('should replace leading white space', () => {
+        widget.activeCell.model.source = '      foo';
+        NotebookActions.setMarkdownHeader(widget, 2);
+        expect(widget.activeCell.model.source).to.be('## foo');
+      });
 
-    it('should unrender the cells', () => {
+      it('should unrender the cells', () => {
+        NotebookActions.setMarkdownHeader(widget, 1);
+        expect((widget.activeCell as MarkdownCellWidget).rendered).to.be(false);
+      });
 
     });