瀏覽代碼

wip implement markdown header level setting

Steven Silvester 8 年之前
父節點
當前提交
47d399060d
共有 2 個文件被更改,包括 80 次插入0 次删除
  1. 48 0
      src/notebook/notebook/actions.ts
  2. 32 0
      test/src/notebook/notebook/actions.spec.ts

+ 48 - 0
src/notebook/notebook/actions.ts

@@ -782,6 +782,38 @@ namespace NotebookActions {
       }
     }
   }
+
+  /**
+   * Set the markdown header level.
+   *
+   * @param widget - The target notebook widget.
+   *
+   * @param level - The header level.
+   *
+   * #### Notes
+   * All selected cells will be switched to markdown.
+   * The level will be clamped between 1 and 6.
+   * If there is an existing header, it will be replaced.
+   * There will always be one blank space after the header.
+   * The cells will be unrendered.
+   */
+  export
+  function setMarkdownHeader(widget: Notebook, level: number) {
+    if (!widget.model || !widget.activeCell) {
+      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);
+      if (widget.isSelected(child)) {
+        Private.setMarkdownHeader(cell, level);
+        widget.rendered = false;
+      }
+    }
+  }
 }
 
 
@@ -837,4 +869,20 @@ namespace Private {
     }
     return Promise.resolve(true);
   }
+
+  /**
+   * Set the markdown header level of a cell.
+   */
+  export
+  function setMarkdownHeader(cell: ICellModel, level: number) {
+    let source = cell.source;
+    let newHeader = Array(level + 1).join('#') + ' ';
+    // Remove existing header or leading white space.
+    let regex = /^(#+\s*)|^(\s*)/;
+    let matches = regex.exec(source);
+    if (matches) {
+      source = source.slice(matches[0].length);
+    }
+    cell.source = newHeader + source;
+  }
 }

+ 32 - 0
test/src/notebook/notebook/actions.spec.ts

@@ -1229,4 +1229,36 @@ describe('notebook/notebook/actions', () => {
 
   });
 
+  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', () => {
+
+    });
+
+    it('should be clamped between 1 and 6', () => {
+
+    });
+
+    it('should replace an existing header', () => {
+
+    });
+
+    it('should replace leading white space', () => {
+
+    });
+
+    it('should unrender the cells', () => {
+
+    });
+
+  });
+
 });